blob: 09b09f624bd269a2d72796b2f860d94cb4d2ce77 [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
dchengc963c7142016-04-08 03:55:228#include <memory>
[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
avia2f4804a2015-12-24 23:11:1312#include "base/macros.h"
[email protected]28a69d32012-05-30 07:58:1813#include "base/memory/ref_counted.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"
rdevlin.cronin1f047f7b2016-06-06 18:30:1816#include "base/scoped_observer.h"
[email protected]78ce3022012-09-24 01:48:4817#include "chrome/browser/extensions/active_tab_permission_granter.h"
rdevlin.cronin092c6772014-11-20 23:52:4018#include "chrome/browser/extensions/extension_reenabler.h"
[email protected]ced522c2014-07-23 20:23:5919#include "chrome/common/extensions/webstore_install_result.h"
[email protected]93f50462013-05-10 04:40:4020#include "chrome/common/web_application_info.h"
[email protected]619f86182012-07-03 21:30:1821#include "content/public/browser/notification_observer.h"
22#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2523#include "content/public/browser/web_contents_observer.h"
[email protected]46b3c982012-10-09 18:38:3024#include "content/public/browser/web_contents_user_data.h"
[email protected]0b9de032014-03-15 05:47:0125#include "extensions/browser/extension_function_dispatcher.h"
rdevlin.cronin1f047f7b2016-06-06 18:30:1826#include "extensions/browser/extension_registry_observer.h"
[email protected]16a4206f2014-08-15 09:44:4327#include "extensions/browser/script_execution_observer.h"
28#include "extensions/browser/script_executor.h"
rdevlin.cronind8076cd62016-07-08 23:05:5829#include "extensions/common/extension_id.h"
[email protected]88b50b62013-09-01 23:05:0630#include "extensions/common/stack_frame.h"
[email protected]36fb2c7c2011-04-04 15:49:0831#include "third_party/skia/include/core/SkBitmap.h"
32
[email protected]75b91fd2013-02-12 22:14:2533namespace gfx {
34class Image;
35}
36
[email protected]a83b8402012-05-17 06:56:4437namespace extensions {
rdevlin.cronin8408b4f92016-03-15 19:14:1438class ExtensionActionRunner;
[email protected]33c87892014-03-25 06:28:1539class BookmarkAppHelper;
[email protected]1c321ee52012-05-21 03:02:3440class Extension;
[email protected]3d6d676522013-10-14 20:44:5541class WebstoreInlineInstallerFactory;
[email protected]a83b8402012-05-17 06:56:4442
[email protected]74e51402011-04-06 14:17:5943// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4344class TabHelper : public content::WebContentsObserver,
rdevlin.cronin1f047f7b2016-06-06 18:30:1845 public ExtensionFunctionDispatcher::Delegate,
46 public ExtensionRegistryObserver,
[email protected]7381d9f2012-09-12 20:26:2247 public content::NotificationObserver,
[email protected]46b3c982012-10-09 18:38:3048 public content::WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0849 public:
dchengae36a4a2014-10-21 12:36:3650 ~TabHelper() override;
[email protected]36fb2c7c2011-04-04 15:49:0851
[email protected]488e3952013-11-18 05:29:1452 void CreateHostedAppFromWebContents();
[email protected]92086542014-04-08 08:45:2953 bool CanCreateBookmarkApp() const;
[email protected]1739e57d2011-11-30 21:18:2554
[email protected]fd14a442014-07-02 20:40:0555 void UpdateShortcutOnLoadComplete() {
56 update_shortcut_on_load_complete_ = true;
[email protected]619f86182012-07-03 21:30:1857 }
[email protected]553602e12011-04-05 17:01:1858
[email protected]16a4206f2014-08-15 09:44:4359 // ScriptExecutionObserver::Delegate
60 virtual void AddScriptExecutionObserver(ScriptExecutionObserver* observer);
61 virtual void RemoveScriptExecutionObserver(ScriptExecutionObserver* observer);
62
[email protected]36fb2c7c2011-04-04 15:49:0863 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:1064 // tab becomes an app-tab. WebContents does not listen for unload events for
65 // the extension. It's up to consumers of WebContents to do that.
[email protected]36fb2c7c2011-04-04 15:49:0866 //
67 // NOTE: this should only be manipulated before the tab is added to a browser.
68 // TODO(sky): resolve if this is the right way to identify an app tab. If it
69 // is, than this should be passed in the constructor.
[email protected]a6394ae2012-07-16 20:58:4370 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:0871
72 // Convenience for setting the app extension by id. This does nothing if
73 // |extension_app_id| is empty, or an extension can't be found given the
74 // specified id.
rdevlin.cronind8076cd62016-07-08 23:05:5875 void SetExtensionAppById(const ExtensionId& extension_app_id);
[email protected]36fb2c7c2011-04-04 15:49:0876
[email protected]dd290d32012-03-06 02:47:5177 // Set just the app icon, used by panels created by an extension.
rdevlin.cronind8076cd62016-07-08 23:05:5878 void SetExtensionAppIconById(const ExtensionId& extension_app_id);
[email protected]dd290d32012-03-06 02:47:5179
[email protected]a6394ae2012-07-16 20:58:4380 const Extension* extension_app() const { return extension_app_; }
[email protected]36fb2c7c2011-04-04 15:49:0881 bool is_app() const { return extension_app_ != NULL; }
[email protected]553602e12011-04-05 17:01:1882 const WebApplicationInfo& web_app_info() const {
83 return web_app_info_;
84 }
[email protected]36fb2c7c2011-04-04 15:49:0885
[email protected]0932b30c2012-04-17 13:25:1086 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:0887 // is returned.
88 //
89 // NOTE: the returned icon is larger than 16x16 (its size is
[email protected]1d5e58b2013-01-31 08:41:4090 // extension_misc::EXTENSION_ICON_SMALLISH).
[email protected]36fb2c7c2011-04-04 15:49:0891 SkBitmap* GetExtensionAppIcon();
92
[email protected]a6394ae2012-07-16 20:58:4393 ScriptExecutor* script_executor() {
[email protected]09ae70d42012-11-07 00:42:0994 return script_executor_.get();
[email protected]af78a802012-07-10 23:47:0295 }
[email protected]a83b8402012-05-17 06:56:4496
rdevlin.cronin8408b4f92016-03-15 19:14:1497 ExtensionActionRunner* extension_action_runner() {
98 return extension_action_runner_.get();
rdevlin.cronin91f162a12014-09-03 16:48:4099 }
100
[email protected]78ce3022012-09-24 01:48:48101 ActiveTabPermissionGranter* active_tab_permission_granter() {
102 return active_tab_permission_granter_.get();
[email protected]fc5e65d6b2012-06-13 00:22:57103 }
104
[email protected]0932b30c2012-04-17 13:25:10105 // Sets a non-extension app icon associated with WebContents and fires an
[email protected]d9083482012-01-06 00:38:46106 // INVALIDATE_TYPE_TITLE navigation state change to trigger repaint of title.
[email protected]74e51402011-04-06 14:17:59107 void SetAppIcon(const SkBitmap& app_icon);
108
[email protected]3d6d676522013-10-14 20:44:55109 // Sets the factory used to create inline webstore item installers.
110 // Used for testing. Takes ownership of the factory instance.
111 void SetWebstoreInlineInstallerFactoryForTests(
112 WebstoreInlineInstallerFactory* factory);
113
[email protected]36fb2c7c2011-04-04 15:49:08114 private:
rdevlin.cronind30a8bd2016-06-30 16:02:29115 class InlineInstallObserver;
116
wittman2b0e40652015-06-10 00:53:03117 // Utility function to invoke member functions on all relevant
118 // ContentRulesRegistries.
119 template <class Func>
120 void InvokeForContentRulesRegistries(const Func& func);
121
[email protected]fd14a442014-07-02 20:40:05122 // Different types of action when web app info is available.
123 // OnDidGetApplicationInfo uses this to dispatch calls.
124 enum WebAppAction {
125 NONE, // No action at all.
[email protected]fd14a442014-07-02 20:40:05126 CREATE_HOSTED_APP, // Create and install a hosted app.
127 UPDATE_SHORTCUT // Update icon for app shortcut.
128 };
129
[email protected]a78f03c2012-09-15 05:08:19130 explicit TabHelper(content::WebContents* web_contents);
[email protected]46b3c982012-10-09 18:38:30131 friend class content::WebContentsUserData<TabHelper>;
[email protected]a78f03c2012-09-15 05:08:19132
[email protected]33c87892014-03-25 06:28:15133 // Displays UI for completion of creating a bookmark hosted app.
134 void FinishCreateBookmarkApp(const extensions::Extension* extension,
135 const WebApplicationInfo& web_app_info);
[email protected]898697902013-12-18 03:25:46136
[email protected]d8c660432011-12-22 20:51:25137 // content::WebContentsObserver overrides.
rdevlin.croninb67a34772015-06-04 16:32:38138 void RenderFrameCreated(content::RenderFrameHost* host) override;
jamf1c7f0c2017-02-02 01:04:37139 void DidFinishNavigation(
140 content::NavigationHandle* navigation_handle) override;
dchengae36a4a2014-10-21 12:36:36141 bool OnMessageReceived(const IPC::Message& message) override;
142 bool OnMessageReceived(const IPC::Message& message,
143 content::RenderFrameHost* render_frame_host) override;
144 void DidCloneToNewWebContents(
[email protected]7381d9f2012-09-12 20:26:22145 content::WebContents* old_web_contents,
mostynba15bee12014-10-04 00:40:32146 content::WebContents* new_web_contents) override;
[email protected]553602e12011-04-05 17:01:18147
[email protected]1a0436892014-04-01 00:38:25148 // extensions::ExtensionFunctionDispatcher::Delegate overrides.
dchengae36a4a2014-10-21 12:36:36149 extensions::WindowController* GetExtensionWindowController() const override;
150 content::WebContents* GetAssociatedWebContents() const override;
[email protected]c5dbef02011-05-13 05:06:09151
rdevlin.cronin1f047f7b2016-06-06 18:30:18152 // ExtensionRegistryObserver:
153 void OnExtensionUnloaded(content::BrowserContext* browser_context,
154 const Extension* extension,
limasdf0deef2042017-05-03 19:17:17155 UnloadedExtensionReason reason) override;
rdevlin.cronin1f047f7b2016-06-06 18:30:18156
[email protected]553602e12011-04-05 17:01:18157 // Message handlers.
mlamouri89ccc632014-09-16 19:29:58158 void OnDidGetWebApplicationInfo(const WebApplicationInfo& info);
rdevlin.cronin7df58052015-07-10 20:30:20159 void OnInlineWebstoreInstall(content::RenderFrameHost* host,
160 int install_id,
[email protected]7b921042012-02-11 01:41:27161 int return_route_id,
[email protected]a221ef092011-09-07 01:34:10162 const std::string& webstore_item_id,
[email protected]c80fe5f2014-03-26 04:36:30163 int listeners_mask);
rdevlin.cronin7df58052015-07-10 20:30:20164 void OnGetAppInstallState(content::RenderFrameHost* host,
165 const GURL& requestor_url,
[email protected]f2fe87c2012-04-24 17:53:49166 int return_route_id,
167 int callback_id);
[email protected]d5b528c2012-09-27 16:30:20168 void OnContentScriptsExecuting(
rdevlin.cronin7df58052015-07-10 20:30:20169 content::RenderFrameHost* host,
[email protected]09ae70d42012-11-07 00:42:09170 const ScriptExecutionObserver::ExecutingScriptsMap& extension_ids,
[email protected]d5b528c2012-09-27 16:30:20171 const GURL& on_url);
[email protected]36fb2c7c2011-04-04 15:49:08172
173 // App extensions related methods:
174
[email protected]75b91fd2013-02-12 22:14:25175 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
176 // the extension's image asynchronously.
[email protected]a6394ae2012-07-16 20:58:43177 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08178
rdevlin.cronind8076cd62016-07-08 23:05:58179 const Extension* GetExtension(const ExtensionId& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51180
[email protected]7069ed82013-01-26 01:39:53181 void OnImageLoaded(const gfx::Image& image);
[email protected]8915f342011-08-29 22:14:37182
[email protected]734bcec2012-10-08 20:29:05183 // WebstoreStandaloneInstaller::Callback.
rdevlin.cronin092c6772014-11-20 23:52:40184 void OnInlineInstallComplete(int install_id,
185 int return_route_id,
rdevlin.cronind8076cd62016-07-08 23:05:58186 const ExtensionId& extension_id,
rdevlin.cronin092c6772014-11-20 23:52:40187 bool success,
188 const std::string& error,
189 webstore_install::Result result);
190
191 // ExtensionReenabler::Callback.
192 void OnReenableComplete(int install_id,
193 int return_route_id,
rdevlin.cronind8076cd62016-07-08 23:05:58194 const ExtensionId& extension_id,
rdevlin.cronin092c6772014-11-20 23:52:40195 ExtensionReenabler::ReenableResult result);
[email protected]36fb2c7c2011-04-04 15:49:08196
[email protected]619f86182012-07-03 21:30:18197 // content::NotificationObserver.
dchengae36a4a2014-10-21 12:36:36198 void Observe(int type,
199 const content::NotificationSource& source,
200 const content::NotificationDetails& details) override;
[email protected]619f86182012-07-03 21:30:18201
202 // Requests application info for the specified page. This is an asynchronous
203 // request. The delegate is notified by way of OnDidGetApplicationInfo when
204 // the data is available.
[email protected]fd14a442014-07-02 20:40:05205 void GetApplicationInfo(WebAppAction action);
[email protected]619f86182012-07-03 21:30:18206
rdevlin.croninb67a34772015-06-04 16:32:38207 // Sends our tab ID to |render_frame_host|.
208 void SetTabId(content::RenderFrameHost* render_frame_host);
[email protected]2fc1cad12013-05-21 00:41:39209
wittman2b0e40652015-06-10 00:53:03210 Profile* profile_;
[email protected]36fb2c7c2011-04-04 15:49:08211
[email protected]d5b528c2012-09-27 16:30:20212 // Our content script observers. Declare at top so that it will outlive all
213 // other members, since they might add themselves as observers.
brettwd195c952015-06-02 17:31:12214 base::ObserverList<ScriptExecutionObserver> script_execution_observers_;
[email protected]a83b8402012-05-17 06:56:44215
[email protected]36fb2c7c2011-04-04 15:49:08216 // If non-null this tab is an app tab and this is the extension the tab was
217 // created for.
[email protected]a6394ae2012-07-16 20:58:43218 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08219
[email protected]74e51402011-04-06 14:17:59220 // Icon for extension_app_ (if non-null) or a manually-set icon for
221 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08222 SkBitmap extension_app_icon_;
223
[email protected]553602e12011-04-05 17:01:18224 // Cached web app info data.
225 WebApplicationInfo web_app_info_;
226
[email protected]619f86182012-07-03 21:30:18227 // Which deferred action to perform when OnDidGetApplicationInfo is notified
228 // from a WebContents.
229 WebAppAction pending_web_app_action_;
230
avi1a908722015-01-15 16:01:49231 // Which navigation entry was active when the GetApplicationInfo request was
232 // sent, for verification when the reply returns.
233 int last_committed_nav_entry_unique_id_;
[email protected]fd14a442014-07-02 20:40:05234
235 // Whether to trigger an update when the page load completes.
236 bool update_shortcut_on_load_complete_;
237
[email protected]619f86182012-07-03 21:30:18238 content::NotificationRegistrar registrar_;
239
dchengc963c7142016-04-08 03:55:22240 std::unique_ptr<ScriptExecutor> script_executor_;
[email protected]af78a802012-07-10 23:47:02241
dchengc963c7142016-04-08 03:55:22242 std::unique_ptr<ExtensionActionRunner> extension_action_runner_;
rdevlin.cronin91f162a12014-09-03 16:48:40243
dchengc963c7142016-04-08 03:55:22244 std::unique_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
[email protected]fc5e65d6b2012-06-13 00:22:57245
dchengc963c7142016-04-08 03:55:22246 std::unique_ptr<BookmarkAppHelper> bookmark_app_helper_;
[email protected]898697902013-12-18 03:25:46247
[email protected]3d6d676522013-10-14 20:44:55248 // Creates WebstoreInlineInstaller instances for inline install triggers.
dchengc963c7142016-04-08 03:55:22249 std::unique_ptr<WebstoreInlineInstallerFactory>
250 webstore_inline_installer_factory_;
[email protected]3d6d676522013-10-14 20:44:55251
rdevlin.cronin092c6772014-11-20 23:52:40252 // The reenable prompt for disabled extensions, if any.
dchengc963c7142016-04-08 03:55:22253 std::unique_ptr<ExtensionReenabler> extension_reenabler_;
rdevlin.cronin092c6772014-11-20 23:52:40254
rdevlin.cronin1f047f7b2016-06-06 18:30:18255 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
256 registry_observer_;
257
rdevlin.cronind8076cd62016-07-08 23:05:58258 // Map of InlineInstallObservers for inline installations that have progress
259 // listeners.
260 std::map<ExtensionId, std::unique_ptr<InlineInstallObserver>>
rdevlin.cronind30a8bd2016-06-30 16:02:29261 install_observers_;
262
263 // The set of extension ids that are currently being installed.
rdevlin.cronind8076cd62016-07-08 23:05:58264 std::set<ExtensionId> pending_inline_installations_;
rdevlin.cronind30a8bd2016-06-30 16:02:29265
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_