[email protected] | 8809f144 | 2012-01-20 21:21:47 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 7 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
[email protected] | a340bb4 | 2014-01-09 07:33:20 | [diff] [blame] | 9 | #include <set> |
[email protected] | 4b64d71 | 2013-01-17 17:53:17 | [diff] [blame] | 10 | #include <string> |
[email protected] | a714e46 | 2013-01-26 06:33:10 | [diff] [blame] | 11 | |
Alexey Baskakov | 094762b7 | 2018-10-23 07:50:58 | [diff] [blame] | 12 | #include "base/callback_forward.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 28a69d3 | 2012-05-30 07:58:18 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 82a4373 | 2011-10-07 16:09:11 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 16 | #include "base/observer_list.h" |
rdevlin.cronin | 1f047f7b | 2016-06-06 18:30:18 | [diff] [blame] | 17 | #include "base/scoped_observer.h" |
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 18 | #include "chrome/browser/extensions/active_tab_permission_granter.h" |
cm.sanchi | 2b601c3 | 2017-12-18 10:49:28 | [diff] [blame] | 19 | #include "chrome/common/chrome_render_frame.mojom.h" |
[email protected] | ced522c | 2014-07-23 20:23:59 | [diff] [blame] | 20 | #include "chrome/common/extensions/webstore_install_result.h" |
[email protected] | 93f5046 | 2013-05-10 04:40:40 | [diff] [blame] | 21 | #include "chrome/common/web_application_info.h" |
catmullings | ba53910 | 2017-05-26 19:45:10 | [diff] [blame] | 22 | #include "content/public/browser/web_contents_binding_set.h" |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 23 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 24 | #include "content/public/browser/web_contents_user_data.h" |
[email protected] | 0b9de03 | 2014-03-15 05:47:01 | [diff] [blame] | 25 | #include "extensions/browser/extension_function_dispatcher.h" |
rdevlin.cronin | 1f047f7b | 2016-06-06 18:30:18 | [diff] [blame] | 26 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 16a4206f | 2014-08-15 09:44:43 | [diff] [blame] | 27 | #include "extensions/browser/script_executor.h" |
rdevlin.cronin | d8076cd6 | 2016-07-08 23:05:58 | [diff] [blame] | 28 | #include "extensions/common/extension_id.h" |
[email protected] | 88b50b6 | 2013-09-01 23:05:06 | [diff] [blame] | 29 | #include "extensions/common/stack_frame.h" |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 30 | #include "third_party/skia/include/core/SkBitmap.h" |
| 31 | |
lukasza | 53fe8fd8 | 2017-06-20 23:38:34 | [diff] [blame] | 32 | namespace content { |
| 33 | class RenderFrameHost; |
| 34 | } |
| 35 | |
[email protected] | 75b91fd | 2013-02-12 22:14:25 | [diff] [blame] | 36 | namespace gfx { |
| 37 | class Image; |
| 38 | } |
| 39 | |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 40 | namespace extensions { |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 41 | class ExtensionActionRunner; |
[email protected] | 33c8789 | 2014-03-25 06:28:15 | [diff] [blame] | 42 | class BookmarkAppHelper; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 43 | class Extension; |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 44 | |
[email protected] | 74e5140 | 2011-04-06 14:17:59 | [diff] [blame] | 45 | // Per-tab extension helper. Also handles non-extension apps. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 46 | class TabHelper : public content::WebContentsObserver, |
rdevlin.cronin | 1f047f7b | 2016-06-06 18:30:18 | [diff] [blame] | 47 | public ExtensionFunctionDispatcher::Delegate, |
| 48 | public ExtensionRegistryObserver, |
Benjamin Ackerman | c4fb0230 | 2018-10-03 19:55:41 | [diff] [blame] | 49 | public content::WebContentsUserData<TabHelper> { |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 50 | public: |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 51 | ~TabHelper() override; |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 52 | |
Alexey Baskakov | 094762b7 | 2018-10-23 07:50:58 | [diff] [blame] | 53 | 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] | 9208654 | 2014-04-08 08:45:29 | [diff] [blame] | 58 | bool CanCreateBookmarkApp() const; |
[email protected] | 1739e57d | 2011-11-30 21:18:25 | [diff] [blame] | 59 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 60 | // Sets the extension denoting this as an app. If |extension| is non-null this |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 61 | // 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] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 63 | // |
| 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] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 67 | void SetExtensionApp(const Extension* extension); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 68 | |
| 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.cronin | d8076cd6 | 2016-07-08 23:05:58 | [diff] [blame] | 72 | void SetExtensionAppById(const ExtensionId& extension_app_id); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 73 | |
Alexey Baskakov | 2117478 | 2018-09-19 02:46:39 | [diff] [blame] | 74 | // 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] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 80 | |
[email protected] | 0932b30c | 2012-04-17 13:25:10 | [diff] [blame] | 81 | // If an app extension has been explicitly set for this WebContents its icon |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 82 | // is returned. |
| 83 | // |
| 84 | // NOTE: the returned icon is larger than 16x16 (its size is |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 85 | // extension_misc::EXTENSION_ICON_SMALLISH). |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 86 | SkBitmap* GetExtensionAppIcon(); |
| 87 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 88 | ScriptExecutor* script_executor() { |
[email protected] | 09ae70d4 | 2012-11-07 00:42:09 | [diff] [blame] | 89 | return script_executor_.get(); |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 90 | } |
[email protected] | a83b840 | 2012-05-17 06:56:44 | [diff] [blame] | 91 | |
rdevlin.cronin | 8408b4f9 | 2016-03-15 19:14:14 | [diff] [blame] | 92 | ExtensionActionRunner* extension_action_runner() { |
| 93 | return extension_action_runner_.get(); |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 78ce302 | 2012-09-24 01:48:48 | [diff] [blame] | 96 | ActiveTabPermissionGranter* active_tab_permission_granter() { |
| 97 | return active_tab_permission_granter_.get(); |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 100 | private: |
wittman | 2b0e4065 | 2015-06-10 00:53:03 | [diff] [blame] | 101 | // Utility function to invoke member functions on all relevant |
| 102 | // ContentRulesRegistries. |
| 103 | template <class Func> |
| 104 | void InvokeForContentRulesRegistries(const Func& func); |
| 105 | |
[email protected] | fd14a44 | 2014-07-02 20:40:05 | [diff] [blame] | 106 | // Different types of action when web app info is available. |
Alan Cutter | 4b60641 | 2018-02-08 23:32:20 | [diff] [blame] | 107 | // OnDidGetWebApplicationInfo uses this to dispatch calls. |
[email protected] | fd14a44 | 2014-07-02 20:40:05 | [diff] [blame] | 108 | enum WebAppAction { |
| 109 | NONE, // No action at all. |
[email protected] | fd14a44 | 2014-07-02 20:40:05 | [diff] [blame] | 110 | CREATE_HOSTED_APP, // Create and install a hosted app. |
[email protected] | fd14a44 | 2014-07-02 20:40:05 | [diff] [blame] | 111 | }; |
| 112 | |
[email protected] | a78f03c | 2012-09-15 05:08:19 | [diff] [blame] | 113 | explicit TabHelper(content::WebContents* web_contents); |
catmullings | ba53910 | 2017-05-26 19:45:10 | [diff] [blame] | 114 | |
[email protected] | 46b3c98 | 2012-10-09 18:38:30 | [diff] [blame] | 115 | friend class content::WebContentsUserData<TabHelper>; |
[email protected] | a78f03c | 2012-09-15 05:08:19 | [diff] [blame] | 116 | |
[email protected] | 33c8789 | 2014-03-25 06:28:15 | [diff] [blame] | 117 | // Displays UI for completion of creating a bookmark hosted app. |
catmullings | ba53910 | 2017-05-26 19:45:10 | [diff] [blame] | 118 | void FinishCreateBookmarkApp(const Extension* extension, |
[email protected] | 33c8789 | 2014-03-25 06:28:15 | [diff] [blame] | 119 | const WebApplicationInfo& web_app_info); |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 120 | |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 121 | // content::WebContentsObserver overrides. |
rdevlin.cronin | b67a3477 | 2015-06-04 16:32:38 | [diff] [blame] | 122 | void RenderFrameCreated(content::RenderFrameHost* host) override; |
jam | f1c7f0c | 2017-02-02 01:04:37 | [diff] [blame] | 123 | void DidFinishNavigation( |
| 124 | content::NavigationHandle* navigation_handle) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 125 | bool OnMessageReceived(const IPC::Message& message, |
lukasza | 53fe8fd8 | 2017-06-20 23:38:34 | [diff] [blame] | 126 | content::RenderFrameHost* sender) override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 127 | void DidCloneToNewWebContents( |
[email protected] | 7381d9f | 2012-09-12 20:26:22 | [diff] [blame] | 128 | content::WebContents* old_web_contents, |
mostynb | a15bee1 | 2014-10-04 00:40:32 | [diff] [blame] | 129 | content::WebContents* new_web_contents) override; |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 130 | |
catmullings | ba53910 | 2017-05-26 19:45:10 | [diff] [blame] | 131 | // ExtensionFunctionDispatcher::Delegate overrides. |
| 132 | WindowController* GetExtensionWindowController() const override; |
dcheng | ae36a4a | 2014-10-21 12:36:36 | [diff] [blame] | 133 | content::WebContents* GetAssociatedWebContents() const override; |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 134 | |
rdevlin.cronin | 1f047f7b | 2016-06-06 18:30:18 | [diff] [blame] | 135 | // ExtensionRegistryObserver: |
| 136 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 137 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 138 | UnloadedExtensionReason reason) override; |
rdevlin.cronin | 1f047f7b | 2016-06-06 18:30:18 | [diff] [blame] | 139 | |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 140 | // Message handlers. |
cm.sanchi | 2b601c3 | 2017-12-18 10:49:28 | [diff] [blame] | 141 | void OnDidGetWebApplicationInfo( |
| 142 | chrome::mojom::ChromeRenderFrameAssociatedPtr chrome_render_frame, |
Alan Cutter | 81ac72668 | 2018-09-25 00:18:36 | [diff] [blame] | 143 | bool shortcut_app_requested, |
cm.sanchi | 2b601c3 | 2017-12-18 10:49:28 | [diff] [blame] | 144 | const WebApplicationInfo& info); |
rdevlin.cronin | 7df5805 | 2015-07-10 20:30:20 | [diff] [blame] | 145 | void OnGetAppInstallState(content::RenderFrameHost* host, |
| 146 | const GURL& requestor_url, |
[email protected] | f2fe87c | 2012-04-24 17:53:49 | [diff] [blame] | 147 | int return_route_id, |
| 148 | int callback_id); |
Trent Apted | 8f733b9 | 2018-10-04 00:54:45 | [diff] [blame] | 149 | void OnContentScriptsExecuting(content::RenderFrameHost* host, |
| 150 | const ExecutingScriptsMap& extension_ids, |
| 151 | const GURL& on_url); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 152 | |
| 153 | // App extensions related methods: |
| 154 | |
[email protected] | 75b91fd | 2013-02-12 22:14:25 | [diff] [blame] | 155 | // Resets app_icon_ and if |extension| is non-null uses ImageLoader to load |
| 156 | // the extension's image asynchronously. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 157 | void UpdateExtensionAppIcon(const Extension* extension); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 158 | |
rdevlin.cronin | d8076cd6 | 2016-07-08 23:05:58 | [diff] [blame] | 159 | const Extension* GetExtension(const ExtensionId& extension_app_id); |
[email protected] | dd290d3 | 2012-03-06 02:47:51 | [diff] [blame] | 160 | |
[email protected] | 7069ed8 | 2013-01-26 01:39:53 | [diff] [blame] | 161 | void OnImageLoaded(const gfx::Image& image); |
[email protected] | 8915f34 | 2011-08-29 22:14:37 | [diff] [blame] | 162 | |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 163 | // Requests application info for the specified page. This is an asynchronous |
Alan Cutter | 4b60641 | 2018-02-08 23:32:20 | [diff] [blame] | 164 | // request. The delegate is notified by way of OnDidGetWebApplicationInfo when |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 165 | // the data is available. |
Alan Cutter | 81ac72668 | 2018-09-25 00:18:36 | [diff] [blame] | 166 | void GetApplicationInfo(WebAppAction action, bool shortcut_app_requested); |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 167 | |
rdevlin.cronin | b67a3477 | 2015-06-04 16:32:38 | [diff] [blame] | 168 | // Sends our tab ID to |render_frame_host|. |
| 169 | void SetTabId(content::RenderFrameHost* render_frame_host); |
[email protected] | 2fc1cad1 | 2013-05-21 00:41:39 | [diff] [blame] | 170 | |
wittman | 2b0e4065 | 2015-06-10 00:53:03 | [diff] [blame] | 171 | Profile* profile_; |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 172 | |
| 173 | // If non-null this tab is an app tab and this is the extension the tab was |
| 174 | // created for. |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 175 | const Extension* extension_app_; |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 176 | |
[email protected] | 74e5140 | 2011-04-06 14:17:59 | [diff] [blame] | 177 | // Icon for extension_app_ (if non-null) or a manually-set icon for |
| 178 | // non-extension apps. |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 179 | SkBitmap extension_app_icon_; |
| 180 | |
[email protected] | 553602e1 | 2011-04-05 17:01:18 | [diff] [blame] | 181 | // Cached web app info data. |
| 182 | WebApplicationInfo web_app_info_; |
| 183 | |
Alan Cutter | 4b60641 | 2018-02-08 23:32:20 | [diff] [blame] | 184 | // Which deferred action to perform when OnDidGetWebApplicationInfo is |
| 185 | // notified from a WebContents. |
[email protected] | 619f8618 | 2012-07-03 21:30:18 | [diff] [blame] | 186 | WebAppAction pending_web_app_action_; |
| 187 | |
avi | 1a90872 | 2015-01-15 16:01:49 | [diff] [blame] | 188 | // 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] | fd14a44 | 2014-07-02 20:40:05 | [diff] [blame] | 191 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 192 | std::unique_ptr<ScriptExecutor> script_executor_; |
[email protected] | af78a80 | 2012-07-10 23:47:02 | [diff] [blame] | 193 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 194 | std::unique_ptr<ExtensionActionRunner> extension_action_runner_; |
rdevlin.cronin | 91f162a1 | 2014-09-03 16:48:40 | [diff] [blame] | 195 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 196 | std::unique_ptr<ActiveTabPermissionGranter> active_tab_permission_granter_; |
[email protected] | fc5e65d6b | 2012-06-13 00:22:57 | [diff] [blame] | 197 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 198 | std::unique_ptr<BookmarkAppHelper> bookmark_app_helper_; |
[email protected] | 89869790 | 2013-12-18 03:25:46 | [diff] [blame] | 199 | |
Alexey Baskakov | 094762b7 | 2018-10-23 07:50:58 | [diff] [blame] | 200 | // Reponse to CreateHostedAppFromWebContents request. |
| 201 | OnceInstallCallback install_callback_; |
| 202 | |
rdevlin.cronin | 1f047f7b | 2016-06-06 18:30:18 | [diff] [blame] | 203 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 204 | registry_observer_; |
| 205 | |
mohan.reddy | 63d6300 | 2014-09-16 04:13:56 | [diff] [blame] | 206 | // Vend weak pointers that can be invalidated to stop in-progress loads. |
| 207 | base::WeakPtrFactory<TabHelper> image_loader_ptr_factory_; |
| 208 | |
rdevlin.cronin | 092c677 | 2014-11-20 23:52:40 | [diff] [blame] | 209 | // Generic weak ptr factory for posting callbacks. |
| 210 | base::WeakPtrFactory<TabHelper> weak_ptr_factory_; |
| 211 | |
François Doray | 4f51d5d | 2018-12-03 22:26:24 | [diff] [blame^] | 212 | WEB_CONTENTS_USER_DATA_KEY_DECL(); |
| 213 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 214 | DISALLOW_COPY_AND_ASSIGN(TabHelper); |
[email protected] | 36fb2c7c | 2011-04-04 15:49:08 | [diff] [blame] | 215 | }; |
| 216 | |
[email protected] | a6394ae | 2012-07-16 20:58:43 | [diff] [blame] | 217 | } // namespace extensions |
| 218 | |
| 219 | #endif // CHROME_BROWSER_EXTENSIONS_TAB_HELPER_H_ |