tab_helper.h revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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_TAB_HELPER_H_ 6#define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ 7 8#include <map> 9#include <set> 10#include <string> 11#include <vector> 12 13#include "base/memory/ref_counted.h" 14#include "base/memory/scoped_ptr.h" 15#include "base/memory/weak_ptr.h" 16#include "base/observer_list.h" 17#include "chrome/browser/extensions/active_tab_permission_granter.h" 18#include "chrome/common/web_application_info.h" 19#include "content/public/browser/notification_observer.h" 20#include "content/public/browser/notification_registrar.h" 21#include "content/public/browser/web_contents_observer.h" 22#include "content/public/browser/web_contents_user_data.h" 23#include "extensions/browser/extension_function_dispatcher.h" 24#include "extensions/common/stack_frame.h" 25#include "third_party/skia/include/core/SkBitmap.h" 26 27class FaviconDownloader; 28 29namespace content { 30struct LoadCommittedDetails; 31} 32 33namespace gfx { 34class Image; 35} 36 37namespace extensions { 38class BookmarkAppHelper; 39class Extension; 40class LocationBarController; 41class ScriptExecutor; 42class WebstoreInlineInstallerFactory; 43 44// Per-tab extension helper. Also handles non-extension apps. 45class TabHelper : public content::WebContentsObserver, 46 public extensions::ExtensionFunctionDispatcher::Delegate, 47 public base::SupportsWeakPtr<TabHelper>, 48 public content::NotificationObserver, 49 public content::WebContentsUserData<TabHelper> { 50 public: 51 // Different types of action when web app info is available. 52 // OnDidGetApplicationInfo uses this to dispatch calls. 53 enum WebAppAction { 54 NONE, // No action at all. 55 CREATE_SHORTCUT, // Bring up create application shortcut dialog. 56 CREATE_HOSTED_APP, // Create and install a hosted app. 57 UPDATE_SHORTCUT // Update icon for app shortcut. 58 }; 59 60 // Observer base class for classes that need to be notified when content 61 // scripts and/or tabs.executeScript calls run on a page. 62 class ScriptExecutionObserver { 63 public: 64 // Map of extensions IDs to the executing script paths. 65 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; 66 67 // Automatically observes and unobserves |tab_helper| on construction 68 // and destruction. |tab_helper| must outlive |this|. 69 explicit ScriptExecutionObserver(TabHelper* tab_helper); 70 ScriptExecutionObserver(); 71 72 // Called when script(s) have executed on a page. 73 // 74 // |executing_scripts_map| contains all extensions that are executing 75 // scripts, mapped to the paths for those scripts. This may be an empty set 76 // if the script has no path associated with it (e.g. in the case of 77 // tabs.executeScript). 78 virtual void OnScriptsExecuted( 79 const content::WebContents* web_contents, 80 const ExecutingScriptsMap& executing_scripts_map, 81 int32 on_page_id, 82 const GURL& on_url) = 0; 83 84 protected: 85 virtual ~ScriptExecutionObserver(); 86 87 TabHelper* tab_helper_; 88 }; 89 90 virtual ~TabHelper(); 91 92 void AddScriptExecutionObserver(ScriptExecutionObserver* observer) { 93 script_execution_observers_.AddObserver(observer); 94 } 95 96 void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) { 97 script_execution_observers_.RemoveObserver(observer); 98 } 99 100 void CreateApplicationShortcuts(); 101 void CreateHostedAppFromWebContents(); 102 bool CanCreateApplicationShortcuts() const; 103 bool CanCreateBookmarkApp() const; 104 105 void set_pending_web_app_action(WebAppAction action) { 106 pending_web_app_action_ = action; 107 } 108 109 // App extensions ------------------------------------------------------------ 110 111 // Sets the extension denoting this as an app. If |extension| is non-null this 112 // tab becomes an app-tab. WebContents does not listen for unload events for 113 // the extension. It's up to consumers of WebContents to do that. 114 // 115 // NOTE: this should only be manipulated before the tab is added to a browser. 116 // TODO(sky): resolve if this is the right way to identify an app tab. If it 117 // is, than this should be passed in the constructor. 118 void SetExtensionApp(const Extension* extension); 119 120 // Convenience for setting the app extension by id. This does nothing if 121 // |extension_app_id| is empty, or an extension can't be found given the 122 // specified id. 123 void SetExtensionAppById(const std::string& extension_app_id); 124 125 // Set just the app icon, used by panels created by an extension. 126 void SetExtensionAppIconById(const std::string& extension_app_id); 127 128 const Extension* extension_app() const { return extension_app_; } 129 bool is_app() const { return extension_app_ != NULL; } 130 const WebApplicationInfo& web_app_info() const { 131 return web_app_info_; 132 } 133 134 // If an app extension has been explicitly set for this WebContents its icon 135 // is returned. 136 // 137 // NOTE: the returned icon is larger than 16x16 (its size is 138 // extension_misc::EXTENSION_ICON_SMALLISH). 139 SkBitmap* GetExtensionAppIcon(); 140 141 content::WebContents* web_contents() const { 142 return content::WebContentsObserver::web_contents(); 143 } 144 145 ScriptExecutor* script_executor() { 146 return script_executor_.get(); 147 } 148 149 LocationBarController* location_bar_controller() { 150 return location_bar_controller_.get(); 151 } 152 153 ActiveTabPermissionGranter* active_tab_permission_granter() { 154 return active_tab_permission_granter_.get(); 155 } 156 157 // Sets a non-extension app icon associated with WebContents and fires an 158 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title. 159 void SetAppIcon(const SkBitmap& app_icon); 160 161 // Sets the factory used to create inline webstore item installers. 162 // Used for testing. Takes ownership of the factory instance. 163 void SetWebstoreInlineInstallerFactoryForTests( 164 WebstoreInlineInstallerFactory* factory); 165 166 private: 167 explicit TabHelper(content::WebContents* web_contents); 168 friend class content::WebContentsUserData<TabHelper>; 169 170 // Displays UI for completion of creating a bookmark hosted app. 171 void FinishCreateBookmarkApp(const extensions::Extension* extension, 172 const WebApplicationInfo& web_app_info); 173 174 // content::WebContentsObserver overrides. 175 virtual void RenderViewCreated( 176 content::RenderViewHost* render_view_host) OVERRIDE; 177 virtual void DidNavigateMainFrame( 178 const content::LoadCommittedDetails& details, 179 const content::FrameNavigateParams& params) OVERRIDE; 180 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 181 virtual void DidCloneToNewWebContents( 182 content::WebContents* old_web_contents, 183 content::WebContents* new_web_contents) OVERRIDE; 184 185 // extensions::ExtensionFunctionDispatcher::Delegate overrides. 186 virtual extensions::WindowController* GetExtensionWindowController() 187 const OVERRIDE; 188 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE; 189 190 // Message handlers. 191 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info); 192 void OnInlineWebstoreInstall(int install_id, 193 int return_route_id, 194 const std::string& webstore_item_id, 195 const GURL& requestor_url, 196 int listeners_mask); 197 void OnGetAppInstallState(const GURL& requestor_url, 198 int return_route_id, 199 int callback_id); 200 void OnRequest(const ExtensionHostMsg_Request_Params& params); 201 void OnContentScriptsExecuting( 202 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids, 203 int32 page_id, 204 const GURL& on_url); 205 void OnWatchedPageChange(const std::vector<std::string>& css_selectors); 206 void OnDetailedConsoleMessageAdded(const base::string16& message, 207 const base::string16& source, 208 const StackTrace& stack_trace, 209 int32 severity_level); 210 211 // App extensions related methods: 212 213 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load 214 // the extension's image asynchronously. 215 void UpdateExtensionAppIcon(const Extension* extension); 216 217 const Extension* GetExtension(const std::string& extension_app_id); 218 219 void OnImageLoaded(const gfx::Image& image); 220 221 // WebstoreStandaloneInstaller::Callback. 222 virtual void OnInlineInstallComplete(int install_id, 223 int return_route_id, 224 bool success, 225 const std::string& error); 226 227 // content::NotificationObserver. 228 virtual void Observe(int type, 229 const content::NotificationSource& source, 230 const content::NotificationDetails& details) OVERRIDE; 231 232 // Requests application info for the specified page. This is an asynchronous 233 // request. The delegate is notified by way of OnDidGetApplicationInfo when 234 // the data is available. 235 void GetApplicationInfo(int32 page_id); 236 237 // Sends our tab ID to |render_view_host|. 238 void SetTabId(content::RenderViewHost* render_view_host); 239 240 // Data for app extensions --------------------------------------------------- 241 242 // Our content script observers. Declare at top so that it will outlive all 243 // other members, since they might add themselves as observers. 244 ObserverList<ScriptExecutionObserver> script_execution_observers_; 245 246 // If non-null this tab is an app tab and this is the extension the tab was 247 // created for. 248 const Extension* extension_app_; 249 250 // Icon for extension_app_ (if non-null) or a manually-set icon for 251 // non-extension apps. 252 SkBitmap extension_app_icon_; 253 254 // Process any extension messages coming from the tab. 255 extensions::ExtensionFunctionDispatcher extension_function_dispatcher_; 256 257 // Cached web app info data. 258 WebApplicationInfo web_app_info_; 259 260 // Which deferred action to perform when OnDidGetApplicationInfo is notified 261 // from a WebContents. 262 WebAppAction pending_web_app_action_; 263 264 content::NotificationRegistrar registrar_; 265 266 scoped_ptr<ScriptExecutor> script_executor_; 267 268 scoped_ptr<LocationBarController> location_bar_controller_; 269 270 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_; 271 272 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_; 273 274 Profile* profile_; 275 276 // Vend weak pointers that can be invalidated to stop in-progress loads. 277 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_; 278 279 // Creates WebstoreInlineInstaller instances for inline install triggers. 280 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_; 281 282 DISALLOW_COPY_AND_ASSIGN(TabHelper); 283}; 284 285} // namespace extensions 286 287#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ 288