Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / process_manager.h
1 // Copyright 2013 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 EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
6 #define EXTENSIONS_BROWSER_PROCESS_MANAGER_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/callback.h"
13 #include "base/compiler_specific.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/time/time.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/common/view_type.h"
20
21 class GURL;
22
23 namespace content {
24 class BrowserContext;
25 class DevToolsAgentHost;
26 class RenderViewHost;
27 class RenderFrameHost;
28 class SiteInstance;
29 };
30
31 namespace extensions {
32
33 class Extension;
34 class ExtensionHost;
35
36 // Manages dynamic state of running Chromium extensions. There is one instance
37 // of this class per Profile. OTR Profiles have a separate instance that keeps
38 // track of split-mode extensions only.
39 class ProcessManager : public content::NotificationObserver {
40  public:
41   typedef std::set<extensions::ExtensionHost*> ExtensionHostSet;
42   typedef ExtensionHostSet::const_iterator const_iterator;
43
44   static ProcessManager* Create(content::BrowserContext* context);
45   virtual ~ProcessManager();
46
47   const ExtensionHostSet& background_hosts() const {
48     return background_hosts_;
49   }
50
51   typedef std::set<content::RenderViewHost*> ViewSet;
52   const ViewSet GetAllViews() const;
53
54   // Creates a new UI-less extension instance.  Like CreateViewHost, but not
55   // displayed anywhere.
56   virtual ExtensionHost* CreateBackgroundHost(const Extension* extension,
57                                               const GURL& url);
58
59   // Gets the ExtensionHost for the background page for an extension, or NULL if
60   // the extension isn't running or doesn't have a background page.
61   ExtensionHost* GetBackgroundHostForExtension(const std::string& extension_id);
62
63   // Returns the SiteInstance that the given URL belongs to.
64   // TODO(aa): This only returns correct results for extensions and packaged
65   // apps, not hosted apps.
66   virtual content::SiteInstance* GetSiteInstanceForURL(const GURL& url);
67
68   // Unregisters a RenderViewHost as hosting any extension.
69   void UnregisterRenderViewHost(content::RenderViewHost* render_view_host);
70
71   // Returns all RenderViewHosts that are registered for the specified
72   // extension.
73   std::set<content::RenderViewHost*> GetRenderViewHostsForExtension(
74       const std::string& extension_id);
75
76   // Returns the extension associated with the specified RenderViewHost, or
77   // NULL.
78   const Extension* GetExtensionForRenderViewHost(
79       content::RenderViewHost* render_view_host);
80
81   // Returns true if the (lazy) background host for the given extension has
82   // already been sent the unload event and is shutting down.
83   bool IsBackgroundHostClosing(const std::string& extension_id);
84
85   // Getter and setter for the lazy background page's keepalive count. This is
86   // the count of how many outstanding "things" are keeping the page alive.
87   // When this reaches 0, we will begin the process of shutting down the page.
88   // "Things" include pending events, resource loads, and API calls.
89   int GetLazyKeepaliveCount(const Extension* extension);
90   void IncrementLazyKeepaliveCount(const Extension* extension);
91   void DecrementLazyKeepaliveCount(const Extension* extension);
92
93   void IncrementLazyKeepaliveCountForView(
94       content::RenderViewHost* render_view_host);
95
96   // Keeps a background page alive. Unlike IncrementLazyKeepaliveCount, these
97   // impulses will only keep the page alive for a limited amount of time unless
98   // called regularly.
99   void KeepaliveImpulse(const Extension* extension);
100
101   // Handles a response to the ShouldSuspend message, used for lazy background
102   // pages.
103   void OnShouldSuspendAck(const std::string& extension_id, int sequence_id);
104
105   // Same as above, for the Suspend message.
106   void OnSuspendAck(const std::string& extension_id);
107
108   // Tracks network requests for a given RenderFrameHost, used to know
109   // when network activity is idle for lazy background pages.
110   void OnNetworkRequestStarted(content::RenderFrameHost* render_frame_host);
111   void OnNetworkRequestDone(content::RenderFrameHost* render_frame_host);
112
113   // Prevents |extension|'s background page from being closed and sends the
114   // onSuspendCanceled() event to it.
115   void CancelSuspend(const Extension* extension);
116
117   // Ensures background hosts are loaded for a new browser window.
118   void OnBrowserWindowReady();
119
120   // Gets the BrowserContext associated with site_instance_ and all other
121   // related SiteInstances.
122   content::BrowserContext* GetBrowserContext() const;
123
124   // Sets callbacks for testing keepalive impulse behavior.
125   typedef base::Callback<void(const std::string& extension_id)>
126       ImpulseCallbackForTesting;
127   void SetKeepaliveImpulseCallbackForTesting(
128       const ImpulseCallbackForTesting& callback);
129   void SetKeepaliveImpulseDecrementCallbackForTesting(
130       const ImpulseCallbackForTesting& callback);
131
132  protected:
133   // If |context| is incognito pass the master context as |original_context|.
134   // Otherwise pass the same context for both.
135   ProcessManager(content::BrowserContext* context,
136                  content::BrowserContext* original_context);
137
138   // Called on browser shutdown to close our extension hosts.
139   void CloseBackgroundHosts();
140
141   // content::NotificationObserver:
142   virtual void Observe(int type,
143                        const content::NotificationSource& source,
144                        const content::NotificationDetails& details) OVERRIDE;
145
146   // Load all background pages once the profile data is ready and the pages
147   // should be loaded.
148   void CreateBackgroundHostsForProfileStartup();
149
150   content::NotificationRegistrar registrar_;
151
152   // The set of ExtensionHosts running viewless background extensions.
153   ExtensionHostSet background_hosts_;
154
155   // A SiteInstance related to the SiteInstance for all extensions in
156   // this profile.  We create it in such a way that a new
157   // browsing instance is created.  This controls process grouping.
158   scoped_refptr<content::SiteInstance> site_instance_;
159
160  private:
161   friend class ProcessManagerTest;
162
163   // Extra information we keep for each extension's background page.
164   struct BackgroundPageData;
165   typedef std::string ExtensionId;
166   typedef std::map<ExtensionId, BackgroundPageData> BackgroundPageDataMap;
167   typedef std::map<content::RenderViewHost*,
168       extensions::ViewType> ExtensionRenderViews;
169
170   // Called just after |host| is created so it can be registered in our lists.
171   void OnBackgroundHostCreated(ExtensionHost* host);
172
173   // Close the given |host| iff it's a background page.
174   void CloseBackgroundHost(ExtensionHost* host);
175
176   // Internal implementation of DecrementLazyKeepaliveCount with an
177   // |extension_id| known to have a lazy background page.
178   void DecrementLazyKeepaliveCount(const std::string& extension_id);
179
180   // Checks if keepalive impulses have occured, and adjusts keep alive count.
181   void OnKeepaliveImpulseCheck();
182
183   // These are called when the extension transitions between idle and active.
184   // They control the process of closing the background page when idle.
185   void OnLazyBackgroundPageIdle(const std::string& extension_id,
186                                 int sequence_id);
187   void OnLazyBackgroundPageActive(const std::string& extension_id);
188   void CloseLazyBackgroundPageNow(const std::string& extension_id,
189                                   int sequence_id);
190
191   // Potentially registers a RenderViewHost, if it is associated with an
192   // extension. Does nothing if this is not an extension renderer.
193   // Returns true, if render_view_host was registered (it is associated
194   // with an extension).
195   bool RegisterRenderViewHost(content::RenderViewHost* render_view_host);
196
197   // Unregister RenderViewHosts and clear background page data for an extension
198   // which has been unloaded.
199   void UnregisterExtension(const std::string& extension_id);
200
201   // Clears background page data for this extension.
202   void ClearBackgroundPageData(const std::string& extension_id);
203
204   // Returns true if loading background pages should be deferred.
205   bool DeferLoadingBackgroundHosts() const;
206
207   void OnDevToolsStateChanged(content::DevToolsAgentHost*, bool attached);
208
209   // Contains all active extension-related RenderViewHost instances for all
210   // extensions. We also keep a cache of the host's view type, because that
211   // information is not accessible at registration/deregistration time.
212   ExtensionRenderViews all_extension_views_;
213
214   BackgroundPageDataMap background_page_data_;
215
216   // The time to delay between an extension becoming idle and
217   // sending a ShouldSuspend message; read from command-line switch.
218   base::TimeDelta event_page_idle_time_;
219
220   // The time to delay between sending a ShouldSuspend message and
221   // sending a Suspend message; read from command-line switch.
222   base::TimeDelta event_page_suspending_time_;
223
224   // True if we have created the startup set of background hosts.
225   bool startup_background_hosts_created_;
226
227   base::Callback<void(content::DevToolsAgentHost*, bool)> devtools_callback_;
228
229   ImpulseCallbackForTesting keepalive_impulse_callback_for_testing_;
230   ImpulseCallbackForTesting keepalive_impulse_decrement_callback_for_testing_;
231
232   base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
233
234   DISALLOW_COPY_AND_ASSIGN(ProcessManager);
235 };
236
237 }  // namespace extensions
238
239 #endif  // EXTENSIONS_BROWSER_PROCESS_MANAGER_H_