Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / process_manager.h
blobe7be9951e03233c48dfa27100a3bad0ade98e704
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 "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "extensions/common/view_type.h"
22 class GURL;
24 namespace content {
25 class BrowserContext;
26 class DevToolsAgentHost;
27 class RenderViewHost;
28 class RenderFrameHost;
29 class SiteInstance;
32 namespace extensions {
34 class Extension;
35 class ExtensionHost;
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 {
44 public:
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
65 // Incognito.
66 virtual bool CreateBackgroundHost(const Extension* extension,
67 const GURL& url);
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
82 // extension.
83 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
84 const std::string& extension_id);
86 // Returns the extension associated with the specified RenderViewHost, or
87 // NULL.
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
108 // called regularly.
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,
113 int render_frame_id,
114 const std::string& extension_id);
116 // Handles a response to the ShouldSuspend message, used for lazy background
117 // pages.
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
133 // loaded.
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 // Creates a non-incognito instance for tests. |registry| allows unit tests
152 // to inject an ExtensionRegistry that is not managed by the usual
153 // BrowserContextKeyedServiceFactory system.
154 static ProcessManager* CreateForTesting(content::BrowserContext* context,
155 ExtensionRegistry* registry);
157 // Creates an incognito-context instance for tests.
158 static ProcessManager* CreateIncognitoForTesting(
159 content::BrowserContext* incognito_context,
160 content::BrowserContext* original_context,
161 ProcessManager* original_manager,
162 ExtensionRegistry* registry);
164 bool startup_background_hosts_created_for_test() const {
165 return startup_background_hosts_created_;
168 protected:
169 // If |context| is incognito pass the master context as |original_context|.
170 // Otherwise pass the same context for both. Pass the ExtensionRegistry for
171 // |context| as |registry|, or override it for testing.
172 ProcessManager(content::BrowserContext* context,
173 content::BrowserContext* original_context,
174 ExtensionRegistry* registry);
176 // content::NotificationObserver:
177 virtual void Observe(int type,
178 const content::NotificationSource& source,
179 const content::NotificationDetails& details) OVERRIDE;
181 content::NotificationRegistrar registrar_;
183 // The set of ExtensionHosts running viewless background extensions.
184 ExtensionHostSet background_hosts_;
186 // A SiteInstance related to the SiteInstance for all extensions in
187 // this profile. We create it in such a way that a new
188 // browsing instance is created. This controls process grouping.
189 scoped_refptr<content::SiteInstance> site_instance_;
191 // Not owned. Also used by IncognitoProcessManager.
192 ExtensionRegistry* extension_registry_;
194 private:
195 friend class ProcessManagerTest;
197 // Extra information we keep for each extension's background page.
198 struct BackgroundPageData;
199 typedef std::string ExtensionId;
200 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
201 typedef std::map<content::RenderViewHost*,
202 extensions::ViewType> ExtensionRenderViews;
204 // Load all background pages once the profile data is ready and the pages
205 // should be loaded.
206 void CreateStartupBackgroundHosts();
208 // Called just after |host| is created so it can be registered in our lists.
209 void OnBackgroundHostCreated(ExtensionHost* host);
211 // Close the given |host| iff it's a background page.
212 void CloseBackgroundHost(ExtensionHost* host);
214 // Internal implementation of DecrementLazyKeepaliveCount with an
215 // |extension_id| known to have a lazy background page.
216 void DecrementLazyKeepaliveCount(const std::string& extension_id);
218 // Checks if keepalive impulses have occured, and adjusts keep alive count.
219 void OnKeepaliveImpulseCheck();
221 // These are called when the extension transitions between idle and active.
222 // They control the process of closing the background page when idle.
223 void OnLazyBackgroundPageIdle(const std::string& extension_id,
224 uint64 sequence_id);
225 void OnLazyBackgroundPageActive(const std::string& extension_id);
226 void CloseLazyBackgroundPageNow(const std::string& extension_id,
227 uint64 sequence_id);
229 // Potentially registers a RenderViewHost, if it is associated with an
230 // extension. Does nothing if this is not an extension renderer.
231 // Returns true, if render_view_host was registered (it is associated
232 // with an extension).
233 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
235 // Unregister RenderViewHosts and clear background page data for an extension
236 // which has been unloaded.
237 void UnregisterExtension(const std::string& extension_id);
239 // Clears background page data for this extension.
240 void ClearBackgroundPageData(const std::string& extension_id);
242 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
244 // Contains all active extension-related RenderViewHost instances for all
245 // extensions. We also keep a cache of the host's view type, because that
246 // information is not accessible at registration/deregistration time.
247 ExtensionRenderViews all_extension_views_;
249 BackgroundPageDataMap background_page_data_;
251 // The time to delay between an extension becoming idle and
252 // sending a ShouldSuspend message; read from command-line switch.
253 base::TimeDelta event_page_idle_time_;
255 // The time to delay between sending a ShouldSuspend message and
256 // sending a Suspend message; read from command-line switch.
257 base::TimeDelta event_page_suspending_time_;
259 // True if we have created the startup set of background hosts.
260 bool startup_background_hosts_created_;
262 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
264 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
265 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
267 ObserverList<ProcessManagerObserver> observer_list_;
269 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
270 // members. These IDs are tracked per extension in background_page_data_ and
271 // are used to verify that nothing has interrupted the process of closing a
272 // lazy background process.
274 // Any interruption obtains a new ID by incrementing
275 // last_background_close_sequence_id_ and storing it in background_page_data_
276 // for a particular extension. Callbacks and round-trip IPC messages store the
277 // value of the extension's close_sequence_id at the beginning of the process.
278 // Thus comparisons can be done to halt when IDs no longer match.
280 // This counter provides unique IDs even when BackgroundPageData objects are
281 // reset.
282 uint64 last_background_close_sequence_id_;
284 // Must be last member, see doc on WeakPtrFactory.
285 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
287 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
290 } // namespace extensions
292 #endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_