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