blob: 1059db3028bfcfbf17c09886e04b823eeb1b2a8e [file] [log] [blame]
[email protected]98b6d942013-11-10 00:34:071// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]481e1a42009-05-06 20:56:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]98b6d942013-11-10 00:34:075#ifndef EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
6#define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
[email protected]481e1a42009-05-06 20:56:057
[email protected]45776222009-07-15 20:21:588#include <map>
[email protected]8a17bd52009-06-06 08:19:499#include <set>
[email protected]45776222009-07-15 20:21:5810#include <string>
[email protected]481e1a42009-05-06 20:56:0511
[email protected]a23f62e2013-04-26 13:13:0212#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5413#include "base/compiler_specific.h"
[email protected]3b63f8f42011-03-28 01:54:1514#include "base/memory/ref_counted.h"
[email protected]99c49b52012-05-02 01:50:2015#include "base/memory/weak_ptr.h"
[email protected]41a17c52013-06-28 00:27:5316#include "base/time/time.h"
[email protected]6c2381d2011-10-19 02:52:5317#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
[email protected]cb2edf22013-04-01 20:25:2319#include "extensions/common/view_type.h"
[email protected]481e1a42009-05-06 20:56:0520
[email protected]481e1a42009-05-06 20:56:0521class GURL;
[email protected]b6583592012-01-25 19:52:3322
23namespace content {
[email protected]7f474212013-11-05 04:26:1624class BrowserContext;
[email protected]a23f62e2013-04-26 13:13:0225class DevToolsAgentHost;
[email protected]eaabba22012-03-07 15:02:1126class RenderViewHost;
[email protected]9dfed872013-12-30 23:08:5627class RenderFrameHost;
[email protected]481e1a42009-05-06 20:56:0528class SiteInstance;
[email protected]b6583592012-01-25 19:52:3329};
[email protected]481e1a42009-05-06 20:56:0530
[email protected]1c321ee2012-05-21 03:02:3431namespace extensions {
[email protected]98b6d942013-11-10 00:34:0732
[email protected]1c321ee2012-05-21 03:02:3433class Extension;
[email protected]3a1dc572012-07-31 22:25:1334class ExtensionHost;
[email protected]1c321ee2012-05-21 03:02:3435
[email protected]bc535ee52010-08-31 18:40:3236// 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.
[email protected]98b6d942013-11-10 00:34:0739class ProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0540 public:
[email protected]3a1dc572012-07-31 22:25:1341 typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
[email protected]d1fe1352012-04-26 00:47:3242 typedef ExtensionHostSet::const_iterator const_iterator;
43
[email protected]98b6d942013-11-10 00:34:0744 static ProcessManager* Create(content::BrowserContext* context);
45 virtual ~ProcessManager();
[email protected]481e1a42009-05-06 20:56:0546
[email protected]d1fe1352012-04-26 00:47:3247 const ExtensionHostSet& background_hosts() const {
48 return background_hosts_;
49 }
50
[email protected]d1fe1352012-04-26 00:47:3251 typedef std::set<content::RenderViewHost*> ViewSet;
52 const ViewSet GetAllViews() const;
53
[email protected]029ad372011-05-20 17:12:5654 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]bc535ee52010-08-31 18:40:3255 // displayed anywhere.
[email protected]98b6d942013-11-10 00:34:0756 virtual ExtensionHost* CreateBackgroundHost(const Extension* extension,
57 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3258
[email protected]4814b512009-11-07 00:12:2959 // 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.
[email protected]98b6d942013-11-10 00:34:0761 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2962
[email protected]481e1a42009-05-06 20:56:0563 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4664 // TODO(aa): This only returns correct results for extensions and packaged
65 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3366 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0567
[email protected]3e194992011-10-20 05:39:1068 // Unregisters a RenderViewHost as hosting any extension.
[email protected]eaabba22012-03-07 15:02:1169 void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]3e194992011-10-20 05:39:1070
71 // Returns all RenderViewHosts that are registered for the specified
72 // extension.
[email protected]eaabba22012-03-07 15:02:1173 std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
[email protected]3e194992011-10-20 05:39:1074 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:5875
[email protected]d1fe1352012-04-26 00:47:3276 // Returns the extension associated with the specified RenderViewHost, or
77 // NULL.
[email protected]98b6d942013-11-10 00:34:0778 const Extension* GetExtensionForRenderViewHost(
[email protected]d1fe1352012-04-26 00:47:3279 content::RenderViewHost* render_view_host);
[email protected]27e469a2010-01-11 20:35:0980
[email protected]7042b682012-04-19 22:57:5181 // 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);
84
[email protected]720ad1312012-02-27 23:07:3685 // 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.
[email protected]98b6d942013-11-10 00:34:0789 int GetLazyKeepaliveCount(const Extension* extension);
[email protected]388770152013-12-03 01:25:3290 void IncrementLazyKeepaliveCount(const Extension* extension);
91 void DecrementLazyKeepaliveCount(const Extension* extension);
[email protected]720ad1312012-02-27 23:07:3692
[email protected]d72d3a62012-05-10 03:45:0893 void IncrementLazyKeepaliveCountForView(
94 content::RenderViewHost* render_view_host);
95
[email protected]388770152013-12-03 01:25:3296 // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
97 // impulses will only keep the page alive for a limited amount of time unless
98 // called regularly.
99 void KeepaliveImpulse(const Extension* extension);
100
[email protected]584e6572013-02-16 07:02:33101 // Handles a response to the ShouldSuspend message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36102 // pages.
[email protected]584e6572013-02-16 07:02:33103 void OnShouldSuspendAck(const std::string& extension_id, int sequence_id);
[email protected]103f19f2012-04-02 19:30:12104
[email protected]584e6572013-02-16 07:02:33105 // Same as above, for the Suspend message.
106 void OnSuspendAck(const std::string& extension_id);
[email protected]06024c62011-10-20 20:57:12107
[email protected]9dfed872013-12-30 23:08:56108 // Tracks network requests for a given RenderFrameHost, used to know
[email protected]6baff0b52012-03-06 01:30:18109 // when network activity is idle for lazy background pages.
[email protected]9dfed872013-12-30 23:08:56110 void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host);
111 void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host);
[email protected]6baff0b52012-03-06 01:30:18112
[email protected]0d475e072012-07-26 02:30:42113 // Prevents |extension|'s background page from being closed and sends the
114 // onSuspendCanceled() event to it.
[email protected]98b6d942013-11-10 00:34:07115 void CancelSuspend(const Extension* extension);
[email protected]0d475e072012-07-26 02:30:42116
[email protected]aa3dd492013-11-05 17:09:09117 // Ensures background hosts are loaded for a new browser window.
118 void OnBrowserWindowReady();
119
120 // Gets the BrowserContext associated with site_instance_ and all other
121 // related SiteInstances.
122 content::BrowserContext* GetBrowserContext() const;
123
[email protected]b6cd7202014-01-07 17:46:55124 // 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);
131
[email protected]bc535ee52010-08-31 18:40:32132 protected:
[email protected]7f474212013-11-05 04:26:16133 // If |context| is incognito pass the master context as |original_context|.
134 // Otherwise pass the same context for both.
[email protected]98b6d942013-11-10 00:34:07135 ProcessManager(content::BrowserContext* context,
136 content::BrowserContext* original_context);
[email protected]bc535ee52010-08-31 18:40:32137
[email protected]ae5497f2009-11-05 00:39:46138 // Called on browser shutdown to close our extension hosts.
139 void CloseBackgroundHosts();
140
[email protected]6c2381d2011-10-19 02:52:53141 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27142 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53143 const content::NotificationSource& source,
144 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32145
[email protected]a2f37552013-03-18 07:03:24146 // Load all background pages once the profile data is ready and the pages
147 // should be loaded.
148 void CreateBackgroundHostsForProfileStartup();
149
[email protected]6c2381d2011-10-19 02:52:53150 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06151
[email protected]d1fe1352012-04-26 00:47:32152 // The set of ExtensionHosts running viewless background extensions.
[email protected]8a17bd52009-06-06 08:19:49153 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05154
[email protected]72daaa92012-01-18 13:39:02155 // 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.
[email protected]b6583592012-01-25 19:52:33158 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05159
[email protected]3e194992011-10-20 05:39:10160 private:
[email protected]98b6d942013-11-10 00:34:07161 friend class ProcessManagerTest;
[email protected]7f474212013-11-05 04:26:16162
[email protected]103f19f2012-04-02 19:30:12163 // 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;
[email protected]eaabba22012-03-07 15:02:11167 typedef std::map<content::RenderViewHost*,
[email protected]cb2edf22013-04-01 20:25:23168 extensions::ViewType> ExtensionRenderViews;
[email protected]3e194992011-10-20 05:39:10169
[email protected]94de8cb2013-11-07 06:29:21170 // Called just after |host| is created so it can be registered in our lists.
[email protected]98b6d942013-11-10 00:34:07171 void OnBackgroundHostCreated(ExtensionHost* host);
[email protected]94de8cb2013-11-07 06:29:21172
[email protected]06024c62011-10-20 20:57:12173 // Close the given |host| iff it's a background page.
[email protected]98b6d942013-11-10 00:34:07174 void CloseBackgroundHost(ExtensionHost* host);
[email protected]06024c62011-10-20 20:57:12175
[email protected]388770152013-12-03 01:25:32176 // Internal implementation of DecrementLazyKeepaliveCount with an
177 // |extension_id| known to have a lazy background page.
178 void DecrementLazyKeepaliveCount(const std::string& extension_id);
179
180 // Checks if keepalive impulses have occured, and adjusts keep alive count.
181 void OnKeepaliveImpulseCheck();
182
[email protected]6baff0b52012-03-06 01:30:18183 // These are called when the extension transitions between idle and active.
184 // They control the process of closing the background page when idle.
[email protected]99c49b52012-05-02 01:50:20185 void OnLazyBackgroundPageIdle(const std::string& extension_id,
186 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18187 void OnLazyBackgroundPageActive(const std::string& extension_id);
[email protected]0d475e072012-07-26 02:30:42188 void CloseLazyBackgroundPageNow(const std::string& extension_id,
189 int sequence_id);
[email protected]6baff0b52012-03-06 01:30:18190
[email protected]35e0e0792012-11-30 02:35:48191 // Potentially registers a RenderViewHost, if it is associated with an
192 // extension. Does nothing if this is not an extension renderer.
[email protected]96cac5e2014-02-18 16:22:15193 // Returns true, if render_view_host was registered (it is associated
194 // with an extension).
195 bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12196
[email protected]5b3ee852013-09-26 06:33:10197 // Unregister RenderViewHosts and clear background page data for an extension
198 // which has been unloaded.
199 void UnregisterExtension(const std::string& extension_id);
200
[email protected]caffe702012-05-03 05:30:17201 // Clears background page data for this extension.
202 void ClearBackgroundPageData(const std::string& extension_id);
203
[email protected]7f474212013-11-05 04:26:16204 // Returns true if loading background pages should be deferred.
[email protected]ba93a0a2013-04-19 23:03:15205 bool DeferLoadingBackgroundHosts() const;
206
[email protected]a23f62e2013-04-26 13:13:02207 void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
208
[email protected]35e0e0792012-11-30 02:35:48209 // Contains all active extension-related RenderViewHost instances for all
210 // extensions. We also keep a cache of the host's view type, because that
211 // information is not accessible at registration/deregistration time.
212 ExtensionRenderViews all_extension_views_;
213
[email protected]103f19f2012-04-02 19:30:12214 BackgroundPageDataMap background_page_data_;
215
[email protected]99c49b52012-05-02 01:50:20216 // The time to delay between an extension becoming idle and
[email protected]584e6572013-02-16 07:02:33217 // sending a ShouldSuspend message; read from command-line switch.
[email protected]99c49b52012-05-02 01:50:20218 base::TimeDelta event_page_idle_time_;
219
[email protected]584e6572013-02-16 07:02:33220 // The time to delay between sending a ShouldSuspend message and
221 // sending a Suspend message; read from command-line switch.
222 base::TimeDelta event_page_suspending_time_;
[email protected]99c49b52012-05-02 01:50:20223
[email protected]aa3dd492013-11-05 17:09:09224 // True if we have created the startup set of background hosts.
225 bool startup_background_hosts_created_;
226
[email protected]75e7bac2013-11-01 23:33:42227 base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
228
[email protected]b6cd7202014-01-07 17:46:55229 ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
230 ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
231
[email protected]98b6d942013-11-10 00:34:07232 base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
[email protected]7f474212013-11-05 04:26:16233
[email protected]98b6d942013-11-10 00:34:07234 DISALLOW_COPY_AND_ASSIGN(ProcessManager);
[email protected]481e1a42009-05-06 20:56:05235};
236
[email protected]98b6d942013-11-10 00:34:07237} // namespace extensions
238
239#endif // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_