blob: 5d8708e794eb90839c6b291e6f9f75d1dac1e7f5 [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]78ce3022012-09-24 01:48:4811#include "chrome/browser/extensions/active_tab_permission_granter.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]553602e12011-04-05 17:01:1816#include "chrome/common/web_apps.h"
[email protected]619f86182012-07-03 21:30:1817#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2519#include "content/public/browser/web_contents_observer.h"
[email protected]46b3c982012-10-09 18:38:3020#include "content/public/browser/web_contents_user_data.h"
[email protected]36fb2c7c2011-04-04 15:49:0821#include "third_party/skia/include/core/SkBitmap.h"
22
[email protected]553602e12011-04-05 17:01:1823struct WebApplicationInfo;
[email protected]36fb2c7c2011-04-04 15:49:0824
[email protected]8286f51a2011-05-31 17:39:1325namespace content {
26struct LoadCommittedDetails;
27}
28
[email protected]a83b8402012-05-17 06:56:4429namespace extensions {
[email protected]1c321ee52012-05-21 03:02:3430class Extension;
[email protected]4a988162012-05-27 05:30:0131class LocationBarController;
[email protected]4f886012012-05-19 03:51:1032class ScriptBadgeController;
[email protected]ee837d32012-10-02 22:25:4933class ScriptBubbleController;
[email protected]a83b8402012-05-17 06:56:4434
[email protected]74e51402011-04-06 14:17:5935// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4336class TabHelper : public content::WebContentsObserver,
37 public ExtensionFunctionDispatcher::Delegate,
38 public ImageLoadingTracker::Observer,
[email protected]a6394ae2012-07-16 20:58:4339 public AppNotifyChannelSetup::Delegate,
40 public base::SupportsWeakPtr<TabHelper>,
[email protected]7381d9f2012-09-12 20:26:2241 public content::NotificationObserver,
[email protected]46b3c982012-10-09 18:38:3042 public content::WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0843 public:
[email protected]619f86182012-07-03 21:30:1844 // 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]d5b528c2012-09-27 16:30:2052 // 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]d5b528c2012-09-27 16:30:2073 TabHelper* tab_helper_;
74 };
75
[email protected]a6394ae2012-07-16 20:58:4376 virtual ~TabHelper();
[email protected]36fb2c7c2011-04-04 15:49:0877
[email protected]d5b528c2012-09-27 16:30:2078 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]619f86182012-07-03 21:30:1886 void CreateApplicationShortcuts();
87 bool CanCreateApplicationShortcuts() const;
[email protected]1739e57d2011-11-30 21:18:2588
[email protected]619f86182012-07-03 21:30:1889 void set_pending_web_app_action(WebAppAction action) {
90 pending_web_app_action_ = action;
91 }
[email protected]553602e12011-04-05 17:01:1892
[email protected]36fb2c7c2011-04-04 15:49:0893 // App extensions ------------------------------------------------------------
94
95 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:1096 // 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]36fb2c7c2011-04-04 15:49:0898 //
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]a6394ae2012-07-16 20:58:43102 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08103
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]dd290d32012-03-06 02:47:51109 // Set just the app icon, used by panels created by an extension.
110 void SetExtensionAppIconById(const std::string& extension_app_id);
111
[email protected]a6394ae2012-07-16 20:58:43112 const Extension* extension_app() const { return extension_app_; }
[email protected]36fb2c7c2011-04-04 15:49:08113 bool is_app() const { return extension_app_ != NULL; }
[email protected]553602e12011-04-05 17:01:18114 const WebApplicationInfo& web_app_info() const {
115 return web_app_info_;
116 }
[email protected]36fb2c7c2011-04-04 15:49:08117
[email protected]0932b30c2012-04-17 13:25:10118 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:08119 // 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]ea049a02011-12-25 21:37:09125 content::WebContents* web_contents() const {
126 return content::WebContentsObserver::web_contents();
[email protected]36fb2c7c2011-04-04 15:49:08127 }
128
[email protected]a6394ae2012-07-16 20:58:43129 ScriptExecutor* script_executor() {
[email protected]6c4f0a992012-07-18 07:41:06130 return &script_executor_;
[email protected]af78a802012-07-10 23:47:02131 }
[email protected]a83b8402012-05-17 06:56:44132
[email protected]a6394ae2012-07-16 20:58:43133 LocationBarController* location_bar_controller() {
[email protected]af78a802012-07-10 23:47:02134 return location_bar_controller_.get();
135 }
[email protected]a83b8402012-05-17 06:56:44136
[email protected]78ce3022012-09-24 01:48:48137 ActiveTabPermissionGranter* active_tab_permission_granter() {
138 return active_tab_permission_granter_.get();
[email protected]fc5e65d6b2012-06-13 00:22:57139 }
140
[email protected]ee837d32012-10-02 22:25:49141 ScriptBubbleController* script_bubble_controller() {
142 return script_bubble_controller_.get();
143 }
144
[email protected]0932b30c2012-04-17 13:25:10145 // Sets a non-extension app icon associated with WebContents and fires an
[email protected]d9083482012-01-06 00:38:46146 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
[email protected]74e51402011-04-06 14:17:59147 void SetAppIcon(const SkBitmap& app_icon);
148
[email protected]36fb2c7c2011-04-04 15:49:08149 private:
[email protected]a78f03c2012-09-15 05:08:19150 explicit TabHelper(content::WebContents* web_contents);
[email protected]46b3c982012-10-09 18:38:30151 friend class content::WebContentsUserData<TabHelper>;
[email protected]a78f03c2012-09-15 05:08:19152
[email protected]d8c660432011-12-22 20:51:25153 // content::WebContentsObserver overrides.
[email protected]fc5e65d6b2012-06-13 00:22:57154 virtual void RenderViewCreated(
155 content::RenderViewHost* render_view_host) OVERRIDE;
[email protected]a6e16aec2011-11-11 18:53:04156 virtual void DidNavigateMainFrame(
[email protected]8286f51a2011-05-31 17:39:13157 const content::LoadCommittedDetails& details,
[email protected]6766b172011-11-21 18:29:36158 const content::FrameNavigateParams& params) OVERRIDE;
[email protected]8915f342011-08-29 22:14:37159 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]7381d9f2012-09-12 20:26:22160 virtual void DidCloneToNewWebContents(
161 content::WebContents* old_web_contents,
162 content::WebContents* new_web_contents) OVERRIDE;
[email protected]553602e12011-04-05 17:01:18163
[email protected]c5dbef02011-05-13 05:06:09164 // ExtensionFunctionDispatcher::Delegate overrides.
[email protected]44f4b132012-07-17 20:36:57165 virtual extensions::WindowController* GetExtensionWindowController()
[email protected]b51f35622012-05-05 22:01:43166 const OVERRIDE;
[email protected]ea049a02011-12-25 21:37:09167 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
[email protected]c5dbef02011-05-13 05:06:09168
[email protected]553602e12011-04-05 17:01:18169 // Message handlers.
170 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
171 void OnInstallApplication(const WebApplicationInfo& info);
[email protected]a221ef092011-09-07 01:34:10172 void OnInlineWebstoreInstall(int install_id,
[email protected]7b921042012-02-11 01:41:27173 int return_route_id,
[email protected]a221ef092011-09-07 01:34:10174 const std::string& webstore_item_id,
175 const GURL& requestor_url);
[email protected]2df5db742011-10-12 01:37:22176 void OnGetAppNotifyChannel(const GURL& requestor_url,
177 const std::string& client_id,
178 int return_route_id,
179 int callback_id);
[email protected]f2fe87c2012-04-24 17:53:49180 void OnGetAppInstallState(const GURL& requestor_url,
181 int return_route_id,
182 int callback_id);
[email protected]c5dbef02011-05-13 05:06:09183 void OnRequest(const ExtensionHostMsg_Request_Params& params);
[email protected]d5b528c2012-09-27 16:30:20184 void OnContentScriptsExecuting(
185 const ContentScriptObserver::ExecutingScriptsMap& extension_ids,
186 int32 page_id,
187 const GURL& on_url);
[email protected]36fb2c7c2011-04-04 15:49:08188
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]a6394ae2012-07-16 20:58:43193 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08194
[email protected]a6394ae2012-07-16 20:58:43195 const Extension* GetExtension(
[email protected]1c321ee52012-05-21 03:02:34196 const std::string& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51197
[email protected]36fb2c7c2011-04-04 15:49:08198 // ImageLoadingTracker::Observer.
[email protected]bdd6eec2012-03-03 19:58:06199 virtual void OnImageLoaded(const gfx::Image& image,
200 const std::string& extension_id,
[email protected]8915f342011-08-29 22:14:37201 int index) OVERRIDE;
202
[email protected]734bcec2012-10-08 20:29:05203 // WebstoreStandaloneInstaller::Callback.
[email protected]d2a639e2012-09-17 07:41:21204 virtual void OnInlineInstallComplete(int install_id,
205 int return_route_id,
206 bool success,
207 const std::string& error);
[email protected]36fb2c7c2011-04-04 15:49:08208
[email protected]82a43732011-10-07 16:09:11209 // AppNotifyChannelSetup::Delegate.
[email protected]b2689a902011-12-01 00:41:09210 virtual void AppNotifyChannelSetupComplete(
211 const std::string& channel_id,
212 const std::string& error,
213 const AppNotifyChannelSetup* setup) OVERRIDE;
[email protected]82a43732011-10-07 16:09:11214
[email protected]619f86182012-07-03 21:30:18215 // 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]36fb2c7c2011-04-04 15:49:08225 // Data for app extensions ---------------------------------------------------
226
[email protected]d5b528c2012-09-27 16:30:20227 // 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]a83b8402012-05-17 06:56:44230
[email protected]36fb2c7c2011-04-04 15:49:08231 // If non-null this tab is an app tab and this is the extension the tab was
232 // created for.
[email protected]a6394ae2012-07-16 20:58:43233 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08234
[email protected]74e51402011-04-06 14:17:59235 // Icon for extension_app_ (if non-null) or a manually-set icon for
236 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08237 SkBitmap extension_app_icon_;
238
[email protected]c5dbef02011-05-13 05:06:09239 // Process any extension messages coming from the tab.
240 ExtensionFunctionDispatcher extension_function_dispatcher_;
241
[email protected]36fb2c7c2011-04-04 15:49:08242 // Used for loading extension_app_icon_.
243 scoped_ptr<ImageLoadingTracker> extension_app_image_loader_;
244
[email protected]553602e12011-04-05 17:01:18245 // Cached web app info data.
246 WebApplicationInfo web_app_info_;
247
[email protected]619f86182012-07-03 21:30:18248 // 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]6c4f0a992012-07-18 07:41:06254 ScriptExecutor script_executor_;
[email protected]af78a802012-07-10 23:47:02255
[email protected]a6394ae2012-07-16 20:58:43256 scoped_ptr<LocationBarController> location_bar_controller_;
[email protected]a83b8402012-05-17 06:56:44257
[email protected]78ce3022012-09-24 01:48:48258 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
[email protected]fc5e65d6b2012-06-13 00:22:57259
[email protected]ee837d32012-10-02 22:25:49260 scoped_ptr<ScriptBubbleController> script_bubble_controller_;
261
[email protected]a6394ae2012-07-16 20:58:43262 DISALLOW_COPY_AND_ASSIGN(TabHelper);
[email protected]36fb2c7c2011-04-04 15:49:08263};
264
[email protected]a6394ae2012-07-16 20:58:43265} // namespace extensions
266
267#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_