[email protected] | b3841c50 | 2011-03-09 01:21:31 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 2 | // 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] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 8 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 9 | #include <map> |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 10 | #include <set> |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 11 | #include <string> |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 12 | |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 15 | #include "content/public/common/view_types.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 16 | #include "content/public/browser/notification_observer.h" |
| 17 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 18 | |
| 19 | class Browser; |
| 20 | class BrowsingInstance; |
| 21 | class Extension; |
| 22 | class ExtensionHost; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 23 | class GURL; |
| 24 | class Profile; |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 25 | class RenderProcessHost; |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 26 | class RenderViewHost; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 27 | class SiteInstance; |
| 28 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 29 | // Manages dynamic state of running Chromium extensions. There is one instance |
| 30 | // of this class per Profile. OTR Profiles have a separate instance that keeps |
| 31 | // track of split-mode extensions only. |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 32 | class ExtensionProcessManager : public content::NotificationObserver { |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 33 | public: |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 34 | static ExtensionProcessManager* Create(Profile* profile); |
| 35 | virtual ~ExtensionProcessManager(); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 36 | |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 37 | // Creates a new ExtensionHost with its associated view, grouping it in the |
| 38 | // appropriate SiteInstance (and therefore process) based on the URL and |
| 39 | // profile. |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 40 | virtual ExtensionHost* CreateViewHost(const Extension* extension, |
| 41 | const GURL& url, |
| 42 | Browser* browser, |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 43 | content::ViewType view_type); |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 44 | ExtensionHost* CreateViewHost(const GURL& url, |
| 45 | Browser* browser, |
[email protected] | da4dfc4 | 2011-10-12 15:53:56 | [diff] [blame] | 46 | content::ViewType view_type); |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 47 | ExtensionHost* CreatePopupHost(const Extension* extension, |
| 48 | const GURL& url, |
| 49 | Browser* browser); |
| 50 | ExtensionHost* CreatePopupHost(const GURL& url, Browser* browser); |
| 51 | ExtensionHost* CreateDialogHost(const GURL& url, Browser* browser); |
| 52 | ExtensionHost* CreateInfobarHost(const Extension* extension, |
| 53 | const GURL& url, |
| 54 | Browser* browser); |
| 55 | ExtensionHost* CreateInfobarHost(const GURL& url, |
| 56 | Browser* browser); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 57 | |
[email protected] | 550e994 | 2010-03-10 01:40:34 | [diff] [blame] | 58 | // Open the extension's options page. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 59 | void OpenOptionsPage(const Extension* extension, Browser* browser); |
[email protected] | 550e994 | 2010-03-10 01:40:34 | [diff] [blame] | 60 | |
[email protected] | 029ad37 | 2011-05-20 17:12:56 | [diff] [blame] | 61 | // Creates a new UI-less extension instance. Like CreateViewHost, but not |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 62 | // displayed anywhere. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 63 | virtual void CreateBackgroundHost(const Extension* extension, |
| 64 | const GURL& url); |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 65 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 66 | // Gets the ExtensionHost for the background page for an extension, or NULL if |
| 67 | // the extension isn't running or doesn't have a background page. |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 68 | ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id); |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 69 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 70 | // Returns the SiteInstance that the given URL belongs to. |
[email protected] | 6f37144 | 2011-11-09 06:45:46 | [diff] [blame^] | 71 | // TODO(aa): This only returns correct results for extensions and packaged |
| 72 | // apps, not hosted apps. |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 73 | virtual SiteInstance* GetSiteInstanceForURL(const GURL& url); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 74 | |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 75 | // Registers a RenderViewHost as hosting a given extension. |
| 76 | void RegisterRenderViewHost(RenderViewHost* render_view_host, |
| 77 | const Extension* extension); |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 78 | |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 79 | // Unregisters a RenderViewHost as hosting any extension. |
| 80 | void UnregisterRenderViewHost(RenderViewHost* render_view_host); |
| 81 | |
| 82 | // Returns all RenderViewHosts that are registered for the specified |
| 83 | // extension. |
| 84 | std::set<RenderViewHost*> GetRenderViewHostsForExtension( |
| 85 | const std::string& extension_id); |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 86 | |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 87 | // Returns true if |host| is managed by this process manager. |
| 88 | bool HasExtensionHost(ExtensionHost* host) const; |
| 89 | |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 90 | // Called when the render reports that the extension is idle (only if |
| 91 | // lazy background pages are enabled). |
| 92 | void OnExtensionIdle(const std::string& extension_id); |
| 93 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 94 | typedef std::set<ExtensionHost*> ExtensionHostSet; |
| 95 | typedef ExtensionHostSet::const_iterator const_iterator; |
| 96 | const_iterator begin() const { return all_hosts_.begin(); } |
| 97 | const_iterator end() const { return all_hosts_.end(); } |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 98 | |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 99 | protected: |
| 100 | explicit ExtensionProcessManager(Profile* profile); |
| 101 | |
[email protected] | 94fe52e | 2009-06-13 10:09:06 | [diff] [blame] | 102 | // Called just after |host| is created so it can be registered in our lists. |
| 103 | void OnExtensionHostCreated(ExtensionHost* host, bool is_background); |
| 104 | |
[email protected] | ae5497f | 2009-11-05 00:39:46 | [diff] [blame] | 105 | // Called on browser shutdown to close our extension hosts. |
| 106 | void CloseBackgroundHosts(); |
| 107 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 108 | // content::NotificationObserver: |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 109 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 110 | const content::NotificationSource& source, |
| 111 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | bc535ee5 | 2010-08-31 18:40:32 | [diff] [blame] | 112 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 113 | content::NotificationRegistrar registrar_; |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 114 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 115 | // The set of all ExtensionHosts managed by this process manager. |
| 116 | ExtensionHostSet all_hosts_; |
| 117 | |
| 118 | // The set of running viewless background extensions. |
| 119 | ExtensionHostSet background_hosts_; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 120 | |
| 121 | // The BrowsingInstance shared by all extensions in this profile. This |
| 122 | // controls process grouping. |
| 123 | scoped_refptr<BrowsingInstance> browsing_instance_; |
| 124 | |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 125 | private: |
[email protected] | 3e19499 | 2011-10-20 05:39:10 | [diff] [blame] | 126 | // Contains all extension-related RenderViewHost instances for all extensions. |
| 127 | typedef std::set<RenderViewHost*> RenderViewHostSet; |
| 128 | RenderViewHostSet all_extension_views_; |
| 129 | |
[email protected] | 06024c6 | 2011-10-20 20:57:12 | [diff] [blame] | 130 | // Close the given |host| iff it's a background page. |
| 131 | void CloseBackgroundHost(ExtensionHost* host); |
| 132 | |
| 133 | // Excludes background page. |
| 134 | bool HasVisibleViews(const std::string& extension_id); |
| 135 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 136 | DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); |
| 137 | }; |
| 138 | |
| 139 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |