| 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 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 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 "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "extensions/common/view_type.h" | 19 #include "extensions/common/view_type.h" |
| 20 | 20 |
| 21 class Browser; | 21 class Browser; |
| 22 class GURL; | 22 class GURL; |
| 23 class Profile; | 23 class Profile; |
| 24 | 24 |
| 25 namespace content { | 25 namespace content { |
| 26 class BrowserContext; |
| 26 class DevToolsAgentHost; | 27 class DevToolsAgentHost; |
| 27 class RenderViewHost; | 28 class RenderViewHost; |
| 28 class SiteInstance; | 29 class SiteInstance; |
| 29 }; | 30 }; |
| 30 | 31 |
| 31 namespace extensions { | 32 namespace extensions { |
| 32 class Extension; | 33 class Extension; |
| 33 class ExtensionHost; | 34 class ExtensionHost; |
| 34 } | 35 } |
| 35 | 36 |
| 36 // Manages dynamic state of running Chromium extensions. There is one instance | 37 // Manages dynamic state of running Chromium extensions. There is one instance |
| 37 // of this class per Profile. OTR Profiles have a separate instance that keeps | 38 // of this class per Profile. OTR Profiles have a separate instance that keeps |
| 38 // track of split-mode extensions only. | 39 // track of split-mode extensions only. |
| 39 class ExtensionProcessManager : public content::NotificationObserver { | 40 class ExtensionProcessManager : public content::NotificationObserver { |
| 40 public: | 41 public: |
| 41 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet; | 42 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet; |
| 42 typedef ExtensionHostSet::const_iterator const_iterator; | 43 typedef ExtensionHostSet::const_iterator const_iterator; |
| 43 | 44 |
| 44 static ExtensionProcessManager* Create(Profile* profile); | 45 static ExtensionProcessManager* Create(content::BrowserContext* context); |
| 45 virtual ~ExtensionProcessManager(); | 46 virtual ~ExtensionProcessManager(); |
| 46 | 47 |
| 47 const ExtensionHostSet& background_hosts() const { | 48 const ExtensionHostSet& background_hosts() const { |
| 48 return background_hosts_; | 49 return background_hosts_; |
| 49 } | 50 } |
| 50 | 51 |
| 51 typedef std::set<content::RenderViewHost*> ViewSet; | 52 typedef std::set<content::RenderViewHost*> ViewSet; |
| 52 const ViewSet GetAllViews() const; | 53 const ViewSet GetAllViews() const; |
| 53 | 54 |
| 54 // Creates a new ExtensionHost with its associated view, grouping it in the | 55 // Creates a new ExtensionHost with its associated view, grouping it in the |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 // Prevents |extension|'s background page from being closed and sends the | 135 // Prevents |extension|'s background page from being closed and sends the |
| 135 // onSuspendCanceled() event to it. | 136 // onSuspendCanceled() event to it. |
| 136 void CancelSuspend(const extensions::Extension* extension); | 137 void CancelSuspend(const extensions::Extension* extension); |
| 137 | 138 |
| 138 // If |defer| is true background host creation is to be deferred until this is | 139 // If |defer| is true background host creation is to be deferred until this is |
| 139 // called again with |defer| set to false, at which point all deferred | 140 // called again with |defer| set to false, at which point all deferred |
| 140 // background hosts will be created. Defaults to false. | 141 // background hosts will be created. Defaults to false. |
| 141 void DeferBackgroundHostCreation(bool defer); | 142 void DeferBackgroundHostCreation(bool defer); |
| 142 | 143 |
| 143 protected: | 144 protected: |
| 144 explicit ExtensionProcessManager(Profile* profile); | 145 // If |context| is incognito pass the master context as |original_context|. |
| 146 // Otherwise pass the same context for both. |
| 147 ExtensionProcessManager(content::BrowserContext* context, |
| 148 content::BrowserContext* original_context); |
| 145 | 149 |
| 146 // Called just after |host| is created so it can be registered in our lists. | 150 // Called just after |host| is created so it can be registered in our lists. |
| 147 void OnExtensionHostCreated(extensions::ExtensionHost* host, | 151 void OnExtensionHostCreated(extensions::ExtensionHost* host, |
| 148 bool is_background); | 152 bool is_background); |
| 149 | 153 |
| 150 // Called on browser shutdown to close our extension hosts. | 154 // Called on browser shutdown to close our extension hosts. |
| 151 void CloseBackgroundHosts(); | 155 void CloseBackgroundHosts(); |
| 152 | 156 |
| 153 // content::NotificationObserver: | 157 // content::NotificationObserver: |
| 154 virtual void Observe(int type, | 158 virtual void Observe(int type, |
| 155 const content::NotificationSource& source, | 159 const content::NotificationSource& source, |
| 156 const content::NotificationDetails& details) OVERRIDE; | 160 const content::NotificationDetails& details) OVERRIDE; |
| 157 | 161 |
| 158 // Load all background pages once the profile data is ready and the pages | 162 // Load all background pages once the profile data is ready and the pages |
| 159 // should be loaded. | 163 // should be loaded. |
| 160 void CreateBackgroundHostsForProfileStartup(); | 164 void CreateBackgroundHostsForProfileStartup(); |
| 161 | 165 |
| 162 // Gets the profile associated with site_instance_ and all other | 166 // Gets the profile associated with site_instance_ and all other |
| 163 // related SiteInstances. | 167 // related SiteInstances. |
| 164 Profile* GetProfile() const; | 168 Profile* GetProfile() const; |
| 165 | 169 |
| 170 // Returns the same value as GetProfile() as a BrowserContext. |
| 171 content::BrowserContext* GetBrowserContext() const; |
| 172 |
| 166 content::NotificationRegistrar registrar_; | 173 content::NotificationRegistrar registrar_; |
| 167 | 174 |
| 168 // The set of ExtensionHosts running viewless background extensions. | 175 // The set of ExtensionHosts running viewless background extensions. |
| 169 ExtensionHostSet background_hosts_; | 176 ExtensionHostSet background_hosts_; |
| 170 | 177 |
| 171 // A SiteInstance related to the SiteInstance for all extensions in | 178 // A SiteInstance related to the SiteInstance for all extensions in |
| 172 // this profile. We create it in such a way that a new | 179 // this profile. We create it in such a way that a new |
| 173 // browsing instance is created. This controls process grouping. | 180 // browsing instance is created. This controls process grouping. |
| 174 scoped_refptr<content::SiteInstance> site_instance_; | 181 scoped_refptr<content::SiteInstance> site_instance_; |
| 175 | 182 |
| 176 private: | 183 private: |
| 184 friend class ExtensionProcessManagerTest; |
| 185 |
| 177 // Extra information we keep for each extension's background page. | 186 // Extra information we keep for each extension's background page. |
| 178 struct BackgroundPageData; | 187 struct BackgroundPageData; |
| 179 typedef std::string ExtensionId; | 188 typedef std::string ExtensionId; |
| 180 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; | 189 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; |
| 181 typedef std::map<content::RenderViewHost*, | 190 typedef std::map<content::RenderViewHost*, |
| 182 extensions::ViewType> ExtensionRenderViews; | 191 extensions::ViewType> ExtensionRenderViews; |
| 183 | 192 |
| 184 // Close the given |host| iff it's a background page. | 193 // Close the given |host| iff it's a background page. |
| 185 void CloseBackgroundHost(extensions::ExtensionHost* host); | 194 void CloseBackgroundHost(extensions::ExtensionHost* host); |
| 186 | 195 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 200 // extension. Does nothing if this is not an extension renderer. | 209 // extension. Does nothing if this is not an extension renderer. |
| 201 void RegisterRenderViewHost(content::RenderViewHost* render_view_host); | 210 void RegisterRenderViewHost(content::RenderViewHost* render_view_host); |
| 202 | 211 |
| 203 // Unregister RenderViewHosts and clear background page data for an extension | 212 // Unregister RenderViewHosts and clear background page data for an extension |
| 204 // which has been unloaded. | 213 // which has been unloaded. |
| 205 void UnregisterExtension(const std::string& extension_id); | 214 void UnregisterExtension(const std::string& extension_id); |
| 206 | 215 |
| 207 // Clears background page data for this extension. | 216 // Clears background page data for this extension. |
| 208 void ClearBackgroundPageData(const std::string& extension_id); | 217 void ClearBackgroundPageData(const std::string& extension_id); |
| 209 | 218 |
| 210 // Returns true if loading background pages should be deferred. This is | 219 // Returns true if loading background pages should be deferred. |
| 211 // true if there are no browser windows open and the browser process was | |
| 212 // started to show the app launcher, or if DeferBackgroundHostCreation was | |
| 213 // called with true, or if the profile is not yet valid. | |
| 214 bool DeferLoadingBackgroundHosts() const; | 220 bool DeferLoadingBackgroundHosts() const; |
| 215 | 221 |
| 216 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached); | 222 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached); |
| 217 | 223 |
| 218 // Contains all active extension-related RenderViewHost instances for all | 224 // Contains all active extension-related RenderViewHost instances for all |
| 219 // extensions. We also keep a cache of the host's view type, because that | 225 // extensions. We also keep a cache of the host's view type, because that |
| 220 // information is not accessible at registration/deregistration time. | 226 // information is not accessible at registration/deregistration time. |
| 221 ExtensionRenderViews all_extension_views_; | 227 ExtensionRenderViews all_extension_views_; |
| 222 | 228 |
| 223 BackgroundPageDataMap background_page_data_; | 229 BackgroundPageDataMap background_page_data_; |
| 224 | 230 |
| 225 // The time to delay between an extension becoming idle and | 231 // The time to delay between an extension becoming idle and |
| 226 // sending a ShouldSuspend message; read from command-line switch. | 232 // sending a ShouldSuspend message; read from command-line switch. |
| 227 base::TimeDelta event_page_idle_time_; | 233 base::TimeDelta event_page_idle_time_; |
| 228 | 234 |
| 229 // The time to delay between sending a ShouldSuspend message and | 235 // The time to delay between sending a ShouldSuspend message and |
| 230 // sending a Suspend message; read from command-line switch. | 236 // sending a Suspend message; read from command-line switch. |
| 231 base::TimeDelta event_page_suspending_time_; | 237 base::TimeDelta event_page_suspending_time_; |
| 232 | 238 |
| 233 // If true, then creation of background hosts is suspended. | 239 // If true, then creation of background hosts is suspended. |
| 234 bool defer_background_host_creation_; | 240 bool defer_background_host_creation_; |
| 235 | 241 |
| 242 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; |
| 243 |
| 236 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_; | 244 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_; |
| 237 | 245 |
| 238 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; | |
| 239 | |
| 240 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); | 246 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); |
| 241 }; | 247 }; |
| 242 | 248 |
| 243 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | 249 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |
| OLD | NEW |