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/timer/elapsed_timer.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/browser/extension_function_dispatcher.h"
19 #include "extensions/common/stack_frame.h"
20 #include "extensions/common/view_type.h"
26 class RenderProcessHost
;
27 class RenderWidgetHostView
;
31 namespace extensions
{
33 class ExtensionHostDelegate
;
34 class WindowController
;
36 // This class is the browser component of an extension component's RenderView.
37 // It handles setting up the renderer process, if needed, with special
38 // privileges available to extensions. It may have a view to be shown in the
39 // browser UI, or it may be hidden.
41 // If you are adding code that only affects visible extension views (and not
42 // invisible background pages) you should add it to ExtensionViewHost.
43 class ExtensionHost
: public content::WebContentsDelegate
,
44 public content::WebContentsObserver
,
45 public ExtensionFunctionDispatcher::Delegate
,
46 public content::NotificationObserver
{
48 class ProcessCreationQueue
;
50 ExtensionHost(const Extension
* extension
,
51 content::SiteInstance
* site_instance
,
52 const GURL
& url
, ViewType host_type
);
53 virtual ~ExtensionHost();
55 const Extension
* extension() const { return extension_
; }
56 const std::string
& extension_id() const { return extension_id_
; }
57 content::WebContents
* host_contents() const { return host_contents_
.get(); }
58 content::RenderViewHost
* render_view_host() const;
59 content::RenderProcessHost
* render_process_host() const;
60 bool did_stop_loading() const { return did_stop_loading_
; }
61 bool document_element_available() const {
62 return document_element_available_
;
65 content::BrowserContext
* browser_context() { return browser_context_
; }
67 ViewType
extension_host_type() const { return extension_host_type_
; }
68 const GURL
& GetURL() const;
70 // Returns true if the render view is initialized and didn't crash.
71 bool IsRenderViewLive() const;
73 // Prepares to initializes our RenderViewHost by creating its RenderView and
74 // navigating to this host's url. Uses host_view for the RenderViewHost's view
75 // (can be NULL). This happens delayed to avoid locking the UI.
76 void CreateRenderViewSoon();
78 // content::WebContentsObserver
79 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
80 virtual void RenderViewCreated(
81 content::RenderViewHost
* render_view_host
) OVERRIDE
;
82 virtual void RenderViewDeleted(
83 content::RenderViewHost
* render_view_host
) OVERRIDE
;
84 virtual void RenderViewReady() OVERRIDE
;
85 virtual void RenderProcessGone(base::TerminationStatus status
) OVERRIDE
;
86 virtual void DocumentAvailableInMainFrame() OVERRIDE
;
87 virtual void DidStopLoading(
88 content::RenderViewHost
* render_view_host
) OVERRIDE
;
90 // content::WebContentsDelegate
91 virtual content::JavaScriptDialogManager
*
92 GetJavaScriptDialogManager() OVERRIDE
;
93 virtual void AddNewContents(content::WebContents
* source
,
94 content::WebContents
* new_contents
,
95 WindowOpenDisposition disposition
,
96 const gfx::Rect
& initial_pos
,
98 bool* was_blocked
) OVERRIDE
;
99 virtual void CloseContents(content::WebContents
* contents
) OVERRIDE
;
100 virtual void RequestMediaAccessPermission(
101 content::WebContents
* web_contents
,
102 const content::MediaStreamRequest
& request
,
103 const content::MediaResponseCallback
& callback
) OVERRIDE
;
104 virtual bool IsNeverVisible(content::WebContents
* web_contents
) OVERRIDE
;
106 // content::NotificationObserver
107 virtual void Observe(int type
,
108 const content::NotificationSource
& source
,
109 const content::NotificationDetails
& details
) OVERRIDE
;
112 content::NotificationRegistrar
* registrar() { return ®istrar_
; }
114 // Called after the extension page finishes loading but before the
115 // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
116 virtual void OnDidStopLoading();
118 // Called once when the document first becomes available.
119 virtual void OnDocumentAvailable();
121 // Navigates to the initial page.
122 virtual void LoadInitialURL();
124 // Returns true if we're hosting a background page.
125 virtual bool IsBackgroundPage() const;
127 // Closes this host (results in deletion).
131 friend class ProcessCreationQueue
;
133 // Actually create the RenderView for this host. See CreateRenderViewSoon.
134 void CreateRenderViewNow();
137 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
139 void OnIncrementLazyKeepaliveCount();
140 void OnDecrementLazyKeepaliveCount();
142 // Delegate for functionality that cannot exist in the extensions module.
143 scoped_ptr
<ExtensionHostDelegate
> delegate_
;
145 // The extension that we're hosting in this view.
146 const Extension
* extension_
;
148 // Id of extension that we're hosting in this view.
149 const std::string extension_id_
;
151 // The browser context that this host is tied to.
152 content::BrowserContext
* browser_context_
;
154 // The host for our HTML content.
155 scoped_ptr
<content::WebContents
> host_contents_
;
157 // A weak pointer to the current or pending RenderViewHost. We don't access
158 // this through the host_contents because we want to deal with the pending
159 // host, so we can send messages to it before it finishes loading.
160 content::RenderViewHost
* render_view_host_
;
162 // Whether the RenderWidget has reported that it has stopped loading.
163 bool did_stop_loading_
;
165 // True if the main frame has finished parsing.
166 bool document_element_available_
;
168 // The original URL of the page being hosted.
171 content::NotificationRegistrar registrar_
;
173 ExtensionFunctionDispatcher extension_function_dispatcher_
;
175 // The type of view being hosted.
176 ViewType extension_host_type_
;
178 // Used to measure how long it's been since the host was created.
179 base::ElapsedTimer since_created_
;
181 DISALLOW_COPY_AND_ASSIGN(ExtensionHost
);
184 } // namespace extensions
186 #endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_