ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / extensions / browser / process_manager.h
blob16691b4773bb2047daae17a1effee5362b090a59
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
6 #define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/time/time.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21 #include "extensions/browser/extension_registry_observer.h"
22 #include "extensions/common/view_type.h"
24 class GURL;
26 namespace content {
27 class BrowserContext;
28 class DevToolsAgentHost;
29 class RenderViewHost;
30 class RenderFrameHost;
31 class SiteInstance;
34 namespace extensions {
36 class Extension;
37 class ExtensionHost;
38 class ExtensionRegistry;
39 class ProcessManagerDelegate;
40 class ProcessManagerObserver;
42 // Manages dynamic state of running Chromium extensions. There is one instance
43 // of this class per Profile. OTR Profiles have a separate instance that keeps
44 // track of split-mode extensions only.
45 class ProcessManager : public KeyedService,
46 public content::NotificationObserver,
47 public ExtensionRegistryObserver {
48 public:
49 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
50 typedef ExtensionHostSet::const_iterator const_iterator;
52 static ProcessManager* Get(content::BrowserContext* context);
53 ~ProcessManager() override;
55 const ExtensionHostSet& background_hosts() const {
56 return background_hosts_;
59 typedef std::set<content::RenderViewHost*> ViewSet;
60 const ViewSet GetAllViews() const;
62 // The typical observer interface.
63 void AddObserver(ProcessManagerObserver* observer);
64 void RemoveObserver(ProcessManagerObserver* observer);
66 // Creates a new UI-less extension instance. Like CreateViewHost, but not
67 // displayed anywhere. Returns false if no background host can be created,
68 // for example for hosted apps and extensions that aren't enabled in
69 // Incognito.
70 virtual bool CreateBackgroundHost(const Extension* extension,
71 const GURL& url);
73 // Gets the ExtensionHost for the background page for an extension, or NULL if
74 // the extension isn't running or doesn't have a background page.
75 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
77 // Returns the SiteInstance that the given URL belongs to.
78 // TODO(aa): This only returns correct results for extensions and packaged
79 // apps, not hosted apps.
80 virtual scoped_refptr<content::SiteInstance> GetSiteInstanceForURL(
81 const GURL& url);
83 // If the view isn't keeping the lazy background page alive, increments the
84 // keepalive count to do so.
85 void AcquireLazyKeepaliveCountForView(
86 content::RenderViewHost* render_view_host);
88 // If the view is keeping the lazy background page alive, decrements the
89 // keepalive count to stop doing it.
90 void ReleaseLazyKeepaliveCountForView(
91 content::RenderViewHost* render_view_host);
93 // Unregisters a RenderViewHost as hosting any extension.
94 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
96 // Returns all RenderViewHosts that are registered for the specified
97 // extension.
98 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
99 const std::string& extension_id);
101 // Returns the extension associated with the specified RenderViewHost, or
102 // NULL.
103 const Extension* GetExtensionForRenderViewHost(
104 content::RenderViewHost* render_view_host);
106 // Returns true if the (lazy) background host for the given extension has
107 // already been sent the unload event and is shutting down.
108 bool IsBackgroundHostClosing(const std::string& extension_id);
110 // Getter and setter for the lazy background page's keepalive count. This is
111 // the count of how many outstanding "things" are keeping the page alive.
112 // When this reaches 0, we will begin the process of shutting down the page.
113 // "Things" include pending events, resource loads, and API calls.
114 int GetLazyKeepaliveCount(const Extension* extension);
115 void IncrementLazyKeepaliveCount(const Extension* extension);
116 void DecrementLazyKeepaliveCount(const Extension* extension);
118 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
119 // impulses will only keep the page alive for a limited amount of time unless
120 // called regularly.
121 void KeepaliveImpulse(const Extension* extension);
123 // Triggers a keepalive impulse for a plug-in (e.g NaCl).
124 static void OnKeepaliveFromPlugin(int render_process_id,
125 int render_frame_id,
126 const std::string& extension_id);
128 // Handles a response to the ShouldSuspend message, used for lazy background
129 // pages.
130 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
132 // Same as above, for the Suspend message.
133 void OnSuspendAck(const std::string& extension_id);
135 // Tracks network requests for a given RenderFrameHost, used to know
136 // when network activity is idle for lazy background pages.
137 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host,
138 uint64 request_id);
139 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host,
140 uint64 request_id);
142 // Prevents |extension|'s background page from being closed and sends the
143 // onSuspendCanceled() event to it.
144 void CancelSuspend(const Extension* extension);
146 // Creates background hosts if the embedder is ready and they are not already
147 // loaded.
148 void MaybeCreateStartupBackgroundHosts();
150 // Called on shutdown to close our extension hosts.
151 void CloseBackgroundHosts();
153 // Gets the BrowserContext associated with site_instance_ and all other
154 // related SiteInstances.
155 content::BrowserContext* GetBrowserContext() const;
157 // Sets callbacks for testing keepalive impulse behavior.
158 typedef base::Callback<void(const std::string& extension_id)>
159 ImpulseCallbackForTesting;
160 void SetKeepaliveImpulseCallbackForTesting(
161 const ImpulseCallbackForTesting& callback);
162 void SetKeepaliveImpulseDecrementCallbackForTesting(
163 const ImpulseCallbackForTesting& callback);
165 // Sets the time in milliseconds that an extension event page can
166 // be idle before it is shut down; must be > 0.
167 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
169 // Sets the time in milliseconds that an extension event page has
170 // between being notified of its impending unload and that unload
171 // happening.
172 static void SetEventPageSuspendingTimeForTesting(
173 unsigned suspending_time_msec);
175 // Creates a non-incognito instance for tests. |registry| allows unit tests
176 // to inject an ExtensionRegistry that is not managed by the usual
177 // BrowserContextKeyedServiceFactory system.
178 static ProcessManager* CreateForTesting(content::BrowserContext* context,
179 ExtensionRegistry* registry);
181 // Creates an incognito-context instance for tests.
182 static ProcessManager* CreateIncognitoForTesting(
183 content::BrowserContext* incognito_context,
184 content::BrowserContext* original_context,
185 ExtensionRegistry* registry);
187 bool startup_background_hosts_created_for_test() const {
188 return startup_background_hosts_created_;
191 protected:
192 static ProcessManager* Create(content::BrowserContext* context);
194 // |context| is incognito pass the master context as |original_context|.
195 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
196 // |context| as |registry|, or override it for testing.
197 ProcessManager(content::BrowserContext* context,
198 content::BrowserContext* original_context,
199 ExtensionRegistry* registry);
201 // content::NotificationObserver:
202 void Observe(int type,
203 const content::NotificationSource& source,
204 const content::NotificationDetails& details) override;
206 // ExtensionRegistryObserver:
207 void OnExtensionLoaded(content::BrowserContext* browser_context,
208 const Extension* extension) override;
209 void OnExtensionUnloaded(content::BrowserContext* browser_context,
210 const Extension* extension,
211 UnloadedExtensionInfo::Reason reason) override;
213 content::NotificationRegistrar registrar_;
215 // The set of ExtensionHosts running viewless background extensions.
216 ExtensionHostSet background_hosts_;
218 // A SiteInstance related to the SiteInstance for all extensions in
219 // this profile. We create it in such a way that a new
220 // browsing instance is created. This controls process grouping.
221 scoped_refptr<content::SiteInstance> site_instance_;
223 // Not owned. Also used by IncognitoProcessManager.
224 ExtensionRegistry* extension_registry_;
226 private:
227 friend class ProcessManagerFactory;
228 friend class ProcessManagerTest;
230 // Extra information we keep for each extension's background page.
231 struct BackgroundPageData;
232 struct ExtensionRenderViewData;
233 typedef std::string ExtensionId;
234 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
235 typedef std::map<content::RenderViewHost*, ExtensionRenderViewData>
236 ExtensionRenderViews;
238 // Load all background pages once the profile data is ready and the pages
239 // should be loaded.
240 void CreateStartupBackgroundHosts();
242 // Called just after |host| is created so it can be registered in our lists.
243 void OnBackgroundHostCreated(ExtensionHost* host);
245 // Close the given |host| iff it's a background page.
246 void CloseBackgroundHost(ExtensionHost* host);
248 // Internal implementation of DecrementLazyKeepaliveCount with an
249 // |extension_id| known to have a lazy background page.
250 void DecrementLazyKeepaliveCount(const std::string& extension_id);
252 // Checks if keepalive impulses have occured, and adjusts keep alive count.
253 void OnKeepaliveImpulseCheck();
255 // These are called when the extension transitions between idle and active.
256 // They control the process of closing the background page when idle.
257 void OnLazyBackgroundPageIdle(const std::string& extension_id,
258 uint64 sequence_id);
259 void OnLazyBackgroundPageActive(const std::string& extension_id);
260 void CloseLazyBackgroundPageNow(const std::string& extension_id,
261 uint64 sequence_id);
263 // Potentially registers a RenderViewHost, if it is associated with an
264 // extension. Does nothing if this is not an extension renderer.
265 // Returns true, if render_view_host was registered (it is associated
266 // with an extension).
267 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
269 // Unregister RenderViewHosts and clear background page data for an extension
270 // which has been unloaded.
271 void UnregisterExtension(const std::string& extension_id);
273 // Clears background page data for this extension.
274 void ClearBackgroundPageData(const std::string& extension_id);
276 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
278 // Contains all active extension-related RenderViewHost instances for all
279 // extensions. We also keep a cache of the host's view type, because that
280 // information is not accessible at registration/deregistration time.
281 ExtensionRenderViews all_extension_views_;
283 BackgroundPageDataMap background_page_data_;
285 // True if we have created the startup set of background hosts.
286 bool startup_background_hosts_created_;
288 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
290 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
291 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
293 ObserverList<ProcessManagerObserver> observer_list_;
295 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
296 // members. These IDs are tracked per extension in background_page_data_ and
297 // are used to verify that nothing has interrupted the process of closing a
298 // lazy background process.
300 // Any interruption obtains a new ID by incrementing
301 // last_background_close_sequence_id_ and storing it in background_page_data_
302 // for a particular extension. Callbacks and round-trip IPC messages store the
303 // value of the extension's close_sequence_id at the beginning of the process.
304 // Thus comparisons can be done to halt when IDs no longer match.
306 // This counter provides unique IDs even when BackgroundPageData objects are
307 // reset.
308 uint64 last_background_close_sequence_id_;
310 // Must be last member, see doc on WeakPtrFactory.
311 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
313 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
316 } // namespace extensions
318 #endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_