[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 1 | // Copyright (c) 2010 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 | |
| 13 | #include "base/ref_counted.h" |
[email protected] | 4d36536b | 2010-08-20 06:23:27 | [diff] [blame] | 14 | #include "chrome/common/notification_observer.h" |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 15 | #include "chrome/common/notification_registrar.h" |
[email protected] | 2d8d923 | 2009-10-02 20:19:20 | [diff] [blame] | 16 | #include "chrome/common/view_types.h" |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 17 | |
| 18 | class Browser; |
| 19 | class BrowsingInstance; |
| 20 | class Extension; |
| 21 | class ExtensionHost; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 22 | class GURL; |
| 23 | class Profile; |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 24 | class RenderProcessHost; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 25 | class SiteInstance; |
| 26 | |
| 27 | // Manages dynamic state of running Chromium extensions. There is one instance |
| 28 | // of this class per Profile (including OTR). |
| 29 | class ExtensionProcessManager : public NotificationObserver { |
| 30 | public: |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 31 | explicit ExtensionProcessManager(Profile* profile); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 32 | ~ExtensionProcessManager(); |
| 33 | |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 34 | // Creates a new ExtensionHost with its associated view, grouping it in the |
| 35 | // appropriate SiteInstance (and therefore process) based on the URL and |
| 36 | // profile. |
| 37 | ExtensionHost* CreateView(Extension* extension, |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 38 | const GURL& url, |
[email protected] | 2d8d923 | 2009-10-02 20:19:20 | [diff] [blame] | 39 | Browser* browser, |
| 40 | ViewType::Type view_type); |
| 41 | ExtensionHost* CreateView(const GURL& url, |
| 42 | Browser* browser, |
| 43 | ViewType::Type view_type); |
[email protected] | 2d8d923 | 2009-10-02 20:19:20 | [diff] [blame] | 44 | ExtensionHost* CreatePopup(Extension* extension, |
| 45 | const GURL& url, |
| 46 | Browser* browser); |
| 47 | ExtensionHost* CreatePopup(const GURL& url, Browser* browser); |
[email protected] | f34e7963 | 2010-03-17 02:34:08 | [diff] [blame] | 48 | ExtensionHost* CreateInfobar(Extension* extension, |
| 49 | const GURL& url, |
| 50 | Browser* browser); |
| 51 | ExtensionHost* CreateInfobar(const GURL& url, |
| 52 | Browser* browser); |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 53 | |
| 54 | // Creates a new UI-less extension instance. Like CreateView, but not |
| 55 | // displayed anywhere. |
[email protected] | ab4eaf78 | 2009-06-10 00:11:24 | [diff] [blame] | 56 | ExtensionHost* CreateBackgroundHost(Extension* extension, const GURL& url); |
[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. |
| 59 | void OpenOptionsPage(Extension* extension, Browser* browser); |
| 60 | |
[email protected] | 4814b51 | 2009-11-07 00:12:29 | [diff] [blame] | 61 | // Gets the ExtensionHost for the background page for an extension, or NULL if |
| 62 | // the extension isn't running or doesn't have a background page. |
| 63 | ExtensionHost* GetBackgroundHostForExtension(Extension* extension); |
| 64 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 65 | // Returns the SiteInstance that the given URL belongs to. |
| 66 | SiteInstance* GetSiteInstanceForURL(const GURL& url); |
| 67 | |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 68 | // Registers an extension process by |extension_id| and specifying which |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 69 | // |process_id| it belongs to. |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 70 | void RegisterExtensionProcess(const std::string& extension_id, |
| 71 | int process_id); |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 72 | |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 73 | // Unregisters an extension process with specified |process_id|. |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 74 | void UnregisterExtensionProcess(int process_id); |
| 75 | |
[email protected] | 5ab79b0 | 2010-04-26 16:47:11 | [diff] [blame] | 76 | // Returns the extension process that |url| is associated with if it exists. |
| 77 | RenderProcessHost* GetExtensionProcess(const GURL& url); |
| 78 | |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 79 | // Returns the process that the extension with the given ID is running in. |
[email protected] | 5ab79b0 | 2010-04-26 16:47:11 | [diff] [blame] | 80 | // NOTE: This does not currently handle app processes with no |
| 81 | // ExtensionFunctionDispatcher objects. |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 82 | RenderProcessHost* GetExtensionProcess(const std::string& extension_id); |
| 83 | |
[email protected] | 27e469a | 2010-01-11 20:35:09 | [diff] [blame] | 84 | // Returns true if |host| is managed by this process manager. |
| 85 | bool HasExtensionHost(ExtensionHost* host) const; |
| 86 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 87 | // NotificationObserver: |
| 88 | virtual void Observe(NotificationType type, |
| 89 | const NotificationSource& source, |
| 90 | const NotificationDetails& details); |
| 91 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 92 | typedef std::set<ExtensionHost*> ExtensionHostSet; |
| 93 | typedef ExtensionHostSet::const_iterator const_iterator; |
| 94 | const_iterator begin() const { return all_hosts_.begin(); } |
| 95 | const_iterator end() const { return all_hosts_.end(); } |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 96 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 97 | private: |
[email protected] | 94fe52e | 2009-06-13 10:09:06 | [diff] [blame] | 98 | // Called just after |host| is created so it can be registered in our lists. |
| 99 | void OnExtensionHostCreated(ExtensionHost* host, bool is_background); |
| 100 | |
[email protected] | ae5497f | 2009-11-05 00:39:46 | [diff] [blame] | 101 | // Called on browser shutdown to close our extension hosts. |
| 102 | void CloseBackgroundHosts(); |
| 103 | |
[email protected] | fc36895 | 2009-05-21 21:37:06 | [diff] [blame] | 104 | NotificationRegistrar registrar_; |
| 105 | |
[email protected] | 8a17bd5 | 2009-06-06 08:19:49 | [diff] [blame] | 106 | // The set of all ExtensionHosts managed by this process manager. |
| 107 | ExtensionHostSet all_hosts_; |
| 108 | |
| 109 | // The set of running viewless background extensions. |
| 110 | ExtensionHostSet background_hosts_; |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 111 | |
| 112 | // The BrowsingInstance shared by all extensions in this profile. This |
| 113 | // controls process grouping. |
| 114 | scoped_refptr<BrowsingInstance> browsing_instance_; |
| 115 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 116 | // A map of extension ID to the render_process_id that the extension lives in. |
| 117 | typedef std::map<std::string, int> ProcessIDMap; |
| 118 | ProcessIDMap process_ids_; |
| 119 | |
[email protected] | 481e1a4 | 2009-05-06 20:56:05 | [diff] [blame] | 120 | DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager); |
| 121 | }; |
| 122 | |
| 123 | #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_ |