blob: 5f2a4d721a0aa8adcc8028456067282288d3f56a [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
Alexey Baskakov094762b72018-10-23 07:50:5812#include "base/callback_forward.h"
avia2f4804a2015-12-24 23:11:1313#include "base/macros.h"
[email protected]28a69d32012-05-30 07:58:1814#include "base/memory/ref_counted.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"
rdevlin.cronin1f047f7b2016-06-06 18:30:1817#include "base/scoped_observer.h"
[email protected]78ce3022012-09-24 01:48:4818#include "chrome/browser/extensions/active_tab_permission_granter.h"
cm.sanchi2b601c32017-12-18 10:49:2819#include "chrome/common/chrome_render_frame.mojom.h"
[email protected]ced522c2014-07-23 20:23:5920#include "chrome/common/extensions/webstore_install_result.h"
[email protected]93f50462013-05-10 04:40:4021#include "chrome/common/web_application_info.h"
catmullingsba539102017-05-26 19:45:1022#include "content/public/browser/web_contents_binding_set.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_executor.h"
rdevlin.cronind8076cd62016-07-08 23:05:5828#include "extensions/common/extension_id.h"
[email protected]88b50b62013-09-01 23:05:0629#include "extensions/common/stack_frame.h"
[email protected]36fb2c7c2011-04-04 15:49:0830#include "third_party/skia/include/core/SkBitmap.h"
31
lukasza53fe8fd82017-06-20 23:38:3432namespace content {
33class RenderFrameHost;
34}
35
[email protected]75b91fd2013-02-12 22:14:2536namespace gfx {
37class Image;
38}
39
[email protected]a83b8402012-05-17 06:56:4440namespace extensions {
rdevlin.cronin8408b4f92016-03-15 19:14:1441class ExtensionActionRunner;
[email protected]33c87892014-03-25 06:28:1542class BookmarkAppHelper;
[email protected]1c321ee52012-05-21 03:02:3443class Extension;
[email protected]a83b8402012-05-17 06:56:4444
[email protected]74e51402011-04-06 14:17:5945// Per-tab extension helper. Also handles non-extension apps.
[email protected]a6394ae2012-07-16 20:58:4346class TabHelper : public content::WebContentsObserver,
rdevlin.cronin1f047f7b2016-06-06 18:30:1847 public ExtensionFunctionDispatcher::Delegate,
48 public ExtensionRegistryObserver,
Benjamin Ackermanc4fb02302018-10-03 19:55:4149 public content::WebContentsUserData<TabHelper> {
[email protected]36fb2c7c2011-04-04 15:49:0850 public:
dchengae36a4a2014-10-21 12:36:3651 ~TabHelper() override;
[email protected]36fb2c7c2011-04-04 15:49:0852
Alexey Baskakov094762b72018-10-23 07:50:5853 using OnceInstallCallback =
54 base::OnceCallback<void(const ExtensionId& app_id, bool success)>;
55
56 void CreateHostedAppFromWebContents(bool shortcut_app_requested,
57 OnceInstallCallback callback);
[email protected]92086542014-04-08 08:45:2958 bool CanCreateBookmarkApp() const;
[email protected]1739e57d2011-11-30 21:18:2559
[email protected]36fb2c7c2011-04-04 15:49:0860 // Sets the extension denoting this as an app. If |extension| is non-null this
[email protected]0932b30c2012-04-17 13:25:1061 // tab becomes an app-tab. WebContents does not listen for unload events for
62 // the extension. It's up to consumers of WebContents to do that.
[email protected]36fb2c7c2011-04-04 15:49:0863 //
64 // NOTE: this should only be manipulated before the tab is added to a browser.
65 // TODO(sky): resolve if this is the right way to identify an app tab. If it
66 // is, than this should be passed in the constructor.
[email protected]a6394ae2012-07-16 20:58:4367 void SetExtensionApp(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:0868
69 // Convenience for setting the app extension by id. This does nothing if
70 // |extension_app_id| is empty, or an extension can't be found given the
71 // specified id.
rdevlin.cronind8076cd62016-07-08 23:05:5872 void SetExtensionAppById(const ExtensionId& extension_app_id);
[email protected]36fb2c7c2011-04-04 15:49:0873
Alexey Baskakov21174782018-09-19 02:46:3974 // Returns true if an app extension has been set.
75 bool is_app() const { return extension_app_ != nullptr; }
76
77 // Return ExtensionId for extension app.
78 // If an app extension has not been set, returns empty id.
79 ExtensionId GetAppId() const;
[email protected]36fb2c7c2011-04-04 15:49:0880
[email protected]0932b30c2012-04-17 13:25:1081 // If an app extension has been explicitly set for this WebContents its icon
[email protected]36fb2c7c2011-04-04 15:49:0882 // is returned.
83 //
84 // NOTE: the returned icon is larger than 16x16 (its size is
[email protected]1d5e58b2013-01-31 08:41:4085 // extension_misc::EXTENSION_ICON_SMALLISH).
[email protected]36fb2c7c2011-04-04 15:49:0886 SkBitmap* GetExtensionAppIcon();
87
[email protected]a6394ae2012-07-16 20:58:4388 ScriptExecutor* script_executor() {
[email protected]09ae70d42012-11-07 00:42:0989 return script_executor_.get();
[email protected]af78a802012-07-10 23:47:0290 }
[email protected]a83b8402012-05-17 06:56:4491
rdevlin.cronin8408b4f92016-03-15 19:14:1492 ExtensionActionRunner* extension_action_runner() {
93 return extension_action_runner_.get();
rdevlin.cronin91f162a12014-09-03 16:48:4094 }
95
[email protected]78ce3022012-09-24 01:48:4896 ActiveTabPermissionGranter* active_tab_permission_granter() {
97 return active_tab_permission_granter_.get();
[email protected]fc5e65d6b2012-06-13 00:22:5798 }
99
[email protected]36fb2c7c2011-04-04 15:49:08100 private:
wittman2b0e40652015-06-10 00:53:03101 // Utility function to invoke member functions on all relevant
102 // ContentRulesRegistries.
103 template <class Func>
104 void InvokeForContentRulesRegistries(const Func& func);
105
[email protected]fd14a442014-07-02 20:40:05106 // Different types of action when web app info is available.
Alan Cutter4b606412018-02-08 23:32:20107 // OnDidGetWebApplicationInfo uses this to dispatch calls.
[email protected]fd14a442014-07-02 20:40:05108 enum WebAppAction {
109 NONE, // No action at all.
[email protected]fd14a442014-07-02 20:40:05110 CREATE_HOSTED_APP, // Create and install a hosted app.
[email protected]fd14a442014-07-02 20:40:05111 };
112
[email protected]a78f03c2012-09-15 05:08:19113 explicit TabHelper(content::WebContents* web_contents);
catmullingsba539102017-05-26 19:45:10114
[email protected]46b3c982012-10-09 18:38:30115 friend class content::WebContentsUserData<TabHelper>;
[email protected]a78f03c2012-09-15 05:08:19116
[email protected]33c87892014-03-25 06:28:15117 // Displays UI for completion of creating a bookmark hosted app.
catmullingsba539102017-05-26 19:45:10118 void FinishCreateBookmarkApp(const Extension* extension,
[email protected]33c87892014-03-25 06:28:15119 const WebApplicationInfo& web_app_info);
[email protected]898697902013-12-18 03:25:46120
[email protected]d8c660432011-12-22 20:51:25121 // content::WebContentsObserver overrides.
rdevlin.croninb67a34772015-06-04 16:32:38122 void RenderFrameCreated(content::RenderFrameHost* host) override;
jamf1c7f0c2017-02-02 01:04:37123 void DidFinishNavigation(
124 content::NavigationHandle* navigation_handle) override;
dchengae36a4a2014-10-21 12:36:36125 bool OnMessageReceived(const IPC::Message& message,
lukasza53fe8fd82017-06-20 23:38:34126 content::RenderFrameHost* sender) override;
dchengae36a4a2014-10-21 12:36:36127 void DidCloneToNewWebContents(
[email protected]7381d9f2012-09-12 20:26:22128 content::WebContents* old_web_contents,
mostynba15bee12014-10-04 00:40:32129 content::WebContents* new_web_contents) override;
[email protected]553602e12011-04-05 17:01:18130
catmullingsba539102017-05-26 19:45:10131 // ExtensionFunctionDispatcher::Delegate overrides.
132 WindowController* GetExtensionWindowController() const override;
dchengae36a4a2014-10-21 12:36:36133 content::WebContents* GetAssociatedWebContents() const override;
[email protected]c5dbef02011-05-13 05:06:09134
rdevlin.cronin1f047f7b2016-06-06 18:30:18135 // ExtensionRegistryObserver:
136 void OnExtensionUnloaded(content::BrowserContext* browser_context,
137 const Extension* extension,
limasdf0deef2042017-05-03 19:17:17138 UnloadedExtensionReason reason) override;
rdevlin.cronin1f047f7b2016-06-06 18:30:18139
[email protected]553602e12011-04-05 17:01:18140 // Message handlers.
cm.sanchi2b601c32017-12-18 10:49:28141 void OnDidGetWebApplicationInfo(
142 chrome::mojom::ChromeRenderFrameAssociatedPtr chrome_render_frame,
Alan Cutter81ac726682018-09-25 00:18:36143 bool shortcut_app_requested,
cm.sanchi2b601c32017-12-18 10:49:28144 const WebApplicationInfo& info);
rdevlin.cronin7df58052015-07-10 20:30:20145 void OnGetAppInstallState(content::RenderFrameHost* host,
146 const GURL& requestor_url,
[email protected]f2fe87c2012-04-24 17:53:49147 int return_route_id,
148 int callback_id);
Trent Apted8f733b92018-10-04 00:54:45149 void OnContentScriptsExecuting(content::RenderFrameHost* host,
150 const ExecutingScriptsMap& extension_ids,
151 const GURL& on_url);
[email protected]36fb2c7c2011-04-04 15:49:08152
153 // App extensions related methods:
154
[email protected]75b91fd2013-02-12 22:14:25155 // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load
156 // the extension's image asynchronously.
[email protected]a6394ae2012-07-16 20:58:43157 void UpdateExtensionAppIcon(const Extension* extension);
[email protected]36fb2c7c2011-04-04 15:49:08158
rdevlin.cronind8076cd62016-07-08 23:05:58159 const Extension* GetExtension(const ExtensionId& extension_app_id);
[email protected]dd290d32012-03-06 02:47:51160
[email protected]7069ed82013-01-26 01:39:53161 void OnImageLoaded(const gfx::Image& image);
[email protected]8915f342011-08-29 22:14:37162
[email protected]619f86182012-07-03 21:30:18163 // Requests application info for the specified page. This is an asynchronous
Alan Cutter4b606412018-02-08 23:32:20164 // request. The delegate is notified by way of OnDidGetWebApplicationInfo when
[email protected]619f86182012-07-03 21:30:18165 // the data is available.
Alan Cutter81ac726682018-09-25 00:18:36166 void GetApplicationInfo(WebAppAction action, bool shortcut_app_requested);
[email protected]619f86182012-07-03 21:30:18167
rdevlin.croninb67a34772015-06-04 16:32:38168 // Sends our tab ID to |render_frame_host|.
169 void SetTabId(content::RenderFrameHost* render_frame_host);
[email protected]2fc1cad12013-05-21 00:41:39170
wittman2b0e40652015-06-10 00:53:03171 Profile* profile_;
[email protected]36fb2c7c2011-04-04 15:49:08172
173 // If non-null this tab is an app tab and this is the extension the tab was
174 // created for.
[email protected]a6394ae2012-07-16 20:58:43175 const Extension* extension_app_;
[email protected]36fb2c7c2011-04-04 15:49:08176
[email protected]74e51402011-04-06 14:17:59177 // Icon for extension_app_ (if non-null) or a manually-set icon for
178 // non-extension apps.
[email protected]36fb2c7c2011-04-04 15:49:08179 SkBitmap extension_app_icon_;
180
[email protected]553602e12011-04-05 17:01:18181 // Cached web app info data.
182 WebApplicationInfo web_app_info_;
183
Alan Cutter4b606412018-02-08 23:32:20184 // Which deferred action to perform when OnDidGetWebApplicationInfo is
185 // notified from a WebContents.
[email protected]619f86182012-07-03 21:30:18186 WebAppAction pending_web_app_action_;
187
avi1a908722015-01-15 16:01:49188 // Which navigation entry was active when the GetApplicationInfo request was
189 // sent, for verification when the reply returns.
190 int last_committed_nav_entry_unique_id_;
[email protected]fd14a442014-07-02 20:40:05191
dchengc963c7142016-04-08 03:55:22192 std::unique_ptr<ScriptExecutor> script_executor_;
[email protected]af78a802012-07-10 23:47:02193
dchengc963c7142016-04-08 03:55:22194 std::unique_ptr<ExtensionActionRunner> extension_action_runner_;
rdevlin.cronin91f162a12014-09-03 16:48:40195
dchengc963c7142016-04-08 03:55:22196 std::unique_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_;
[email protected]fc5e65d6b2012-06-13 00:22:57197
dchengc963c7142016-04-08 03:55:22198 std::unique_ptr<BookmarkAppHelper> bookmark_app_helper_;
[email protected]898697902013-12-18 03:25:46199
Alexey Baskakov094762b72018-10-23 07:50:58200 // Reponse to CreateHostedAppFromWebContents request.
201 OnceInstallCallback install_callback_;
202
rdevlin.cronin1f047f7b2016-06-06 18:30:18203 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
204 registry_observer_;
205
mohan.reddy63d63002014-09-16 04:13:56206 // Vend weak pointers that can be invalidated to stop in-progress loads.
207 base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_;
208
rdevlin.cronin092c6772014-11-20 23:52:40209 // Generic weak ptr factory for posting callbacks.
210 base::WeakPtrFactory<TabHelper> weak_ptr_factory_;
211
François Doray4f51d5d2018-12-03 22:26:24212 WEB_CONTENTS_USER_DATA_KEY_DECL();
213
[email protected]a6394ae2012-07-16 20:58:43214 DISALLOW_COPY_AND_ASSIGN(TabHelper);
[email protected]36fb2c7c2011-04-04 15:49:08215};
216
[email protected]a6394ae2012-07-16 20:58:43217} // namespace extensions
218
219#endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_