blob: 5564f914d169e2f7275f589f45e7666aa1cf8269 [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]a340bb42014-01-09 07:33:208#include <set>
[email protected]4b64d712013-01-17 17:53:179#include <string>
[email protected]a714e462013-01-26 06:33:1010#include <vector>
11
[email protected]28a69d32012-05-30 07:58:1812#include "base/memory/ref_counted.h"
[email protected]3d6d676522013-10-14 20:44:5513#include "base/memory/scoped_ptr.h"
[email protected]82a43732011-10-07 16:09:1114#include "base/memory/weak_ptr.h"
[email protected]a83b8402012-05-17 06:56:4415#include "base/observer_list.h"
[email protected]78ce3022012-09-24 01:48:4816#include "chrome/browser/extensions/active_tab_permission_granter.h"
rdevlin.cronin092c6772014-11-20 23:52:4017#include "chrome/browser/extensions/extension_reenabler.h"
[email protected]ced522c2014-07-23 20:23:5918#include "chrome/common/extensions/webstore_install_result.h"
[email protected]93f50462013-05-10 04:40:4019#include "chrome/common/web_application_info.h"
[email protected]619f86182012-07-03 21:30:1820#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2522#include "content/public/browser/web_contents_observer.h"
[email protected]46b3c982012-10-09 18:38:3023#include "content/public/browser/web_contents_user_data.h"
[email protected]0b9de032014-03-15 05:47:0124#include "extensions/browser/extension_function_dispatcher.h"
[email protected]16a4206f2014-08-15 09:44:4325#include "extensions/browser/script_execution_observer.h"
26#include "extensions/browser/script_executor.h"
[email protected]88b50b62013-09-01 23:05:0627#include "extensions/common/stack_frame.h"
[email protected]36fb2c7c2011-04-04 15:49:0828#include "third_party/skia/include/core/SkBitmap.h"
29
[email protected]898697902013-12-18 03:25:4630class FaviconDownloader;
31
[email protected]8286f51a2011-05-31 17:39:1332namespace content {
33struct LoadCommittedDetails;
[email protected]6f94fac2014-06-02 12:47:0434class RenderFrameHost;
[email protected]8286f51a2011-05-31 17:39:1335}
36
[email protected]75b91fd2013-02-12 22:14:2537namespace gfx {
38class Image;
39}
40
[email protected]a83b8402012-05-17 06:56:4441namespace extensions {
rdevlin.cronin91f162a12014-09-03 16:48:4042class ActiveScriptController;
[email protected]33c87892014-03-25 06:28:1543class BookmarkAppHelper;
[email protected]1c321ee52012-05-21 03:02:3444class Extension;
[email protected]4a988162012-05-27 05:30:0145class LocationBarController;
[email protected]3d6d676522013-10-14 20:44:5546class WebstoreInlineInstallerFactory;
[email protected]a83b8402012-05-17 06:56:4447
[email protected]74e51402011-04-06 14:17:5948// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4349class TabHelper : public content::WebContentsObserver,
[email protected]1a0436892014-04-01 00:38:2550 public extensions::ExtensionFunctionDispatcher::Delegate,
[email protected]a6394ae2012-07-16 20:58:4351 public base::SupportsWeakPtr<TabHelper>,
[email protected]7381d9f2012-09-12 20:26:2252 public content::NotificationObserver,
[email protected]46b3c982012-10-09 18:38:3053 public content::WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0854 public:
dchengae36a4a2014-10-21 12:36:3655 ~TabHelper() override;
[email protected]36fb2c7c2011-04-04 15:49:0856
[email protected]619f86182012-07-03 21:30:1857 void CreateApplicationShortcuts();
[email protected]488e3952013-11-18 05:29:1458 void CreateHostedAppFromWebContents();
[email protected]619f86182012-07-03 21:30:1859 bool CanCreateApplicationShortcuts() const;
[email protected]92086542014-04-08 08:45:2960 bool CanCreateBookmarkApp() const;
[email protected]1739e57d2011-11-30 21:18:2561
[email protected]fd14a442014-07-02 20:40:0562 void UpdateShortcutOnLoadComplete() {
63 update_shortcut_on_load_complete_ = true;
[email protected]619f86182012-07-03 21:30:1864 }
[email protected]553602e12011-04-05 17:01:1865
[email protected]16a4206f2014-08-15 09:44:4366 // ScriptExecutionObserver::Delegate
67 virtual void AddScriptExecutionObserver(ScriptExecutionObserver* observer);
68 virtual void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer);
69
[email protected]36fb2c7c2011-04-04 15:49:0870 // App extensions ------------------------------------------------------------
71
72 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:1073 // tab becomes an app-tab. WebContents does not listen for unload events for
74 // the extension. It's up to consumers of WebContents to do that.
[email protected]36fb2c7c2011-04-04 15:49:0875 //
76 // NOTE: this should only be manipulated before the tab is added to a browser.
77 // TODO(sky): resolve if this is the right way to identify an app tab. If it
78 // is, than this should be passed in the constructor.
[email protected]a6394ae2012-07-16 20:58:4379 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:0880
81 // Convenience for setting the app extension by id. This does nothing if
82 // |extension_app_id| is empty, or an extension can't be found given the
83 // specified id.
84 void SetExtensionAppById(const std::string& extension_app_id);
85
[email protected]dd290d32012-03-06 02:47:5186 // Set just the app icon, used by panels created by an extension.
87 void SetExtensionAppIconById(const std::string& extension_app_id);
88
[email protected]a6394ae2012-07-16 20:58:4389 const Extension* extension_app() const { return extension_app_; }
[email protected]36fb2c7c2011-04-04 15:49:0890 bool is_app() const { return extension_app_ != NULL; }
[email protected]553602e12011-04-05 17:01:1891 const WebApplicationInfo& web_app_info() const {
92 return web_app_info_;
93 }
[email protected]36fb2c7c2011-04-04 15:49:0894
[email protected]0932b30c2012-04-17 13:25:1095 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:0896 // is returned.
97 //
98 // NOTE: the returned icon is larger than 16x16 (its size is
[email protected]1d5e58b2013-01-31 08:41:4099 // extension_misc::EXTENSION_ICON_SMALLISH).
[email protected]36fb2c7c2011-04-04 15:49:08100 SkBitmap* GetExtensionAppIcon();
101
[email protected]a6394ae2012-07-16 20:58:43102 ScriptExecutor* script_executor() {
[email protected]09ae70d42012-11-07 00:42:09103 return script_executor_.get();
[email protected]af78a802012-07-10 23:47:02104 }
[email protected]a83b8402012-05-17 06:56:44105
[email protected]a6394ae2012-07-16 20:58:43106 LocationBarController* location_bar_controller() {
[email protected]af78a802012-07-10 23:47:02107 return location_bar_controller_.get();
108 }
[email protected]a83b8402012-05-17 06:56:44109
rdevlin.cronin91f162a12014-09-03 16:48:40110 ActiveScriptController* active_script_controller() {
111 return active_script_controller_.get();
112 }
113
[email protected]78ce3022012-09-24 01:48:48114 ActiveTabPermissionGranter* active_tab_permission_granter() {
115 return active_tab_permission_granter_.get();
[email protected]fc5e65d6b2012-06-13 00:22:57116 }
117
[email protected]0932b30c2012-04-17 13:25:10118 // Sets a non-extension app icon associated with WebContents and fires an
[email protected]d9083482012-01-06 00:38:46119 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
[email protected]74e51402011-04-06 14:17:59120 void SetAppIcon(const SkBitmap& app_icon);
121
[email protected]3d6d676522013-10-14 20:44:55122 // Sets the factory used to create inline webstore item installers.
123 // Used for testing. Takes ownership of the factory instance.
124 void SetWebstoreInlineInstallerFactoryForTests(
125 WebstoreInlineInstallerFactory* factory);
126
[email protected]36fb2c7c2011-04-04 15:49:08127 private:
[email protected]fd14a442014-07-02 20:40:05128 // Different types of action when web app info is available.
129 // OnDidGetApplicationInfo uses this to dispatch calls.
130 enum WebAppAction {
131 NONE, // No action at all.
132 CREATE_SHORTCUT, // Bring up create application shortcut dialog.
133 CREATE_HOSTED_APP, // Create and install a hosted app.
134 UPDATE_SHORTCUT // Update icon for app shortcut.
135 };
136
[email protected]a78f03c2012-09-15 05:08:19137 explicit TabHelper(content::WebContents* web_contents);
[email protected]46b3c982012-10-09 18:38:30138 friend class content::WebContentsUserData<TabHelper>;
[email protected]a78f03c2012-09-15 05:08:19139
[email protected]33c87892014-03-25 06:28:15140 // Displays UI for completion of creating a bookmark hosted app.
141 void FinishCreateBookmarkApp(const extensions::Extension* extension,
142 const WebApplicationInfo& web_app_info);
[email protected]898697902013-12-18 03:25:46143
[email protected]d8c660432011-12-22 20:51:25144 // content::WebContentsObserver overrides.
rdevlin.croninb67a34772015-06-04 16:32:38145 void RenderFrameCreated(content::RenderFrameHost* host) override;
dchengae36a4a2014-10-21 12:36:36146 void DidNavigateMainFrame(
[email protected]8286f51a2011-05-31 17:39:13147 const content::LoadCommittedDetails& details,
mostynba15bee12014-10-04 00:40:32148 const content::FrameNavigateParams& params) override;
dchengae36a4a2014-10-21 12:36:36149 bool OnMessageReceived(const IPC::Message& message) override;
150 bool OnMessageReceived(const IPC::Message& message,
151 content::RenderFrameHost* render_frame_host) override;
152 void DidCloneToNewWebContents(
[email protected]7381d9f2012-09-12 20:26:22153 content::WebContents* old_web_contents,
mostynba15bee12014-10-04 00:40:32154 content::WebContents* new_web_contents) override;
[email protected]553602e12011-04-05 17:01:18155
[email protected]1a0436892014-04-01 00:38:25156 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
dchengae36a4a2014-10-21 12:36:36157 extensions::WindowController* GetExtensionWindowController() const override;
158 content::WebContents* GetAssociatedWebContents() const override;
[email protected]c5dbef02011-05-13 05:06:09159
[email protected]553602e12011-04-05 17:01:18160 // Message handlers.
mlamouri89ccc632014-09-16 19:29:58161 void OnDidGetWebApplicationInfo(const WebApplicationInfo& info);
[email protected]a221ef092011-09-07 01:34:10162 void OnInlineWebstoreInstall(int install_id,
[email protected]7b921042012-02-11 01:41:27163 int return_route_id,
[email protected]a221ef092011-09-07 01:34:10164 const std::string& webstore_item_id,
[email protected]c80fe5f2014-03-26 04:36:30165 const GURL& requestor_url,
166 int listeners_mask);
[email protected]f2fe87c2012-04-24 17:53:49167 void OnGetAppInstallState(const GURL& requestor_url,
168 int return_route_id,
169 int callback_id);
[email protected]c5dbef02011-05-13 05:06:09170 void OnRequest(const ExtensionHostMsg_Request_Params& params);
[email protected]d5b528c2012-09-27 16:30:20171 void OnContentScriptsExecuting(
[email protected]09ae70d42012-11-07 00:42:09172 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
[email protected]d5b528c2012-09-27 16:30:20173 const GURL& on_url);
[email protected]a714e462013-01-26 06:33:10174 void OnWatchedPageChange(const std::vector<std::string>& css_selectors);
[email protected]88b50b62013-09-01 23:05:06175 void OnDetailedConsoleMessageAdded(const base::string16& message,
176 const base::string16& source,
177 const StackTrace& stack_trace,
178 int32 severity_level);
[email protected]36fb2c7c2011-04-04 15:49:08179
180 // App extensions related methods:
181
[email protected]75b91fd2013-02-12 22:14:25182 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
183 // the extension's image asynchronously.
[email protected]a6394ae2012-07-16 20:58:43184 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08185
[email protected]7069ed82013-01-26 01:39:53186 const Extension* GetExtension(const std::string& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51187
[email protected]7069ed82013-01-26 01:39:53188 void OnImageLoaded(const gfx::Image& image);
[email protected]8915f342011-08-29 22:14:37189
[email protected]734bcec2012-10-08 20:29:05190 // WebstoreStandaloneInstaller::Callback.
rdevlin.cronin092c6772014-11-20 23:52:40191 void OnInlineInstallComplete(int install_id,
192 int return_route_id,
193 bool success,
194 const std::string& error,
195 webstore_install::Result result);
196
197 // ExtensionReenabler::Callback.
198 void OnReenableComplete(int install_id,
199 int return_route_id,
200 ExtensionReenabler::ReenableResult result);
[email protected]36fb2c7c2011-04-04 15:49:08201
[email protected]619f86182012-07-03 21:30:18202 // content::NotificationObserver.
dchengae36a4a2014-10-21 12:36:36203 void Observe(int type,
204 const content::NotificationSource& source,
205 const content::NotificationDetails& details) override;
[email protected]619f86182012-07-03 21:30:18206
207 // Requests application info for the specified page. This is an asynchronous
208 // request. The delegate is notified by way of OnDidGetApplicationInfo when
209 // the data is available.
[email protected]fd14a442014-07-02 20:40:05210 void GetApplicationInfo(WebAppAction action);
[email protected]619f86182012-07-03 21:30:18211
rdevlin.croninb67a34772015-06-04 16:32:38212 // Sends our tab ID to |render_frame_host|.
213 void SetTabId(content::RenderFrameHost* render_frame_host);
[email protected]2fc1cad12013-05-21 00:41:39214
[email protected]36fb2c7c2011-04-04 15:49:08215 // Data for app extensions ---------------------------------------------------
216
[email protected]d5b528c2012-09-27 16:30:20217 // Our content script observers. Declare at top so that it will outlive all
218 // other members, since they might add themselves as observers.
brettwd195c952015-06-02 17:31:12219 base::ObserverList<ScriptExecutionObserver> script_execution_observers_;
[email protected]a83b8402012-05-17 06:56:44220
[email protected]36fb2c7c2011-04-04 15:49:08221 // If non-null this tab is an app tab and this is the extension the tab was
222 // created for.
[email protected]a6394ae2012-07-16 20:58:43223 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08224
[email protected]74e51402011-04-06 14:17:59225 // Icon for extension_app_ (if non-null) or a manually-set icon for
226 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08227 SkBitmap extension_app_icon_;
228
[email protected]c5dbef02011-05-13 05:06:09229 // Process any extension messages coming from the tab.
[email protected]1a0436892014-04-01 00:38:25230 extensions::ExtensionFunctionDispatcher extension_function_dispatcher_;
[email protected]c5dbef02011-05-13 05:06:09231
[email protected]553602e12011-04-05 17:01:18232 // Cached web app info data.
233 WebApplicationInfo web_app_info_;
234
[email protected]619f86182012-07-03 21:30:18235 // Which deferred action to perform when OnDidGetApplicationInfo is notified
236 // from a WebContents.
237 WebAppAction pending_web_app_action_;
238
avi1a908722015-01-15 16:01:49239 // Which navigation entry was active when the GetApplicationInfo request was
240 // sent, for verification when the reply returns.
241 int last_committed_nav_entry_unique_id_;
[email protected]fd14a442014-07-02 20:40:05242
243 // Whether to trigger an update when the page load completes.
244 bool update_shortcut_on_load_complete_;
245
[email protected]619f86182012-07-03 21:30:18246 content::NotificationRegistrar registrar_;
247
[email protected]09ae70d42012-11-07 00:42:09248 scoped_ptr<ScriptExecutor> script_executor_;
[email protected]af78a802012-07-10 23:47:02249
[email protected]a6394ae2012-07-16 20:58:43250 scoped_ptr<LocationBarController> location_bar_controller_;
[email protected]a83b8402012-05-17 06:56:44251
rdevlin.cronin91f162a12014-09-03 16:48:40252 scoped_ptr<ActiveScriptController> active_script_controller_;
253
[email protected]78ce3022012-09-24 01:48:48254 scoped_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
[email protected]fc5e65d6b2012-06-13 00:22:57255
[email protected]33c87892014-03-25 06:28:15256 scoped_ptr<BookmarkAppHelper> bookmark_app_helper_;
[email protected]898697902013-12-18 03:25:46257
[email protected]4b64d712013-01-17 17:53:17258 Profile* profile_;
259
[email protected]3d6d676522013-10-14 20:44:55260 // Creates WebstoreInlineInstaller instances for inline install triggers.
261 scoped_ptr<WebstoreInlineInstallerFactory> webstore_inline_installer_factory_;
262
rdevlin.cronin092c6772014-11-20 23:52:40263 // The reenable prompt for disabled extensions, if any.
264 scoped_ptr<ExtensionReenabler> extension_reenabler_;
265
mohan.reddy63d63002014-09-16 04:13:56266 // Vend weak pointers that can be invalidated to stop in-progress loads.
267 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
268
rdevlin.cronin092c6772014-11-20 23:52:40269 // Generic weak ptr factory for posting callbacks.
270 base::WeakPtrFactory<TabHelper> weak_ptr_factory_;
271
[email protected]a6394ae2012-07-16 20:58:43272 DISALLOW_COPY_AND_ASSIGN(TabHelper);
[email protected]36fb2c7c2011-04-04 15:49:08273};
274
[email protected]a6394ae2012-07-16 20:58:43275} // namespace extensions
276
277#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_