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