blob: f9fac327aeacf62a4c9cc606fb95d1b96160670c [file] [log] [blame]
[email protected]8809f1442012-01-20 21:21:471// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]36fb2c7c2011-04-04 15:49:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a6394ae2012-07-16 20:58:435#ifndef CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
6#define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_
[email protected]36fb2c7c2011-04-04 15:49:087
[email protected]28a69d32012-05-30 07:58:188#include "base/memory/ref_counted.h"
[email protected]82a43732011-10-07 16:09:119#include "base/memory/weak_ptr.h"
[email protected]a83b8402012-05-17 06:56:4410#include "base/observer_list.h"
[email protected]fc5e65d6b2012-06-13 00:22:5711#include "chrome/browser/extensions/active_tab_permission_manager.h"
[email protected]82a43732011-10-07 16:09:1112#include "chrome/browser/extensions/app_notify_channel_setup.h"
[email protected]c5dbef02011-05-13 05:06:0913#include "chrome/browser/extensions/extension_function_dispatcher.h"
[email protected]36fb2c7c2011-04-04 15:49:0814#include "chrome/browser/extensions/image_loading_tracker.h"
[email protected]6c4f0a992012-07-18 07:41:0615#include "chrome/browser/extensions/script_executor.h"
[email protected]8915f342011-08-29 22:14:3716#include "chrome/browser/extensions/webstore_inline_installer.h"
[email protected]7381d9f2012-09-12 20:26:2217#include "chrome/browser/tab_contents/web_contents_user_data.h"
[email protected]553602e12011-04-05 17:01:1818#include "chrome/common/web_apps.h"
[email protected]619f86182012-07-03 21:30:1819#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2521#include "content/public/browser/web_contents_observer.h"
[email protected]36fb2c7c2011-04-04 15:49:0822#include "third_party/skia/include/core/SkBitmap.h"
23
[email protected]553602e12011-04-05 17:01:1824struct WebApplicationInfo;
[email protected]36fb2c7c2011-04-04 15:49:0825
[email protected]8286f51a2011-05-31 17:39:1326namespace content {
27struct LoadCommittedDetails;
28}
29
[email protected]a83b8402012-05-17 06:56:4430namespace extensions {
[email protected]1c321ee52012-05-21 03:02:3431class Extension;
[email protected]4a988162012-05-27 05:30:0132class LocationBarController;
[email protected]4f886012012-05-19 03:51:1033class ScriptBadgeController;
[email protected]a83b8402012-05-17 06:56:4434class ScriptExecutor;
[email protected]a83b8402012-05-17 06:56:4435
[email protected]74e51402011-04-06 14:17:5936// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4337class TabHelper : public content::WebContentsObserver,
38 public ExtensionFunctionDispatcher::Delegate,
39 public ImageLoadingTracker::Observer,
[email protected]a6394ae2012-07-16 20:58:4340 public AppNotifyChannelSetup::Delegate,
41 public base::SupportsWeakPtr<TabHelper>,
[email protected]7381d9f2012-09-12 20:26:2242 public content::NotificationObserver,
43 public WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0844 public:
[email protected]619f86182012-07-03 21:30:1845 // 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]a6394ae2012-07-16 20:58:4353 virtual ~TabHelper();
[email protected]36fb2c7c2011-04-04 15:49:0854
[email protected]619f86182012-07-03 21:30:1855 void CreateApplicationShortcuts();
56 bool CanCreateApplicationShortcuts() const;
[email protected]1739e57d2011-11-30 21:18:2557
[email protected]619f86182012-07-03 21:30:1858 void set_pending_web_app_action(WebAppAction action) {
59 pending_web_app_action_ = action;
60 }
[email protected]553602e12011-04-05 17:01:1861
[email protected]36fb2c7c2011-04-04 15:49:0862 // App extensions ------------------------------------------------------------
63
64 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:1065 // 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]36fb2c7c2011-04-04 15:49:0867 //
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]a6394ae2012-07-16 20:58:4371 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:0872
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]dd290d32012-03-06 02:47:5178 // Set just the app icon, used by panels created by an extension.
79 void SetExtensionAppIconById(const std::string& extension_app_id);
80
[email protected]a6394ae2012-07-16 20:58:4381 const Extension* extension_app() const { return extension_app_; }
[email protected]36fb2c7c2011-04-04 15:49:0882 bool is_app() const { return extension_app_ != NULL; }
[email protected]553602e12011-04-05 17:01:1883 const WebApplicationInfo& web_app_info() const {
84 return web_app_info_;
85 }
[email protected]36fb2c7c2011-04-04 15:49:0886
[email protected]0932b30c2012-04-17 13:25:1087 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:0888 // 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]ea049a02011-12-25 21:37:0994 content::WebContents* web_contents() const {
95 return content::WebContentsObserver::web_contents();
[email protected]36fb2c7c2011-04-04 15:49:0896 }
97
[email protected]a6394ae2012-07-16 20:58:4398 ScriptExecutor* script_executor() {
[email protected]6c4f0a992012-07-18 07:41:0699 return &script_executor_;
[email protected]af78a802012-07-10 23:47:02100 }
[email protected]a83b8402012-05-17 06:56:44101
[email protected]a6394ae2012-07-16 20:58:43102 LocationBarController* location_bar_controller() {
[email protected]af78a802012-07-10 23:47:02103 return location_bar_controller_.get();
104 }
[email protected]a83b8402012-05-17 06:56:44105
[email protected]a6394ae2012-07-16 20:58:43106 ActiveTabPermissionManager* active_tab_permission_manager() {
[email protected]7381d9f2012-09-12 20:26:22107 return active_tab_permission_manager_.get();
[email protected]fc5e65d6b2012-06-13 00:22:57108 }
109
[email protected]0932b30c2012-04-17 13:25:10110 // Sets a non-extension app icon associated with WebContents and fires an
[email protected]d9083482012-01-06 00:38:46111 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
[email protected]74e51402011-04-06 14:17:59112 void SetAppIcon(const SkBitmap& app_icon);
113
[email protected]36fb2c7c2011-04-04 15:49:08114 private:
[email protected]a78f03c2012-09-15 05:08:19115 explicit TabHelper(content::WebContents* web_contents);
116 static int kUserDataKey;
117 friend class WebContentsUserData<TabHelper>;
118
[email protected]d8c660432011-12-22 20:51:25119 // content::WebContentsObserver overrides.
[email protected]fc5e65d6b2012-06-13 00:22:57120 virtual void RenderViewCreated(
121 content::RenderViewHost* render_view_host) OVERRIDE;
[email protected]a6e16aec2011-11-11 18:53:04122 virtual void DidNavigateMainFrame(
[email protected]8286f51a2011-05-31 17:39:13123 const content::LoadCommittedDetails& details,
[email protected]6766b172011-11-21 18:29:36124 const content::FrameNavigateParams& params) OVERRIDE;
[email protected]8915f342011-08-29 22:14:37125 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]7381d9f2012-09-12 20:26:22126 virtual void DidCloneToNewWebContents(
127 content::WebContents* old_web_contents,
128 content::WebContents* new_web_contents) OVERRIDE;
[email protected]553602e12011-04-05 17:01:18129
[email protected]c5dbef02011-05-13 05:06:09130 // ExtensionFunctionDispatcher::Delegate overrides.
[email protected]44f4b132012-07-17 20:36:57131 virtual extensions::WindowController* GetExtensionWindowController()
[email protected]b51f35622012-05-05 22:01:43132 const OVERRIDE;
[email protected]ea049a02011-12-25 21:37:09133 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
[email protected]c5dbef02011-05-13 05:06:09134
[email protected]553602e12011-04-05 17:01:18135 // Message handlers.
136 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
137 void OnInstallApplication(const WebApplicationInfo& info);
[email protected]a221ef092011-09-07 01:34:10138 void OnInlineWebstoreInstall(int install_id,
[email protected]7b921042012-02-11 01:41:27139 int return_route_id,
[email protected]a221ef092011-09-07 01:34:10140 const std::string& webstore_item_id,
141 const GURL& requestor_url);
[email protected]2df5db742011-10-12 01:37:22142 void OnGetAppNotifyChannel(const GURL& requestor_url,
143 const std::string& client_id,
144 int return_route_id,
145 int callback_id);
[email protected]f2fe87c2012-04-24 17:53:49146 void OnGetAppInstallState(const GURL& requestor_url,
147 int return_route_id,
148 int callback_id);
[email protected]c5dbef02011-05-13 05:06:09149 void OnRequest(const ExtensionHostMsg_Request_Params& params);
[email protected]36fb2c7c2011-04-04 15:49:08150
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]a6394ae2012-07-16 20:58:43155 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08156
[email protected]a6394ae2012-07-16 20:58:43157 const Extension* GetExtension(
[email protected]1c321ee52012-05-21 03:02:34158 const std::string& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51159
[email protected]36fb2c7c2011-04-04 15:49:08160 // ImageLoadingTracker::Observer.
[email protected]bdd6eec2012-03-03 19:58:06161 virtual void OnImageLoaded(const gfx::Image& image,
162 const std::string& extension_id,
[email protected]8915f342011-08-29 22:14:37163 int index) OVERRIDE;
164
[email protected]d2a639e2012-09-17 07:41:21165 // WebstoreInlineInstaller::Callback.
166 virtual void OnInlineInstallComplete(int install_id,
167 int return_route_id,
168 bool success,
169 const std::string& error);
[email protected]36fb2c7c2011-04-04 15:49:08170
[email protected]82a43732011-10-07 16:09:11171 // AppNotifyChannelSetup::Delegate.
[email protected]b2689a902011-12-01 00:41:09172 virtual void AppNotifyChannelSetupComplete(
173 const std::string& channel_id,
174 const std::string& error,
175 const AppNotifyChannelSetup* setup) OVERRIDE;
[email protected]82a43732011-10-07 16:09:11176
[email protected]619f86182012-07-03 21:30:18177 // 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]36fb2c7c2011-04-04 15:49:08187 // Data for app extensions ---------------------------------------------------
188
[email protected]a83b8402012-05-17 06:56:44189 // 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]36fb2c7c2011-04-04 15:49:08193 // If non-null this tab is an app tab and this is the extension the tab was
194 // created for.
[email protected]a6394ae2012-07-16 20:58:43195 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08196
[email protected]74e51402011-04-06 14:17:59197 // Icon for extension_app_ (if non-null) or a manually-set icon for
198 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08199 SkBitmap extension_app_icon_;
200
[email protected]c5dbef02011-05-13 05:06:09201 // Process any extension messages coming from the tab.
202 ExtensionFunctionDispatcher extension_function_dispatcher_;
203
[email protected]36fb2c7c2011-04-04 15:49:08204 // Used for loading extension_app_icon_.
205 scoped_ptr<ImageLoadingTracker> extension_app_image_loader_;
206
[email protected]553602e12011-04-05 17:01:18207 // Cached web app info data.
208 WebApplicationInfo web_app_info_;
209
[email protected]619f86182012-07-03 21:30:18210 // 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]6c4f0a992012-07-18 07:41:06216 ScriptExecutor script_executor_;
[email protected]af78a802012-07-10 23:47:02217
[email protected]a6394ae2012-07-16 20:58:43218 scoped_ptr<LocationBarController> location_bar_controller_;
[email protected]a83b8402012-05-17 06:56:44219
[email protected]7381d9f2012-09-12 20:26:22220 scoped_ptr<ActiveTabPermissionManager> active_tab_permission_manager_;
[email protected]fc5e65d6b2012-06-13 00:22:57221
[email protected]a6394ae2012-07-16 20:58:43222 DISALLOW_COPY_AND_ASSIGN(TabHelper);
[email protected]36fb2c7c2011-04-04 15:49:08223};
224
[email protected]a6394ae2012-07-16 20:58:43225} // namespace extensions
226
227#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_