[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 2 | // 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] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 8 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 9 | #include <map> |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 10 | #include <set> |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 11 | #include <string> |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 12 | |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 14afc01 | 2011-12-01 01:04:39 | [diff] [blame] | 15 | #include "content/public/common/view_type.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 16 | #include "content/public/browser/notification_observer.h" |
| 17 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 18 | |
| 19 | class Browser; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 20 | class Extension; |
| 21 | class ExtensionHost; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 22 | class GURL; |
| 23 | class Profile; |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 24 | class RenderViewHost; |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 25 | |
| 26 | namespace content { |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 27 | class SiteInstance; |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 28 | }; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 29 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 30 | // Manages dynamic state of running Chromium extensions. There is one instance |
| 31 | // of this class per Profile. OTR Profiles have a separate instance that keeps |
| 32 | // track of split-mode extensions only. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 33 | class ExtensionProcessManager : public content::NotificationObserver { |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 34 | public: |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 35 | static ExtensionProcessManager* Create(Profile* profile); |
| 36 | virtual ~ExtensionProcessManager(); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 37 | |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 38 | // Creates a new ExtensionHost with its associated view, grouping it in the |
| 39 | // appropriate SiteInstance (and therefore process) based on the URL and |
| 40 | // profile. |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 41 | virtual ExtensionHost* CreateViewHost(const Extension* extension, |
| 42 | const GURL& url, |
| 43 | Browser* browser, |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 44 | content::ViewType view_type); |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 45 | ExtensionHost* CreateViewHost(const GURL& url, |
| 46 | Browser* browser, |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 47 | content::ViewType view_type); |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 48 | ExtensionHost* CreatePopupHost(const Extension* extension, |
| 49 | const GURL& url, |
| 50 | Browser* browser); |
| 51 | ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser); |
| 52 | ExtensionHost* CreateDialogHost(const GURL& url, Browser* browser); |
| 53 | ExtensionHost* CreateInfobarHost(const Extension* extension, |
| 54 | const GURL& url, |
| 55 | Browser* browser); |
| 56 | ExtensionHost* CreateInfobarHost(const GURL& url, |
| 57 | Browser* browser); |
[email protected] | a6080c0 | 2012-01-11 23:33:49 | [diff] [blame] | 58 | ExtensionHost* CreateShellHost(const Extension* extension, const GURL& url); |
[email protected] | a4465720 | 2012-01-09 05:48:31 | [diff] [blame] | 59 | |
[email protected] | 550e994 | 2010-03-10 01:40:34 | [diff] [blame] | 60 | // Open the extension's options page. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 61 | void OpenOptionsPage(const Extension* extension, Browser* browser); |
[email protected] | 550e994 | 2010-03-10 01:40:34 | [diff] [blame] | 62 | |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 63 | // Creates a new UI-less extension instance. Like CreateViewHost, but not |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 64 | // displayed anywhere. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 65 | virtual void CreateBackgroundHost(const Extension* extension, |
| 66 | const GURL& url); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 67 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 68 | // Gets the ExtensionHost for the background page for an extension, or NULL if |
| 69 | // the extension isn't running or doesn't have a background page. |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 70 | ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 71 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 72 | // Returns the SiteInstance that the given URL belongs to. |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame] | 73 | // TODO(aa): This only returns correct results for extensions and packaged |
| 74 | // apps, not hosted apps. |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 75 | virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 76 | |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 77 | // Registers a RenderViewHost as hosting a given extension. |
| 78 | void RegisterRenderViewHost(RenderViewHost* render_view_host, |
| 79 | const Extension* extension); |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 80 | |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 81 | // Unregisters a RenderViewHost as hosting any extension. |
| 82 | void UnregisterRenderViewHost(RenderViewHost* render_view_host); |
| 83 | |
| 84 | // Returns all RenderViewHosts that are registered for the specified |
| 85 | // extension. |
| 86 | std::set<RenderViewHost*> GetRenderViewHostsForExtension( |
| 87 | const std::string& extension_id); |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 88 | |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 89 | // Returns true if |host| is managed by this process manager. |
| 90 | bool HasExtensionHost(ExtensionHost* host) const; |
| 91 | |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 92 | // Getter and setter for the lazy background page's keepalive count. This is |
| 93 | // the count of how many outstanding "things" are keeping the page alive. |
| 94 | // When this reaches 0, we will begin the process of shutting down the page. |
| 95 | // "Things" include pending events, resource loads, and API calls. |
| 96 | int GetLazyKeepaliveCount(const Extension* extension); |
| 97 | int IncrementLazyKeepaliveCount(const Extension* extension); |
| 98 | int DecrementLazyKeepaliveCount(const Extension* extension); |
| 99 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame^] | 100 | // Handles a response to the ShouldClose message, used for lazy background |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 101 | // pages. |
| 102 | void OnShouldCloseAck(const std::string& extension_id, int sequence_id); |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 103 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame^] | 104 | // Tracks network requests for a given RenderViewHost, used to know |
| 105 | // when network activity is idle for lazy background pages. |
| 106 | void OnNetworkRequestStarted(RenderViewHost* render_view_host); |
| 107 | void OnNetworkRequestDone(RenderViewHost* render_view_host); |
| 108 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 109 | typedef std::set<ExtensionHost*> ExtensionHostSet; |
| 110 | typedef ExtensionHostSet::const_iterator const_iterator; |
| 111 | const_iterator begin() const { return all_hosts_.begin(); } |
| 112 | const_iterator end() const { return all_hosts_.end(); } |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 113 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 114 | protected: |
| 115 | explicit ExtensionProcessManager(Profile* profile); |
| 116 | |
[email protected] | 94fe52e | 2009-06-13 10:09:06 | [diff] [blame] | 117 | // Called just after |host| is created so it can be registered in our lists. |
| 118 | void OnExtensionHostCreated(ExtensionHost* host, bool is_background); |
| 119 | |
[email protected] | ae5497f | 2009-11-05 00:39:46 | [diff] [blame] | 120 | // Called on browser shutdown to close our extension hosts. |
| 121 | void CloseBackgroundHosts(); |
| 122 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 123 | // content::NotificationObserver: |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 124 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 125 | const content::NotificationSource& source, |
| 126 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 127 | |
[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 128 | // Gets the profile associated with site_instance_ and all other |
| 129 | // related SiteInstances. |
| 130 | Profile* GetProfile() const; |
| 131 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 132 | content::NotificationRegistrar registrar_; |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 133 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 134 | // The set of all ExtensionHosts managed by this process manager. |
| 135 | ExtensionHostSet all_hosts_; |
| 136 | |
| 137 | // The set of running viewless background extensions. |
| 138 | ExtensionHostSet background_hosts_; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 139 | |
[email protected] | 72daaa9 | 2012-01-18 13:39:02 | [diff] [blame] | 140 | // A SiteInstance related to the SiteInstance for all extensions in |
| 141 | // this profile. We create it in such a way that a new |
| 142 | // browsing instance is created. This controls process grouping. |
[email protected] | b658359 | 2012-01-25 19:52:33 | [diff] [blame] | 143 | scoped_refptr<content::SiteInstance> site_instance_; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 144 | |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 145 | private: |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 146 | // Contains all extension-related RenderViewHost instances for all extensions. |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame^] | 147 | // We also keep a cache of the host's view type, because that information |
| 148 | // is not accessible at registration/deregistration time. |
| 149 | typedef std::map<RenderViewHost*, content::ViewType> ExtensionRenderViews; |
| 150 | ExtensionRenderViews all_extension_views_; |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 151 | |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 152 | // Close the given |host| iff it's a background page. |
| 153 | void CloseBackgroundHost(ExtensionHost* host); |
| 154 | |
[email protected] | 6baff0b5 | 2012-03-06 01:30:18 | [diff] [blame^] | 155 | // These are called when the extension transitions between idle and active. |
| 156 | // They control the process of closing the background page when idle. |
| 157 | void OnLazyBackgroundPageIdle(const std::string& extension_id); |
| 158 | void OnLazyBackgroundPageActive(const std::string& extension_id); |
| 159 | |
| 160 | // Updates a potentially-registered RenderViewHost once it has been |
| 161 | // associated with a WebContents. This allows us to gather information that |
| 162 | // was not available when the host was first registered. |
| 163 | void UpdateRegisteredRenderView(RenderViewHost* render_view_host); |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 164 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 165 | DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); |
| 166 | }; |
| 167 | |
| 168 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |