extension_host.h revision 58537e28ecd584eab876aee8be7156509866d23a
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. 4 5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ 6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/logging.h" 12#include "base/memory/scoped_ptr.h" 13#include "base/test/perftimer.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" 22 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" 31#endif 32 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" 36#endif 37 38class Browser; 39class PrefsTabHelper; 40 41namespace content { 42class RenderProcessHost; 43class RenderWidgetHostView; 44class SiteInstance; 45} 46 47namespace extensions { 48class Extension; 49class WindowController; 50 51// This class is the browser component of an extension component's RenderView. 52// It handles setting up the renderer process, if needed, with special 53// privileges available to extensions. It may have a view to be shown in the 54// browser UI, or it may be hidden. 55class ExtensionHost : public content::WebContentsDelegate, 56#if !defined(OS_ANDROID) 57 public ChromeWebModalDialogManagerDelegate, 58 public web_modal::WebContentsModalDialogHost, 59#endif 60 public content::WebContentsObserver, 61 public ExtensionFunctionDispatcher::Delegate, 62 public content::NotificationObserver { 63 public: 64 class ProcessCreationQueue; 65 66#if defined(TOOLKIT_VIEWS) 67 typedef ExtensionViewViews PlatformExtensionView; 68#elif defined(OS_MACOSX) 69 typedef ExtensionViewMac PlatformExtensionView; 70#elif defined(TOOLKIT_GTK) 71 typedef ExtensionViewGtk PlatformExtensionView; 72#elif defined(OS_ANDROID) 73 // Android does not support extensions. 74 typedef ExtensionViewAndroid PlatformExtensionView; 75#endif 76 77 ExtensionHost(const Extension* extension, 78 content::SiteInstance* site_instance, 79 const GURL& url, ViewType host_type); 80 virtual ~ExtensionHost(); 81 82#if defined(TOOLKIT_VIEWS) 83 void set_view(PlatformExtensionView* view) { view_.reset(view); } 84#endif 85 86 const PlatformExtensionView* view() const { 87#if defined(OS_ANDROID) 88 NOTREACHED(); 89#endif 90 return view_.get(); 91 } 92 93 PlatformExtensionView* view() { 94#if defined(OS_ANDROID) 95 NOTREACHED(); 96#endif 97 return view_.get(); 98 } 99 100 // Create an ExtensionView and tie it to this host and |browser|. Note NULL 101 // is a valid argument for |browser|. Extension views may be bound to 102 // tab-contents hosted in ExternalTabContainer objects, which do not 103 // instantiate Browser objects. 104 void CreateView(Browser* browser); 105 106 const Extension* extension() const { return extension_; } 107 const std::string& extension_id() const { return extension_id_; } 108 content::WebContents* host_contents() const { return host_contents_.get(); } 109 content::RenderViewHost* render_view_host() const; 110 content::RenderProcessHost* render_process_host() const; 111 bool did_stop_loading() const { return did_stop_loading_; } 112 bool document_element_available() const { 113 return document_element_available_; 114 } 115 116 Profile* profile() const { return profile_; } 117 118 ViewType extension_host_type() const { return extension_host_type_; } 119 const GURL& GetURL() const; 120 121 // ExtensionFunctionDispatcher::Delegate 122 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE; 123 virtual content::WebContents* GetVisibleWebContents() const OVERRIDE; 124 void SetAssociatedWebContents(content::WebContents* web_contents); 125 126 // Returns true if the render view is initialized and didn't crash. 127 bool IsRenderViewLive() const; 128 129 // Prepares to initializes our RenderViewHost by creating its RenderView and 130 // navigating to this host's url. Uses host_view for the RenderViewHost's view 131 // (can be NULL). This happens delayed to avoid locking the UI. 132 void CreateRenderViewSoon(); 133 134 // Insert a default style sheet for Extension Infobars. 135 void InsertInfobarCSS(); 136 137 // Notifications from the JavaScriptDialogManager when a dialog is being 138 // opened/closed. 139 void WillRunJavaScriptDialog(); 140 void DidCloseJavaScriptDialog(); 141 142 // content::WebContentsObserver 143 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 144 virtual void RenderViewCreated( 145 content::RenderViewHost* render_view_host) OVERRIDE; 146 virtual void RenderViewDeleted( 147 content::RenderViewHost* render_view_host) OVERRIDE; 148 virtual void RenderViewReady() OVERRIDE; 149 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; 150 virtual void DocumentAvailableInMainFrame() OVERRIDE; 151 virtual void DidStopLoading( 152 content::RenderViewHost* render_view_host) OVERRIDE; 153 154 // content::WebContentsDelegate 155 virtual content::WebContents* OpenURLFromTab( 156 content::WebContents* source, 157 const content::OpenURLParams& params) OVERRIDE; 158 virtual bool PreHandleKeyboardEvent( 159 content::WebContents* source, 160 const content::NativeWebKeyboardEvent& event, 161 bool* is_keyboard_shortcut) OVERRIDE; 162 virtual void HandleKeyboardEvent( 163 content::WebContents* source, 164 const content::NativeWebKeyboardEvent& event) OVERRIDE; 165 virtual void ResizeDueToAutoResize(content::WebContents* source, 166 const gfx::Size& new_size) OVERRIDE; 167 virtual content::JavaScriptDialogManager* 168 GetJavaScriptDialogManager() OVERRIDE; 169 virtual content::ColorChooser* OpenColorChooser( 170 content::WebContents* web_contents, SkColor color) OVERRIDE; 171 virtual void RunFileChooser( 172 content::WebContents* tab, 173 const content::FileChooserParams& params) OVERRIDE; 174 virtual void AddNewContents(content::WebContents* source, 175 content::WebContents* new_contents, 176 WindowOpenDisposition disposition, 177 const gfx::Rect& initial_pos, 178 bool user_gesture, 179 bool* was_blocked) OVERRIDE; 180 virtual void CloseContents(content::WebContents* contents) OVERRIDE; 181 virtual void RequestMediaAccessPermission( 182 content::WebContents* web_contents, 183 const content::MediaStreamRequest& request, 184 const content::MediaResponseCallback& callback) OVERRIDE; 185 186 // content::NotificationObserver 187 virtual void Observe(int type, 188 const content::NotificationSource& source, 189 const content::NotificationDetails& details) OVERRIDE; 190 191 private: 192 friend class ProcessCreationQueue; 193 194 // Actually create the RenderView for this host. See CreateRenderViewSoon. 195 void CreateRenderViewNow(); 196 197 // Navigates to the initial page. 198 void LoadInitialURL(); 199 200 // Closes this host (results in deletion). 201 void Close(); 202 203#if !defined(OS_ANDROID) 204 // ChromeWebModalDialogManagerDelegate 205 virtual web_modal::WebContentsModalDialogHost* 206 GetWebContentsModalDialogHost() OVERRIDE; 207 208 // web_modal::WebContentsModalDialogHost 209 virtual gfx::NativeView GetHostView() const OVERRIDE; 210 virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE; 211 virtual gfx::Size GetMaximumDialogSize() OVERRIDE; 212 virtual void AddObserver( 213 web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE; 214 virtual void RemoveObserver( 215 web_modal::WebContentsModalDialogHostObserver* observer) OVERRIDE; 216#endif 217 218 // ExtensionFunctionDispatcher::Delegate 219 virtual WindowController* GetExtensionWindowController() const OVERRIDE; 220 221 // Message handlers. 222 void OnRequest(const ExtensionHostMsg_Request_Params& params); 223 void OnEventAck(); 224 void OnIncrementLazyKeepaliveCount(); 225 void OnDecrementLazyKeepaliveCount(); 226 void OnDetailedConsoleMessageAdded( 227 const base::string16& message, 228 const base::string16& source, 229 const StackTrace& stack_trace, 230 int32 severity_level); 231 232 // Handles keyboard events that were not handled by HandleKeyboardEvent(). 233 // Platform specific implementation may override this method to handle the 234 // event in platform specific way. 235 virtual void UnhandledKeyboardEvent( 236 content::WebContents* source, 237 const content::NativeWebKeyboardEvent& event); 238 239 // Returns true if we're hosting a background page. 240 // This isn't valid until CreateRenderView is called. 241 bool is_background_page() const { return !view(); } 242 243 // The extension that we're hosting in this view. 244 const Extension* extension_; 245 246 // Id of extension that we're hosting in this view. 247 const std::string extension_id_; 248 249 // The profile that this host is tied to. 250 Profile* profile_; 251 252 // Optional view that shows the rendered content in the UI. 253 scoped_ptr<PlatformExtensionView> view_; 254 255 // Used to create dialog boxes. 256 // It must outlive host_contents_ as host_contents_ will access it 257 // during destruction. 258 scoped_ptr<content::JavaScriptDialogManager> dialog_manager_; 259 260 // The host for our HTML content. 261 scoped_ptr<content::WebContents> host_contents_; 262 263 // A weak pointer to the current or pending RenderViewHost. We don't access 264 // this through the host_contents because we want to deal with the pending 265 // host, so we can send messages to it before it finishes loading. 266 content::RenderViewHost* render_view_host_; 267 268 // Whether the RenderWidget has reported that it has stopped loading. 269 bool did_stop_loading_; 270 271 // True if the main frame has finished parsing. 272 bool document_element_available_; 273 274 // The original URL of the page being hosted. 275 GURL initial_url_; 276 277 content::NotificationRegistrar registrar_; 278 279 ExtensionFunctionDispatcher extension_function_dispatcher_; 280 281 // The type of view being hosted. 282 ViewType extension_host_type_; 283 284 // The relevant WebContents associated with this ExtensionHost, if any. 285 content::WebContents* associated_web_contents_; 286 287 // Used to measure how long it's been since the host was created. 288 PerfTimer since_created_; 289 290 DISALLOW_COPY_AND_ASSIGN(ExtensionHost); 291}; 292 293} // namespace extensions 294 295#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ 296