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