blob: 64d4d261434877d312b2753062b42ba1b30989c4 [file] [log] [blame]
[email protected]a44657202012-01-09 05:48:311// Copyright (c) 2012 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
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_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]41a17c52013-06-28 00:27:5316#include "base/time/time.h"
[email protected]6c2381d2011-10-19 02:52:5317#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
[email protected]cb2edf22013-04-01 20:25:2319#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0520
21class Browser;
[email protected]481e1a42009-05-06 20:56:0522class GURL;
23class Profile;
[email protected]b6583592012-01-25 19:52:3324
25namespace content {
[email protected]a23f62e2013-04-26 13:13:0226class DevToolsAgentHost;
[email protected]eaabba22012-03-07 15:02:1127class RenderViewHost;
[email protected]481e1a42009-05-06 20:56:0528class SiteInstance;
[email protected]b6583592012-01-25 19:52:3329};
[email protected]481e1a42009-05-06 20:56:0530
[email protected]1c321ee2012-05-21 03:02:3431namespace extensions {
32class Extension;
[email protected]3a1dc572012-07-31 22:25:1333class ExtensionHost;
[email protected]1c321ee2012-05-21 03:02:3434}
35
[email protected]bc535ee52010-08-31 18:40:3236// Manages dynamic state of running Chromium extensions. There is one instance
37// of this class per Profile. OTR Profiles have a separate instance that keeps
38// track of split-mode extensions only.
[email protected]6c2381d2011-10-19 02:52:5339class ExtensionProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0540 public:
[email protected]3a1dc572012-07-31 22:25:1341 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3242 typedef ExtensionHostSet::const_iterator const_iterator;
43
[email protected]bc535ee52010-08-31 18:40:3244 static ExtensionProcessManager* Create(Profile* profile);
45 virtual ~ExtensionProcessManager();
[email protected]481e1a42009-05-06 20:56:0546
[email protected]d1fe1352012-04-26 00:47:3247 const ExtensionHostSet& background_hosts() const {
48 return background_hosts_;
49 }
50
[email protected]d1fe1352012-04-26 00:47:3251 typedef std::set<content::RenderViewHost*> ViewSet;
52 const ViewSet GetAllViews() const;
53
[email protected]ab4eaf782009-06-10 00:11:2454 // Creates a new ExtensionHost with its associated view, grouping it in the
55 // appropriate SiteInstance (and therefore process) based on the URL and
56 // profile.
[email protected]3a1dc572012-07-31 22:25:1357 virtual extensions::ExtensionHost* CreateViewHost(
58 const extensions::Extension* extension,
59 const GURL& url,
60 Browser* browser,
[email protected]cb2edf22013-04-01 20:25:2361 extensions::ViewType view_type);
[email protected]3a1dc572012-07-31 22:25:1362 extensions::ExtensionHost* CreateViewHost(const GURL& url,
[email protected]cb2edf22013-04-01 20:25:2363 Browser* browser,
64 extensions::ViewType view_type);
[email protected]3a1dc572012-07-31 22:25:1365 extensions::ExtensionHost* CreatePopupHost(
66 const extensions::Extension* extension,
67 const GURL& url,
68 Browser* browser);
69 extensions::ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser);
70 extensions::ExtensionHost* CreateDialogHost(const GURL& url);
71 extensions::ExtensionHost* CreateInfobarHost(
72 const extensions::Extension* extension,
73 const GURL& url,
74 Browser* browser);
75 extensions::ExtensionHost* CreateInfobarHost(const GURL& url,
76 Browser* browser);
[email protected]a44657202012-01-09 05:48:3177
[email protected]029ad372011-05-20 17:12:5678 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]bc535ee52010-08-31 18:40:3279 // displayed anywhere.
[email protected]1c321ee2012-05-21 03:02:3480 virtual void CreateBackgroundHost(const extensions::Extension* extension,
[email protected]9adb9692010-10-29 23:14:0281 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3282
[email protected]4814b512009-11-07 00:12:2983 // Gets the ExtensionHost for the background page for an extension, or NULL if
84 // the extension isn't running or doesn't have a background page.
[email protected]3a1dc572012-07-31 22:25:1385 extensions::ExtensionHost* GetBackgroundHostForExtension(
86 const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2987
[email protected]481e1a42009-05-06 20:56:0588 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4689 // TODO(aa): This only returns correct results for extensions and packaged
90 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3391 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0592
[email protected]3e194992011-10-20 05:39:1093 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1194 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1095
96 // Returns all RenderViewHosts that are registered for the specified
97 // extension.
[email protected]eaabba22012-03-07 15:02:1198 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1099 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:58100
[email protected]d1fe1352012-04-26 00:47:32101 // Returns the extension associated with the specified RenderViewHost, or
102 // NULL.
[email protected]1c321ee2012-05-21 03:02:34103 const extensions::Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:32104 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:09105
[email protected]7042b682012-04-19 22:57:51106 // 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);
109
[email protected]720ad1312012-02-27 23:07:36110 // 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.
[email protected]1c321ee2012-05-21 03:02:34114 int GetLazyKeepaliveCount(const extensions::Extension* extension);
115 int IncrementLazyKeepaliveCount(const extensions::Extension* extension);
116 int DecrementLazyKeepaliveCount(const extensions::Extension* extension);
[email protected]720ad1312012-02-27 23:07:36117
[email protected]d72d3a62012-05-10 03:45:08118 void IncrementLazyKeepaliveCountForView(
119 content::RenderViewHost* render_view_host);
120
[email protected]584e6572013-02-16 07:02:33121 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36122 // pages.
[email protected]584e6572013-02-16 07:02:33123 void OnShouldSuspendAck(const std::string& extension_id, int sequence_id);
[email protected]103f19f2012-04-02 19:30:12124
[email protected]584e6572013-02-16 07:02:33125 // Same as above, for the Suspend message.
126 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12127
[email protected]6baff0b52012-03-06 01:30:18128 // Tracks network requests for a given RenderViewHost, used to know
129 // when network activity is idle for lazy background pages.
[email protected]eaabba22012-03-07 15:02:11130 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host);
131 void OnNetworkRequestDone(content::RenderViewHost* render_view_host);
[email protected]6baff0b52012-03-06 01:30:18132
[email protected]0d475e072012-07-26 02:30:42133 // Prevents |extension|'s background page from being closed and sends the
134 // onSuspendCanceled() event to it.
135 void CancelSuspend(const extensions::Extension* extension);
136
[email protected]736f4562013-09-06 07:21:55137 // If |defer| is true background host creation is to be deferred until this is
138 // called again with |defer| set to false, at which point all deferred
139 // background hosts will be created. Defaults to false.
140 void DeferBackgroundHostCreation(bool defer);
141
[email protected]bc535ee52010-08-31 18:40:32142 protected:
143 explicit ExtensionProcessManager(Profile* profile);
144
[email protected]94fe52e2009-06-13 10:09:06145 // Called just after |host| is created so it can be registered in our lists.
[email protected]3a1dc572012-07-31 22:25:13146 void OnExtensionHostCreated(extensions::ExtensionHost* host,
147 bool is_background);
[email protected]94fe52e2009-06-13 10:09:06148
[email protected]ae5497f2009-11-05 00:39:46149 // Called on browser shutdown to close our extension hosts.
150 void CloseBackgroundHosts();
151
[email protected]6c2381d2011-10-19 02:52:53152 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27153 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53154 const content::NotificationSource& source,
155 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32156
[email protected]a2f37552013-03-18 07:03:24157 // Load all background pages once the profile data is ready and the pages
158 // should be loaded.
159 void CreateBackgroundHostsForProfileStartup();
160
[email protected]72daaa92012-01-18 13:39:02161 // Gets the profile associated with site_instance_ and all other
162 // related SiteInstances.
163 Profile* GetProfile() const;
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]103f19f2012-04-02 19:30:12176 // Extra information we keep for each extension's background page.
177 struct BackgroundPageData;
178 typedef std::string ExtensionId;
179 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
[email protected]eaabba22012-03-07 15:02:11180 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23181 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10182
[email protected]06024c62011-10-20 20:57:12183 // Close the given |host| iff it's a background page.
[email protected]3a1dc572012-07-31 22:25:13184 void CloseBackgroundHost(extensions::ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12185
[email protected]c64827a2012-03-23 01:36:21186 // Ensure browser object is not null except for certain situations.
187 void EnsureBrowserWhenRequired(Browser* browser,
[email protected]cb2edf22013-04-01 20:25:23188 extensions::ViewType view_type);
[email protected]c64827a2012-03-23 01:36:21189
[email protected]6baff0b52012-03-06 01:30:18190 // These are called when the extension transitions between idle and active.
191 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20192 void OnLazyBackgroundPageIdle(const std::string& extension_id,
193 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18194 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42195 void CloseLazyBackgroundPageNow(const std::string& extension_id,
196 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18197
[email protected]35e0e0792012-11-30 02:35:48198 // Potentially registers a RenderViewHost, if it is associated with an
199 // extension. Does nothing if this is not an extension renderer.
200 void RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12201
[email protected]5b3ee852013-09-26 06:33:10202 // Unregister RenderViewHosts and clear background page data for an extension
203 // which has been unloaded.
204 void UnregisterExtension(const std::string& extension_id);
205
[email protected]caffe702012-05-03 05:30:17206 // Clears background page data for this extension.
207 void ClearBackgroundPageData(const std::string& extension_id);
208
[email protected]ba93a0a2013-04-19 23:03:15209 // Returns true if loading background pages should be deferred. This is
210 // true if there are no browser windows open and the browser process was
[email protected]736f4562013-09-06 07:21:55211 // started to show the app launcher, or if DeferBackgroundHostCreation was
212 // called with true, or if the profile is not yet valid.
[email protected]ba93a0a2013-04-19 23:03:15213 bool DeferLoadingBackgroundHosts() const;
214
[email protected]a23f62e2013-04-26 13:13:02215 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
216
[email protected]35e0e0792012-11-30 02:35:48217 // Contains all active extension-related RenderViewHost instances for all
218 // extensions. We also keep a cache of the host's view type, because that
219 // information is not accessible at registration/deregistration time.
220 ExtensionRenderViews all_extension_views_;
221
[email protected]103f19f2012-04-02 19:30:12222 BackgroundPageDataMap background_page_data_;
223
[email protected]99c49b52012-05-02 01:50:20224 // The time to delay between an extension becoming idle and
[email protected]584e6572013-02-16 07:02:33225 // sending a ShouldSuspend message; read from command-line switch.
[email protected]99c49b52012-05-02 01:50:20226 base::TimeDelta event_page_idle_time_;
227
[email protected]584e6572013-02-16 07:02:33228 // The time to delay between sending a ShouldSuspend message and
229 // sending a Suspend message; read from command-line switch.
230 base::TimeDelta event_page_suspending_time_;
[email protected]99c49b52012-05-02 01:50:20231
[email protected]736f4562013-09-06 07:21:55232 // If true, then creation of background hosts is suspended.
233 bool defer_background_host_creation_;
234
[email protected]99c49b52012-05-02 01:50:20235 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_;
236
[email protected]a23f62e2013-04-26 13:13:02237 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
238
[email protected]481e1a42009-05-06 20:56:05239 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager);
240};
241
242#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_