1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/timer/elapsed_timer.h"
14 #include "chrome/browser/extensions/extension_function_dispatcher.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/browser/web_contents_observer.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 WindowController
;
35 // This class is the browser component of an extension component's RenderView.
36 // It handles setting up the renderer process, if needed, with special
37 // privileges available to extensions. It may have a view to be shown in the
38 // browser UI, or it may be hidden.
39 class ExtensionHost
: public content::WebContentsDelegate
,
40 public content::WebContentsObserver
,
41 public ExtensionFunctionDispatcher::Delegate
,
42 public content::NotificationObserver
{
44 class ProcessCreationQueue
;
46 ExtensionHost(const Extension
* extension
,
47 content::SiteInstance
* site_instance
,
48 const GURL
& url
, ViewType host_type
);
49 virtual ~ExtensionHost();
51 const Extension
* extension() const { return extension_
; }
52 const std::string
& extension_id() const { return extension_id_
; }
53 content::WebContents
* host_contents() const { return host_contents_
.get(); }
54 content::RenderViewHost
* render_view_host() const;
55 content::RenderProcessHost
* render_process_host() const;
56 bool did_stop_loading() const { return did_stop_loading_
; }
57 bool document_element_available() const {
58 return document_element_available_
;
61 content::BrowserContext
* browser_context() { return browser_context_
; }
63 ViewType
extension_host_type() const { return extension_host_type_
; }
64 const GURL
& GetURL() const;
66 // Returns true if the render view is initialized and didn't crash.
67 bool IsRenderViewLive() const;
69 // Prepares to initializes our RenderViewHost by creating its RenderView and
70 // navigating to this host's url. Uses host_view for the RenderViewHost's view
71 // (can be NULL). This happens delayed to avoid locking the UI.
72 void CreateRenderViewSoon();
74 // content::WebContentsObserver
75 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
76 virtual void RenderViewCreated(
77 content::RenderViewHost
* render_view_host
) OVERRIDE
;
78 virtual void RenderViewDeleted(
79 content::RenderViewHost
* render_view_host
) OVERRIDE
;
80 virtual void RenderViewReady() OVERRIDE
;
81 virtual void RenderProcessGone(base::TerminationStatus status
) OVERRIDE
;
82 virtual void DocumentAvailableInMainFrame() OVERRIDE
;
83 virtual void DidStopLoading(
84 content::RenderViewHost
* render_view_host
) OVERRIDE
;
86 // content::WebContentsDelegate
87 virtual content::JavaScriptDialogManager
*
88 GetJavaScriptDialogManager() OVERRIDE
;
89 virtual void AddNewContents(content::WebContents
* source
,
90 content::WebContents
* new_contents
,
91 WindowOpenDisposition disposition
,
92 const gfx::Rect
& initial_pos
,
94 bool* was_blocked
) OVERRIDE
;
95 virtual void CloseContents(content::WebContents
* contents
) OVERRIDE
;
96 virtual void RequestMediaAccessPermission(
97 content::WebContents
* web_contents
,
98 const content::MediaStreamRequest
& request
,
99 const content::MediaResponseCallback
& callback
) OVERRIDE
;
100 virtual bool PreHandleGestureEvent(
101 content::WebContents
* source
,
102 const blink::WebGestureEvent
& event
) OVERRIDE
;
104 // content::NotificationObserver
105 virtual void Observe(int type
,
106 const content::NotificationSource
& source
,
107 const content::NotificationDetails
& details
) OVERRIDE
;
110 content::NotificationRegistrar
* registrar() { return ®istrar_
; }
112 // Called after the extension page finishes loading but before the
113 // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
114 virtual void OnDidStopLoading();
116 // Called once when the document first becomes available.
117 virtual void OnDocumentAvailable();
119 // Navigates to the initial page.
120 virtual void LoadInitialURL();
122 // Returns true if we're hosting a background page.
123 virtual bool IsBackgroundPage() const;
125 // Closes this host (results in deletion).
129 friend class ProcessCreationQueue
;
131 // Actually create the RenderView for this host. See CreateRenderViewSoon.
132 void CreateRenderViewNow();
135 void OnRequest(const ExtensionHostMsg_Request_Params
& params
);
137 void OnIncrementLazyKeepaliveCount();
138 void OnDecrementLazyKeepaliveCount();
139 void OnDetailedConsoleMessageAdded(
140 const base::string16
& message
,
141 const base::string16
& source
,
142 const StackTrace
& stack_trace
,
143 int32 severity_level
);
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 // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_