blob: 9561c4b2a7afb67afc907bdc5821912530844191 [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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]481e1a42009-05-06 20:56:058
[email protected]45776222009-07-15 20:21:589#include <map>
[email protected]8a17bd52009-06-06 08:19:4910#include <set>
[email protected]45776222009-07-15 20:21:5811#include <string>
[email protected]481e1a42009-05-06 20:56:0512
[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"
16#include "base/time.h"
[email protected]299d7f12012-05-23 05:31:1517#include "chrome/common/view_type.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]481e1a42009-05-06 20:56:0520
21class Browser;
[email protected]481e1a42009-05-06 20:56:0522class ExtensionHost;
[email protected]481e1a42009-05-06 20:56:0523class GURL;
24class Profile;
[email protected]b6583592012-01-25 19:52:3325
26namespace content {
[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;
33}
34
[email protected]bc535ee52010-08-31 18:40:3235// Manages dynamic state of running Chromium extensions. There is one instance
36// of this class per Profile. OTR Profiles have a separate instance that keeps
37// track of split-mode extensions only.
[email protected]6c2381d2011-10-19 02:52:5338class ExtensionProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0539 public:
[email protected]d1fe1352012-04-26 00:47:3240 typedef std::set<ExtensionHost*> ExtensionHostSet;
41 typedef ExtensionHostSet::const_iterator const_iterator;
42
[email protected]bc535ee52010-08-31 18:40:3243 static ExtensionProcessManager* Create(Profile* profile);
44 virtual ~ExtensionProcessManager();
[email protected]481e1a42009-05-06 20:56:0545
[email protected]d1fe1352012-04-26 00:47:3246 const ExtensionHostSet& background_hosts() const {
47 return background_hosts_;
48 }
49
[email protected]d1fe1352012-04-26 00:47:3250 typedef std::set<content::RenderViewHost*> ViewSet;
51 const ViewSet GetAllViews() const;
52
[email protected]ab4eaf782009-06-10 00:11:2453 // Creates a new ExtensionHost with its associated view, grouping it in the
54 // appropriate SiteInstance (and therefore process) based on the URL and
55 // profile.
[email protected]1c321ee2012-05-21 03:02:3456 virtual ExtensionHost* CreateViewHost(const extensions::Extension* extension,
[email protected]029ad372011-05-20 17:12:5657 const GURL& url,
58 Browser* browser,
[email protected]299d7f12012-05-23 05:31:1559 chrome::ViewType view_type);
[email protected]029ad372011-05-20 17:12:5660 ExtensionHost* CreateViewHost(const GURL& url,
61 Browser* browser,
[email protected]299d7f12012-05-23 05:31:1562 chrome::ViewType view_type);
[email protected]1c321ee2012-05-21 03:02:3463 ExtensionHost* CreatePopupHost(const extensions::Extension* extension,
[email protected]029ad372011-05-20 17:12:5664 const GURL& url,
65 Browser* browser);
66 ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser);
67 ExtensionHost* CreateDialogHost(const GURL& url, Browser* browser);
[email protected]1c321ee2012-05-21 03:02:3468 ExtensionHost* CreateInfobarHost(const extensions::Extension* extension,
[email protected]029ad372011-05-20 17:12:5669 const GURL& url,
70 Browser* browser);
71 ExtensionHost* CreateInfobarHost(const GURL& url,
72 Browser* browser);
[email protected]a44657202012-01-09 05:48:3173
[email protected]550e9942010-03-10 01:40:3474 // Open the extension's options page.
[email protected]1c321ee2012-05-21 03:02:3475 void OpenOptionsPage(const extensions::Extension* extension,
76 Browser* browser);
[email protected]550e9942010-03-10 01:40:3477
[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]06024c62011-10-20 20:57:1285 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2986
[email protected]481e1a42009-05-06 20:56:0587 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4688 // TODO(aa): This only returns correct results for extensions and packaged
89 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3390 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0591
[email protected]3e194992011-10-20 05:39:1092 // Registers a RenderViewHost as hosting a given extension.
[email protected]eaabba22012-03-07 15:02:1193 void RegisterRenderViewHost(content::RenderViewHost* render_view_host,
[email protected]1c321ee2012-05-21 03:02:3494 const extensions::Extension* extension);
[email protected]45776222009-07-15 20:21:5895
[email protected]3e194992011-10-20 05:39:1096 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1197 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1098
99 // Returns all RenderViewHosts that are registered for the specified
100 // extension.
[email protected]eaabba22012-03-07 15:02:11101 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:10102 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:58103
[email protected]d1fe1352012-04-26 00:47:32104 // Returns the extension associated with the specified RenderViewHost, or
105 // NULL.
[email protected]1c321ee2012-05-21 03:02:34106 const extensions::Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:32107 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:09108
[email protected]7042b682012-04-19 22:57:51109 // Returns true if the (lazy) background host for the given extension has
110 // already been sent the unload event and is shutting down.
111 bool IsBackgroundHostClosing(const std::string& extension_id);
112
[email protected]720ad1312012-02-27 23:07:36113 // Getter and setter for the lazy background page's keepalive count. This is
114 // the count of how many outstanding "things" are keeping the page alive.
115 // When this reaches 0, we will begin the process of shutting down the page.
116 // "Things" include pending events, resource loads, and API calls.
[email protected]1c321ee2012-05-21 03:02:34117 int GetLazyKeepaliveCount(const extensions::Extension* extension);
118 int IncrementLazyKeepaliveCount(const extensions::Extension* extension);
119 int DecrementLazyKeepaliveCount(const extensions::Extension* extension);
[email protected]720ad1312012-02-27 23:07:36120
[email protected]d72d3a62012-05-10 03:45:08121 void IncrementLazyKeepaliveCountForView(
122 content::RenderViewHost* render_view_host);
123
[email protected]103f19f2012-04-02 19:30:12124 // Handles a response to the ShouldUnload message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36125 // pages.
[email protected]103f19f2012-04-02 19:30:12126 void OnShouldUnloadAck(const std::string& extension_id, int sequence_id);
127
128 // Same as above, for the Unload message.
129 void OnUnloadAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12130
[email protected]6baff0b52012-03-06 01:30:18131 // Tracks network requests for a given RenderViewHost, used to know
132 // when network activity is idle for lazy background pages.
[email protected]eaabba22012-03-07 15:02:11133 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host);
134 void OnNetworkRequestDone(content::RenderViewHost* render_view_host);
[email protected]6baff0b52012-03-06 01:30:18135
[email protected]bc535ee52010-08-31 18:40:32136 protected:
137 explicit ExtensionProcessManager(Profile* profile);
138
[email protected]94fe52e2009-06-13 10:09:06139 // Called just after |host| is created so it can be registered in our lists.
140 void OnExtensionHostCreated(ExtensionHost* host, bool is_background);
141
[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]72daaa92012-01-18 13:39:02150 // Gets the profile associated with site_instance_ and all other
151 // related SiteInstances.
152 Profile* GetProfile() const;
153
[email protected]6c2381d2011-10-19 02:52:53154 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06155
[email protected]d1fe1352012-04-26 00:47:32156 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49157 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05158
[email protected]72daaa92012-01-18 13:39:02159 // A SiteInstance related to the SiteInstance for all extensions in
160 // this profile. We create it in such a way that a new
161 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33162 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05163
[email protected]3e194992011-10-20 05:39:10164 private:
[email protected]103f19f2012-04-02 19:30:12165 // Extra information we keep for each extension's background page.
166 struct BackgroundPageData;
167 typedef std::string ExtensionId;
168 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
169
[email protected]3e194992011-10-20 05:39:10170 // Contains all extension-related RenderViewHost instances for all extensions.
[email protected]6baff0b52012-03-06 01:30:18171 // We also keep a cache of the host's view type, because that information
172 // is not accessible at registration/deregistration time.
[email protected]eaabba22012-03-07 15:02:11173 typedef std::map<content::RenderViewHost*,
[email protected]299d7f12012-05-23 05:31:15174 chrome::ViewType> ExtensionRenderViews;
[email protected]6baff0b52012-03-06 01:30:18175 ExtensionRenderViews all_extension_views_;
[email protected]3e194992011-10-20 05:39:10176
[email protected]06024c62011-10-20 20:57:12177 // Close the given |host| iff it's a background page.
178 void CloseBackgroundHost(ExtensionHost* host);
179
[email protected]c64827a2012-03-23 01:36:21180 // Ensure browser object is not null except for certain situations.
181 void EnsureBrowserWhenRequired(Browser* browser,
[email protected]299d7f12012-05-23 05:31:15182 chrome::ViewType view_type);
[email protected]c64827a2012-03-23 01:36:21183
[email protected]6baff0b52012-03-06 01:30:18184 // These are called when the extension transitions between idle and active.
185 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20186 void OnLazyBackgroundPageIdle(const std::string& extension_id,
187 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18188 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]99c49b52012-05-02 01:50:20189 void CloseLazyBackgroundPageNow(const std::string& extension_id);
[email protected]6baff0b52012-03-06 01:30:18190
191 // Updates a potentially-registered RenderViewHost once it has been
192 // associated with a WebContents. This allows us to gather information that
193 // was not available when the host was first registered.
[email protected]eaabba22012-03-07 15:02:11194 void UpdateRegisteredRenderView(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12195
[email protected]caffe702012-05-03 05:30:17196 // Clears background page data for this extension.
197 void ClearBackgroundPageData(const std::string& extension_id);
198
[email protected]103f19f2012-04-02 19:30:12199 BackgroundPageDataMap background_page_data_;
200
[email protected]99c49b52012-05-02 01:50:20201 // The time to delay between an extension becoming idle and
202 // sending a ShouldUnload message; read from command-line switch.
203 base::TimeDelta event_page_idle_time_;
204
205 // The time to delay between sending a ShouldUnload message and
206 // sending a Unload message; read from command-line switch.
207 base::TimeDelta event_page_unloading_time_;
208
209 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_;
210
[email protected]481e1a42009-05-06 20:56:05211 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager);
212};
213
214#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_