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