blob: e580bbd9335205b8b22ec164c25930d9e9853802 [file] [log] [blame]
[email protected]98b6d942013-11-10 00:34:071// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]481e1a42009-05-06 20:56:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]98b6d942013-11-10 00:34:075#ifndef EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
6#define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
[email protected]481e1a42009-05-06 20:56:057
[email protected]45776222009-07-15 20:21:588#include <map>
[email protected]8a17bd52009-06-06 08:19:499#include <set>
[email protected]45776222009-07-15 20:21:5810#include <string>
[email protected]481e1a42009-05-06 20:56:0511
[email protected]a23f62e2013-04-26 13:13:0212#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5413#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]99c49b52012-05-02 01:50:2015#include "base/memory/weak_ptr.h"
[email protected]08d469b22014-03-31 00:51:2416#include "base/observer_list.h"
[email protected]41a17c52013-06-28 00:27:5317#include "base/time/time.h"
[email protected]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
[email protected]cb2edf22013-04-01 20:25:2320#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0521
[email protected]481e1a42009-05-06 20:56:0522class GURL;
[email protected]b6583592012-01-25 19:52:3323
24namespace content {
[email protected]7f474212013-11-05 04:26:1625class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0226class DevToolsAgentHost;
[email protected]eaabba22012-03-07 15:02:1127class RenderViewHost;
[email protected]9dfed872013-12-30 23:08:5628class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0529class SiteInstance;
[email protected]b6583592012-01-25 19:52:3330};
[email protected]481e1a42009-05-06 20:56:0531
[email protected]1c321ee2012-05-21 03:02:3432namespace extensions {
[email protected]98b6d942013-11-10 00:34:0733
[email protected]1c321ee2012-05-21 03:02:3434class Extension;
[email protected]3a1dc572012-07-31 22:25:1335class ExtensionHost;
[email protected]08d469b22014-03-31 00:51:2436class ProcessManagerObserver;
[email protected]1c321ee2012-05-21 03:02:3437
[email protected]bc535ee52010-08-31 18:40:3238// Manages dynamic state of running Chromium extensions. There is one instance
39// of this class per Profile. OTR Profiles have a separate instance that keeps
40// track of split-mode extensions only.
[email protected]98b6d942013-11-10 00:34:0741class ProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0542 public:
[email protected]3a1dc572012-07-31 22:25:1343 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3244 typedef ExtensionHostSet::const_iterator const_iterator;
45
[email protected]98b6d942013-11-10 00:34:0746 static ProcessManager* Create(content::BrowserContext* context);
47 virtual ~ProcessManager();
[email protected]481e1a42009-05-06 20:56:0548
[email protected]d1fe1352012-04-26 00:47:3249 const ExtensionHostSet& background_hosts() const {
50 return background_hosts_;
51 }
52
[email protected]d1fe1352012-04-26 00:47:3253 typedef std::set<content::RenderViewHost*> ViewSet;
54 const ViewSet GetAllViews() const;
55
[email protected]08d469b22014-03-31 00:51:2456 // The typical observer interface.
57 void AddObserver(ProcessManagerObserver* observer);
58 void RemoveObserver(ProcessManagerObserver* observer);
59
[email protected]029ad372011-05-20 17:12:5660 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]6ad9cdf72014-02-27 13:12:4161 // displayed anywhere. Returns false if no background host can be created,
62 // for example for hosted apps and extensions that aren't enabled in
63 // Incognito.
64 virtual bool CreateBackgroundHost(const Extension* extension,
65 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3266
[email protected]4814b512009-11-07 00:12:2967 // Gets the ExtensionHost for the background page for an extension, or NULL if
68 // the extension isn't running or doesn't have a background page.
[email protected]98b6d942013-11-10 00:34:0769 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2970
[email protected]481e1a42009-05-06 20:56:0571 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4672 // TODO(aa): This only returns correct results for extensions and packaged
73 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3374 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0575
[email protected]3e194992011-10-20 05:39:1076 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1177 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1078
79 // Returns all RenderViewHosts that are registered for the specified
80 // extension.
[email protected]eaabba22012-03-07 15:02:1181 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1082 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:5883
[email protected]d1fe1352012-04-26 00:47:3284 // Returns the extension associated with the specified RenderViewHost, or
85 // NULL.
[email protected]98b6d942013-11-10 00:34:0786 const Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:3287 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:0988
[email protected]7042b682012-04-19 22:57:5189 // Returns true if the (lazy) background host for the given extension has
90 // already been sent the unload event and is shutting down.
91 bool IsBackgroundHostClosing(const std::string& extension_id);
92
[email protected]720ad1312012-02-27 23:07:3693 // Getter and setter for the lazy background page's keepalive count. This is
94 // the count of how many outstanding "things" are keeping the page alive.
95 // When this reaches 0, we will begin the process of shutting down the page.
96 // "Things" include pending events, resource loads, and API calls.
[email protected]98b6d942013-11-10 00:34:0797 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:3298 void IncrementLazyKeepaliveCount(const Extension* extension);
99 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:36100
[email protected]d72d3a62012-05-10 03:45:08101 void IncrementLazyKeepaliveCountForView(
102 content::RenderViewHost* render_view_host);
103
[email protected]388770152013-12-03 01:25:32104 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
105 // impulses will only keep the page alive for a limited amount of time unless
106 // called regularly.
107 void KeepaliveImpulse(const Extension* extension);
108
[email protected]584e6572013-02-16 07:02:33109 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36110 // pages.
[email protected]77a538f2014-07-14 22:25:37111 void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
[email protected]103f19f2012-04-02 19:30:12112
[email protected]584e6572013-02-16 07:02:33113 // Same as above, for the Suspend message.
114 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12115
[email protected]9dfed872013-12-30 23:08:56116 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18117 // when network activity is idle for lazy background pages.
[email protected]9dfed872013-12-30 23:08:56118 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host);
119 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18120
[email protected]0d475e072012-07-26 02:30:42121 // Prevents |extension|'s background page from being closed and sends the
122 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07123 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42124
[email protected]aa3dd492013-11-05 17:09:09125 // Ensures background hosts are loaded for a new browser window.
126 void OnBrowserWindowReady();
127
128 // Gets the BrowserContext associated with site_instance_ and all other
129 // related SiteInstances.
130 content::BrowserContext* GetBrowserContext() const;
131
[email protected]b6cd7202014-01-07 17:46:55132 // Sets callbacks for testing keepalive impulse behavior.
133 typedef base::Callback<void(const std::string& extension_id)>
134 ImpulseCallbackForTesting;
135 void SetKeepaliveImpulseCallbackForTesting(
136 const ImpulseCallbackForTesting& callback);
137 void SetKeepaliveImpulseDecrementCallbackForTesting(
138 const ImpulseCallbackForTesting& callback);
139
[email protected]b9f6ba32014-03-10 18:34:08140 // Creates an incognito-context instance for tests. Tests for non-incognito
141 // contexts can just use Create() above.
142 static ProcessManager* CreateIncognitoForTesting(
143 content::BrowserContext* incognito_context,
144 content::BrowserContext* original_context,
145 ProcessManager* original_manager);
146
[email protected]bc535ee52010-08-31 18:40:32147 protected:
[email protected]7f474212013-11-05 04:26:16148 // If |context| is incognito pass the master context as |original_context|.
149 // Otherwise pass the same context for both.
[email protected]98b6d942013-11-10 00:34:07150 ProcessManager(content::BrowserContext* context,
151 content::BrowserContext* original_context);
[email protected]bc535ee52010-08-31 18:40:32152
[email protected]ae5497f2009-11-05 00:39:46153 // Called on browser shutdown to close our extension hosts.
154 void CloseBackgroundHosts();
155
[email protected]6c2381d2011-10-19 02:52:53156 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27157 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53158 const content::NotificationSource& source,
159 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32160
[email protected]a2f37552013-03-18 07:03:24161 // Load all background pages once the profile data is ready and the pages
162 // should be loaded.
163 void CreateBackgroundHostsForProfileStartup();
164
[email protected]6c2381d2011-10-19 02:52:53165 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06166
[email protected]d1fe1352012-04-26 00:47:32167 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49168 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05169
[email protected]72daaa92012-01-18 13:39:02170 // A SiteInstance related to the SiteInstance for all extensions in
171 // this profile. We create it in such a way that a new
172 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33173 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05174
[email protected]3e194992011-10-20 05:39:10175 private:
[email protected]98b6d942013-11-10 00:34:07176 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16177
[email protected]103f19f2012-04-02 19:30:12178 // Extra information we keep for each extension's background page.
179 struct BackgroundPageData;
180 typedef std::string ExtensionId;
181 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
[email protected]eaabba22012-03-07 15:02:11182 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23183 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10184
[email protected]94de8cb2013-11-07 06:29:21185 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07186 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21187
[email protected]06024c62011-10-20 20:57:12188 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07189 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12190
[email protected]388770152013-12-03 01:25:32191 // Internal implementation of DecrementLazyKeepaliveCount with an
192 // |extension_id| known to have a lazy background page.
193 void DecrementLazyKeepaliveCount(const std::string& extension_id);
194
195 // Checks if keepalive impulses have occured, and adjusts keep alive count.
196 void OnKeepaliveImpulseCheck();
197
[email protected]6baff0b52012-03-06 01:30:18198 // These are called when the extension transitions between idle and active.
199 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20200 void OnLazyBackgroundPageIdle(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37201 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18202 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42203 void CloseLazyBackgroundPageNow(const std::string& extension_id,
[email protected]77a538f2014-07-14 22:25:37204 uint64 sequence_id);
[email protected]6baff0b52012-03-06 01:30:18205
[email protected]35e0e0792012-11-30 02:35:48206 // Potentially registers a RenderViewHost, if it is associated with an
207 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15208 // Returns true, if render_view_host was registered (it is associated
209 // with an extension).
210 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12211
[email protected]5b3ee852013-09-26 06:33:10212 // Unregister RenderViewHosts and clear background page data for an extension
213 // which has been unloaded.
214 void UnregisterExtension(const std::string& extension_id);
215
[email protected]caffe702012-05-03 05:30:17216 // Clears background page data for this extension.
217 void ClearBackgroundPageData(const std::string& extension_id);
218
[email protected]7f474212013-11-05 04:26:16219 // Returns true if loading background pages should be deferred.
[email protected]ba93a0a2013-04-19 23:03:15220 bool DeferLoadingBackgroundHosts() const;
221
[email protected]a23f62e2013-04-26 13:13:02222 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
223
[email protected]35e0e0792012-11-30 02:35:48224 // Contains all active extension-related RenderViewHost instances for all
225 // extensions. We also keep a cache of the host's view type, because that
226 // information is not accessible at registration/deregistration time.
227 ExtensionRenderViews all_extension_views_;
228
[email protected]103f19f2012-04-02 19:30:12229 BackgroundPageDataMap background_page_data_;
230
[email protected]99c49b52012-05-02 01:50:20231 // The time to delay between an extension becoming idle and
[email protected]584e6572013-02-16 07:02:33232 // sending a ShouldSuspend message; read from command-line switch.
[email protected]99c49b52012-05-02 01:50:20233 base::TimeDelta event_page_idle_time_;
234
[email protected]584e6572013-02-16 07:02:33235 // The time to delay between sending a ShouldSuspend message and
236 // sending a Suspend message; read from command-line switch.
237 base::TimeDelta event_page_suspending_time_;
[email protected]99c49b52012-05-02 01:50:20238
[email protected]aa3dd492013-11-05 17:09:09239 // True if we have created the startup set of background hosts.
240 bool startup_background_hosts_created_;
241
[email protected]75e7bac2013-11-01 23:33:42242 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
243
[email protected]b6cd7202014-01-07 17:46:55244 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
245 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
246
[email protected]08d469b22014-03-31 00:51:24247 ObserverList<ProcessManagerObserver> observer_list_;
248
[email protected]77a538f2014-07-14 22:25:37249 // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
250 // members. These IDs are tracked per extension in background_page_data_ and
251 // are used to verify that nothing has interrupted the process of closing a
252 // lazy background process.
253 //
254 // Any interruption obtains a new ID by incrementing
255 // last_background_close_sequence_id_ and storing it in background_page_data_
256 // for a particular extension. Callbacks and round-trip IPC messages store the
257 // value of the extension's close_sequence_id at the beginning of the process.
258 // Thus comparisons can be done to halt when IDs no longer match.
259 //
260 // This counter provides unique IDs even when BackgroundPageData objects are
261 // reset.
262 uint64 last_background_close_sequence_id_;
263
264 // Must be last member, see doc on WeakPtrFactory.
[email protected]98b6d942013-11-10 00:34:07265 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16266
[email protected]98b6d942013-11-10 00:34:07267 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05268};
269
[email protected]98b6d942013-11-10 00:34:07270} // namespace extensions
271
272#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_