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_
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"
27 class RenderProcessHost
;
28 class RenderWidgetHostView
;
32 namespace extensions
{
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
{
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 did_stop_loading() const { return did_stop_loading_
; }
63 bool document_element_available() const {
64 return document_element_available_
;
67 content::BrowserContext
* browser_context() { return browser_context_
; }
69 ViewType
extension_host_type() const { return extension_host_type_
; }
70 const GURL
& GetURL() const;
72 // Returns true if the render view is initialized and didn't crash.
73 bool IsRenderViewLive() const;
75 // Prepares to initializes our RenderViewHost by creating its RenderView and
76 // navigating to this host's url. Uses host_view for the RenderViewHost's view
77 // (can be NULL). This happens delayed to avoid locking the UI.
78 void CreateRenderViewSoon();
80 // Closes this host (results in [possibly asynchronous] deletion).
83 // Typical observer interface.
84 void AddObserver(ExtensionHostObserver
* observer
);
85 void RemoveObserver(ExtensionHostObserver
* observer
);
87 // Called when an event is dispatched to the event page associated with this
89 void OnBackgroundEventDispatched(const std::string
& event_name
, int event_id
);
91 // Called by the ProcessManager when a network request is started by the
92 // extension corresponding to this ExtensionHost.
93 void OnNetworkRequestStarted(uint64 request_id
);
95 // Called by the ProcessManager when a previously started network request is
97 void OnNetworkRequestDone(uint64 request_id
);
99 // content::WebContentsObserver:
100 bool OnMessageReceived(const IPC::Message
& message
) override
;
101 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
102 void RenderViewDeleted(content::RenderViewHost
* render_view_host
) override
;
103 void RenderViewReady() override
;
104 void RenderProcessGone(base::TerminationStatus status
) override
;
105 void DocumentAvailableInMainFrame() override
;
106 void DidStartLoading(content::RenderViewHost
* render_view_host
) override
;
107 void DidStopLoading(content::RenderViewHost
* render_view_host
) override
;
109 // content::WebContentsDelegate:
110 content::JavaScriptDialogManager
* GetJavaScriptDialogManager(
111 content::WebContents
* source
) override
;
112 void AddNewContents(content::WebContents
* source
,
113 content::WebContents
* new_contents
,
114 WindowOpenDisposition disposition
,
115 const gfx::Rect
& initial_rect
,
117 bool* was_blocked
) override
;
118 void CloseContents(content::WebContents
* contents
) override
;
119 void RequestMediaAccessPermission(
120 content::WebContents
* web_contents
,
121 const content::MediaStreamRequest
& request
,
122 const content::MediaResponseCallback
& callback
) override
;
123 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
124 const GURL
& security_origin
,
125 content::MediaStreamType type
) override
;
126 bool IsNeverVisible(content::WebContents
* web_contents
) override
;
128 // ExtensionRegistryObserver:
129 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
130 const Extension
* extension
,
131 UnloadedExtensionInfo::Reason reason
) override
;
134 // Called after the extension page finishes loading but before the
135 // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
136 virtual void OnDidStopLoading();
138 // Navigates to the initial page.
139 virtual void LoadInitialURL();
141 // Returns true if we're hosting a background page.
142 virtual bool IsBackgroundPage() const;
145 // DeferredStartRenderHost:
146 void CreateRenderViewNow() override
;
147 void AddDeferredStartRenderHostObserver(
148 DeferredStartRenderHostObserver
* observer
) override
;
149 void RemoveDeferredStartRenderHostObserver(
150 DeferredStartRenderHostObserver
* observer
) override
;
153 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
154 void OnEventAck(int event_id
);
155 void OnIncrementLazyKeepaliveCount();
156 void OnDecrementLazyKeepaliveCount();
158 // Delegate for functionality that cannot exist in the extensions module.
159 scoped_ptr
<ExtensionHostDelegate
> delegate_
;
161 // The extension that we're hosting in this view.
162 const Extension
* extension_
;
164 // Id of extension that we're hosting in this view.
165 const std::string extension_id_
;
167 // The browser context that this host is tied to.
168 content::BrowserContext
* browser_context_
;
170 // The host for our HTML content.
171 scoped_ptr
<content::WebContents
> host_contents_
;
173 // A weak pointer to the current or pending RenderViewHost. We don't access
174 // this through the host_contents because we want to deal with the pending
175 // host, so we can send messages to it before it finishes loading.
176 content::RenderViewHost
* render_view_host_
;
178 // Whether the RenderWidget has reported that it has stopped loading.
179 bool did_stop_loading_
;
181 // True if the main frame has finished parsing.
182 bool document_element_available_
;
184 // The original URL of the page being hosted.
187 // Messages sent out to the renderer that have not been acknowledged yet.
188 std::set
<int> unacked_messages_
;
190 ExtensionFunctionDispatcher extension_function_dispatcher_
;
192 // The type of view being hosted.
193 ViewType extension_host_type_
;
195 // Measures how long since the initial URL started loading.
196 scoped_ptr
<base::ElapsedTimer
> load_start_
;
198 ObserverList
<ExtensionHostObserver
> observer_list_
;
199 ObserverList
<DeferredStartRenderHostObserver
>
200 deferred_start_render_host_observer_list_
;
202 DISALLOW_COPY_AND_ASSIGN(ExtensionHost
);
205 } // namespace extensions
207 #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_