1// Copyright 2014 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 EXTENSIONS_BROWSER_EXTENSION_HOST_H_ 6#define EXTENSIONS_BROWSER_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/timer/elapsed_timer.h" 14#include "content/public/browser/notification_observer.h" 15#include "content/public/browser/notification_registrar.h" 16#include "content/public/browser/web_contents_delegate.h" 17#include "content/public/browser/web_contents_observer.h" 18#include "extensions/browser/extension_function_dispatcher.h" 19#include "extensions/common/stack_frame.h" 20#include "extensions/common/view_type.h" 21 22class PrefsTabHelper; 23 24namespace content { 25class BrowserContext; 26class RenderProcessHost; 27class RenderWidgetHostView; 28class SiteInstance; 29} 30 31namespace extensions { 32class Extension; 33class ExtensionHostDelegate; 34class WindowController; 35 36// This class is the browser component of an extension component's RenderView. 37// It handles setting up the renderer process, if needed, with special 38// privileges available to extensions. It may have a view to be shown in the 39// browser UI, or it may be hidden. 40// 41// If you are adding code that only affects visible extension views (and not 42// invisible background pages) you should add it to ExtensionViewHost. 43class ExtensionHost : public content::WebContentsDelegate, 44 public content::WebContentsObserver, 45 public ExtensionFunctionDispatcher::Delegate, 46 public content::NotificationObserver { 47 public: 48 class ProcessCreationQueue; 49 50 ExtensionHost(const Extension* extension, 51 content::SiteInstance* site_instance, 52 const GURL& url, ViewType host_type); 53 virtual ~ExtensionHost(); 54 55 const Extension* extension() const { return extension_; } 56 const std::string& extension_id() const { return extension_id_; } 57 content::WebContents* host_contents() const { return host_contents_.get(); } 58 content::RenderViewHost* render_view_host() const; 59 content::RenderProcessHost* render_process_host() const; 60 bool did_stop_loading() const { return did_stop_loading_; } 61 bool document_element_available() const { 62 return document_element_available_; 63 } 64 65 content::BrowserContext* browser_context() { return browser_context_; } 66 67 ViewType extension_host_type() const { return extension_host_type_; } 68 const GURL& GetURL() const; 69 70 // Returns true if the render view is initialized and didn't crash. 71 bool IsRenderViewLive() const; 72 73 // Prepares to initializes our RenderViewHost by creating its RenderView and 74 // navigating to this host's url. Uses host_view for the RenderViewHost's view 75 // (can be NULL). This happens delayed to avoid locking the UI. 76 void CreateRenderViewSoon(); 77 78 // content::WebContentsObserver 79 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 80 virtual void RenderViewCreated( 81 content::RenderViewHost* render_view_host) OVERRIDE; 82 virtual void RenderViewDeleted( 83 content::RenderViewHost* render_view_host) OVERRIDE; 84 virtual void RenderViewReady() OVERRIDE; 85 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; 86 virtual void DocumentAvailableInMainFrame() OVERRIDE; 87 virtual void DidStopLoading( 88 content::RenderViewHost* render_view_host) OVERRIDE; 89 90 // content::WebContentsDelegate 91 virtual content::JavaScriptDialogManager* 92 GetJavaScriptDialogManager() OVERRIDE; 93 virtual void AddNewContents(content::WebContents* source, 94 content::WebContents* new_contents, 95 WindowOpenDisposition disposition, 96 const gfx::Rect& initial_pos, 97 bool user_gesture, 98 bool* was_blocked) OVERRIDE; 99 virtual void CloseContents(content::WebContents* contents) OVERRIDE; 100 virtual void RequestMediaAccessPermission( 101 content::WebContents* web_contents, 102 const content::MediaStreamRequest& request, 103 const content::MediaResponseCallback& callback) OVERRIDE; 104 virtual bool IsNeverVisible(content::WebContents* web_contents) OVERRIDE; 105 106 // content::NotificationObserver 107 virtual void Observe(int type, 108 const content::NotificationSource& source, 109 const content::NotificationDetails& details) OVERRIDE; 110 111 protected: 112 content::NotificationRegistrar* registrar() { return ®istrar_; } 113 114 // Called after the extension page finishes loading but before the 115 // EXTENSION_HOST_DID_STOP_LOADING notification is sent. 116 virtual void OnDidStopLoading(); 117 118 // Called once when the document first becomes available. 119 virtual void OnDocumentAvailable(); 120 121 // Navigates to the initial page. 122 virtual void LoadInitialURL(); 123 124 // Returns true if we're hosting a background page. 125 virtual bool IsBackgroundPage() const; 126 127 // Closes this host (results in deletion). 128 void Close(); 129 130 private: 131 friend class ProcessCreationQueue; 132 133 // Actually create the RenderView for this host. See CreateRenderViewSoon. 134 void CreateRenderViewNow(); 135 136 // Message handlers. 137 void OnRequest(const ExtensionHostMsg_Request_Params& params); 138 void OnEventAck(); 139 void OnIncrementLazyKeepaliveCount(); 140 void OnDecrementLazyKeepaliveCount(); 141 142 // Delegate for functionality that cannot exist in the extensions module. 143 scoped_ptr<ExtensionHostDelegate> delegate_; 144 145 // The extension that we're hosting in this view. 146 const Extension* extension_; 147 148 // Id of extension that we're hosting in this view. 149 const std::string extension_id_; 150 151 // The browser context that this host is tied to. 152 content::BrowserContext* browser_context_; 153 154 // The host for our HTML content. 155 scoped_ptr<content::WebContents> host_contents_; 156 157 // A weak pointer to the current or pending RenderViewHost. We don't access 158 // this through the host_contents because we want to deal with the pending 159 // host, so we can send messages to it before it finishes loading. 160 content::RenderViewHost* render_view_host_; 161 162 // Whether the RenderWidget has reported that it has stopped loading. 163 bool did_stop_loading_; 164 165 // True if the main frame has finished parsing. 166 bool document_element_available_; 167 168 // The original URL of the page being hosted. 169 GURL initial_url_; 170 171 content::NotificationRegistrar registrar_; 172 173 ExtensionFunctionDispatcher extension_function_dispatcher_; 174 175 // The type of view being hosted. 176 ViewType extension_host_type_; 177 178 // Used to measure how long it's been since the host was created. 179 base::ElapsedTimer since_created_; 180 181 DISALLOW_COPY_AND_ASSIGN(ExtensionHost); 182}; 183 184} // namespace extensions 185 186#endif // EXTENSIONS_BROWSER_EXTENSION_HOST_H_ 187