1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
6 #define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/time.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/common/view_type.h"
25 class DevToolsAgentHost
;
27 class RenderFrameHost
;
31 namespace extensions
{
36 // 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 // track of split-mode extensions only.
39 class ProcessManager
: public content::NotificationObserver
{
41 typedef std::set
<extensions::ExtensionHost
*> ExtensionHostSet
;
42 typedef ExtensionHostSet::const_iterator const_iterator
;
44 static ProcessManager
* Create(content::BrowserContext
* context
);
45 virtual ~ProcessManager();
47 const ExtensionHostSet
& background_hosts() const {
48 return background_hosts_
;
51 typedef std::set
<content::RenderViewHost
*> ViewSet
;
52 const ViewSet
GetAllViews() const;
54 // Creates a new UI-less extension instance. Like CreateViewHost, but not
55 // displayed anywhere.
56 virtual ExtensionHost
* CreateBackgroundHost(const Extension
* extension
,
59 // 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.
61 ExtensionHost
* GetBackgroundHostForExtension(const std::string
& extension_id
);
63 // Returns the SiteInstance that the given URL belongs to.
64 // TODO(aa): This only returns correct results for extensions and packaged
65 // apps, not hosted apps.
66 virtual content::SiteInstance
* GetSiteInstanceForURL(const GURL
& url
);
68 // Unregisters a RenderViewHost as hosting any extension.
69 void UnregisterRenderViewHost(content::RenderViewHost
* render_view_host
);
71 // Returns all RenderViewHosts that are registered for the specified
73 std::set
<content::RenderViewHost
*> GetRenderViewHostsForExtension(
74 const std::string
& extension_id
);
76 // Returns the extension associated with the specified RenderViewHost, or
78 const Extension
* GetExtensionForRenderViewHost(
79 content::RenderViewHost
* render_view_host
);
81 // Returns true if the (lazy) background host for the given extension has
82 // already been sent the unload event and is shutting down.
83 bool IsBackgroundHostClosing(const std::string
& extension_id
);
85 // Getter and setter for the lazy background page's keepalive count. This is
86 // the count of how many outstanding "things" are keeping the page alive.
87 // When this reaches 0, we will begin the process of shutting down the page.
88 // "Things" include pending events, resource loads, and API calls.
89 int GetLazyKeepaliveCount(const Extension
* extension
);
90 void IncrementLazyKeepaliveCount(const Extension
* extension
);
91 void DecrementLazyKeepaliveCount(const Extension
* extension
);
93 void IncrementLazyKeepaliveCountForView(
94 content::RenderViewHost
* render_view_host
);
96 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
97 // impulses will only keep the page alive for a limited amount of time unless
99 void KeepaliveImpulse(const Extension
* extension
);
101 // Handles a response to the ShouldSuspend message, used for lazy background
103 void OnShouldSuspendAck(const std::string
& extension_id
, int sequence_id
);
105 // Same as above, for the Suspend message.
106 void OnSuspendAck(const std::string
& extension_id
);
108 // Tracks network requests for a given RenderFrameHost, used to know
109 // when network activity is idle for lazy background pages.
110 void OnNetworkRequestStarted(content::RenderFrameHost
* render_frame_host
);
111 void OnNetworkRequestDone(content::RenderFrameHost
* render_frame_host
);
113 // Prevents |extension|'s background page from being closed and sends the
114 // onSuspendCanceled() event to it.
115 void CancelSuspend(const Extension
* extension
);
117 // Ensures background hosts are loaded for a new browser window.
118 void OnBrowserWindowReady();
120 // Gets the BrowserContext associated with site_instance_ and all other
121 // related SiteInstances.
122 content::BrowserContext
* GetBrowserContext() const;
124 // Sets callbacks for testing keepalive impulse behavior.
125 typedef base::Callback
<void(const std::string
& extension_id
)>
126 ImpulseCallbackForTesting
;
127 void SetKeepaliveImpulseCallbackForTesting(
128 const ImpulseCallbackForTesting
& callback
);
129 void SetKeepaliveImpulseDecrementCallbackForTesting(
130 const ImpulseCallbackForTesting
& callback
);
133 // If |context| is incognito pass the master context as |original_context|.
134 // Otherwise pass the same context for both.
135 ProcessManager(content::BrowserContext
* context
,
136 content::BrowserContext
* original_context
);
138 // Called on browser shutdown to close our extension hosts.
139 void CloseBackgroundHosts();
141 // content::NotificationObserver:
142 virtual void Observe(int type
,
143 const content::NotificationSource
& source
,
144 const content::NotificationDetails
& details
) OVERRIDE
;
146 // Load all background pages once the profile data is ready and the pages
148 void CreateBackgroundHostsForProfileStartup();
150 content::NotificationRegistrar registrar_
;
152 // The set of ExtensionHosts running viewless background extensions.
153 ExtensionHostSet background_hosts_
;
155 // A SiteInstance related to the SiteInstance for all extensions in
156 // this profile. We create it in such a way that a new
157 // browsing instance is created. This controls process grouping.
158 scoped_refptr
<content::SiteInstance
> site_instance_
;
161 friend class ProcessManagerTest
;
163 // Extra information we keep for each extension's background page.
164 struct BackgroundPageData
;
165 typedef std::string ExtensionId
;
166 typedef std::map
<ExtensionId
, BackgroundPageData
> BackgroundPageDataMap
;
167 typedef std::map
<content::RenderViewHost
*,
168 extensions::ViewType
> ExtensionRenderViews
;
170 // Called just after |host| is created so it can be registered in our lists.
171 void OnBackgroundHostCreated(ExtensionHost
* host
);
173 // Close the given |host| iff it's a background page.
174 void CloseBackgroundHost(ExtensionHost
* host
);
176 // Internal implementation of DecrementLazyKeepaliveCount with an
177 // |extension_id| known to have a lazy background page.
178 void DecrementLazyKeepaliveCount(const std::string
& extension_id
);
180 // Checks if keepalive impulses have occured, and adjusts keep alive count.
181 void OnKeepaliveImpulseCheck();
183 // These are called when the extension transitions between idle and active.
184 // They control the process of closing the background page when idle.
185 void OnLazyBackgroundPageIdle(const std::string
& extension_id
,
187 void OnLazyBackgroundPageActive(const std::string
& extension_id
);
188 void CloseLazyBackgroundPageNow(const std::string
& extension_id
,
191 // Potentially registers a RenderViewHost, if it is associated with an
192 // extension. Does nothing if this is not an extension renderer.
193 void RegisterRenderViewHost(content::RenderViewHost
* render_view_host
);
195 // Unregister RenderViewHosts and clear background page data for an extension
196 // which has been unloaded.
197 void UnregisterExtension(const std::string
& extension_id
);
199 // Clears background page data for this extension.
200 void ClearBackgroundPageData(const std::string
& extension_id
);
202 // Returns true if loading background pages should be deferred.
203 bool DeferLoadingBackgroundHosts() const;
205 void OnDevToolsStateChanged(content::DevToolsAgentHost
*, bool attached
);
207 // Contains all active extension-related RenderViewHost instances for all
208 // extensions. We also keep a cache of the host's view type, because that
209 // information is not accessible at registration/deregistration time.
210 ExtensionRenderViews all_extension_views_
;
212 BackgroundPageDataMap background_page_data_
;
214 // The time to delay between an extension becoming idle and
215 // sending a ShouldSuspend message; read from command-line switch.
216 base::TimeDelta event_page_idle_time_
;
218 // The time to delay between sending a ShouldSuspend message and
219 // sending a Suspend message; read from command-line switch.
220 base::TimeDelta event_page_suspending_time_
;
222 // True if we have created the startup set of background hosts.
223 bool startup_background_hosts_created_
;
225 base::Callback
<void(content::DevToolsAgentHost
*, bool)> devtools_callback_
;
227 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_
;
228 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_
;
230 base::WeakPtrFactory
<ProcessManager
> weak_ptr_factory_
;
232 DISALLOW_COPY_AND_ASSIGN(ProcessManager
);
235 } // namespace extensions
237 #endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_