File: offscreen_document_manager.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (120 lines) | stat: -rw-r--r-- 4,509 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_API_OFFSCREEN_OFFSCREEN_DOCUMENT_MANAGER_H_
#define EXTENSIONS_BROWSER_API_OFFSCREEN_OFFSCREEN_DOCUMENT_MANAGER_H_

#include <map>
#include <memory>
#include <set>

#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/common/api/offscreen.h"
#include "extensions/common/extension_id.h"

class BrowserContextKeyedServiceFactory;
class GURL;

namespace extensions {
class Extension;
class ExtensionHost;
class ProcessManager;
class OffscreenDocumentHost;
class OffscreenDocumentLifetimeEnforcer;

// The OffscreenDocumentManager is responsible for managing offscreen documents
// created by extensions through the `offscreen` API.
class OffscreenDocumentManager : public KeyedService,
                                 public ExtensionRegistryObserver {
 public:
  explicit OffscreenDocumentManager(content::BrowserContext* browser_context);

  OffscreenDocumentManager(const OffscreenDocumentManager&) = delete;
  OffscreenDocumentManager& operator=(const OffscreenDocumentManager&) = delete;

  ~OffscreenDocumentManager() override;

  // Returns the OffscreenDocumentManager for the given `browser_context`.
  // Note: This class has a separate instance in incognito.
  static OffscreenDocumentManager* Get(
      content::BrowserContext* browser_context);

  // Returns the KeyedServiceFactory for the OffscreenDocumentManager.
  static BrowserContextKeyedServiceFactory* GetFactory();

  // Creates and returns an offscreen document for the given `extension` and
  // `url`, created for the given `reason`.
  OffscreenDocumentHost* CreateOffscreenDocument(
      const Extension& extension,
      const GURL& url,
      std::set<api::offscreen::Reason> reasons);

  // Returns the current offscreen document for the given `extension`, if one
  // exists.
  OffscreenDocumentHost* GetOffscreenDocumentForExtension(
      const Extension& extension);

  // Closes the current offscreen document. Note: This requires that there *is*
  // an active offscreen document.
  void CloseOffscreenDocumentForExtension(const Extension& extension);

 private:
  struct OffscreenDocumentData {
    // Appease Chromium clang plugin with out-of-line ctors/dtors.
    OffscreenDocumentData();
    OffscreenDocumentData(OffscreenDocumentData&&);
    ~OffscreenDocumentData();

    std::unique_ptr<OffscreenDocumentHost> host;

    // The lifetime enforcers for the offscreen document. Note that currently
    // this will always only have a single entry, but will have more when we
    // support creating a document with multiple reasons.
    std::vector<std::unique_ptr<OffscreenDocumentLifetimeEnforcer>> enforcers;

    // TODO(crbug.com/40849649): This will need more fields to include
    // attributes like the associated reason and justification for the
    // document.
  };

  // Closes the offscreen document for the extension with the given
  // `extension_id`.
  void CloseOffscreenDocumentForExtensionId(const ExtensionId& extension_id);

  // Called when the active state changes for the offscreen document associated
  // with the extension with the given `extension_id`.
  void OnOffscreenDocumentActivityChanged(const ExtensionId& extension_id);

  // ExtensionRegistry:
  void OnExtensionUnloaded(content::BrowserContext* browser_context,
                           const Extension* extension,
                           UnloadedExtensionReason reason) override;

  // KeyedService:
  void Shutdown() override;

  // Closes the specified `offscreen_document`.
  void CloseOffscreenDocument(ExtensionHost* offscreen_document);

  // The collection of offscreen documents, mapped to extension ID.
  std::map<ExtensionId, OffscreenDocumentData> offscreen_documents_;

  // The associated browser context.
  raw_ptr<content::BrowserContext> browser_context_;

  // The process manager for the `browser_context_`.
  raw_ptr<ProcessManager> process_manager_;

  // Observe ExtensionRegistry for extensions being unloaded.
  base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
      registry_observation_{this};
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_OFFSCREEN_OFFSCREEN_DOCUMENT_MANAGER_H_