[email protected] | 8809f144 | 2012-01-20 21:21:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 7 | |
[email protected] | 28a69d3 | 2012-05-30 07:58:18 | [diff] [blame] | 8 | #include "base/memory/ref_counted.h" |
[email protected] | 82a4373 | 2011-10-07 16:09:11 | [diff] [blame] | 9 | #include "base/memory/weak_ptr.h" |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 10 | #include "base/observer_list.h" |
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 11 | #include "chrome/browser/extensions/active_tab_permission_granter.h" |
[email protected] | 82a4373 | 2011-10-07 16:09:11 | [diff] [blame] | 12 | #include "chrome/browser/extensions/app_notify_channel_setup.h" |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 13 | #include "chrome/browser/extensions/extension_function_dispatcher.h" |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 14 | #include "chrome/browser/extensions/image_loading_tracker.h" |
[email protected] | 6c4f0a99 | 2012-07-18 07:41:06 | [diff] [blame] | 15 | #include "chrome/browser/extensions/script_executor.h" |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 16 | #include "chrome/common/web_apps.h" |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 17 | #include "content/public/browser/notification_observer.h" |
| 18 | #include "content/public/browser/notification_registrar.h" |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 19 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame^] | 20 | #include "content/public/browser/web_contents_user_data.h" |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 21 | #include "third_party/skia/include/core/SkBitmap.h" |
| 22 | |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 23 | struct WebApplicationInfo; |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 24 | |
[email protected] | 8286f51a | 2011-05-31 17:39:13 | [diff] [blame] | 25 | namespace content { |
| 26 | struct LoadCommittedDetails; |
| 27 | } |
| 28 | |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 29 | namespace extensions { |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 30 | class Extension; |
[email protected] | 4a98816 | 2012-05-27 05:30:01 | [diff] [blame] | 31 | class LocationBarController; |
[email protected] | 4f88601 | 2012-05-19 03:51:10 | [diff] [blame] | 32 | class ScriptBadgeController; |
[email protected] | ee837d3 | 2012-10-02 22:25:49 | [diff] [blame] | 33 | class ScriptBubbleController; |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 34 | |
[email protected] | 74e5140 | 2011-04-06 14:17:59 | [diff] [blame] | 35 | // Per-tab extension helper. Also handles non-extension apps. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 36 | class TabHelper : public content::WebContentsObserver, |
| 37 | public ExtensionFunctionDispatcher::Delegate, |
| 38 | public ImageLoadingTracker::Observer, |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 39 | public AppNotifyChannelSetup::Delegate, |
| 40 | public base::SupportsWeakPtr<TabHelper>, |
[email protected] | 7381d9f | 2012-09-12 20:26:22 | [diff] [blame] | 41 | public content::NotificationObserver, |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame^] | 42 | public content::WebContentsUserData<TabHelper> { |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 43 | public: |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 44 | // Different types of action when web app info is available. |
| 45 | // OnDidGetApplicationInfo uses this to dispatch calls. |
| 46 | enum WebAppAction { |
| 47 | NONE, // No action at all. |
| 48 | CREATE_SHORTCUT, // Bring up create application shortcut dialog. |
| 49 | UPDATE_SHORTCUT // Update icon for app shortcut. |
| 50 | }; |
| 51 | |
[email protected] | d5b528c | 2012-09-27 16:30:20 | [diff] [blame] | 52 | // Observer base class for classes listening for content script messages |
| 53 | // from the renderer. |
| 54 | class ContentScriptObserver { |
| 55 | public: |
| 56 | // Map of extensions IDs to the executing script paths. |
| 57 | typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap; |
| 58 | |
| 59 | // Automatically observes and unobserves |tab_helper| on construction |
| 60 | // and destruction. |tab_helper| must outlive |this|. |
| 61 | explicit ContentScriptObserver(TabHelper* tab_helper); |
| 62 | ContentScriptObserver(); |
| 63 | |
| 64 | virtual void OnContentScriptsExecuting( |
| 65 | const content::WebContents* web_contents, |
| 66 | const ExecutingScriptsMap& executing_scripts_map, |
| 67 | int32 on_page_id, |
| 68 | const GURL& on_url) = 0; |
| 69 | |
| 70 | protected: |
| 71 | virtual ~ContentScriptObserver(); |
| 72 | |
[email protected] | d5b528c | 2012-09-27 16:30:20 | [diff] [blame] | 73 | TabHelper* tab_helper_; |
| 74 | }; |
| 75 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 76 | virtual ~TabHelper(); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 77 | |
[email protected] | d5b528c | 2012-09-27 16:30:20 | [diff] [blame] | 78 | void AddContentScriptObserver(ContentScriptObserver* observer) { |
| 79 | content_script_observers_.AddObserver(observer); |
| 80 | } |
| 81 | |
| 82 | void RemoveContentScriptObserver(ContentScriptObserver* observer) { |
| 83 | content_script_observers_.RemoveObserver(observer); |
| 84 | } |
| 85 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 86 | void CreateApplicationShortcuts(); |
| 87 | bool CanCreateApplicationShortcuts() const; |
[email protected] | 1739e57d | 2011-11-30 21:18:25 | [diff] [blame] | 88 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 89 | void set_pending_web_app_action(WebAppAction action) { |
| 90 | pending_web_app_action_ = action; |
| 91 | } |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 92 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 93 | // App extensions ------------------------------------------------------------ |
| 94 | |
| 95 | // Sets the extension denoting this as an app. If |extension| is non-null this |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 96 | // tab becomes an app-tab. WebContents does not listen for unload events for |
| 97 | // the extension. It's up to consumers of WebContents to do that. |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 98 | // |
| 99 | // NOTE: this should only be manipulated before the tab is added to a browser. |
| 100 | // TODO(sky): resolve if this is the right way to identify an app tab. If it |
| 101 | // is, than this should be passed in the constructor. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 102 | void SetExtensionApp(const Extension* extension); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 103 | |
| 104 | // Convenience for setting the app extension by id. This does nothing if |
| 105 | // |extension_app_id| is empty, or an extension can't be found given the |
| 106 | // specified id. |
| 107 | void SetExtensionAppById(const std::string& extension_app_id); |
| 108 | |
[email protected] | dd290d3 | 2012-03-06 02:47:51 | [diff] [blame] | 109 | // Set just the app icon, used by panels created by an extension. |
| 110 | void SetExtensionAppIconById(const std::string& extension_app_id); |
| 111 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 112 | const Extension* extension_app() const { return extension_app_; } |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 113 | bool is_app() const { return extension_app_ != NULL; } |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 114 | const WebApplicationInfo& web_app_info() const { |
| 115 | return web_app_info_; |
| 116 | } |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 117 | |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 118 | // If an app extension has been explicitly set for this WebContents its icon |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 119 | // is returned. |
| 120 | // |
| 121 | // NOTE: the returned icon is larger than 16x16 (its size is |
| 122 | // Extension::EXTENSION_ICON_SMALLISH). |
| 123 | SkBitmap* GetExtensionAppIcon(); |
| 124 | |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 125 | content::WebContents* web_contents() const { |
| 126 | return content::WebContentsObserver::web_contents(); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 129 | ScriptExecutor* script_executor() { |
[email protected] | 6c4f0a99 | 2012-07-18 07:41:06 | [diff] [blame] | 130 | return &script_executor_; |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 131 | } |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 132 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 133 | LocationBarController* location_bar_controller() { |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 134 | return location_bar_controller_.get(); |
| 135 | } |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 136 | |
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 137 | ActiveTabPermissionGranter* active_tab_permission_granter() { |
| 138 | return active_tab_permission_granter_.get(); |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | ee837d3 | 2012-10-02 22:25:49 | [diff] [blame] | 141 | ScriptBubbleController* script_bubble_controller() { |
| 142 | return script_bubble_controller_.get(); |
| 143 | } |
| 144 | |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 145 | // Sets a non-extension app icon associated with WebContents and fires an |
[email protected] | d908348 | 2012-01-06 00:38:46 | [diff] [blame] | 146 | // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title. |
[email protected] | 74e5140 | 2011-04-06 14:17:59 | [diff] [blame] | 147 | void SetAppIcon(const SkBitmap& app_icon); |
| 148 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 149 | private: |
[email protected] | a78f03c | 2012-09-15 05:08:19 | [diff] [blame] | 150 | explicit TabHelper(content::WebContents* web_contents); |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame^] | 151 | friend class content::WebContentsUserData<TabHelper>; |
[email protected] | a78f03c | 2012-09-15 05:08:19 | [diff] [blame] | 152 | |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 153 | // content::WebContentsObserver overrides. |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 154 | virtual void RenderViewCreated( |
| 155 | content::RenderViewHost* render_view_host) OVERRIDE; |
[email protected] | a6e16aec | 2011-11-11 18:53:04 | [diff] [blame] | 156 | virtual void DidNavigateMainFrame( |
[email protected] | 8286f51a | 2011-05-31 17:39:13 | [diff] [blame] | 157 | const content::LoadCommittedDetails& details, |
[email protected] | 6766b17 | 2011-11-21 18:29:36 | [diff] [blame] | 158 | const content::FrameNavigateParams& params) OVERRIDE; |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 159 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
[email protected] | 7381d9f | 2012-09-12 20:26:22 | [diff] [blame] | 160 | virtual void DidCloneToNewWebContents( |
| 161 | content::WebContents* old_web_contents, |
| 162 | content::WebContents* new_web_contents) OVERRIDE; |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 163 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 164 | // ExtensionFunctionDispatcher::Delegate overrides. |
[email protected] | 44f4b13 | 2012-07-17 20:36:57 | [diff] [blame] | 165 | virtual extensions::WindowController* GetExtensionWindowController() |
[email protected] | b51f3562 | 2012-05-05 22:01:43 | [diff] [blame] | 166 | const OVERRIDE; |
[email protected] | ea049a0 | 2011-12-25 21:37:09 | [diff] [blame] | 167 | virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE; |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 168 | |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 169 | // Message handlers. |
| 170 | void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info); |
| 171 | void OnInstallApplication(const WebApplicationInfo& info); |
[email protected] | a221ef09 | 2011-09-07 01:34:10 | [diff] [blame] | 172 | void OnInlineWebstoreInstall(int install_id, |
[email protected] | 7b92104 | 2012-02-11 01:41:27 | [diff] [blame] | 173 | int return_route_id, |
[email protected] | a221ef09 | 2011-09-07 01:34:10 | [diff] [blame] | 174 | const std::string& webstore_item_id, |
| 175 | const GURL& requestor_url); |
[email protected] | 2df5db74 | 2011-10-12 01:37:22 | [diff] [blame] | 176 | void OnGetAppNotifyChannel(const GURL& requestor_url, |
| 177 | const std::string& client_id, |
| 178 | int return_route_id, |
| 179 | int callback_id); |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 180 | void OnGetAppInstallState(const GURL& requestor_url, |
| 181 | int return_route_id, |
| 182 | int callback_id); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 183 | void OnRequest(const ExtensionHostMsg_Request_Params& params); |
[email protected] | d5b528c | 2012-09-27 16:30:20 | [diff] [blame] | 184 | void OnContentScriptsExecuting( |
| 185 | const ContentScriptObserver::ExecutingScriptsMap& extension_ids, |
| 186 | int32 page_id, |
| 187 | const GURL& on_url); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 188 | |
| 189 | // App extensions related methods: |
| 190 | |
| 191 | // Resets app_icon_ and if |extension| is non-null creates a new |
| 192 | // ImageLoadingTracker to load the extension's image. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 193 | void UpdateExtensionAppIcon(const Extension* extension); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 194 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 195 | const Extension* GetExtension( |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 196 | const std::string& extension_app_id); |
[email protected] | dd290d3 | 2012-03-06 02:47:51 | [diff] [blame] | 197 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 198 | // ImageLoadingTracker::Observer. |
[email protected] | bdd6eec | 2012-03-03 19:58:06 | [diff] [blame] | 199 | virtual void OnImageLoaded(const gfx::Image& image, |
| 200 | const std::string& extension_id, |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 201 | int index) OVERRIDE; |
| 202 | |
[email protected] | 734bcec | 2012-10-08 20:29:05 | [diff] [blame] | 203 | // WebstoreStandaloneInstaller::Callback. |
[email protected] | d2a639e | 2012-09-17 07:41:21 | [diff] [blame] | 204 | virtual void OnInlineInstallComplete(int install_id, |
| 205 | int return_route_id, |
| 206 | bool success, |
| 207 | const std::string& error); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 208 | |
[email protected] | 82a4373 | 2011-10-07 16:09:11 | [diff] [blame] | 209 | // AppNotifyChannelSetup::Delegate. |
[email protected] | b2689a90 | 2011-12-01 00:41:09 | [diff] [blame] | 210 | virtual void AppNotifyChannelSetupComplete( |
| 211 | const std::string& channel_id, |
| 212 | const std::string& error, |
| 213 | const AppNotifyChannelSetup* setup) OVERRIDE; |
[email protected] | 82a4373 | 2011-10-07 16:09:11 | [diff] [blame] | 214 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 215 | // content::NotificationObserver. |
| 216 | virtual void Observe(int type, |
| 217 | const content::NotificationSource& source, |
| 218 | const content::NotificationDetails& details) OVERRIDE; |
| 219 | |
| 220 | // Requests application info for the specified page. This is an asynchronous |
| 221 | // request. The delegate is notified by way of OnDidGetApplicationInfo when |
| 222 | // the data is available. |
| 223 | void GetApplicationInfo(int32 page_id); |
| 224 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 225 | // Data for app extensions --------------------------------------------------- |
| 226 | |
[email protected] | d5b528c | 2012-09-27 16:30:20 | [diff] [blame] | 227 | // Our content script observers. Declare at top so that it will outlive all |
| 228 | // other members, since they might add themselves as observers. |
| 229 | ObserverList<ContentScriptObserver> content_script_observers_; |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 230 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 231 | // If non-null this tab is an app tab and this is the extension the tab was |
| 232 | // created for. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 233 | const Extension* extension_app_; |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 234 | |
[email protected] | 74e5140 | 2011-04-06 14:17:59 | [diff] [blame] | 235 | // Icon for extension_app_ (if non-null) or a manually-set icon for |
| 236 | // non-extension apps. |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 237 | SkBitmap extension_app_icon_; |
| 238 | |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 239 | // Process any extension messages coming from the tab. |
| 240 | ExtensionFunctionDispatcher extension_function_dispatcher_; |
| 241 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 242 | // Used for loading extension_app_icon_. |
| 243 | scoped_ptr<ImageLoadingTracker> extension_app_image_loader_; |
| 244 | |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 245 | // Cached web app info data. |
| 246 | WebApplicationInfo web_app_info_; |
| 247 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 248 | // Which deferred action to perform when OnDidGetApplicationInfo is notified |
| 249 | // from a WebContents. |
| 250 | WebAppAction pending_web_app_action_; |
| 251 | |
| 252 | content::NotificationRegistrar registrar_; |
| 253 | |
[email protected] | 6c4f0a99 | 2012-07-18 07:41:06 | [diff] [blame] | 254 | ScriptExecutor script_executor_; |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 255 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 256 | scoped_ptr<LocationBarController> location_bar_controller_; |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 257 | |
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 258 | scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_; |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 259 | |
[email protected] | ee837d3 | 2012-10-02 22:25:49 | [diff] [blame] | 260 | scoped_ptr<ScriptBubbleController> script_bubble_controller_; |
| 261 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 262 | DISALLOW_COPY_AND_ASSIGN(TabHelper); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 263 | }; |
| 264 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 265 | } // namespace extensions |
| 266 | |
| 267 | #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ |