1 // Copyright (c) 2011 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 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/memory/scoped_ptr.h" 13 #include "base/perftimer.h" 14 #include "chrome/browser/extensions/extension_function_dispatcher.h" 15 #include "chrome/browser/tab_contents/render_view_host_delegate_helper.h" 16 #include "chrome/browser/ui/app_modal_dialogs/js_modal_dialog.h" 17 #include "content/browser/renderer_host/render_view_host_delegate.h" 18 #include "content/common/notification_registrar.h" 19 20 #if defined(TOOLKIT_VIEWS) 21 #include "chrome/browser/ui/views/extensions/extension_view.h" 22 #elif defined(OS_MACOSX) 23 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h" 24 #elif defined(TOOLKIT_GTK) 25 #include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h" 26 #endif 27 28 class Browser; 29 class Extension; 30 class FileSelectHelper; 31 class RenderProcessHost; 32 class RenderWidgetHostView; 33 class TabContents; 34 struct ViewHostMsg_RunFileChooser_Params; 35 struct WebPreferences; 36 37 // This class is the browser component of an extension component's RenderView. 38 // It handles setting up the renderer process, if needed, with special 39 // privileges available to extensions. It may have a view to be shown in the 40 // browser UI, or it may be hidden. 41 class ExtensionHost : public RenderViewHostDelegate, 42 public RenderViewHostDelegate::View, 43 public ExtensionFunctionDispatcher::Delegate, 44 public NotificationObserver, 45 public JavaScriptAppModalDialogDelegate { 46 public: 47 class ProcessCreationQueue; 48 49 // Enable DOM automation in created render view hosts. EnableDOMAutomation()50 static void EnableDOMAutomation() { enable_dom_automation_ = true; } 51 52 ExtensionHost(const Extension* extension, SiteInstance* site_instance, 53 const GURL& url, ViewType::Type host_type); 54 ~ExtensionHost(); 55 56 #if defined(TOOLKIT_VIEWS) set_view(ExtensionView * view)57 void set_view(ExtensionView* view) { view_.reset(view); } view()58 const ExtensionView* view() const { return view_.get(); } view()59 ExtensionView* view() { return view_.get(); } 60 #elif defined(OS_MACOSX) view()61 const ExtensionViewMac* view() const { return view_.get(); } view()62 ExtensionViewMac* view() { return view_.get(); } 63 #elif defined(TOOLKIT_GTK) view()64 const ExtensionViewGtk* view() const { return view_.get(); } view()65 ExtensionViewGtk* view() { return view_.get(); } 66 #endif 67 68 // Create an ExtensionView and tie it to this host and |browser|. Note NULL 69 // is a valid argument for |browser|. Extension views may be bound to 70 // tab-contents hosted in ExternalTabContainer objects, which do not 71 // instantiate Browser objects. 72 void CreateView(Browser* browser); 73 extension()74 const Extension* extension() const { return extension_; } extension_id()75 const std::string& extension_id() const { return extension_id_; } render_view_host()76 RenderViewHost* render_view_host() const { return render_view_host_; } 77 RenderProcessHost* render_process_host() const; 78 SiteInstance* site_instance() const; did_stop_loading()79 bool did_stop_loading() const { return did_stop_loading_; } document_element_available()80 bool document_element_available() const { 81 return document_element_available_; 82 } 83 profile()84 Profile* profile() const { return profile_; } 85 extension_host_type()86 ViewType::Type extension_host_type() const { return extension_host_type_; } 87 88 // ExtensionFunctionDispatcher::Delegate 89 virtual TabContents* associated_tab_contents() const; set_associated_tab_contents(TabContents * associated_tab_contents)90 void set_associated_tab_contents(TabContents* associated_tab_contents) { 91 associated_tab_contents_ = associated_tab_contents; 92 } 93 94 // Returns true if the render view is initialized and didn't crash. 95 bool IsRenderViewLive() const; 96 97 // Prepares to initializes our RenderViewHost by creating its RenderView and 98 // navigating to this host's url. Uses host_view for the RenderViewHost's view 99 // (can be NULL). This happens delayed to avoid locking the UI. 100 void CreateRenderViewSoon(RenderWidgetHostView* host_view); 101 102 // Sets |url_| and navigates |render_view_host_|. 103 void NavigateToURL(const GURL& url); 104 105 // Insert a default style sheet for Extension Infobars. 106 void InsertInfobarCSS(); 107 108 // Tell the renderer not to draw scrollbars on windows smaller than 109 // |size_limit| in both width and height. 110 void DisableScrollbarsForSmallWindows(const gfx::Size& size_limit); 111 112 // RenderViewHostDelegate implementation. 113 virtual bool OnMessageReceived(const IPC::Message& message); 114 virtual const GURL& GetURL() const; 115 virtual void RenderViewCreated(RenderViewHost* render_view_host); 116 virtual ViewType::Type GetRenderViewType() const; 117 virtual int GetBrowserWindowID() const; 118 virtual void RenderViewGone(RenderViewHost* render_view_host, 119 base::TerminationStatus status, 120 int error_code); 121 virtual void DidNavigate(RenderViewHost* render_view_host, 122 const ViewHostMsg_FrameNavigate_Params& params); 123 virtual void DidStopLoading(); 124 virtual void DocumentAvailableInMainFrame(RenderViewHost* render_view_host); 125 virtual void DocumentOnLoadCompletedInMainFrame( 126 RenderViewHost* render_view_host, 127 int32 page_id); 128 129 // RenderViewHostDelegate implementation. 130 virtual RenderViewHostDelegate::View* GetViewDelegate(); 131 virtual WebPreferences GetWebkitPrefs(); 132 virtual void ProcessWebUIMessage( 133 const ExtensionHostMsg_DomMessage_Params& params); 134 virtual void RunJavaScriptMessage(const std::wstring& message, 135 const std::wstring& default_prompt, 136 const GURL& frame_url, 137 const int flags, 138 IPC::Message* reply_msg, 139 bool* did_suppress_message); 140 virtual void Close(RenderViewHost* render_view_host); 141 virtual RendererPreferences GetRendererPrefs(Profile* profile) const; 142 143 // RenderViewHostDelegate::View 144 virtual void CreateNewWindow( 145 int route_id, 146 const ViewHostMsg_CreateWindow_Params& params); 147 virtual void CreateNewWidget(int route_id, WebKit::WebPopupType popup_type); 148 virtual void CreateNewFullscreenWidget(int route_id); 149 virtual void ShowCreatedWindow(int route_id, 150 WindowOpenDisposition disposition, 151 const gfx::Rect& initial_pos, 152 bool user_gesture); 153 virtual void ShowCreatedWidget(int route_id, 154 const gfx::Rect& initial_pos); 155 virtual void ShowCreatedFullscreenWidget(int route_id); 156 virtual void ShowContextMenu(const ContextMenuParams& params); 157 virtual void ShowPopupMenu(const gfx::Rect& bounds, 158 int item_height, 159 double item_font_size, 160 int selected_item, 161 const std::vector<WebMenuItem>& items, 162 bool right_aligned); 163 virtual void StartDragging(const WebDropData& drop_data, 164 WebKit::WebDragOperationsMask allowed_operations, 165 const SkBitmap& image, 166 const gfx::Point& image_offset); 167 virtual void UpdateDragCursor(WebKit::WebDragOperation operation); 168 virtual void GotFocus(); 169 virtual void TakeFocus(bool reverse); 170 virtual void LostCapture(); 171 virtual void Activate(); 172 virtual void Deactivate(); 173 virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event, 174 bool* is_keyboard_shortcut); 175 virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event); 176 virtual void HandleMouseMove(); 177 virtual void HandleMouseDown(); 178 virtual void HandleMouseLeave(); 179 virtual void HandleMouseUp(); 180 virtual void HandleMouseActivate(); 181 virtual void UpdatePreferredSize(const gfx::Size& new_size); 182 virtual void UpdateInspectorSetting(const std::string& key, 183 const std::string& value); 184 virtual void ClearInspectorSettings(); 185 186 // NotificationObserver 187 virtual void Observe(NotificationType type, 188 const NotificationSource& source, 189 const NotificationDetails& details); 190 191 // Overridden from JavaScriptAppModalDialogDelegate: 192 virtual void OnMessageBoxClosed(IPC::Message* reply_msg, 193 bool success, 194 const std::wstring& prompt); 195 virtual void SetSuppressMessageBoxes(bool suppress_message_boxes); 196 virtual gfx::NativeWindow GetMessageBoxRootWindow(); 197 virtual TabContents* AsTabContents(); 198 virtual ExtensionHost* AsExtensionHost(); 199 200 protected: 201 // Internal functions used to support the CreateNewWidget() method. If a 202 // platform requires plugging into widget creation at a lower level, then a 203 // subclass might want to override these functions, but otherwise they should 204 // be fine just implementing RenderWidgetHostView::InitAsPopup(). 205 // 206 // The Create function returns the newly created widget so it can be 207 // associated with the given route. When the widget needs to be shown later, 208 // we'll look it up again and pass the object to the Show functions rather 209 // than the route ID. 210 virtual RenderWidgetHostView* CreateNewWidgetInternal( 211 int route_id, 212 WebKit::WebPopupType popup_type); 213 virtual void ShowCreatedWidgetInternal(RenderWidgetHostView* widget_host_view, 214 const gfx::Rect& initial_pos); 215 private: 216 friend class ProcessCreationQueue; 217 218 // Whether to allow DOM automation for created RenderViewHosts. This is used 219 // for testing. 220 static bool enable_dom_automation_; 221 222 // Actually create the RenderView for this host. See CreateRenderViewSoon. 223 void CreateRenderViewNow(); 224 225 // Const version of below function. 226 const Browser* GetBrowser() const; 227 228 // ExtensionFunctionDispatcher::Delegate 229 virtual Browser* GetBrowser(); 230 virtual gfx::NativeView GetNativeViewOfHost(); 231 232 // Message handlers. 233 void OnRunFileChooser(const ViewHostMsg_RunFileChooser_Params& params); 234 235 // Handles keyboard events that were not handled by HandleKeyboardEvent(). 236 // Platform specific implementation may override this method to handle the 237 // event in platform specific way. UnhandledKeyboardEvent(const NativeWebKeyboardEvent & event)238 virtual void UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) {} 239 240 // Returns true if we're hosting a background page. 241 // This isn't valid until CreateRenderView is called. is_background_page()242 bool is_background_page() const { return !view(); } 243 244 // The extension that we're hosting in this view. 245 const Extension* extension_; 246 247 // Id of extension that we're hosting in this view. 248 const std::string extension_id_; 249 250 // The profile that this host is tied to. 251 Profile* profile_; 252 253 // Optional view that shows the rendered content in the UI. 254 #if defined(TOOLKIT_VIEWS) 255 scoped_ptr<ExtensionView> view_; 256 #elif defined(OS_MACOSX) 257 scoped_ptr<ExtensionViewMac> view_; 258 #elif defined(TOOLKIT_GTK) 259 scoped_ptr<ExtensionViewGtk> view_; 260 #endif 261 262 // The host for our HTML content. 263 RenderViewHost* render_view_host_; 264 265 // Common implementations of some RenderViewHostDelegate::View methods. 266 RenderViewHostDelegateViewHelper delegate_view_helper_; 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 URL being hosted. 275 GURL url_; 276 277 NotificationRegistrar registrar_; 278 279 scoped_ptr<ExtensionFunctionDispatcher> extension_function_dispatcher_; 280 281 // Only EXTENSION_INFOBAR, EXTENSION_POPUP, and EXTENSION_BACKGROUND_PAGE 282 // are used here, others are not hosted by ExtensionHost. 283 ViewType::Type extension_host_type_; 284 285 // The relevant TabContents associated with this ExtensionHost, if any. 286 TabContents* associated_tab_contents_; 287 288 // Used to measure how long it's been since the host was created. 289 PerfTimer since_created_; 290 291 // FileSelectHelper, lazily created. 292 scoped_ptr<FileSelectHelper> file_select_helper_; 293 294 // The time that the last javascript message was dismissed. 295 base::TimeTicks last_javascript_message_dismissal_; 296 297 // Whether to suppress all javascript messages. 298 bool suppress_javascript_messages_; 299 300 DISALLOW_COPY_AND_ASSIGN(ExtensionHost); 301 }; 302 303 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_ 304