• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
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_
7 #pragma once
8 
9 #include <map>
10 #include <set>
11 #include <string>
12 
13 #include "base/memory/ref_counted.h"
14 #include "chrome/common/view_types.h"
15 #include "content/common/notification_observer.h"
16 #include "content/common/notification_registrar.h"
17 
18 class Browser;
19 class BrowsingInstance;
20 class Extension;
21 class ExtensionHost;
22 class GURL;
23 class Profile;
24 class RenderProcessHost;
25 class SiteInstance;
26 
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.
30 class ExtensionProcessManager : public NotificationObserver {
31  public:
32   static ExtensionProcessManager* Create(Profile* profile);
33   virtual ~ExtensionProcessManager();
34 
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.
38   virtual ExtensionHost* CreateView(const Extension* extension,
39                             const GURL& url,
40                             Browser* browser,
41                             ViewType::Type view_type);
42   ExtensionHost* CreateView(const GURL& url,
43                             Browser* browser,
44                             ViewType::Type view_type);
45   ExtensionHost* CreatePopup(const Extension* extension,
46                              const GURL& url,
47                              Browser* browser);
48   ExtensionHost* CreatePopup(const GURL& url, Browser* browser);
49   ExtensionHost* CreateInfobar(const Extension* extension,
50                                const GURL& url,
51                                Browser* browser);
52   ExtensionHost* CreateInfobar(const GURL& url,
53                                Browser* browser);
54 
55   // Open the extension's options page.
56   void OpenOptionsPage(const Extension* extension, Browser* browser);
57 
58   // Creates a new UI-less extension instance.  Like CreateView, but not
59   // displayed anywhere.
60   virtual void CreateBackgroundHost(const Extension* extension,
61                                     const GURL& url);
62 
63   // Gets the ExtensionHost for the background page for an extension, or NULL if
64   // the extension isn't running or doesn't have a background page.
65   ExtensionHost* GetBackgroundHostForExtension(const Extension* extension);
66 
67   // Returns the SiteInstance that the given URL belongs to.
68   virtual SiteInstance* GetSiteInstanceForURL(const GURL& url);
69 
70   // Registers an extension process by |extension_id| and specifying which
71   // |process_id| it belongs to.
72   void RegisterExtensionProcess(const std::string& extension_id,
73                                 int process_id);
74 
75   // Unregisters an extension process with specified |process_id|.
76   void UnregisterExtensionProcess(int process_id);
77 
78   // Returns the extension process that |url| is associated with if it exists.
79   virtual RenderProcessHost* GetExtensionProcess(const GURL& url);
80 
81   // Returns the process that the extension with the given ID is running in.
82   RenderProcessHost* GetExtensionProcess(const std::string& extension_id);
83 
84   // Returns true if |host| is managed by this process manager.
85   bool HasExtensionHost(ExtensionHost* host) const;
86 
87   typedef std::set<ExtensionHost*> ExtensionHostSet;
88   typedef ExtensionHostSet::const_iterator const_iterator;
begin()89   const_iterator begin() const { return all_hosts_.begin(); }
end()90   const_iterator end() const { return all_hosts_.end(); }
91 
92  protected:
93   explicit ExtensionProcessManager(Profile* profile);
94 
95   // Called just after |host| is created so it can be registered in our lists.
96   void OnExtensionHostCreated(ExtensionHost* host, bool is_background);
97 
98   // Called on browser shutdown to close our extension hosts.
99   void CloseBackgroundHosts();
100 
101   // NotificationObserver:
102   virtual void Observe(NotificationType type,
103                        const NotificationSource& source,
104                        const NotificationDetails& details);
105 
106   NotificationRegistrar registrar_;
107 
108   // The set of all ExtensionHosts managed by this process manager.
109   ExtensionHostSet all_hosts_;
110 
111   // The set of running viewless background extensions.
112   ExtensionHostSet background_hosts_;
113 
114   // The BrowsingInstance shared by all extensions in this profile.  This
115   // controls process grouping.
116   scoped_refptr<BrowsingInstance> browsing_instance_;
117 
118   // A map of extension ID to the render_process_id that the extension lives in.
119   typedef std::map<std::string, int> ProcessIDMap;
120   ProcessIDMap process_ids_;
121 
122   DISALLOW_COPY_AND_ASSIGN(ExtensionProcessManager);
123 };
124 
125 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_PROCESS_MANAGER_H_
126