blob: b0416eb585ad94ee21a19ca8cdc4a935b872a889 [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]17902752011-08-31 22:52:5412#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/ref_counted.h"
[email protected]99c49b52012-05-02 01:50:2014#include "base/memory/weak_ptr.h"
15#include "base/time.h"
[email protected]6c2381d2011-10-19 02:52:5316#include "content/public/browser/notification_observer.h"
17#include "content/public/browser/notification_registrar.h"
[email protected]cb2edf22013-04-01 20:25:2318#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0519
20class Browser;
[email protected]481e1a42009-05-06 20:56:0521class GURL;
22class Profile;
[email protected]b6583592012-01-25 19:52:3323
24namespace content {
[email protected]eaabba22012-03-07 15:02:1125class RenderViewHost;
[email protected]481e1a42009-05-06 20:56:0526class SiteInstance;
[email protected]b6583592012-01-25 19:52:3327};
[email protected]481e1a42009-05-06 20:56:0528
[email protected]1c321ee2012-05-21 03:02:3429namespace extensions {
30class Extension;
[email protected]3a1dc572012-07-31 22:25:1331class ExtensionHost;
[email protected]1c321ee2012-05-21 03:02:3432}
33
[email protected]bc535ee52010-08-31 18:40:3234// Manages dynamic state of running Chromium extensions. There is one instance
35// of this class per Profile. OTR Profiles have a separate instance that keeps
36// track of split-mode extensions only.
[email protected]6c2381d2011-10-19 02:52:5337class ExtensionProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0538 public:
[email protected]3a1dc572012-07-31 22:25:1339 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3240 typedef ExtensionHostSet::const_iterator const_iterator;
41
[email protected]bc535ee52010-08-31 18:40:3242 static ExtensionProcessManager* Create(Profile* profile);
43 virtual ~ExtensionProcessManager();
[email protected]481e1a42009-05-06 20:56:0544
[email protected]d1fe1352012-04-26 00:47:3245 const ExtensionHostSet& background_hosts() const {
46 return background_hosts_;
47 }
48
[email protected]d1fe1352012-04-26 00:47:3249 typedef std::set<content::RenderViewHost*> ViewSet;
50 const ViewSet GetAllViews() const;
51
[email protected]ab4eaf782009-06-10 00:11:2452 // Creates a new ExtensionHost with its associated view, grouping it in the
53 // appropriate SiteInstance (and therefore process) based on the URL and
54 // profile.
[email protected]3a1dc572012-07-31 22:25:1355 virtual extensions::ExtensionHost* CreateViewHost(
56 const extensions::Extension* extension,
57 const GURL& url,
58 Browser* browser,
[email protected]cb2edf22013-04-01 20:25:2359 extensions::ViewType view_type);
[email protected]3a1dc572012-07-31 22:25:1360 extensions::ExtensionHost* CreateViewHost(const GURL& url,
[email protected]cb2edf22013-04-01 20:25:2361 Browser* browser,
62 extensions::ViewType view_type);
[email protected]3a1dc572012-07-31 22:25:1363 extensions::ExtensionHost* CreatePopupHost(
64 const extensions::Extension* extension,
65 const GURL& url,
66 Browser* browser);
67 extensions::ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser);
68 extensions::ExtensionHost* CreateDialogHost(const GURL& url);
69 extensions::ExtensionHost* CreateInfobarHost(
70 const extensions::Extension* extension,
71 const GURL& url,
72 Browser* browser);
73 extensions::ExtensionHost* CreateInfobarHost(const GURL& url,
74 Browser* browser);
[email protected]a44657202012-01-09 05:48:3175
[email protected]029ad372011-05-20 17:12:5676 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]bc535ee52010-08-31 18:40:3277 // displayed anywhere.
[email protected]1c321ee2012-05-21 03:02:3478 virtual void CreateBackgroundHost(const extensions::Extension* extension,
[email protected]9adb9692010-10-29 23:14:0279 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3280
[email protected]4814b512009-11-07 00:12:2981 // Gets the ExtensionHost for the background page for an extension, or NULL if
82 // the extension isn't running or doesn't have a background page.
[email protected]3a1dc572012-07-31 22:25:1383 extensions::ExtensionHost* GetBackgroundHostForExtension(
84 const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2985
[email protected]481e1a42009-05-06 20:56:0586 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4687 // TODO(aa): This only returns correct results for extensions and packaged
88 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3389 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0590
[email protected]3e194992011-10-20 05:39:1091 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1192 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1093
94 // Returns all RenderViewHosts that are registered for the specified
95 // extension.
[email protected]eaabba22012-03-07 15:02:1196 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1097 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:5898
[email protected]d1fe1352012-04-26 00:47:3299 // Returns the extension associated with the specified RenderViewHost, or
100 // NULL.
[email protected]1c321ee2012-05-21 03:02:34101 const extensions::Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:32102 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:09103
[email protected]7042b682012-04-19 22:57:51104 // Returns true if the (lazy) background host for the given extension has
105 // already been sent the unload event and is shutting down.
106 bool IsBackgroundHostClosing(const std::string& extension_id);
107
[email protected]720ad1312012-02-27 23:07:36108 // Getter and setter for the lazy background page's keepalive count. This is
109 // the count of how many outstanding "things" are keeping the page alive.
110 // When this reaches 0, we will begin the process of shutting down the page.
111 // "Things" include pending events, resource loads, and API calls.
[email protected]1c321ee2012-05-21 03:02:34112 int GetLazyKeepaliveCount(const extensions::Extension* extension);
113 int IncrementLazyKeepaliveCount(const extensions::Extension* extension);
114 int DecrementLazyKeepaliveCount(const extensions::Extension* extension);
[email protected]720ad1312012-02-27 23:07:36115
[email protected]d72d3a62012-05-10 03:45:08116 void IncrementLazyKeepaliveCountForView(
117 content::RenderViewHost* render_view_host);
118
[email protected]584e6572013-02-16 07:02:33119 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36120 // pages.
[email protected]584e6572013-02-16 07:02:33121 void OnShouldSuspendAck(const std::string& extension_id, int sequence_id);
[email protected]103f19f2012-04-02 19:30:12122
[email protected]584e6572013-02-16 07:02:33123 // Same as above, for the Suspend message.
124 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12125
[email protected]6baff0b52012-03-06 01:30:18126 // Tracks network requests for a given RenderViewHost, used to know
127 // when network activity is idle for lazy background pages.
[email protected]eaabba22012-03-07 15:02:11128 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host);
129 void OnNetworkRequestDone(content::RenderViewHost* render_view_host);
[email protected]6baff0b52012-03-06 01:30:18130
[email protected]0d475e072012-07-26 02:30:42131 // Prevents |extension|'s background page from being closed and sends the
132 // onSuspendCanceled() event to it.
133 void CancelSuspend(const extensions::Extension* extension);
134
[email protected]bc535ee52010-08-31 18:40:32135 protected:
136 explicit ExtensionProcessManager(Profile* profile);
137
[email protected]94fe52e2009-06-13 10:09:06138 // Called just after |host| is created so it can be registered in our lists.
[email protected]3a1dc572012-07-31 22:25:13139 void OnExtensionHostCreated(extensions::ExtensionHost* host,
140 bool is_background);
[email protected]94fe52e2009-06-13 10:09:06141
[email protected]ae5497f2009-11-05 00:39:46142 // Called on browser shutdown to close our extension hosts.
143 void CloseBackgroundHosts();
144
[email protected]6c2381d2011-10-19 02:52:53145 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27146 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53147 const content::NotificationSource& source,
148 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32149
[email protected]a2f37552013-03-18 07:03:24150 // Load all background pages once the profile data is ready and the pages
151 // should be loaded.
152 void CreateBackgroundHostsForProfileStartup();
153
[email protected]72daaa92012-01-18 13:39:02154 // Gets the profile associated with site_instance_ and all other
155 // related SiteInstances.
156 Profile* GetProfile() const;
157
[email protected]6c2381d2011-10-19 02:52:53158 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06159
[email protected]d1fe1352012-04-26 00:47:32160 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49161 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05162
[email protected]72daaa92012-01-18 13:39:02163 // A SiteInstance related to the SiteInstance for all extensions in
164 // this profile. We create it in such a way that a new
165 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33166 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05167
[email protected]3e194992011-10-20 05:39:10168 private:
[email protected]103f19f2012-04-02 19:30:12169 // Extra information we keep for each extension's background page.
170 struct BackgroundPageData;
171 typedef std::string ExtensionId;
172 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
[email protected]eaabba22012-03-07 15:02:11173 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23174 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10175
[email protected]06024c62011-10-20 20:57:12176 // Close the given |host| iff it's a background page.
[email protected]3a1dc572012-07-31 22:25:13177 void CloseBackgroundHost(extensions::ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12178
[email protected]c64827a2012-03-23 01:36:21179 // Ensure browser object is not null except for certain situations.
180 void EnsureBrowserWhenRequired(Browser* browser,
[email protected]cb2edf22013-04-01 20:25:23181 extensions::ViewType view_type);
[email protected]c64827a2012-03-23 01:36:21182
[email protected]6baff0b52012-03-06 01:30:18183 // These are called when the extension transitions between idle and active.
184 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20185 void OnLazyBackgroundPageIdle(const std::string& extension_id,
186 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18187 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42188 void CloseLazyBackgroundPageNow(const std::string& extension_id,
189 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18190
[email protected]35e0e0792012-11-30 02:35:48191 // Potentially registers a RenderViewHost, if it is associated with an
192 // extension. Does nothing if this is not an extension renderer.
193 void RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12194
[email protected]caffe702012-05-03 05:30:17195 // Clears background page data for this extension.
196 void ClearBackgroundPageData(const std::string& extension_id);
197
[email protected]ba93a0a2013-04-19 23:03:15198 // Returns true if loading background pages should be deferred. This is
199 // true if there are no browser windows open and the browser process was
200 // started to show the app launcher.
201 bool DeferLoadingBackgroundHosts() const;
202
[email protected]35e0e0792012-11-30 02:35:48203 // Contains all active extension-related RenderViewHost instances for all
204 // extensions. We also keep a cache of the host's view type, because that
205 // information is not accessible at registration/deregistration time.
206 ExtensionRenderViews all_extension_views_;
207
[email protected]103f19f2012-04-02 19:30:12208 BackgroundPageDataMap background_page_data_;
209
[email protected]99c49b52012-05-02 01:50:20210 // The time to delay between an extension becoming idle and
[email protected]584e6572013-02-16 07:02:33211 // sending a ShouldSuspend message; read from command-line switch.
[email protected]99c49b52012-05-02 01:50:20212 base::TimeDelta event_page_idle_time_;
213
[email protected]584e6572013-02-16 07:02:33214 // The time to delay between sending a ShouldSuspend message and
215 // sending a Suspend message; read from command-line switch.
216 base::TimeDelta event_page_suspending_time_;
[email protected]99c49b52012-05-02 01:50:20217
218 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_;
219
[email protected]481e1a42009-05-06 20:56:05220 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager);
221};
222
223#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_