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