blob: 70a2e28a3b40cb6d7d0d4bc99eb1e2dbbc2034cd [file] [log] [blame]
[email protected]a44657202012-01-09 05:48:311// Copyright (c) 2012 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
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]481e1a42009-05-06 20:56:058
[email protected]45776222009-07-15 20:21:589#include <map>
[email protected]8a17bd52009-06-06 08:19:4910#include <set>
[email protected]45776222009-07-15 20:21:5811#include <string>
[email protected]481e1a42009-05-06 20:56:0512
[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]14afc012011-12-01 01:04:3915#include "content/public/common/view_type.h"
[email protected]6c2381d2011-10-19 02:52:5316#include "content/public/browser/notification_observer.h"
17#include "content/public/browser/notification_registrar.h"
[email protected]481e1a42009-05-06 20:56:0518
19class Browser;
[email protected]481e1a42009-05-06 20:56:0520class Extension;
21class ExtensionHost;
[email protected]481e1a42009-05-06 20:56:0522class GURL;
23class Profile;
[email protected]3e194992011-10-20 05:39:1024class RenderViewHost;
[email protected]b6583592012-01-25 19:52:3325
26namespace content {
[email protected]481e1a42009-05-06 20:56:0527class SiteInstance;
[email protected]b6583592012-01-25 19:52:3328};
[email protected]481e1a42009-05-06 20:56:0529
[email protected]bc535ee52010-08-31 18:40:3230// 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// track of split-mode extensions only.
[email protected]6c2381d2011-10-19 02:52:5333class ExtensionProcessManager : public content::NotificationObserver {
[email protected]481e1a42009-05-06 20:56:0534 public:
[email protected]bc535ee52010-08-31 18:40:3235 static ExtensionProcessManager* Create(Profile* profile);
36 virtual ~ExtensionProcessManager();
[email protected]481e1a42009-05-06 20:56:0537
[email protected]ab4eaf782009-06-10 00:11:2438 // Creates a new ExtensionHost with its associated view, grouping it in the
39 // appropriate SiteInstance (and therefore process) based on the URL and
40 // profile.
[email protected]029ad372011-05-20 17:12:5641 virtual ExtensionHost* CreateViewHost(const Extension* extension,
42 const GURL& url,
43 Browser* browser,
[email protected]da4dfc42011-10-12 15:53:5644 content::ViewType view_type);
[email protected]029ad372011-05-20 17:12:5645 ExtensionHost* CreateViewHost(const GURL& url,
46 Browser* browser,
[email protected]da4dfc42011-10-12 15:53:5647 content::ViewType view_type);
[email protected]029ad372011-05-20 17:12:5648 ExtensionHost* CreatePopupHost(const Extension* extension,
49 const GURL& url,
50 Browser* browser);
51 ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser);
52 ExtensionHost* CreateDialogHost(const GURL& url, Browser* browser);
53 ExtensionHost* CreateInfobarHost(const Extension* extension,
54 const GURL& url,
55 Browser* browser);
56 ExtensionHost* CreateInfobarHost(const GURL& url,
57 Browser* browser);
[email protected]a6080c02012-01-11 23:33:4958 ExtensionHost* CreateShellHost(const Extension* extension, const GURL& url);
[email protected]a44657202012-01-09 05:48:3159
[email protected]550e9942010-03-10 01:40:3460 // Open the extension's options page.
[email protected]9adb9692010-10-29 23:14:0261 void OpenOptionsPage(const Extension* extension, Browser* browser);
[email protected]550e9942010-03-10 01:40:3462
[email protected]029ad372011-05-20 17:12:5663 // Creates a new UI-less extension instance. Like CreateViewHost, but not
[email protected]bc535ee52010-08-31 18:40:3264 // displayed anywhere.
[email protected]9adb9692010-10-29 23:14:0265 virtual void CreateBackgroundHost(const Extension* extension,
66 const GURL& url);
[email protected]bc535ee52010-08-31 18:40:3267
[email protected]4814b512009-11-07 00:12:2968 // 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.
[email protected]06024c62011-10-20 20:57:1270 ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
[email protected]4814b512009-11-07 00:12:2971
[email protected]481e1a42009-05-06 20:56:0572 // Returns the SiteInstance that the given URL belongs to.
[email protected]6f371442011-11-09 06:45:4673 // TODO(aa): This only returns correct results for extensions and packaged
74 // apps, not hosted apps.
[email protected]b6583592012-01-25 19:52:3375 virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
[email protected]481e1a42009-05-06 20:56:0576
[email protected]3e194992011-10-20 05:39:1077 // Registers a RenderViewHost as hosting a given extension.
78 void RegisterRenderViewHost(RenderViewHost* render_view_host,
79 const Extension* extension);
[email protected]45776222009-07-15 20:21:5880
[email protected]3e194992011-10-20 05:39:1081 // Unregisters a RenderViewHost as hosting any extension.
82 void UnregisterRenderViewHost(RenderViewHost* render_view_host);
83
84 // Returns all RenderViewHosts that are registered for the specified
85 // extension.
86 std::set<RenderViewHost*> GetRenderViewHostsForExtension(
87 const std::string& extension_id);
[email protected]45776222009-07-15 20:21:5888
[email protected]27e469a2010-01-11 20:35:0989 // Returns true if |host| is managed by this process manager.
90 bool HasExtensionHost(ExtensionHost* host) const;
91
[email protected]720ad1312012-02-27 23:07:3692 // 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.
94 // When this reaches 0, we will begin the process of shutting down the page.
95 // "Things" include pending events, resource loads, and API calls.
96 int GetLazyKeepaliveCount(const Extension* extension);
97 int IncrementLazyKeepaliveCount(const Extension* extension);
98 int DecrementLazyKeepaliveCount(const Extension* extension);
99
[email protected]6baff0b52012-03-06 01:30:18100 // Handles a response to the ShouldClose message, used for lazy background
[email protected]720ad1312012-02-27 23:07:36101 // pages.
102 void OnShouldCloseAck(const std::string& extension_id, int sequence_id);
[email protected]06024c62011-10-20 20:57:12103
[email protected]6baff0b52012-03-06 01:30:18104 // Tracks network requests for a given RenderViewHost, used to know
105 // when network activity is idle for lazy background pages.
106 void OnNetworkRequestStarted(RenderViewHost* render_view_host);
107 void OnNetworkRequestDone(RenderViewHost* render_view_host);
108
[email protected]8a17bd52009-06-06 08:19:49109 typedef std::set<ExtensionHost*> ExtensionHostSet;
110 typedef ExtensionHostSet::const_iterator const_iterator;
111 const_iterator begin() const { return all_hosts_.begin(); }
112 const_iterator end() const { return all_hosts_.end(); }
[email protected]fc368952009-05-21 21:37:06113
[email protected]bc535ee52010-08-31 18:40:32114 protected:
115 explicit ExtensionProcessManager(Profile* profile);
116
[email protected]94fe52e2009-06-13 10:09:06117 // Called just after |host| is created so it can be registered in our lists.
118 void OnExtensionHostCreated(ExtensionHost* host, bool is_background);
119
[email protected]ae5497f2009-11-05 00:39:46120 // Called on browser shutdown to close our extension hosts.
121 void CloseBackgroundHosts();
122
[email protected]6c2381d2011-10-19 02:52:53123 // content::NotificationObserver:
[email protected]432115822011-07-10 15:52:27124 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53125 const content::NotificationSource& source,
126 const content::NotificationDetails& details) OVERRIDE;
[email protected]bc535ee52010-08-31 18:40:32127
[email protected]72daaa92012-01-18 13:39:02128 // Gets the profile associated with site_instance_ and all other
129 // related SiteInstances.
130 Profile* GetProfile() const;
131
[email protected]6c2381d2011-10-19 02:52:53132 content::NotificationRegistrar registrar_;
[email protected]fc368952009-05-21 21:37:06133
[email protected]8a17bd52009-06-06 08:19:49134 // The set of all ExtensionHosts managed by this process manager.
135 ExtensionHostSet all_hosts_;
136
137 // The set of running viewless background extensions.
138 ExtensionHostSet background_hosts_;
[email protected]481e1a42009-05-06 20:56:05139
[email protected]72daaa92012-01-18 13:39:02140 // A SiteInstance related to the SiteInstance for all extensions in
141 // this profile. We create it in such a way that a new
142 // browsing instance is created. This controls process grouping.
[email protected]b6583592012-01-25 19:52:33143 scoped_refptr<content::SiteInstance> site_instance_;
[email protected]481e1a42009-05-06 20:56:05144
[email protected]3e194992011-10-20 05:39:10145 private:
[email protected]3e194992011-10-20 05:39:10146 // Contains all extension-related RenderViewHost instances for all extensions.
[email protected]6baff0b52012-03-06 01:30:18147 // We also keep a cache of the host's view type, because that information
148 // is not accessible at registration/deregistration time.
149 typedef std::map<RenderViewHost*, content::ViewType> ExtensionRenderViews;
150 ExtensionRenderViews all_extension_views_;
[email protected]3e194992011-10-20 05:39:10151
[email protected]06024c62011-10-20 20:57:12152 // Close the given |host| iff it's a background page.
153 void CloseBackgroundHost(ExtensionHost* host);
154
[email protected]6baff0b52012-03-06 01:30:18155 // These are called when the extension transitions between idle and active.
156 // They control the process of closing the background page when idle.
157 void OnLazyBackgroundPageIdle(const std::string& extension_id);
158 void OnLazyBackgroundPageActive(const std::string& extension_id);
159
160 // Updates a potentially-registered RenderViewHost once it has been
161 // associated with a WebContents. This allows us to gather information that
162 // was not available when the host was first registered.
163 void UpdateRegisteredRenderView(RenderViewHost* render_view_host);
[email protected]06024c62011-10-20 20:57:12164
[email protected]481e1a42009-05-06 20:56:05165 DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager);
166};
167
168#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_