blob: eea12952a7f17e03f172bddf37a65fbe880fdebb [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]299d7f12012-05-23 05:31:1516#include "chrome/common/view_type.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]481e1a42009-05-06 20:56:0519
20class Browser;
[email protected]481e1a42009-05-06 20:56:0521class ExtensionHost;
[email protected]481e1a42009-05-06 20:56:0522class GURL;
23class Profile;
[email protected]b6583592012-01-25 19:52:3324
25namespace content {
[email protected]eaabba22012-03-07 15:02:1126class RenderViewHost;
[email protected]481e1a42009-05-06 20:56:0527class SiteInstance;
[email protected]b6583592012-01-25 19:52:3328};
[email protected]481e1a42009-05-06 20:56:0529
[email protected]1c321ee2012-05-21 03:02:3430namespace extensions {
31class Extension;
32}
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]d1fe1352012-04-26 00:47:3239 typedef std::set<ExtensionHost*> ExtensionHostSet;
40 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]1c321ee2012-05-21 03:02:3455 virtual ExtensionHost* CreateViewHost(const extensions::Extension* extension,
[email protected]029ad372011-05-20 17:12:5656 const GURL& url,
57 Browser* browser,
[email protected]299d7f12012-05-23 05:31:1558 chrome::ViewType view_type);
[email protected]029ad372011-05-20 17:12:5659 ExtensionHost* CreateViewHost(const GURL& url,
60 Browser* browser,
[email protected]299d7f12012-05-23 05:31:1561 chrome::ViewType view_type);
[email protected]1c321ee2012-05-21 03:02:3462 ExtensionHost* CreatePopupHost(const extensions::Extension* extension,
[email protected]029ad372011-05-20 17:12:5663 const GURL& url,
64 Browser* browser);
65 ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser);
[email protected]763cb9072012-06-25 21:39:5166 ExtensionHost* CreateDialogHost(const GURL& url);
[email protected]1c321ee2012-05-21 03:02:3467 ExtensionHost* CreateInfobarHost(const extensions::Extension* extension,
[email protected]029ad372011-05-20 17:12:5668 const GURL& url,
69 Browser* browser);
70 ExtensionHost* CreateInfobarHost(const GURL& url,
71 Browser* browser);
[email protected]a44657202012-01-09 05:48:3172
[email protected]550e9942010-03-10 01:40:3473 // Open the extension's options page.
[email protected]1c321ee2012-05-21 03:02:3474 void OpenOptionsPage(const extensions::Extension* extension,
75 Browser* browser);
[email protected]550e9942010-03-10 01:40:3476
[email protected]029ad372011-05-20 17:12:5677 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]bc535ee52010-08-31 18:40:3278 // displayed anywhere.
[email protected]1c321ee2012-05-21 03:02:3479 virtual void CreateBackgroundHost(const extensions::Extension* extension,
[email protected]9adb9692010-10-29 23:14:0280 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3281
[email protected]4814b512009-11-07 00:12:2982 // Gets the ExtensionHost for the background page for an extension, or NULL if
83 // the extension isn't running or doesn't have a background page.
[email protected]06024c62011-10-20 20:57:1284 ExtensionHost* GetBackgroundHostForExtension(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 // Registers a RenderViewHost as hosting a given extension.
[email protected]eaabba22012-03-07 15:02:1192 void RegisterRenderViewHost(content::RenderViewHost* render_view_host,
[email protected]1c321ee2012-05-21 03:02:3493 const extensions::Extension* extension);
[email protected]45776222009-07-15 20:21:5894
[email protected]3e194992011-10-20 05:39:1095 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1196 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1097
98 // Returns all RenderViewHosts that are registered for the specified
99 // extension.
[email protected]eaabba22012-03-07 15:02:11100 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:10101 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:58102
[email protected]d1fe1352012-04-26 00:47:32103 // Returns the extension associated with the specified RenderViewHost, or
104 // NULL.
[email protected]1c321ee2012-05-21 03:02:34105 const extensions::Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:32106 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:09107
[email protected]7042b682012-04-19 22:57:51108 // Returns true if the (lazy) background host for the given extension has
109 // already been sent the unload event and is shutting down.
110 bool IsBackgroundHostClosing(const std::string& extension_id);
111
[email protected]720ad1312012-02-27 23:07:36112 // Getter and setter for the lazy background page's keepalive count. This is
113 // the count of how many outstanding "things" are keeping the page alive.
114 // When this reaches 0, we will begin the process of shutting down the page.
115 // "Things" include pending events, resource loads, and API calls.
[email protected]1c321ee2012-05-21 03:02:34116 int GetLazyKeepaliveCount(const extensions::Extension* extension);
117 int IncrementLazyKeepaliveCount(const extensions::Extension* extension);
118 int DecrementLazyKeepaliveCount(const extensions::Extension* extension);
[email protected]720ad1312012-02-27 23:07:36119
[email protected]d72d3a62012-05-10 03:45:08120 void IncrementLazyKeepaliveCountForView(
121 content::RenderViewHost* render_view_host);
122
[email protected]103f19f2012-04-02 19:30:12123 // Handles a response to the ShouldUnload message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36124 // pages.
[email protected]103f19f2012-04-02 19:30:12125 void OnShouldUnloadAck(const std::string& extension_id, int sequence_id);
126
127 // Same as above, for the Unload message.
128 void OnUnloadAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12129
[email protected]6baff0b52012-03-06 01:30:18130 // Tracks network requests for a given RenderViewHost, used to know
131 // when network activity is idle for lazy background pages.
[email protected]eaabba22012-03-07 15:02:11132 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host);
133 void OnNetworkRequestDone(content::RenderViewHost* render_view_host);
[email protected]6baff0b52012-03-06 01:30:18134
[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.
139 void OnExtensionHostCreated(ExtensionHost* host, bool is_background);
140
[email protected]ae5497f2009-11-05 00:39:46141 // Called on browser shutdown to close our extension hosts.
142 void CloseBackgroundHosts();
143
[email protected]6c2381d2011-10-19 02:52:53144 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27145 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53146 const content::NotificationSource& source,
147 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32148
[email protected]72daaa92012-01-18 13:39:02149 // Gets the profile associated with site_instance_ and all other
150 // related SiteInstances.
151 Profile* GetProfile() const;
152
[email protected]6c2381d2011-10-19 02:52:53153 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06154
[email protected]d1fe1352012-04-26 00:47:32155 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49156 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05157
[email protected]72daaa92012-01-18 13:39:02158 // A SiteInstance related to the SiteInstance for all extensions in
159 // this profile. We create it in such a way that a new
160 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33161 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05162
[email protected]3e194992011-10-20 05:39:10163 private:
[email protected]103f19f2012-04-02 19:30:12164 // Extra information we keep for each extension's background page.
165 struct BackgroundPageData;
166 typedef std::string ExtensionId;
167 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
168
[email protected]3e194992011-10-20 05:39:10169 // Contains all extension-related RenderViewHost instances for all extensions.
[email protected]6baff0b52012-03-06 01:30:18170 // We also keep a cache of the host's view type, because that information
171 // is not accessible at registration/deregistration time.
[email protected]eaabba22012-03-07 15:02:11172 typedef std::map<content::RenderViewHost*,
[email protected]299d7f12012-05-23 05:31:15173 chrome::ViewType> ExtensionRenderViews;
[email protected]6baff0b52012-03-06 01:30:18174 ExtensionRenderViews all_extension_views_;
[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.
177 void CloseBackgroundHost(ExtensionHost* host);
178
[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]299d7f12012-05-23 05:31:15181 chrome::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]99c49b52012-05-02 01:50:20188 void CloseLazyBackgroundPageNow(const std::string& extension_id);
[email protected]6baff0b52012-03-06 01:30:18189
190 // Updates a potentially-registered RenderViewHost once it has been
191 // associated with a WebContents. This allows us to gather information that
192 // was not available when the host was first registered.
[email protected]eaabba22012-03-07 15:02:11193 void UpdateRegisteredRenderView(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]103f19f2012-04-02 19:30:12198 BackgroundPageDataMap background_page_data_;
199
[email protected]99c49b52012-05-02 01:50:20200 // The time to delay between an extension becoming idle and
201 // sending a ShouldUnload message; read from command-line switch.
202 base::TimeDelta event_page_idle_time_;
203
204 // The time to delay between sending a ShouldUnload message and
205 // sending a Unload message; read from command-line switch.
206 base::TimeDelta event_page_unloading_time_;
207
208 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_;
209
[email protected]481e1a42009-05-06 20:56:05210 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager);
211};
212
213#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_