| 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 EXTENSIONS_BROWSER_PROCESS_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | 6 #define EXTENSIONS_BROWSER_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 GURL; | 21 class GURL; |
| 22 | 22 |
| 23 namespace content { | 23 namespace content { |
| 24 class BrowserContext; | 24 class BrowserContext; |
| 25 class DevToolsAgentHost; | 25 class DevToolsAgentHost; |
| 26 class RenderViewHost; | 26 class RenderViewHost; |
| 27 class SiteInstance; | 27 class SiteInstance; |
| 28 }; | 28 }; |
| 29 | 29 |
| 30 namespace extensions { | 30 namespace extensions { |
| 31 |
| 31 class Extension; | 32 class Extension; |
| 32 class ExtensionHost; | 33 class ExtensionHost; |
| 33 } | |
| 34 | 34 |
| 35 // Manages dynamic state of running Chromium extensions. There is one instance | 35 // 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 | 36 // of this class per Profile. OTR Profiles have a separate instance that keeps |
| 37 // track of split-mode extensions only. | 37 // track of split-mode extensions only. |
| 38 class ExtensionProcessManager : public content::NotificationObserver { | 38 class ProcessManager : public content::NotificationObserver { |
| 39 public: | 39 public: |
| 40 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet; | 40 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet; |
| 41 typedef ExtensionHostSet::const_iterator const_iterator; | 41 typedef ExtensionHostSet::const_iterator const_iterator; |
| 42 | 42 |
| 43 static ExtensionProcessManager* Create(content::BrowserContext* context); | 43 static ProcessManager* Create(content::BrowserContext* context); |
| 44 virtual ~ExtensionProcessManager(); | 44 virtual ~ProcessManager(); |
| 45 | 45 |
| 46 const ExtensionHostSet& background_hosts() const { | 46 const ExtensionHostSet& background_hosts() const { |
| 47 return background_hosts_; | 47 return background_hosts_; |
| 48 } | 48 } |
| 49 | 49 |
| 50 typedef std::set<content::RenderViewHost*> ViewSet; | 50 typedef std::set<content::RenderViewHost*> ViewSet; |
| 51 const ViewSet GetAllViews() const; | 51 const ViewSet GetAllViews() const; |
| 52 | 52 |
| 53 // Creates a new UI-less extension instance. Like CreateViewHost, but not | 53 // Creates a new UI-less extension instance. Like CreateViewHost, but not |
| 54 // displayed anywhere. | 54 // displayed anywhere. |
| 55 virtual extensions::ExtensionHost* CreateBackgroundHost( | 55 virtual ExtensionHost* CreateBackgroundHost(const Extension* extension, |
| 56 const extensions::Extension* extension, | 56 const GURL& url); |
| 57 const GURL& url); | |
| 58 | 57 |
| 59 // Gets the ExtensionHost for the background page for an extension, or NULL if | 58 // Gets the ExtensionHost for the background page for an extension, or NULL if |
| 60 // the extension isn't running or doesn't have a background page. | 59 // the extension isn't running or doesn't have a background page. |
| 61 extensions::ExtensionHost* GetBackgroundHostForExtension( | 60 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id); |
| 62 const std::string& extension_id); | |
| 63 | 61 |
| 64 // Returns the SiteInstance that the given URL belongs to. | 62 // Returns the SiteInstance that the given URL belongs to. |
| 65 // TODO(aa): This only returns correct results for extensions and packaged | 63 // TODO(aa): This only returns correct results for extensions and packaged |
| 66 // apps, not hosted apps. | 64 // apps, not hosted apps. |
| 67 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url); | 65 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url); |
| 68 | 66 |
| 69 // Unregisters a RenderViewHost as hosting any extension. | 67 // Unregisters a RenderViewHost as hosting any extension. |
| 70 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host); | 68 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host); |
| 71 | 69 |
| 72 // Returns all RenderViewHosts that are registered for the specified | 70 // Returns all RenderViewHosts that are registered for the specified |
| 73 // extension. | 71 // extension. |
| 74 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension( | 72 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension( |
| 75 const std::string& extension_id); | 73 const std::string& extension_id); |
| 76 | 74 |
| 77 // Returns the extension associated with the specified RenderViewHost, or | 75 // Returns the extension associated with the specified RenderViewHost, or |
| 78 // NULL. | 76 // NULL. |
| 79 const extensions::Extension* GetExtensionForRenderViewHost( | 77 const Extension* GetExtensionForRenderViewHost( |
| 80 content::RenderViewHost* render_view_host); | 78 content::RenderViewHost* render_view_host); |
| 81 | 79 |
| 82 // Returns true if the (lazy) background host for the given extension has | 80 // Returns true if the (lazy) background host for the given extension has |
| 83 // already been sent the unload event and is shutting down. | 81 // already been sent the unload event and is shutting down. |
| 84 bool IsBackgroundHostClosing(const std::string& extension_id); | 82 bool IsBackgroundHostClosing(const std::string& extension_id); |
| 85 | 83 |
| 86 // Getter and setter for the lazy background page's keepalive count. This is | 84 // Getter and setter for the lazy background page's keepalive count. This is |
| 87 // the count of how many outstanding "things" are keeping the page alive. | 85 // the count of how many outstanding "things" are keeping the page alive. |
| 88 // When this reaches 0, we will begin the process of shutting down the page. | 86 // When this reaches 0, we will begin the process of shutting down the page. |
| 89 // "Things" include pending events, resource loads, and API calls. | 87 // "Things" include pending events, resource loads, and API calls. |
| 90 int GetLazyKeepaliveCount(const extensions::Extension* extension); | 88 int GetLazyKeepaliveCount(const Extension* extension); |
| 91 int IncrementLazyKeepaliveCount(const extensions::Extension* extension); | 89 int IncrementLazyKeepaliveCount(const Extension* extension); |
| 92 int DecrementLazyKeepaliveCount(const extensions::Extension* extension); | 90 int DecrementLazyKeepaliveCount(const Extension* extension); |
| 93 | 91 |
| 94 void IncrementLazyKeepaliveCountForView( | 92 void IncrementLazyKeepaliveCountForView( |
| 95 content::RenderViewHost* render_view_host); | 93 content::RenderViewHost* render_view_host); |
| 96 | 94 |
| 97 // Handles a response to the ShouldSuspend message, used for lazy background | 95 // Handles a response to the ShouldSuspend message, used for lazy background |
| 98 // pages. | 96 // pages. |
| 99 void OnShouldSuspendAck(const std::string& extension_id, int sequence_id); | 97 void OnShouldSuspendAck(const std::string& extension_id, int sequence_id); |
| 100 | 98 |
| 101 // Same as above, for the Suspend message. | 99 // Same as above, for the Suspend message. |
| 102 void OnSuspendAck(const std::string& extension_id); | 100 void OnSuspendAck(const std::string& extension_id); |
| 103 | 101 |
| 104 // Tracks network requests for a given RenderViewHost, used to know | 102 // Tracks network requests for a given RenderViewHost, used to know |
| 105 // when network activity is idle for lazy background pages. | 103 // when network activity is idle for lazy background pages. |
| 106 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host); | 104 void OnNetworkRequestStarted(content::RenderViewHost* render_view_host); |
| 107 void OnNetworkRequestDone(content::RenderViewHost* render_view_host); | 105 void OnNetworkRequestDone(content::RenderViewHost* render_view_host); |
| 108 | 106 |
| 109 // Prevents |extension|'s background page from being closed and sends the | 107 // Prevents |extension|'s background page from being closed and sends the |
| 110 // onSuspendCanceled() event to it. | 108 // onSuspendCanceled() event to it. |
| 111 void CancelSuspend(const extensions::Extension* extension); | 109 void CancelSuspend(const Extension* extension); |
| 112 | 110 |
| 113 // If |defer| is true background host creation is to be deferred until this is | 111 // If |defer| is true background host creation is to be deferred until this is |
| 114 // called again with |defer| set to false, at which point all deferred | 112 // called again with |defer| set to false, at which point all deferred |
| 115 // background hosts will be created. Defaults to false. | 113 // background hosts will be created. Defaults to false. |
| 116 void DeferBackgroundHostCreation(bool defer); | 114 void DeferBackgroundHostCreation(bool defer); |
| 117 | 115 |
| 118 // Ensures background hosts are loaded for a new browser window. | 116 // Ensures background hosts are loaded for a new browser window. |
| 119 void OnBrowserWindowReady(); | 117 void OnBrowserWindowReady(); |
| 120 | 118 |
| 121 // Gets the BrowserContext associated with site_instance_ and all other | 119 // Gets the BrowserContext associated with site_instance_ and all other |
| 122 // related SiteInstances. | 120 // related SiteInstances. |
| 123 content::BrowserContext* GetBrowserContext() const; | 121 content::BrowserContext* GetBrowserContext() const; |
| 124 | 122 |
| 125 protected: | 123 protected: |
| 126 // If |context| is incognito pass the master context as |original_context|. | 124 // If |context| is incognito pass the master context as |original_context|. |
| 127 // Otherwise pass the same context for both. | 125 // Otherwise pass the same context for both. |
| 128 ExtensionProcessManager(content::BrowserContext* context, | 126 ProcessManager(content::BrowserContext* context, |
| 129 content::BrowserContext* original_context); | 127 content::BrowserContext* original_context); |
| 130 | 128 |
| 131 // Called on browser shutdown to close our extension hosts. | 129 // Called on browser shutdown to close our extension hosts. |
| 132 void CloseBackgroundHosts(); | 130 void CloseBackgroundHosts(); |
| 133 | 131 |
| 134 // content::NotificationObserver: | 132 // content::NotificationObserver: |
| 135 virtual void Observe(int type, | 133 virtual void Observe(int type, |
| 136 const content::NotificationSource& source, | 134 const content::NotificationSource& source, |
| 137 const content::NotificationDetails& details) OVERRIDE; | 135 const content::NotificationDetails& details) OVERRIDE; |
| 138 | 136 |
| 139 // Load all background pages once the profile data is ready and the pages | 137 // Load all background pages once the profile data is ready and the pages |
| 140 // should be loaded. | 138 // should be loaded. |
| 141 void CreateBackgroundHostsForProfileStartup(); | 139 void CreateBackgroundHostsForProfileStartup(); |
| 142 | 140 |
| 143 content::NotificationRegistrar registrar_; | 141 content::NotificationRegistrar registrar_; |
| 144 | 142 |
| 145 // The set of ExtensionHosts running viewless background extensions. | 143 // The set of ExtensionHosts running viewless background extensions. |
| 146 ExtensionHostSet background_hosts_; | 144 ExtensionHostSet background_hosts_; |
| 147 | 145 |
| 148 // A SiteInstance related to the SiteInstance for all extensions in | 146 // A SiteInstance related to the SiteInstance for all extensions in |
| 149 // this profile. We create it in such a way that a new | 147 // this profile. We create it in such a way that a new |
| 150 // browsing instance is created. This controls process grouping. | 148 // browsing instance is created. This controls process grouping. |
| 151 scoped_refptr<content::SiteInstance> site_instance_; | 149 scoped_refptr<content::SiteInstance> site_instance_; |
| 152 | 150 |
| 153 private: | 151 private: |
| 154 friend class ExtensionProcessManagerTest; | 152 friend class ProcessManagerTest; |
| 155 | 153 |
| 156 // Extra information we keep for each extension's background page. | 154 // Extra information we keep for each extension's background page. |
| 157 struct BackgroundPageData; | 155 struct BackgroundPageData; |
| 158 typedef std::string ExtensionId; | 156 typedef std::string ExtensionId; |
| 159 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; | 157 typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap; |
| 160 typedef std::map<content::RenderViewHost*, | 158 typedef std::map<content::RenderViewHost*, |
| 161 extensions::ViewType> ExtensionRenderViews; | 159 extensions::ViewType> ExtensionRenderViews; |
| 162 | 160 |
| 163 // Called just after |host| is created so it can be registered in our lists. | 161 // Called just after |host| is created so it can be registered in our lists. |
| 164 void OnBackgroundHostCreated(extensions::ExtensionHost* host); | 162 void OnBackgroundHostCreated(ExtensionHost* host); |
| 165 | 163 |
| 166 // Close the given |host| iff it's a background page. | 164 // Close the given |host| iff it's a background page. |
| 167 void CloseBackgroundHost(extensions::ExtensionHost* host); | 165 void CloseBackgroundHost(ExtensionHost* host); |
| 168 | 166 |
| 169 // These are called when the extension transitions between idle and active. | 167 // These are called when the extension transitions between idle and active. |
| 170 // They control the process of closing the background page when idle. | 168 // They control the process of closing the background page when idle. |
| 171 void OnLazyBackgroundPageIdle(const std::string& extension_id, | 169 void OnLazyBackgroundPageIdle(const std::string& extension_id, |
| 172 int sequence_id); | 170 int sequence_id); |
| 173 void OnLazyBackgroundPageActive(const std::string& extension_id); | 171 void OnLazyBackgroundPageActive(const std::string& extension_id); |
| 174 void CloseLazyBackgroundPageNow(const std::string& extension_id, | 172 void CloseLazyBackgroundPageNow(const std::string& extension_id, |
| 175 int sequence_id); | 173 int sequence_id); |
| 176 | 174 |
| 177 // Potentially registers a RenderViewHost, if it is associated with an | 175 // Potentially registers a RenderViewHost, if it is associated with an |
| (...skipping 28 matching lines...) Expand all Loading... |
| 206 base::TimeDelta event_page_suspending_time_; | 204 base::TimeDelta event_page_suspending_time_; |
| 207 | 205 |
| 208 // If true, then creation of background hosts is suspended. | 206 // If true, then creation of background hosts is suspended. |
| 209 bool defer_background_host_creation_; | 207 bool defer_background_host_creation_; |
| 210 | 208 |
| 211 // True if we have created the startup set of background hosts. | 209 // True if we have created the startup set of background hosts. |
| 212 bool startup_background_hosts_created_; | 210 bool startup_background_hosts_created_; |
| 213 | 211 |
| 214 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; | 212 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_; |
| 215 | 213 |
| 216 base::WeakPtrFactory<ExtensionProcessManager> weak_ptr_factory_; | 214 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_; |
| 217 | 215 |
| 218 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); | 216 DISALLOW_COPY_AND_ASSIGN(ProcessManager); |
| 219 }; | 217 }; |
| 220 | 218 |
| 221 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ | 219 } // namespace extensions |
| 220 |
| 221 #endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_ |
| OLD | NEW |