blob: a984770783c91b98c33423f115c13a4eda92942b [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]4b64d712013-01-17 17:53:178#include <map>
[email protected]a340bb42014-01-09 07:33:209#include <set>
[email protected]4b64d712013-01-17 17:53:1710#include <string>
[email protected]a714e462013-01-26 06:33:1011#include <vector>
12
[email protected]28a69d32012-05-30 07:58:1813#include "base/memory/ref_counted.h"
[email protected]3d6d676522013-10-14 20:44:5514#include "base/memory/scoped_ptr.h"
[email protected]82a43732011-10-07 16:09:1115#include "base/memory/weak_ptr.h"
[email protected]a83b8402012-05-17 06:56:4416#include "base/observer_list.h"
[email protected]78ce3022012-09-24 01:48:4817#include "chrome/browser/extensions/active_tab_permission_granter.h"
[email protected]93f50462013-05-10 04:40:4018#include "chrome/common/web_application_info.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]46b3c982012-10-09 18:38:3022#include "content/public/browser/web_contents_user_data.h"
[email protected]0b9de032014-03-15 05:47:0123#include "extensions/browser/extension_function_dispatcher.h"
[email protected]88b50b62013-09-01 23:05:0624#include "extensions/common/stack_frame.h"
[email protected]36fb2c7c2011-04-04 15:49:0825#include "third_party/skia/include/core/SkBitmap.h"
26
[email protected]898697902013-12-18 03:25:4627class FaviconDownloader;
28
[email protected]8286f51a2011-05-31 17:39:1329namespace content {
30struct LoadCommittedDetails;
31}
32
[email protected]75b91fd2013-02-12 22:14:2533namespace gfx {
34class Image;
35}
36
[email protected]a83b8402012-05-17 06:56:4437namespace extensions {
[email protected]33c87892014-03-25 06:28:1538class BookmarkAppHelper;
[email protected]1c321ee52012-05-21 03:02:3439class Extension;
[email protected]4a988162012-05-27 05:30:0140class LocationBarController;
[email protected]09ae70d42012-11-07 00:42:0941class ScriptExecutor;
[email protected]3d6d676522013-10-14 20:44:5542class WebstoreInlineInstallerFactory;
[email protected]a83b8402012-05-17 06:56:4443
[email protected]74e51402011-04-06 14:17:5944// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4345class TabHelper : public content::WebContentsObserver,
[email protected]1a0436892014-04-01 00:38:2546 public extensions::ExtensionFunctionDispatcher::Delegate,
[email protected]a6394ae2012-07-16 20:58:4347 public base::SupportsWeakPtr<TabHelper>,
[email protected]7381d9f2012-09-12 20:26:2248 public content::NotificationObserver,
[email protected]46b3c982012-10-09 18:38:3049 public content::WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0850 public:
[email protected]619f86182012-07-03 21:30:1851 // Different types of action when web app info is available.
52 // OnDidGetApplicationInfo uses this to dispatch calls.
53 enum WebAppAction {
[email protected]488e3952013-11-18 05:29:1454 NONE, // No action at all.
55 CREATE_SHORTCUT, // Bring up create application shortcut dialog.
56 CREATE_HOSTED_APP, // Create and install a hosted app.
57 UPDATE_SHORTCUT // Update icon for app shortcut.
[email protected]619f86182012-07-03 21:30:1858 };
59
[email protected]09ae70d42012-11-07 00:42:0960 // Observer base class for classes that need to be notified when content
61 // scripts and/or tabs.executeScript calls run on a page.
62 class ScriptExecutionObserver {
[email protected]d5b528c2012-09-27 16:30:2063 public:
64 // Map of extensions IDs to the executing script paths.
65 typedef std::map<std::string, std::set<std::string> > ExecutingScriptsMap;
66
67 // Automatically observes and unobserves |tab_helper| on construction
68 // and destruction. |tab_helper| must outlive |this|.
[email protected]09ae70d42012-11-07 00:42:0969 explicit ScriptExecutionObserver(TabHelper* tab_helper);
70 ScriptExecutionObserver();
[email protected]d5b528c2012-09-27 16:30:2071
[email protected]09ae70d42012-11-07 00:42:0972 // Called when script(s) have executed on a page.
73 //
74 // |executing_scripts_map| contains all extensions that are executing
75 // scripts, mapped to the paths for those scripts. This may be an empty set
76 // if the script has no path associated with it (e.g. in the case of
77 // tabs.executeScript).
78 virtual void OnScriptsExecuted(
[email protected]d5b528c2012-09-27 16:30:2079 const content::WebContents* web_contents,
80 const ExecutingScriptsMap& executing_scripts_map,
81 int32 on_page_id,
82 const GURL& on_url) = 0;
83
84 protected:
[email protected]09ae70d42012-11-07 00:42:0985 virtual ~ScriptExecutionObserver();
[email protected]d5b528c2012-09-27 16:30:2086
[email protected]d5b528c2012-09-27 16:30:2087 TabHelper* tab_helper_;
88 };
89
[email protected]a6394ae2012-07-16 20:58:4390 virtual ~TabHelper();
[email protected]36fb2c7c2011-04-04 15:49:0891
[email protected]09ae70d42012-11-07 00:42:0992 void AddScriptExecutionObserver(ScriptExecutionObserver* observer) {
93 script_execution_observers_.AddObserver(observer);
[email protected]d5b528c2012-09-27 16:30:2094 }
95
[email protected]09ae70d42012-11-07 00:42:0996 void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer) {
97 script_execution_observers_.RemoveObserver(observer);
[email protected]d5b528c2012-09-27 16:30:2098 }
99
[email protected]619f86182012-07-03 21:30:18100 void CreateApplicationShortcuts();
[email protected]488e3952013-11-18 05:29:14101 void CreateHostedAppFromWebContents();
[email protected]619f86182012-07-03 21:30:18102 bool CanCreateApplicationShortcuts() const;
[email protected]1739e57d2011-11-30 21:18:25103
[email protected]619f86182012-07-03 21:30:18104 void set_pending_web_app_action(WebAppAction action) {
105 pending_web_app_action_ = action;
106 }
[email protected]553602e12011-04-05 17:01:18107
[email protected]36fb2c7c2011-04-04 15:49:08108 // App extensions ------------------------------------------------------------
109
110 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:10111 // tab becomes an app-tab. WebContents does not listen for unload events for
112 // the extension. It's up to consumers of WebContents to do that.
[email protected]36fb2c7c2011-04-04 15:49:08113 //
114 // NOTE: this should only be manipulated before the tab is added to a browser.
115 // TODO(sky): resolve if this is the right way to identify an app tab. If it
116 // is, than this should be passed in the constructor.
[email protected]a6394ae2012-07-16 20:58:43117 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08118
119 // Convenience for setting the app extension by id. This does nothing if
120 // |extension_app_id| is empty, or an extension can't be found given the
121 // specified id.
122 void SetExtensionAppById(const std::string& extension_app_id);
123
[email protected]dd290d32012-03-06 02:47:51124 // Set just the app icon, used by panels created by an extension.
125 void SetExtensionAppIconById(const std::string& extension_app_id);
126
[email protected]a6394ae2012-07-16 20:58:43127 const Extension* extension_app() const { return extension_app_; }
[email protected]36fb2c7c2011-04-04 15:49:08128 bool is_app() const { return extension_app_ != NULL; }
[email protected]553602e12011-04-05 17:01:18129 const WebApplicationInfo& web_app_info() const {
130 return web_app_info_;
131 }
[email protected]36fb2c7c2011-04-04 15:49:08132
[email protected]0932b30c2012-04-17 13:25:10133 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:08134 // is returned.
135 //
136 // NOTE: the returned icon is larger than 16x16 (its size is
[email protected]1d5e58b2013-01-31 08:41:40137 // extension_misc::EXTENSION_ICON_SMALLISH).
[email protected]36fb2c7c2011-04-04 15:49:08138 SkBitmap* GetExtensionAppIcon();
139
[email protected]ea049a02011-12-25 21:37:09140 content::WebContents* web_contents() const {
141 return content::WebContentsObserver::web_contents();
[email protected]36fb2c7c2011-04-04 15:49:08142 }
143
[email protected]a6394ae2012-07-16 20:58:43144 ScriptExecutor* script_executor() {
[email protected]09ae70d42012-11-07 00:42:09145 return script_executor_.get();
[email protected]af78a802012-07-10 23:47:02146 }
[email protected]a83b8402012-05-17 06:56:44147
[email protected]a6394ae2012-07-16 20:58:43148 LocationBarController* location_bar_controller() {
[email protected]af78a802012-07-10 23:47:02149 return location_bar_controller_.get();
150 }
[email protected]a83b8402012-05-17 06:56:44151
[email protected]78ce3022012-09-24 01:48:48152 ActiveTabPermissionGranter* active_tab_permission_granter() {
153 return active_tab_permission_granter_.get();
[email protected]fc5e65d6b2012-06-13 00:22:57154 }
155
[email protected]0932b30c2012-04-17 13:25:10156 // Sets a non-extension app icon associated with WebContents and fires an
[email protected]d9083482012-01-06 00:38:46157 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
[email protected]74e51402011-04-06 14:17:59158 void SetAppIcon(const SkBitmap& app_icon);
159
[email protected]3d6d676522013-10-14 20:44:55160 // Sets the factory used to create inline webstore item installers.
161 // Used for testing. Takes ownership of the factory instance.
162 void SetWebstoreInlineInstallerFactoryForTests(
163 WebstoreInlineInstallerFactory* factory);
164
[email protected]36fb2c7c2011-04-04 15:49:08165 private:
[email protected]a78f03c2012-09-15 05:08:19166 explicit TabHelper(content::WebContents* web_contents);
[email protected]46b3c982012-10-09 18:38:30167 friend class content::WebContentsUserData<TabHelper>;
[email protected]a78f03c2012-09-15 05:08:19168
[email protected]33c87892014-03-25 06:28:15169 // Displays UI for completion of creating a bookmark hosted app.
170 void FinishCreateBookmarkApp(const extensions::Extension* extension,
171 const WebApplicationInfo& web_app_info);
[email protected]898697902013-12-18 03:25:46172
[email protected]d8c660432011-12-22 20:51:25173 // content::WebContentsObserver overrides.
[email protected]fc5e65d6b2012-06-13 00:22:57174 virtual void RenderViewCreated(
175 content::RenderViewHost* render_view_host) OVERRIDE;
[email protected]a6e16aec2011-11-11 18:53:04176 virtual void DidNavigateMainFrame(
[email protected]8286f51a2011-05-31 17:39:13177 const content::LoadCommittedDetails& details,
[email protected]6766b172011-11-21 18:29:36178 const content::FrameNavigateParams& params) OVERRIDE;
[email protected]8915f342011-08-29 22:14:37179 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
[email protected]7381d9f2012-09-12 20:26:22180 virtual void DidCloneToNewWebContents(
181 content::WebContents* old_web_contents,
182 content::WebContents* new_web_contents) OVERRIDE;
[email protected]553602e12011-04-05 17:01:18183
[email protected]1a0436892014-04-01 00:38:25184 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
[email protected]44f4b132012-07-17 20:36:57185 virtual extensions::WindowController* GetExtensionWindowController()
[email protected]b51f35622012-05-05 22:01:43186 const OVERRIDE;
[email protected]ea049a02011-12-25 21:37:09187 virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
[email protected]c5dbef02011-05-13 05:06:09188
[email protected]553602e12011-04-05 17:01:18189 // Message handlers.
190 void OnDidGetApplicationInfo(int32 page_id, const WebApplicationInfo& info);
[email protected]a221ef092011-09-07 01:34:10191 void OnInlineWebstoreInstall(int install_id,
[email protected]7b921042012-02-11 01:41:27192 int return_route_id,
[email protected]a221ef092011-09-07 01:34:10193 const std::string& webstore_item_id,
[email protected]c80fe5f2014-03-26 04:36:30194 const GURL& requestor_url,
195 int listeners_mask);
[email protected]f2fe87c2012-04-24 17:53:49196 void OnGetAppInstallState(const GURL& requestor_url,
197 int return_route_id,
198 int callback_id);
[email protected]c5dbef02011-05-13 05:06:09199 void OnRequest(const ExtensionHostMsg_Request_Params& params);
[email protected]d5b528c2012-09-27 16:30:20200 void OnContentScriptsExecuting(
[email protected]09ae70d42012-11-07 00:42:09201 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
[email protected]d5b528c2012-09-27 16:30:20202 int32 page_id,
203 const GURL& on_url);
[email protected]a714e462013-01-26 06:33:10204 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
[email protected]88b50b62013-09-01 23:05:06205 void OnDetailedConsoleMessageAdded(const base::string16& message,
206 const base::string16& source,
207 const StackTrace& stack_trace,
208 int32 severity_level);
[email protected]36fb2c7c2011-04-04 15:49:08209
210 // App extensions related methods:
211
[email protected]75b91fd2013-02-12 22:14:25212 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
213 // the extension's image asynchronously.
[email protected]a6394ae2012-07-16 20:58:43214 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08215
[email protected]7069ed82013-01-26 01:39:53216 const Extension* GetExtension(const std::string& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51217
[email protected]7069ed82013-01-26 01:39:53218 void OnImageLoaded(const gfx::Image& image);
[email protected]8915f342011-08-29 22:14:37219
[email protected]734bcec2012-10-08 20:29:05220 // WebstoreStandaloneInstaller::Callback.
[email protected]d2a639e2012-09-17 07:41:21221 virtual void OnInlineInstallComplete(int install_id,
222 int return_route_id,
223 bool success,
224 const std::string& error);
[email protected]36fb2c7c2011-04-04 15:49:08225
[email protected]619f86182012-07-03 21:30:18226 // content::NotificationObserver.
227 virtual void Observe(int type,
228 const content::NotificationSource& source,
229 const content::NotificationDetails& details) OVERRIDE;
230
231 // Requests application info for the specified page. This is an asynchronous
232 // request. The delegate is notified by way of OnDidGetApplicationInfo when
233 // the data is available.
234 void GetApplicationInfo(int32 page_id);
235
[email protected]2fc1cad12013-05-21 00:41:39236 // Sends our tab ID to |render_view_host|.
237 void SetTabId(content::RenderViewHost* render_view_host);
238
[email protected]36fb2c7c2011-04-04 15:49:08239 // Data for app extensions ---------------------------------------------------
240
[email protected]d5b528c2012-09-27 16:30:20241 // Our content script observers. Declare at top so that it will outlive all
242 // other members, since they might add themselves as observers.
[email protected]09ae70d42012-11-07 00:42:09243 ObserverList<ScriptExecutionObserver> script_execution_observers_;
[email protected]a83b8402012-05-17 06:56:44244
[email protected]36fb2c7c2011-04-04 15:49:08245 // If non-null this tab is an app tab and this is the extension the tab was
246 // created for.
[email protected]a6394ae2012-07-16 20:58:43247 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08248
[email protected]74e51402011-04-06 14:17:59249 // Icon for extension_app_ (if non-null) or a manually-set icon for
250 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08251 SkBitmap extension_app_icon_;
252
[email protected]c5dbef02011-05-13 05:06:09253 // Process any extension messages coming from the tab.
[email protected]1a0436892014-04-01 00:38:25254 extensions::ExtensionFunctionDispatcher extension_function_dispatcher_;
[email protected]c5dbef02011-05-13 05:06:09255
[email protected]553602e12011-04-05 17:01:18256 // Cached web app info data.
257 WebApplicationInfo web_app_info_;
258
[email protected]619f86182012-07-03 21:30:18259 // Which deferred action to perform when OnDidGetApplicationInfo is notified
260 // from a WebContents.
261 WebAppAction pending_web_app_action_;
262
263 content::NotificationRegistrar registrar_;
264
[email protected]09ae70d42012-11-07 00:42:09265 scoped_ptr<ScriptExecutor> script_executor_;
[email protected]af78a802012-07-10 23:47:02266
[email protected]a6394ae2012-07-16 20:58:43267 scoped_ptr<LocationBarController> location_bar_controller_;
[email protected]a83b8402012-05-17 06:56:44268
[email protected]78ce3022012-09-24 01:48:48269 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
[email protected]fc5e65d6b2012-06-13 00:22:57270
[email protected]33c87892014-03-25 06:28:15271 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_;
[email protected]898697902013-12-18 03:25:46272
[email protected]4b64d712013-01-17 17:53:17273 Profile* profile_;
274
[email protected]7069ed82013-01-26 01:39:53275 // Vend weak pointers that can be invalidated to stop in-progress loads.
276 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
277
[email protected]3d6d676522013-10-14 20:44:55278 // Creates WebstoreInlineInstaller instances for inline install triggers.
279 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
280
[email protected]a6394ae2012-07-16 20:58:43281 DISALLOW_COPY_AND_ASSIGN(TabHelper);
[email protected]36fb2c7c2011-04-04 15:49:08282};
283
[email protected]a6394ae2012-07-16 20:58:43284} // namespace extensions
285
286#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_