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_
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 "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/common/view_type.h"
26 class DevToolsAgentHost;
28 class RenderFrameHost;
32 namespace extensions {
36 class ExtensionRegistry;
37 class ProcessManagerDelegate;
38 class ProcessManagerObserver;
40 // Manages dynamic state of running Chromium extensions. There is one instance
41 // of this class per Profile. OTR Profiles have a separate instance that keeps
42 // track of split-mode extensions only.
43 class ProcessManager : public content::NotificationObserver {
45 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
46 typedef ExtensionHostSet::const_iterator const_iterator;
48 static ProcessManager* Create(content::BrowserContext* context);
49 virtual ~ProcessManager();
51 const ExtensionHostSet& background_hosts() const {
52 return background_hosts_;
55 typedef std::set<content::RenderViewHost*> ViewSet;
56 const ViewSet GetAllViews() const;
58 // The typical observer interface.
59 void AddObserver(ProcessManagerObserver* observer);
60 void RemoveObserver(ProcessManagerObserver* observer);
62 // Creates a new UI-less extension instance. Like CreateViewHost, but not
63 // displayed anywhere. Returns false if no background host can be created,
64 // for example for hosted apps and extensions that aren't enabled in
66 virtual bool CreateBackgroundHost(const Extension* extension,
69 // Gets the ExtensionHost for the background page for an extension, or NULL if
70 // the extension isn't running or doesn't have a background page.
71 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
73 // Returns the SiteInstance that the given URL belongs to.
74 // TODO(aa): This only returns correct results for extensions and packaged
75 // apps, not hosted apps.
76 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
78 // Unregisters a RenderViewHost as hosting any extension.
79 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
81 // Returns all RenderViewHosts that are registered for the specified
83 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
84 const std::string& extension_id);
86 // Returns the extension associated with the specified RenderViewHost, or
88 const Extension* GetExtensionForRenderViewHost(
89 content::RenderViewHost* render_view_host);
91 // Returns true if the (lazy) background host for the given extension has
92 // already been sent the unload event and is shutting down.
93 bool IsBackgroundHostClosing(const std::string& extension_id);
95 // Getter and setter for the lazy background page's keepalive count. This is
96 // the count of how many outstanding "things" are keeping the page alive.
97 // When this reaches 0, we will begin the process of shutting down the page.
98 // "Things" include pending events, resource loads, and API calls.
99 int GetLazyKeepaliveCount(const Extension* extension);
100 void IncrementLazyKeepaliveCount(const Extension* extension);
101 void DecrementLazyKeepaliveCount(const Extension* extension);
103 void IncrementLazyKeepaliveCountForView(
104 content::RenderViewHost* render_view_host);
106 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
107 // impulses will only keep the page alive for a limited amount of time unless
109 void KeepaliveImpulse(const Extension* extension);
111 // Triggers a keepalive impulse for a plug-in (e.g NaCl).
112 static void OnKeepaliveFromPlugin(int render_process_id,
114 const std::string& extension_id);
116 // Handles a response to the ShouldSuspend message, used for lazy background
118 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
120 // Same as above, for the Suspend message.
121 void OnSuspendAck(const std::string& extension_id);
123 // Tracks network requests for a given RenderFrameHost, used to know
124 // when network activity is idle for lazy background pages.
125 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host);
126 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host);
128 // Prevents |extension|'s background page from being closed and sends the
129 // onSuspendCanceled() event to it.
130 void CancelSuspend(const Extension* extension);
132 // Creates background hosts if the embedder is ready and they are not already
134 void MaybeCreateStartupBackgroundHosts();
136 // Called on shutdown to close our extension hosts.
137 void CloseBackgroundHosts();
139 // Gets the BrowserContext associated with site_instance_ and all other
140 // related SiteInstances.
141 content::BrowserContext* GetBrowserContext() const;
143 // Sets callbacks for testing keepalive impulse behavior.
144 typedef base::Callback<void(const std::string& extension_id)>
145 ImpulseCallbackForTesting;
146 void SetKeepaliveImpulseCallbackForTesting(
147 const ImpulseCallbackForTesting& callback);
148 void SetKeepaliveImpulseDecrementCallbackForTesting(
149 const ImpulseCallbackForTesting& callback);
151 // Sets the time in milliseconds that an extension event page can
152 // be idle before it is shut down; must be > 0.
153 static void SetEventPageIdleTimeForTesting(unsigned idle_time_msec);
155 // Sets the time in milliseconds that an extension event page has
156 // between being notified of its impending unload and that unload
158 static void SetEventPageSuspendingTimeForTesting(
159 unsigned suspending_time_msec);
161 // Creates a non-incognito instance for tests. |registry| allows unit tests
162 // to inject an ExtensionRegistry that is not managed by the usual
163 // BrowserContextKeyedServiceFactory system.
164 static ProcessManager* CreateForTesting(content::BrowserContext* context,
165 ExtensionRegistry* registry);
167 // Creates an incognito-context instance for tests.
168 static ProcessManager* CreateIncognitoForTesting(
169 content::BrowserContext* incognito_context,
170 content::BrowserContext* original_context,
171 ProcessManager* original_manager,
172 ExtensionRegistry* registry);
174 bool startup_background_hosts_created_for_test() const {
175 return startup_background_hosts_created_;
179 // If |context| is incognito pass the master context as |original_context|.
180 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
181 // |context| as |registry|, or override it for testing.
182 ProcessManager(content::BrowserContext* context,
183 content::BrowserContext* original_context,
184 ExtensionRegistry* registry);
186 // content::NotificationObserver:
187 virtual void Observe(int type,
188 const content::NotificationSource& source,
189 const content::NotificationDetails& details) OVERRIDE;
191 content::NotificationRegistrar registrar_;
193 // The set of ExtensionHosts running viewless background extensions.
194 ExtensionHostSet background_hosts_;
196 // A SiteInstance related to the SiteInstance for all extensions in
197 // this profile. We create it in such a way that a new
198 // browsing instance is created. This controls process grouping.
199 scoped_refptr<content::SiteInstance> site_instance_;
201 // Not owned. Also used by IncognitoProcessManager.
202 ExtensionRegistry* extension_registry_;
205 friend class ProcessManagerTest;
207 // Extra information we keep for each extension's background page.
208 struct BackgroundPageData;
209 typedef std::string ExtensionId;
210 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
211 typedef std::map<content::RenderViewHost*,
212 extensions::ViewType> ExtensionRenderViews;
214 // Load all background pages once the profile data is ready and the pages
216 void CreateStartupBackgroundHosts();
218 // Called just after |host| is created so it can be registered in our lists.
219 void OnBackgroundHostCreated(ExtensionHost* host);
221 // Close the given |host| iff it's a background page.
222 void CloseBackgroundHost(ExtensionHost* host);
224 // Internal implementation of DecrementLazyKeepaliveCount with an
225 // |extension_id| known to have a lazy background page.
226 void DecrementLazyKeepaliveCount(const std::string& extension_id);
228 // Checks if keepalive impulses have occured, and adjusts keep alive count.
229 void OnKeepaliveImpulseCheck();
231 // These are called when the extension transitions between idle and active.
232 // They control the process of closing the background page when idle.
233 void OnLazyBackgroundPageIdle(const std::string& extension_id,
235 void OnLazyBackgroundPageActive(const std::string& extension_id);
236 void CloseLazyBackgroundPageNow(const std::string& extension_id,
239 // Potentially registers a RenderViewHost, if it is associated with an
240 // extension. Does nothing if this is not an extension renderer.
241 // Returns true, if render_view_host was registered (it is associated
242 // with an extension).
243 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
245 // Unregister RenderViewHosts and clear background page data for an extension
246 // which has been unloaded.
247 void UnregisterExtension(const std::string& extension_id);
249 // Clears background page data for this extension.
250 void ClearBackgroundPageData(const std::string& extension_id);
252 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
254 // Contains all active extension-related RenderViewHost instances for all
255 // extensions. We also keep a cache of the host's view type, because that
256 // information is not accessible at registration/deregistration time.
257 ExtensionRenderViews all_extension_views_;
259 BackgroundPageDataMap background_page_data_;
261 // True if we have created the startup set of background hosts.
262 bool startup_background_hosts_created_;
264 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
266 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
267 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
269 ObserverList<ProcessManagerObserver> observer_list_;
271 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
272 // members. These IDs are tracked per extension in background_page_data_ and
273 // are used to verify that nothing has interrupted the process of closing a
274 // lazy background process.
276 // Any interruption obtains a new ID by incrementing
277 // last_background_close_sequence_id_ and storing it in background_page_data_
278 // for a particular extension. Callbacks and round-trip IPC messages store the
279 // value of the extension's close_sequence_id at the beginning of the process.
280 // Thus comparisons can be done to halt when IDs no longer match.
282 // This counter provides unique IDs even when BackgroundPageData objects are
284 uint64 last_background_close_sequence_id_;
286 // Must be last member, see doc on WeakPtrFactory.
287 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
289 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
292 } // namespace extensions
294 #endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_