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