| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "content/public/common/view_type.h" | 15 #include "content/public/common/view_type.h" |
| 16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 18 | 18 |
| 19 class Browser; | 19 class Browser; |
| 20 class Extension; | 20 class Extension; |
| 21 class ExtensionHost; | 21 class ExtensionHost; |
| 22 class GURL; | 22 class GURL; |
| 23 class Profile; | 23 class Profile; |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 class RenderViewHost; | 26 class RenderViewHost; |
| 27 class SiteInstance; | 27 class SiteInstance; |
| 28 class WebContents; |
| 28 }; | 29 }; |
| 29 | 30 |
| 30 // Manages dynamic state of running Chromium extensions. There is one instance | 31 // 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 // of this class per Profile. OTR Profiles have a separate instance that keeps |
| 32 // track of split-mode extensions only. | 33 // track of split-mode extensions only. |
| 33 class ExtensionProcessManager : public content::NotificationObserver { | 34 class ExtensionProcessManager : public content::NotificationObserver { |
| 34 public: | 35 public: |
| 36 typedef std::set<ExtensionHost*> ExtensionHostSet; |
| 37 typedef ExtensionHostSet::const_iterator const_iterator; |
| 38 |
| 35 static ExtensionProcessManager* Create(Profile* profile); | 39 static ExtensionProcessManager* Create(Profile* profile); |
| 36 virtual ~ExtensionProcessManager(); | 40 virtual ~ExtensionProcessManager(); |
| 37 | 41 |
| 42 const ExtensionHostSet& background_hosts() const { |
| 43 return background_hosts_; |
| 44 } |
| 45 |
| 46 const ExtensionHostSet& platform_app_hosts() const { |
| 47 return platform_app_hosts_; |
| 48 } |
| 49 |
| 50 typedef std::set<content::WebContents*> ContentsSet; |
| 51 const ContentsSet GetAllContents() const; |
| 52 |
| 38 // Creates a new ExtensionHost with its associated view, grouping it in the | 53 // Creates a new ExtensionHost with its associated view, grouping it in the |
| 39 // appropriate SiteInstance (and therefore process) based on the URL and | 54 // appropriate SiteInstance (and therefore process) based on the URL and |
| 40 // profile. | 55 // profile. |
| 41 virtual ExtensionHost* CreateViewHost(const Extension* extension, | 56 virtual ExtensionHost* CreateViewHost(const Extension* extension, |
| 42 const GURL& url, | 57 const GURL& url, |
| 43 Browser* browser, | 58 Browser* browser, |
| 44 content::ViewType view_type); | 59 content::ViewType view_type); |
| 45 ExtensionHost* CreateViewHost(const GURL& url, | 60 ExtensionHost* CreateViewHost(const GURL& url, |
| 46 Browser* browser, | 61 Browser* browser, |
| 47 content::ViewType view_type); | 62 content::ViewType view_type); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 67 | 82 |
| 68 // Gets the ExtensionHost for the background page for an extension, or NULL if | 83 // 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. | 84 // the extension isn't running or doesn't have a background page. |
| 70 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id); | 85 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id); |
| 71 | 86 |
| 72 // Returns the SiteInstance that the given URL belongs to. | 87 // Returns the SiteInstance that the given URL belongs to. |
| 73 // TODO(aa): This only returns correct results for extensions and packaged | 88 // TODO(aa): This only returns correct results for extensions and packaged |
| 74 // apps, not hosted apps. | 89 // apps, not hosted apps. |
| 75 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url); | 90 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url); |
| 76 | 91 |
| 77 // Registers a RenderViewHost as hosting a given extension. | 92 // Registers a WebContents as hosting a given extension. |
| 78 void RegisterRenderViewHost(content::RenderViewHost* render_view_host, | 93 void RegisterWebContents(content::WebContents* web_contents, |
| 79 const Extension* extension); | 94 const Extension* extension); |
| 80 | 95 |
| 81 // Unregisters a RenderViewHost as hosting any extension. | 96 // Unregisters a WebContents as hosting any extension. |
| 82 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host); | 97 void UnregisterWebContents(content::WebContents* web_contents); |
| 83 | 98 |
| 84 // Returns all RenderViewHosts that are registered for the specified | 99 // Returns all RenderViewHosts that are registered for the specified |
| 85 // extension. | 100 // extension. |
| 86 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension( | 101 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension( |
| 87 const std::string& extension_id); | 102 const std::string& extension_id); |
| 88 | 103 |
| 89 // Returns true if |host| is managed by this process manager. | 104 // Returns the extension associated with the specified RenderViewHost, or |
| 90 bool HasExtensionHost(ExtensionHost* host) const; | 105 // NULL. |
| 106 const Extension* GetExtensionForRenderViewHost( |
| 107 content::RenderViewHost* render_view_host); |
| 91 | 108 |
| 92 // Getter and setter for the lazy background page's keepalive count. This is | 109 // 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. | 110 // 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. | 111 // When this reaches 0, we will begin the process of shutting down the page. |
| 95 // "Things" include pending events, resource loads, and API calls. | 112 // "Things" include pending events, resource loads, and API calls. |
| 96 int GetLazyKeepaliveCount(const Extension* extension); | 113 int GetLazyKeepaliveCount(const Extension* extension); |
| 97 int IncrementLazyKeepaliveCount(const Extension* extension); | 114 int IncrementLazyKeepaliveCount(const Extension* extension); |
| 98 int DecrementLazyKeepaliveCount(const Extension* extension); | 115 int DecrementLazyKeepaliveCount(const Extension* extension); |
| 99 | 116 |
| 100 // Handles a response to the ShouldUnload message, used for lazy background | 117 // Handles a response to the ShouldUnload message, used for lazy background |
| 101 // pages. | 118 // pages. |
| 102 void OnShouldUnloadAck(const std::string& extension_id, int sequence_id); | 119 void OnShouldUnloadAck(const std::string& extension_id, int sequence_id); |
| 103 | 120 |
| 104 // Same as above, for the Unload message. | 121 // Same as above, for the Unload message. |
| 105 void OnUnloadAck(const std::string& extension_id); | 122 void OnUnloadAck(const std::string& extension_id); |
| 106 | 123 |
| 107 // Tracks network requests for a given RenderViewHost, used to know | 124 // Tracks network requests for a given RenderViewHost, used to know |
| 108 // when network activity is idle for lazy background pages. | 125 // when network activity is idle for lazy background pages. |
| 109 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host); | 126 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host); |
| 110 void OnNetworkRequestDone(content::RenderViewHost* render_view_host); | 127 void OnNetworkRequestDone(content::RenderViewHost* render_view_host); |
| 111 | 128 |
| 112 typedef std::set<ExtensionHost*> ExtensionHostSet; | |
| 113 typedef ExtensionHostSet::const_iterator const_iterator; | |
| 114 const_iterator begin() const { return all_hosts_.begin(); } | |
| 115 const_iterator end() const { return all_hosts_.end(); } | |
| 116 | |
| 117 protected: | 129 protected: |
| 118 explicit ExtensionProcessManager(Profile* profile); | 130 explicit ExtensionProcessManager(Profile* profile); |
| 119 | 131 |
| 120 // Called just after |host| is created so it can be registered in our lists. | 132 // Called just after |host| is created so it can be registered in our lists. |
| 121 void OnExtensionHostCreated(ExtensionHost* host, bool is_background); | 133 void OnExtensionHostCreated(ExtensionHost* host, bool is_background); |
| 122 | 134 |
| 123 // Called on browser shutdown to close our extension hosts. | 135 // Called on browser shutdown to close our extension hosts. |
| 124 void CloseBackgroundHosts(); | 136 void CloseBackgroundHosts(); |
| 125 | 137 |
| 126 // content::NotificationObserver: | 138 // content::NotificationObserver: |
| 127 virtual void Observe(int type, | 139 virtual void Observe(int type, |
| 128 const content::NotificationSource& source, | 140 const content::NotificationSource& source, |
| 129 const content::NotificationDetails& details) OVERRIDE; | 141 const content::NotificationDetails& details) OVERRIDE; |
| 130 | 142 |
| 131 // Gets the profile associated with site_instance_ and all other | 143 // Gets the profile associated with site_instance_ and all other |
| 132 // related SiteInstances. | 144 // related SiteInstances. |
| 133 Profile* GetProfile() const; | 145 Profile* GetProfile() const; |
| 134 | 146 |
| 135 content::NotificationRegistrar registrar_; | 147 content::NotificationRegistrar registrar_; |
| 136 | 148 |
| 137 // The set of all ExtensionHosts managed by this process manager. | 149 // The set of ExtensionHosts running viewless background extensions. |
| 138 ExtensionHostSet all_hosts_; | 150 ExtensionHostSet background_hosts_; |
| 139 | 151 |
| 140 // The set of running viewless background extensions. | 152 // The set of ExtensionHosts running platform apps. |
| 141 ExtensionHostSet background_hosts_; | 153 ExtensionHostSet platform_app_hosts_; |
| 142 | 154 |
| 143 // A SiteInstance related to the SiteInstance for all extensions in | 155 // A SiteInstance related to the SiteInstance for all extensions in |
| 144 // this profile. We create it in such a way that a new | 156 // this profile. We create it in such a way that a new |
| 145 // browsing instance is created. This controls process grouping. | 157 // browsing instance is created. This controls process grouping. |
| 146 scoped_refptr<content::SiteInstance> site_instance_; | 158 scoped_refptr<content::SiteInstance> site_instance_; |
| 147 | 159 |
| 148 private: | 160 private: |
| 149 // Extra information we keep for each extension's background page. | 161 // Extra information we keep for each extension's background page. |
| 150 struct BackgroundPageData; | 162 struct BackgroundPageData; |
| 151 typedef std::string ExtensionId; | 163 typedef std::string ExtensionId; |
| 152 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; | 164 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; |
| 153 | 165 |
| 154 // Contains all extension-related RenderViewHost instances for all extensions. | 166 // Contains all extension-related WebContents instances for all extensions. |
| 155 // We also keep a cache of the host's view type, because that information | 167 // We also keep a cache of the host's view type, because that information |
| 156 // is not accessible at registration/deregistration time. | 168 // is not accessible at registration/deregistration time. |
| 157 typedef std::map<content::RenderViewHost*, | 169 typedef std::map<content::WebContents*, |
| 158 content::ViewType> ExtensionRenderViews; | 170 content::ViewType> ExtensionWebContents; |
| 159 ExtensionRenderViews all_extension_views_; | 171 ExtensionWebContents all_extension_contents_; |
| 160 | 172 |
| 161 // Close the given |host| iff it's a background page. | 173 // Close the given |host| iff it's a background page. |
| 162 void CloseBackgroundHost(ExtensionHost* host); | 174 void CloseBackgroundHost(ExtensionHost* host); |
| 163 | 175 |
| 164 // Ensure browser object is not null except for certain situations. | 176 // Ensure browser object is not null except for certain situations. |
| 165 void EnsureBrowserWhenRequired(Browser* browser, | 177 void EnsureBrowserWhenRequired(Browser* browser, |
| 166 content::ViewType view_type); | 178 content::ViewType view_type); |
| 167 | 179 |
| 168 // These are called when the extension transitions between idle and active. | 180 // These are called when the extension transitions between idle and active. |
| 169 // They control the process of closing the background page when idle. | 181 // They control the process of closing the background page when idle. |
| 170 void OnLazyBackgroundPageIdle(const std::string& extension_id); | 182 void OnLazyBackgroundPageIdle(const std::string& extension_id); |
| 171 void OnLazyBackgroundPageActive(const std::string& extension_id); | 183 void OnLazyBackgroundPageActive(const std::string& extension_id); |
| 172 | 184 |
| 173 // Updates a potentially-registered RenderViewHost once it has been | 185 // Updates a potentially-registered WebContents once it has been |
| 174 // associated with a WebContents. This allows us to gather information that | 186 // associated with a WebContents. This allows us to gather information that |
| 175 // was not available when the host was first registered. | 187 // was not available when the host was first registered. |
| 176 void UpdateRegisteredRenderView(content::RenderViewHost* render_view_host); | 188 void UpdateRegisteredWebContents(content::WebContents* web_contents); |
| 177 | 189 |
| 178 BackgroundPageDataMap background_page_data_; | 190 BackgroundPageDataMap background_page_data_; |
| 179 | 191 |
| 180 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); | 192 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); |
| 181 }; | 193 }; |
| 182 | 194 |
| 183 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | 195 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |
| OLD | NEW |