Durable Storage: Refactor browser test and test the basic "deny" flow.
[chromium-blink-merge.git] / extensions / browser / extension_host.h
blob0b10e9a721c3013e3fce03405959779ba936ad5b
1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_HOST_H_
8 #include <set>
9 #include <string>
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/timer/elapsed_timer.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "extensions/browser/deferred_start_render_host.h"
18 #include "extensions/browser/extension_function_dispatcher.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "extensions/common/stack_frame.h"
21 #include "extensions/common/view_type.h"
23 class PrefsTabHelper;
25 namespace content {
26 class BrowserContext;
27 class RenderProcessHost;
28 class RenderWidgetHostView;
29 class SiteInstance;
32 namespace extensions {
33 class Extension;
34 class ExtensionHostDelegate;
35 class ExtensionHostObserver;
36 class ExtensionHostQueue;
37 class WindowController;
39 // This class is the browser component of an extension component's RenderView.
40 // It handles setting up the renderer process, if needed, with special
41 // privileges available to extensions. It may have a view to be shown in the
42 // browser UI, or it may be hidden.
44 // If you are adding code that only affects visible extension views (and not
45 // invisible background pages) you should add it to ExtensionViewHost.
46 class ExtensionHost : public DeferredStartRenderHost,
47 public content::WebContentsDelegate,
48 public content::WebContentsObserver,
49 public ExtensionFunctionDispatcher::Delegate,
50 public ExtensionRegistryObserver {
51 public:
52 ExtensionHost(const Extension* extension,
53 content::SiteInstance* site_instance,
54 const GURL& url, ViewType host_type);
55 ~ExtensionHost() override;
57 const Extension* extension() const { return extension_; }
58 const std::string& extension_id() const { return extension_id_; }
59 content::WebContents* host_contents() const { return host_contents_.get(); }
60 content::RenderViewHost* render_view_host() const;
61 content::RenderProcessHost* render_process_host() const;
62 bool has_loaded_once() const { return has_loaded_once_; }
63 const GURL& initial_url() const { return initial_url_; }
64 bool document_element_available() const {
65 return document_element_available_;
68 content::BrowserContext* browser_context() { return browser_context_; }
70 ViewType extension_host_type() const { return extension_host_type_; }
71 const GURL& GetURL() const;
73 // Returns true if the render view is initialized and didn't crash.
74 bool IsRenderViewLive() const;
76 // Prepares to initializes our RenderViewHost by creating its RenderView and
77 // navigating to this host's url. Uses host_view for the RenderViewHost's view
78 // (can be NULL). This happens delayed to avoid locking the UI.
79 void CreateRenderViewSoon();
81 // Closes this host (results in [possibly asynchronous] deletion).
82 void Close();
84 // Typical observer interface.
85 void AddObserver(ExtensionHostObserver* observer);
86 void RemoveObserver(ExtensionHostObserver* observer);
88 // Called when an event is dispatched to the event page associated with this
89 // ExtensionHost.
90 void OnBackgroundEventDispatched(const std::string& event_name, int event_id);
92 // Called by the ProcessManager when a network request is started by the
93 // extension corresponding to this ExtensionHost.
94 void OnNetworkRequestStarted(uint64 request_id);
96 // Called by the ProcessManager when a previously started network request is
97 // finished.
98 void OnNetworkRequestDone(uint64 request_id);
100 // content::WebContentsObserver:
101 bool OnMessageReceived(const IPC::Message& message,
102 content::RenderFrameHost* host) override;
103 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
104 void RenderViewDeleted(content::RenderViewHost* render_view_host) override;
105 void RenderViewReady() override;
106 void RenderProcessGone(base::TerminationStatus status) override;
107 void DocumentAvailableInMainFrame() override;
108 void DidStartLoading() override;
109 void DidStopLoading() override;
111 // content::WebContentsDelegate:
112 content::JavaScriptDialogManager* GetJavaScriptDialogManager(
113 content::WebContents* source) override;
114 void AddNewContents(content::WebContents* source,
115 content::WebContents* new_contents,
116 WindowOpenDisposition disposition,
117 const gfx::Rect& initial_rect,
118 bool user_gesture,
119 bool* was_blocked) override;
120 void CloseContents(content::WebContents* contents) override;
121 void RequestMediaAccessPermission(
122 content::WebContents* web_contents,
123 const content::MediaStreamRequest& request,
124 const content::MediaResponseCallback& callback) override;
125 bool CheckMediaAccessPermission(content::WebContents* web_contents,
126 const GURL& security_origin,
127 content::MediaStreamType type) override;
128 bool IsNeverVisible(content::WebContents* web_contents) override;
130 // ExtensionRegistryObserver:
131 void OnExtensionReady(content::BrowserContext* browser_context,
132 const Extension* extension) override;
133 void OnExtensionUnloaded(content::BrowserContext* browser_context,
134 const Extension* extension,
135 UnloadedExtensionInfo::Reason reason) override;
137 protected:
138 // Called each time this ExtensionHost completes a load finishes loading,
139 // before any stop-loading notifications or observer methods are called.
140 virtual void OnDidStopFirstLoad();
142 // Navigates to the initial page.
143 virtual void LoadInitialURL();
145 // Returns true if we're hosting a background page.
146 virtual bool IsBackgroundPage() const;
148 private:
149 // DeferredStartRenderHost:
150 void CreateRenderViewNow() override;
151 void AddDeferredStartRenderHostObserver(
152 DeferredStartRenderHostObserver* observer) override;
153 void RemoveDeferredStartRenderHostObserver(
154 DeferredStartRenderHostObserver* observer) override;
156 // Message handlers.
157 void OnEventAck(int event_id);
158 void OnIncrementLazyKeepaliveCount();
159 void OnDecrementLazyKeepaliveCount();
161 // Records UMA for load events.
162 void RecordStopLoadingUMA();
164 // Delegate for functionality that cannot exist in the extensions module.
165 scoped_ptr<ExtensionHostDelegate> delegate_;
167 // The extension that we're hosting in this view.
168 const Extension* extension_;
170 // Id of extension that we're hosting in this view.
171 const std::string extension_id_;
173 // The browser context that this host is tied to.
174 content::BrowserContext* browser_context_;
176 // The host for our HTML content.
177 scoped_ptr<content::WebContents> host_contents_;
179 // A weak pointer to the current or pending RenderViewHost. We don't access
180 // this through the host_contents because we want to deal with the pending
181 // host, so we can send messages to it before it finishes loading.
182 content::RenderViewHost* render_view_host_;
184 // Whether CreateRenderViewNow was called before the extension was ready.
185 bool is_render_view_creation_pending_;
187 // Whether the ExtensionHost has finished loading some content at least once.
188 // There may be subsequent loads - such as reloads and navigations - and this
189 // will not affect its value (it will remain true).
190 bool has_loaded_once_;
192 // True if the main frame has finished parsing.
193 bool document_element_available_;
195 // The original URL of the page being hosted.
196 GURL initial_url_;
198 // Messages sent out to the renderer that have not been acknowledged yet.
199 std::set<int> unacked_messages_;
201 // The type of view being hosted.
202 ViewType extension_host_type_;
204 // Measures how long since the ExtensionHost object was created. This can be
205 // used to measure the responsiveness of UI. For example, it's important to
206 // keep this as low as possible for popups. Contrast this to |load_start_|,
207 // for which a low value does not necessarily mean a responsive UI, as
208 // ExtensionHosts may sit in an ExtensionHostQueue for a long time.
209 base::ElapsedTimer create_start_;
211 // Measures how long since the initial URL started loading. This timer is
212 // started only once the ExtensionHost has exited the ExtensionHostQueue.
213 scoped_ptr<base::ElapsedTimer> load_start_;
215 base::ObserverList<ExtensionHostObserver> observer_list_;
216 base::ObserverList<DeferredStartRenderHostObserver>
217 deferred_start_render_host_observer_list_;
219 DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
222 } // namespace extensions
224 #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_