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_
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/timer/elapsed_timer.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "extensions/browser/deferred_start_render_host.h"
21 #include "extensions/browser/extension_function_dispatcher.h"
22 #include "extensions/common/stack_frame.h"
23 #include "extensions/common/view_type.h"
29 class RenderProcessHost
;
30 class RenderWidgetHostView
;
34 namespace extensions
{
36 class ExtensionHostDelegate
;
37 class ExtensionHostObserver
;
38 class ExtensionHostQueue
;
39 class WindowController
;
41 // This class is the browser component of an extension component's RenderView.
42 // It handles setting up the renderer process, if needed, with special
43 // privileges available to extensions. It may have a view to be shown in the
44 // browser UI, or it may be hidden.
46 // If you are adding code that only affects visible extension views (and not
47 // invisible background pages) you should add it to ExtensionViewHost.
48 class ExtensionHost
: public DeferredStartRenderHost
,
49 public content::WebContentsDelegate
,
50 public content::WebContentsObserver
,
51 public ExtensionFunctionDispatcher::Delegate
,
52 public content::NotificationObserver
{
54 ExtensionHost(const Extension
* extension
,
55 content::SiteInstance
* site_instance
,
56 const GURL
& url
, ViewType host_type
);
57 ~ExtensionHost() override
;
59 const Extension
* extension() const { return extension_
; }
60 const std::string
& extension_id() const { return extension_id_
; }
61 content::WebContents
* host_contents() const { return host_contents_
.get(); }
62 content::RenderViewHost
* render_view_host() const;
63 content::RenderProcessHost
* render_process_host() const;
64 bool did_stop_loading() const { return did_stop_loading_
; }
65 bool document_element_available() const {
66 return document_element_available_
;
69 content::BrowserContext
* browser_context() { return browser_context_
; }
71 ViewType
extension_host_type() const { return extension_host_type_
; }
72 const GURL
& GetURL() const;
74 // Returns true if the render view is initialized and didn't crash.
75 bool IsRenderViewLive() const;
77 // Prepares to initializes our RenderViewHost by creating its RenderView and
78 // navigating to this host's url. Uses host_view for the RenderViewHost's view
79 // (can be NULL). This happens delayed to avoid locking the UI.
80 void CreateRenderViewSoon();
82 // Closes this host (results in [possibly asynchronous] deletion).
85 // Typical observer interface.
86 void AddObserver(ExtensionHostObserver
* observer
);
87 void RemoveObserver(ExtensionHostObserver
* observer
);
89 // Called when an IPC message is dispatched to the RenderView associated with
90 // this ExtensionHost.
91 void OnMessageDispatched(const std::string
& event_name
, int message_id
);
93 // Called by the ProcessManager when a network request is started by the
94 // extension corresponding to this ExtensionHost.
95 void OnNetworkRequestStarted(uint64 request_id
);
97 // Called by the ProcessManager when a previously started network request is
99 void OnNetworkRequestDone(uint64 request_id
);
101 // content::WebContentsObserver:
102 bool OnMessageReceived(const IPC::Message
& message
) 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 DidStopLoading(content::RenderViewHost
* render_view_host
) override
;
110 // content::WebContentsDelegate:
111 content::JavaScriptDialogManager
* GetJavaScriptDialogManager(
112 content::WebContents
* source
) override
;
113 void AddNewContents(content::WebContents
* source
,
114 content::WebContents
* new_contents
,
115 WindowOpenDisposition disposition
,
116 const gfx::Rect
& initial_rect
,
118 bool* was_blocked
) override
;
119 void CloseContents(content::WebContents
* contents
) override
;
120 void RequestMediaAccessPermission(
121 content::WebContents
* web_contents
,
122 const content::MediaStreamRequest
& request
,
123 const content::MediaResponseCallback
& callback
) override
;
124 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
125 const GURL
& security_origin
,
126 content::MediaStreamType type
) override
;
127 bool IsNeverVisible(content::WebContents
* web_contents
) override
;
129 // content::NotificationObserver:
130 void Observe(int type
,
131 const content::NotificationSource
& source
,
132 const content::NotificationDetails
& details
) override
;
135 content::NotificationRegistrar
* registrar() { return ®istrar_
; }
137 // Called after the extension page finishes loading but before the
138 // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
139 virtual void OnDidStopLoading();
141 // Navigates to the initial page.
142 virtual void LoadInitialURL();
144 // Returns true if we're hosting a background page.
145 virtual bool IsBackgroundPage() const;
148 // DeferredStartRenderHost:
149 void CreateRenderViewNow() override
;
152 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
153 void OnEventAck(int message_id
);
154 void OnIncrementLazyKeepaliveCount();
155 void OnDecrementLazyKeepaliveCount();
157 // Delegate for functionality that cannot exist in the extensions module.
158 scoped_ptr
<ExtensionHostDelegate
> delegate_
;
160 // The extension that we're hosting in this view.
161 const Extension
* extension_
;
163 // Id of extension that we're hosting in this view.
164 const std::string extension_id_
;
166 // The browser context that this host is tied to.
167 content::BrowserContext
* browser_context_
;
169 // The host for our HTML content.
170 scoped_ptr
<content::WebContents
> host_contents_
;
172 // A weak pointer to the current or pending RenderViewHost. We don't access
173 // this through the host_contents because we want to deal with the pending
174 // host, so we can send messages to it before it finishes loading.
175 content::RenderViewHost
* render_view_host_
;
177 // Whether the RenderWidget has reported that it has stopped loading.
178 bool did_stop_loading_
;
180 // True if the main frame has finished parsing.
181 bool document_element_available_
;
183 // The original URL of the page being hosted.
186 // Messages sent out to the renderer that have not been acknowledged yet.
187 std::set
<int> unacked_messages_
;
189 content::NotificationRegistrar registrar_
;
191 ExtensionFunctionDispatcher extension_function_dispatcher_
;
193 // The type of view being hosted.
194 ViewType extension_host_type_
;
196 // Measures how long since the initial URL started loading.
197 scoped_ptr
<base::ElapsedTimer
> load_start_
;
199 ObserverList
<ExtensionHostObserver
> observer_list_
;
201 DISALLOW_COPY_AND_ASSIGN(ExtensionHost
);
204 } // namespace extensions
206 #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_