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/javascript_dialog_manager.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/common/stack_frame.h"
21 #include "extensions/common/view_type.h"
23 #if defined(TOOLKIT_VIEWS)
24 #include "chrome/browser/ui/views/extensions/extension_view_views.h"
25 #elif defined(OS_MACOSX)
26 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h"
27 #elif defined(TOOLKIT_GTK)
28 #include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h"
29 #elif defined(OS_ANDROID)
30 #include "chrome/browser/ui/android/extensions/extension_view_android.h"
33 #if !defined(OS_ANDROID)
34 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
35 #include "components/web_modal/web_contents_modal_dialog_host.h"
43 class RenderProcessHost;
44 class RenderWidgetHostView;
48 namespace extensions {
50 class WindowController;
52 // This class is the browser component of an extension component's RenderView.
53 // It handles setting up the renderer process, if needed, with special
54 // privileges available to extensions. It may have a view to be shown in the
55 // browser UI, or it may be hidden.
56 class ExtensionHost : public content::WebContentsDelegate,
57 #if !defined(OS_ANDROID)
58 public ChromeWebModalDialogManagerDelegate,
59 public web_modal::WebContentsModalDialogHost,
61 public content::WebContentsObserver,
62 public ExtensionFunctionDispatcher::Delegate,
63 public content::NotificationObserver {
65 class ProcessCreationQueue;
67 #if defined(TOOLKIT_VIEWS)
68 typedef ExtensionViewViews PlatformExtensionView;
69 #elif defined(OS_MACOSX)
70 typedef ExtensionViewMac PlatformExtensionView;
71 #elif defined(TOOLKIT_GTK)
72 typedef ExtensionViewGtk PlatformExtensionView;
73 #elif defined(OS_ANDROID)
74 // Android does not support extensions.
75 typedef ExtensionViewAndroid PlatformExtensionView;
78 ExtensionHost(const Extension* extension,
79 content::SiteInstance* site_instance,
80 const GURL& url, ViewType host_type);
81 virtual ~ExtensionHost();
83 #if defined(TOOLKIT_VIEWS)
84 void set_view(PlatformExtensionView* view) { view_.reset(view); }
87 const PlatformExtensionView* view() const {
88 #if defined(OS_ANDROID)
94 PlatformExtensionView* view() {
95 #if defined(OS_ANDROID)
101 // Create an ExtensionView and tie it to this host and |browser|. Note NULL
102 // is a valid argument for |browser|. Extension views may be bound to
103 // tab-contents hosted in ExternalTabContainer objects, which do not
104 // instantiate Browser objects.
105 void CreateView(Browser* browser);
107 const Extension* extension() const { return extension_; }
108 const std::string& extension_id() const { return extension_id_; }
109 content::WebContents* host_contents() const { return host_contents_.get(); }
110 content::RenderViewHost* render_view_host() const;
111 content::RenderProcessHost* render_process_host() const;
112 bool did_stop_loading() const { return did_stop_loading_; }
113 bool document_element_available() const {
114 return document_element_available_;
117 Profile* profile() const { return profile_; }
119 // Returns the same value as profile() but as a BrowserContext. Implemented
120 // in the .cc file to avoid including profile.h in this header.
121 content::BrowserContext* browser_context();
123 ViewType extension_host_type() const { return extension_host_type_; }
124 const GURL& GetURL() const;
126 // ExtensionFunctionDispatcher::Delegate
127 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
128 virtual content::WebContents* GetVisibleWebContents() const OVERRIDE;
129 void SetAssociatedWebContents(content::WebContents* web_contents);
131 // Returns true if the render view is initialized and didn't crash.
132 bool IsRenderViewLive() const;
134 // Prepares to initializes our RenderViewHost by creating its RenderView and
135 // navigating to this host's url. Uses host_view for the RenderViewHost's view
136 // (can be NULL). This happens delayed to avoid locking the UI.
137 void CreateRenderViewSoon();
139 // Insert a default style sheet for Extension Infobars.
140 void InsertInfobarCSS();
142 // Notifications from the JavaScriptDialogManager when a dialog is being
144 void WillRunJavaScriptDialog();
145 void DidCloseJavaScriptDialog();
147 // content::WebContentsObserver
148 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
149 virtual void RenderViewCreated(
150 content::RenderViewHost* render_view_host) OVERRIDE;
151 virtual void RenderViewDeleted(
152 content::RenderViewHost* render_view_host) OVERRIDE;
153 virtual void RenderViewReady() OVERRIDE;
154 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
155 virtual void DocumentAvailableInMainFrame() OVERRIDE;
156 virtual void DidStopLoading(
157 content::RenderViewHost* render_view_host) OVERRIDE;
159 // content::WebContentsDelegate
160 virtual content::WebContents* OpenURLFromTab(
161 content::WebContents* source,
162 const content::OpenURLParams& params) OVERRIDE;
163 virtual bool PreHandleKeyboardEvent(
164 content::WebContents* source,
165 const content::NativeWebKeyboardEvent& event,
166 bool* is_keyboard_shortcut) OVERRIDE;
167 virtual void HandleKeyboardEvent(
168 content::WebContents* source,
169 const content::NativeWebKeyboardEvent& event) OVERRIDE;
170 virtual void ResizeDueToAutoResize(content::WebContents* source,
171 const gfx::Size& new_size) OVERRIDE;
172 virtual content::JavaScriptDialogManager*
173 GetJavaScriptDialogManager() OVERRIDE;
174 virtual content::ColorChooser* OpenColorChooser(
175 content::WebContents* web_contents, SkColor color) OVERRIDE;
176 virtual void RunFileChooser(
177 content::WebContents* tab,
178 const content::FileChooserParams& params) OVERRIDE;
179 virtual void AddNewContents(content::WebContents* source,
180 content::WebContents* new_contents,
181 WindowOpenDisposition disposition,
182 const gfx::Rect& initial_pos,
184 bool* was_blocked) OVERRIDE;
185 virtual void CloseContents(content::WebContents* contents) OVERRIDE;
186 virtual void RequestMediaAccessPermission(
187 content::WebContents* web_contents,
188 const content::MediaStreamRequest& request,
189 const content::MediaResponseCallback& callback) OVERRIDE;
191 // content::NotificationObserver
192 virtual void Observe(int type,
193 const content::NotificationSource& source,
194 const content::NotificationDetails& details) OVERRIDE;
197 friend class ProcessCreationQueue;
199 // Actually create the RenderView for this host. See CreateRenderViewSoon.
200 void CreateRenderViewNow();
202 // Navigates to the initial page.
203 void LoadInitialURL();
205 // Closes this host (results in deletion).
208 #if !defined(OS_ANDROID)
209 // ChromeWebModalDialogManagerDelegate
210 virtual web_modal::WebContentsModalDialogHost*
211 GetWebContentsModalDialogHost() OVERRIDE;
213 // web_modal::WebContentsModalDialogHost
214 virtual gfx::NativeView GetHostView() const OVERRIDE;
215 virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
216 virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
217 virtual void AddObserver(
218 web_modal::ModalDialogHostObserver* observer) OVERRIDE;
219 virtual void RemoveObserver(
220 web_modal::ModalDialogHostObserver* observer) OVERRIDE;
223 // ExtensionFunctionDispatcher::Delegate
224 virtual WindowController* GetExtensionWindowController() const OVERRIDE;
227 void OnRequest(const ExtensionHostMsg_Request_Params& params);
229 void OnIncrementLazyKeepaliveCount();
230 void OnDecrementLazyKeepaliveCount();
231 void OnDetailedConsoleMessageAdded(
232 const base::string16& message,
233 const base::string16& source,
234 const StackTrace& stack_trace,
235 int32 severity_level);
237 // Handles keyboard events that were not handled by HandleKeyboardEvent().
238 // Platform specific implementation may override this method to handle the
239 // event in platform specific way.
240 virtual void UnhandledKeyboardEvent(
241 content::WebContents* source,
242 const content::NativeWebKeyboardEvent& event);
244 // Returns true if we're hosting a background page.
245 // This isn't valid until CreateRenderView is called.
246 bool is_background_page() const { return !view(); }
248 // The extension that we're hosting in this view.
249 const Extension* extension_;
251 // Id of extension that we're hosting in this view.
252 const std::string extension_id_;
254 // The profile that this host is tied to.
257 // Optional view that shows the rendered content in the UI.
258 scoped_ptr<PlatformExtensionView> view_;
260 // Used to create dialog boxes.
261 // It must outlive host_contents_ as host_contents_ will access it
262 // during destruction.
263 scoped_ptr<content::JavaScriptDialogManager> dialog_manager_;
265 // The host for our HTML content.
266 scoped_ptr<content::WebContents> host_contents_;
268 // A weak pointer to the current or pending RenderViewHost. We don't access
269 // this through the host_contents because we want to deal with the pending
270 // host, so we can send messages to it before it finishes loading.
271 content::RenderViewHost* render_view_host_;
273 // Whether the RenderWidget has reported that it has stopped loading.
274 bool did_stop_loading_;
276 // True if the main frame has finished parsing.
277 bool document_element_available_;
279 // The original URL of the page being hosted.
282 content::NotificationRegistrar registrar_;
284 ExtensionFunctionDispatcher extension_function_dispatcher_;
286 // The type of view being hosted.
287 ViewType extension_host_type_;
289 // The relevant WebContents associated with this ExtensionHost, if any.
290 content::WebContents* associated_web_contents_;
292 // Used to measure how long it's been since the host was created.
293 base::ElapsedTimer since_created_;
295 DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
298 } // namespace extensions
300 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_