[email protected] | 9f76c1e | 2012-03-05 15:15:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [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 | |
| 5 | // Implements the Chrome Extensions Debugger API. |
| 6 | |
[email protected] | 1eb8f2f | 2012-07-12 02:04:49 | [diff] [blame] | 7 | #include "chrome/browser/extensions/api/debugger/debugger_api.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 8 | |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 9 | #include <stddef.h> |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 10 | |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 11 | #include <map> |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 12 | #include <memory> |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 13 | #include <set> |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 14 | #include <utility> |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 15 | |
Sebastien Marchand | f1349f5 | 2019-01-25 03:16:41 | [diff] [blame] | 16 | #include "base/bind.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 17 | #include "base/command_line.h" |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 18 | #include "base/json/json_reader.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 19 | #include "base/json/json_writer.h" |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 20 | #include "base/lazy_instance.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 21 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 22 | #include "base/memory/singleton.h" |
Sigurdur Asgeirsson | 2c442dc | 2021-03-25 13:41:20 | [diff] [blame] | 23 | #include "base/scoped_observation.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 25 | #include "base/task/post_task.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 26 | #include "base/values.h" |
[email protected] | 49a01e64 | 2013-07-12 00:29:45 | [diff] [blame] | 27 | #include "chrome/browser/chrome_notification_types.h" |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 28 | #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
[email protected] | 1eb8f2f | 2012-07-12 02:04:49 | [diff] [blame] | 29 | #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h" |
Peter Kasting | cd8f725 | 2020-05-09 01:43:59 | [diff] [blame] | 30 | #include "chrome/browser/extensions/api/debugger/extension_dev_tools_infobar_delegate.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 31 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 32 | #include "chrome/browser/extensions/extension_tab_util.h" |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 33 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 34 | #include "chrome/browser/profiles/profile.h" |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 35 | #include "chrome/browser/ui/browser.h" |
[email protected] | 863f70a | 2012-01-27 02:05:50 | [diff] [blame] | 36 | #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 37 | #include "chrome/common/chrome_switches.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 38 | #include "content/public/browser/browser_task_traits.h" |
dgozman | cb088d0 | 2015-04-28 11:57:45 | [diff] [blame] | 39 | #include "content/public/browser/browser_thread.h" |
[email protected] | 98f6611 | 2012-12-25 12:59:36 | [diff] [blame] | 40 | #include "content/public/browser/devtools_agent_host.h" |
[email protected] | 1c3bd20 | 2011-12-23 05:48:24 | [diff] [blame] | 41 | #include "content/public/browser/notification_service.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 42 | #include "content/public/browser/notification_source.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 43 | #include "content/public/browser/render_process_host.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 44 | #include "content/public/browser/render_widget_host.h" |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 45 | #include "content/public/browser/web_contents.h" |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 46 | #include "content/public/common/content_client.h" |
Dmitry Gozman | 00ba04e | 2018-09-18 00:39:27 | [diff] [blame] | 47 | #include "content/public/common/url_constants.h" |
[email protected] | 3b366ae | 2013-05-17 21:16:58 | [diff] [blame] | 48 | #include "content/public/common/url_utils.h" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 49 | #include "extensions/browser/event_router.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 50 | #include "extensions/browser/extension_host.h" |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 51 | #include "extensions/browser/extension_registry.h" |
| 52 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 53 | #include "extensions/common/constants.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 54 | #include "extensions/common/error_utils.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 55 | #include "extensions/common/extension.h" |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 56 | #include "extensions/common/manifest_constants.h" |
| 57 | #include "extensions/common/permissions/permissions_data.h" |
| 58 | #include "extensions/common/switches.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 59 | |
[email protected] | 0e12d7d | 2011-12-01 16:21:44 | [diff] [blame] | 60 | using content::DevToolsAgentHost; |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 61 | using content::RenderProcessHost; |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 62 | using content::RenderWidgetHost; |
[email protected] | 7320a44e | 2012-05-22 17:01:11 | [diff] [blame] | 63 | using content::WebContents; |
[email protected] | 0e12d7d | 2011-12-01 16:21:44 | [diff] [blame] | 64 | |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 65 | namespace Attach = extensions::api::debugger::Attach; |
| 66 | namespace Detach = extensions::api::debugger::Detach; |
| 67 | namespace OnDetach = extensions::api::debugger::OnDetach; |
| 68 | namespace OnEvent = extensions::api::debugger::OnEvent; |
| 69 | namespace SendCommand = extensions::api::debugger::SendCommand; |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 70 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 71 | namespace extensions { |
| 72 | class ExtensionRegistry; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 73 | class ExtensionDevToolsClientHost; |
[email protected] | 19bcf9d | 2013-07-18 22:00:59 | [diff] [blame] | 74 | |
| 75 | namespace { |
| 76 | |
| 77 | // Helpers -------------------------------------------------------------------- |
| 78 | |
| 79 | void CopyDebuggee(Debuggee* dst, const Debuggee& src) { |
| 80 | if (src.tab_id) |
Peter Boström | 924f803 | 2021-04-02 20:36:02 | [diff] [blame] | 81 | dst->tab_id = std::make_unique<int>(*src.tab_id); |
[email protected] | 19bcf9d | 2013-07-18 22:00:59 | [diff] [blame] | 82 | if (src.extension_id) |
Peter Boström | 924f803 | 2021-04-02 20:36:02 | [diff] [blame] | 83 | dst->extension_id = std::make_unique<std::string>(*src.extension_id); |
[email protected] | 19bcf9d | 2013-07-18 22:00:59 | [diff] [blame] | 84 | if (src.target_id) |
Peter Boström | 924f803 | 2021-04-02 20:36:02 | [diff] [blame] | 85 | dst->target_id = std::make_unique<std::string>(*src.target_id); |
[email protected] | 19bcf9d | 2013-07-18 22:00:59 | [diff] [blame] | 86 | } |
| 87 | |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 88 | // Returns true if the given |Extension| is allowed to attach to the specified |
| 89 | // |url|. |
Andrey Kosyakov | fa3dd0b | 2020-09-19 06:02:05 | [diff] [blame] | 90 | bool ExtensionMayAttachToURL(const Extension& extension, |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 91 | const GURL& url, |
| 92 | Profile* profile, |
| 93 | std::string* error) { |
Andrey Kosyakov | 54f4c4a | 2020-11-03 03:34:56 | [diff] [blame] | 94 | // Allow the extension to attach to about:blank and empty URLs. |
| 95 | if (url.is_empty() || url == "about:") |
| 96 | return true; |
| 97 | |
Dmitry Gozman | 00ba04e | 2018-09-18 00:39:27 | [diff] [blame] | 98 | if (url == content::kUnreachableWebDataURL) |
| 99 | return true; |
| 100 | |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 101 | // NOTE: The `debugger` permission implies all URLs access (and indicates |
| 102 | // such to the user), so we don't check explicit page access. However, we |
| 103 | // still need to check if it's an otherwise-restricted URL. |
| 104 | if (extension.permissions_data()->IsRestrictedUrl(url, error)) |
| 105 | return false; |
| 106 | |
Solomon Kinard | 3565a34 | 2021-04-07 22:29:27 | [diff] [blame] | 107 | // Policy blocked hosts supersede the `debugger` permission. |
| 108 | if (extension.permissions_data()->IsPolicyBlockedHost(url)) |
| 109 | return false; |
| 110 | |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 111 | if (url.SchemeIsFile() && !util::AllowFileAccess(extension.id(), profile)) { |
| 112 | *error = debugger_api_constants::kRestrictedError; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | return true; |
| 117 | } |
| 118 | |
Andrey Kosyakov | ad5d252 | 2020-01-07 22:42:49 | [diff] [blame] | 119 | constexpr char kBrowserTargetId[] = "browser"; |
| 120 | |
| 121 | constexpr char kPerfettoUIExtensionId[] = "lfmkphfpdbjijhpomgecfikhfohaoine"; |
| 122 | |
| 123 | bool ExtensionMayAttachToBrowser(const Extension& extension) { |
| 124 | return extension.id() == kPerfettoUIExtensionId; |
| 125 | } |
| 126 | |
Andrey Kosyakov | fa3dd0b | 2020-09-19 06:02:05 | [diff] [blame] | 127 | bool ExtensionMayAttachToWebContents(const Extension& extension, |
| 128 | WebContents& web_contents, |
| 129 | Profile* profile, |
| 130 | std::string* error) { |
| 131 | // This is *not* redundant to the checks below, as |
| 132 | // web_contents.GetLastCommittedURL() may be different from |
| 133 | // web_contents.GetMainFrame()->GetLastCommittedURL(), with the |
| 134 | // former being a 'virtual' URL as obtained from NavigationEntry. |
| 135 | if (!ExtensionMayAttachToURL(extension, web_contents.GetLastCommittedURL(), |
| 136 | profile, error)) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | for (content::RenderFrameHost* rfh : web_contents.GetAllFrames()) { |
| 141 | if (!ExtensionMayAttachToURL(extension, rfh->GetLastCommittedURL(), profile, |
| 142 | error)) |
| 143 | return false; |
| 144 | } |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | bool ExtensionMayAttachToAgentHost(const Extension& extension, |
| 149 | DevToolsAgentHost& agent_host, |
| 150 | Profile* profile, |
| 151 | std::string* error) { |
| 152 | if (WebContents* wc = agent_host.GetWebContents()) |
| 153 | return ExtensionMayAttachToWebContents(extension, *wc, profile, error); |
| 154 | |
| 155 | return ExtensionMayAttachToURL(extension, agent_host.GetURL(), profile, |
| 156 | error); |
| 157 | } |
| 158 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 159 | } // namespace |
| 160 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 161 | // ExtensionDevToolsClientHost ------------------------------------------------ |
| 162 | |
| 163 | using AttachedClientHosts = std::set<ExtensionDevToolsClientHost*>; |
| 164 | base::LazyInstance<AttachedClientHosts>::Leaky g_attached_client_hosts = |
| 165 | LAZY_INSTANCE_INITIALIZER; |
| 166 | |
| 167 | class ExtensionDevToolsClientHost : public content::DevToolsAgentHostClient, |
| 168 | public content::NotificationObserver, |
| 169 | public ExtensionRegistryObserver { |
| 170 | public: |
| 171 | ExtensionDevToolsClientHost(Profile* profile, |
| 172 | DevToolsAgentHost* agent_host, |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 173 | scoped_refptr<const Extension> extension, |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 174 | const Debuggee& debuggee); |
| 175 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame^] | 176 | ExtensionDevToolsClientHost(const ExtensionDevToolsClientHost&) = delete; |
| 177 | ExtensionDevToolsClientHost& operator=(const ExtensionDevToolsClientHost&) = |
| 178 | delete; |
| 179 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 180 | ~ExtensionDevToolsClientHost() override; |
| 181 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 182 | bool Attach(); |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 183 | const std::string& extension_id() { return extension_->id(); } |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 184 | DevToolsAgentHost* agent_host() { return agent_host_.get(); } |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 185 | void RespondDetachedToPendingRequests(); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 186 | void Close(); |
| 187 | void SendMessageToBackend(DebuggerSendCommandFunction* function, |
| 188 | const std::string& method, |
| 189 | SendCommand::Params::CommandParams* command_params); |
| 190 | |
| 191 | // Closes connection as terminated by the user. |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 192 | void InfoBarDestroyed(); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 193 | |
| 194 | // DevToolsAgentHostClient interface. |
Pavel Feldman | a344d93 | 2017-10-31 20:24:52 | [diff] [blame] | 195 | void AgentHostClosed(DevToolsAgentHost* agent_host) override; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 196 | void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
Johannes Henkel | 21e19401 | 2019-12-20 03:23:17 | [diff] [blame] | 197 | base::span<const uint8_t> message) override; |
Andrey Kosyakov | 0788b1d | 2019-12-10 04:28:11 | [diff] [blame] | 198 | bool MayAttachToURL(const GURL& url, bool is_webui) override; |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 199 | bool MayAttachToBrowser() override; |
Dmitry Gozman | a3023d2 | 2019-02-26 22:11:27 | [diff] [blame] | 200 | bool MayReadLocalFiles() override; |
| 201 | bool MayWriteLocalFiles() override; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 202 | |
| 203 | private: |
| 204 | using PendingRequests = |
| 205 | std::map<int, scoped_refptr<DebuggerSendCommandFunction>>; |
| 206 | |
| 207 | void SendDetachedEvent(); |
| 208 | |
| 209 | // content::NotificationObserver implementation. |
| 210 | void Observe(int type, |
| 211 | const content::NotificationSource& source, |
| 212 | const content::NotificationDetails& details) override; |
| 213 | |
| 214 | // ExtensionRegistryObserver implementation. |
| 215 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 216 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 217 | UnloadedExtensionReason reason) override; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 218 | |
| 219 | Profile* profile_; |
| 220 | scoped_refptr<DevToolsAgentHost> agent_host_; |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 221 | scoped_refptr<const Extension> extension_; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 222 | Debuggee debuggee_; |
| 223 | content::NotificationRegistrar registrar_; |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 224 | int last_request_id_ = 0; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 225 | PendingRequests pending_requests_; |
Peter Kasting | 7ba9440c | 2020-11-22 01:49:02 | [diff] [blame] | 226 | base::CallbackListSubscription subscription_; |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 227 | api::debugger::DetachReason detach_reason_ = |
| 228 | api::debugger::DETACH_REASON_TARGET_CLOSED; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 229 | |
| 230 | // Listen to extension unloaded notification. |
Sigurdur Asgeirsson | 2c442dc | 2021-03-25 13:41:20 | [diff] [blame] | 231 | base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver> |
| 232 | extension_registry_observation_{this}; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 233 | }; |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 234 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 235 | ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 236 | Profile* profile, |
| 237 | DevToolsAgentHost* agent_host, |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 238 | scoped_refptr<const Extension> extension, |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 239 | const Debuggee& debuggee) |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 240 | : profile_(profile), |
| 241 | agent_host_(agent_host), |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 242 | extension_(std::move(extension)) { |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 243 | CopyDebuggee(&debuggee_, debuggee); |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 244 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 245 | g_attached_client_hosts.Get().insert(this); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 246 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 247 | // ExtensionRegistryObserver listen extension unloaded and detach debugger |
| 248 | // from there. |
Sigurdur Asgeirsson | 2c442dc | 2021-03-25 13:41:20 | [diff] [blame] | 249 | extension_registry_observation_.Observe(ExtensionRegistry::Get(profile_)); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 250 | |
[email protected] | 02ff8ad | 2013-04-12 13:54:35 | [diff] [blame] | 251 | // RVH-based agents disconnect from their clients when the app is terminating |
| 252 | // but shared worker-based agents do not. |
| 253 | // Disconnect explicitly to make sure that |this| observer is not leaked. |
| 254 | registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| 255 | content::NotificationService::AllSources()); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 256 | } |
[email protected] | 02ff8ad | 2013-04-12 13:54:35 | [diff] [blame] | 257 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 258 | bool ExtensionDevToolsClientHost::Attach() { |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 259 | // Attach to debugger and tell it we are ready. |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 260 | if (!agent_host_->AttachClient(this)) |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 261 | return false; |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 262 | |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 263 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 264 | ::switches::kSilentDebuggerExtensionAPI)) { |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 265 | return true; |
[email protected] | bde1160 | 2012-05-04 19:05:42 | [diff] [blame] | 266 | } |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 267 | |
| 268 | // We allow policy-installed extensions to circumvent the normal |
| 269 | // infobar warning. See crbug.com/693621. |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 270 | if (Manifest::IsPolicyLocation(extension_->location())) |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 271 | return true; |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 272 | |
Peter Kasting | 8aa2b2b9 | 2020-05-09 01:36:55 | [diff] [blame] | 273 | subscription_ = ExtensionDevToolsInfoBarDelegate::Create( |
Peter Kasting | 03ec380a | 2020-05-08 08:03:56 | [diff] [blame] | 274 | extension_id(), extension_->name(), |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 275 | base::BindOnce(&ExtensionDevToolsClientHost::InfoBarDestroyed, |
| 276 | base::Unretained(this))); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 277 | return true; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { |
Ghazale Hosseinabadi | 301cdb4 | 2020-10-02 20:16:57 | [diff] [blame] | 281 | ExtensionDevToolsInfoBarDelegate::NotifyExtensionDetached(extension_id()); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 282 | g_attached_client_hosts.Get().erase(this); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 283 | } |
| 284 | |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 285 | // DevToolsAgentHostClient implementation. |
| 286 | void ExtensionDevToolsClientHost::AgentHostClosed( |
Pavel Feldman | a344d93 | 2017-10-31 20:24:52 | [diff] [blame] | 287 | DevToolsAgentHost* agent_host) { |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 288 | DCHECK(agent_host == agent_host_.get()); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 289 | RespondDetachedToPendingRequests(); |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 290 | SendDetachedEvent(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 291 | delete this; |
| 292 | } |
| 293 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 294 | void ExtensionDevToolsClientHost::Close() { |
pfeldman | fb8e747 | 2016-06-08 21:13:37 | [diff] [blame] | 295 | agent_host_->DetachClient(this); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 296 | delete this; |
| 297 | } |
| 298 | |
| 299 | void ExtensionDevToolsClientHost::SendMessageToBackend( |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame] | 300 | DebuggerSendCommandFunction* function, |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 301 | const std::string& method, |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 302 | SendCommand::Params::CommandParams* command_params) { |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 303 | base::DictionaryValue protocol_request; |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 304 | int request_id = ++last_request_id_; |
| 305 | pending_requests_[request_id] = function; |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 306 | protocol_request.SetInteger("id", request_id); |
| 307 | protocol_request.SetString("method", method); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 308 | if (command_params) { |
Song Fangzhen | 06c83d5 | 2021-06-24 03:51:27 | [diff] [blame] | 309 | protocol_request.SetKey("params", |
| 310 | command_params->additional_properties.Clone()); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 311 | } |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 312 | |
Johannes Henkel | 21e19401 | 2019-12-20 03:23:17 | [diff] [blame] | 313 | std::string json; |
| 314 | base::JSONWriter::Write(protocol_request, &json); |
| 315 | |
| 316 | agent_host_->DispatchProtocolMessage(this, |
| 317 | base::as_bytes(base::make_span(json))); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 318 | } |
| 319 | |
Peter Kasting | 0cdd929e | 2020-05-08 04:26:32 | [diff] [blame] | 320 | void ExtensionDevToolsClientHost::InfoBarDestroyed() { |
rdevlin.cronin | 00f1fc2 | 2015-04-06 17:19:18 | [diff] [blame] | 321 | detach_reason_ = api::debugger::DETACH_REASON_CANCELED_BY_USER; |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 322 | RespondDetachedToPendingRequests(); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 323 | SendDetachedEvent(); |
| 324 | Close(); |
[email protected] | 87d8a63 | 2012-09-20 13:40:39 | [diff] [blame] | 325 | } |
| 326 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 327 | void ExtensionDevToolsClientHost::RespondDetachedToPendingRequests() { |
| 328 | for (const auto& it : pending_requests_) |
| 329 | it.second->SendDetachedError(); |
| 330 | pending_requests_.clear(); |
| 331 | } |
| 332 | |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 333 | void ExtensionDevToolsClientHost::SendDetachedEvent() { |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 334 | if (!EventRouter::Get(profile_)) |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 335 | return; |
| 336 | |
Lei Zhang | 1f3ec979 | 2021-05-18 00:00:28 | [diff] [blame] | 337 | auto args(OnDetach::Create(debuggee_, detach_reason_)); |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 338 | auto event = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 339 | std::make_unique<Event>(events::DEBUGGER_ON_DETACH, OnDetach::kEventName, |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 340 | std::move(args), profile_); |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 341 | EventRouter::Get(profile_)->DispatchEventToExtension(extension_id(), |
| 342 | std::move(event)); |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 345 | void ExtensionDevToolsClientHost::OnExtensionUnloaded( |
| 346 | content::BrowserContext* browser_context, |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 347 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 348 | UnloadedExtensionReason reason) { |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 349 | if (extension->id() == extension_id()) |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 350 | Close(); |
| 351 | } |
| 352 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 353 | void ExtensionDevToolsClientHost::Observe( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 354 | int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 355 | const content::NotificationSource& source, |
| 356 | const content::NotificationDetails& details) { |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 357 | DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); |
| 358 | Close(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 359 | } |
| 360 | |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 361 | void ExtensionDevToolsClientHost::DispatchProtocolMessage( |
Johannes Henkel | 21e19401 | 2019-12-20 03:23:17 | [diff] [blame] | 362 | DevToolsAgentHost* agent_host, |
| 363 | base::span<const uint8_t> message) { |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 364 | DCHECK(agent_host == agent_host_.get()); |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 365 | if (!EventRouter::Get(profile_)) |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 366 | return; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 367 | |
Johannes Henkel | 21e19401 | 2019-12-20 03:23:17 | [diff] [blame] | 368 | base::StringPiece message_str(reinterpret_cast<const char*>(message.data()), |
| 369 | message.size()); |
Lei Zhang | 582ecd1 | 2019-02-13 20:28:54 | [diff] [blame] | 370 | std::unique_ptr<base::Value> result = base::JSONReader::ReadDeprecated( |
Johannes Henkel | 21e19401 | 2019-12-20 03:23:17 | [diff] [blame] | 371 | message_str, base::JSON_REPLACE_INVALID_CHARACTERS); |
Joey Arhar | 34f1026e | 2019-01-16 06:07:43 | [diff] [blame] | 372 | if (!result || !result->is_dict()) { |
Johannes Henkel | 21e19401 | 2019-12-20 03:23:17 | [diff] [blame] | 373 | LOG(ERROR) << "Tried to send invalid message to extension: " << message_str; |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 374 | return; |
Joey Arhar | 34f1026e | 2019-01-16 06:07:43 | [diff] [blame] | 375 | } |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 376 | base::DictionaryValue* dictionary = |
| 377 | static_cast<base::DictionaryValue*>(result.get()); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 378 | |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 379 | int id; |
| 380 | if (!dictionary->GetInteger("id", &id)) { |
| 381 | std::string method_name; |
| 382 | if (!dictionary->GetString("method", &method_name)) |
[email protected] | 7d713ec | 2011-04-01 14:22:02 | [diff] [blame] | 383 | return; |
| 384 | |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 385 | OnEvent::Params params; |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 386 | base::DictionaryValue* params_value; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 387 | if (dictionary->GetDictionary("params", ¶ms_value)) |
| 388 | params.additional_properties.Swap(params_value); |
| 389 | |
Lei Zhang | 1f3ec979 | 2021-05-18 00:00:28 | [diff] [blame] | 390 | auto args(OnEvent::Create(debuggee_, method_name, params)); |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 391 | auto event = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 392 | std::make_unique<Event>(events::DEBUGGER_ON_EVENT, OnEvent::kEventName, |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 393 | std::move(args), profile_); |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 394 | EventRouter::Get(profile_)->DispatchEventToExtension(extension_id(), |
| 395 | std::move(event)); |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 396 | } else { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 397 | auto it = pending_requests_.find(id); |
| 398 | if (it == pending_requests_.end()) |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 399 | return; |
| 400 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 401 | it->second->SendResponseBody(dictionary); |
| 402 | pending_requests_.erase(it); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 403 | } |
| 404 | } |
| 405 | |
Andrey Kosyakov | 0788b1d | 2019-12-10 04:28:11 | [diff] [blame] | 406 | bool ExtensionDevToolsClientHost::MayAttachToURL(const GURL& url, |
| 407 | bool is_webui) { |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 408 | if (is_webui) |
| 409 | return false; |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 410 | std::string error; |
Andrey Kosyakov | 1ca6a4e | 2020-11-02 19:26:33 | [diff] [blame] | 411 | if (!ExtensionMayAttachToURL(*extension_, url, profile_, &error)) |
| 412 | return false; |
| 413 | // For nested URLs, make sure ExtensionMayAttachToURL() allows both |
| 414 | // the outer and the inner URLs. |
| 415 | if (url.inner_url() && !ExtensionMayAttachToURL(*extension_, *url.inner_url(), |
| 416 | profile_, &error)) { |
| 417 | return false; |
| 418 | } |
| 419 | return true; |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 420 | } |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 421 | |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 422 | bool ExtensionDevToolsClientHost::MayAttachToBrowser() { |
Andrey Kosyakov | ad5d252 | 2020-01-07 22:42:49 | [diff] [blame] | 423 | return ExtensionMayAttachToBrowser(*extension_); |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 424 | } |
| 425 | |
Dmitry Gozman | a3023d2 | 2019-02-26 22:11:27 | [diff] [blame] | 426 | bool ExtensionDevToolsClientHost::MayReadLocalFiles() { |
| 427 | return util::AllowFileAccess(extension_->id(), profile_); |
| 428 | } |
| 429 | |
| 430 | bool ExtensionDevToolsClientHost::MayWriteLocalFiles() { |
Dmitry Gozman | c71d804 | 2018-10-09 18:40:45 | [diff] [blame] | 431 | return false; |
| 432 | } |
| 433 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 434 | // DebuggerFunction ----------------------------------------------------------- |
| 435 | |
Lukasz Anforowicz | c695e53 | 2020-06-09 02:09:45 | [diff] [blame] | 436 | DebuggerFunction::DebuggerFunction() : client_host_(nullptr) {} |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 437 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 438 | DebuggerFunction::~DebuggerFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 439 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 440 | std::string DebuggerFunction::FormatErrorMessage(const std::string& format) { |
| 441 | if (debuggee_.tab_id) { |
| 442 | return ErrorUtils::FormatErrorMessage( |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 443 | format, debugger_api_constants::kTabTargetType, |
Raul Tambre | fff51b75 | 2019-02-04 13:09:47 | [diff] [blame] | 444 | base::NumberToString(*debuggee_.tab_id)); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 445 | } |
| 446 | if (debuggee_.extension_id) { |
| 447 | return ErrorUtils::FormatErrorMessage( |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 448 | format, debugger_api_constants::kBackgroundPageTargetType, |
| 449 | *debuggee_.extension_id); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | return ErrorUtils::FormatErrorMessage( |
| 453 | format, debugger_api_constants::kOpaqueTargetType, *debuggee_.target_id); |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 454 | } |
| 455 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 456 | bool DebuggerFunction::InitAgentHost(std::string* error) { |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 457 | if (debuggee_.tab_id) { |
Istiaque Ahmed | 406b918 | 2019-07-26 06:28:33 | [diff] [blame] | 458 | WebContents* web_contents = nullptr; |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 459 | bool result = ExtensionTabUtil::GetTabById( |
| 460 | *debuggee_.tab_id, browser_context(), include_incognito_information(), |
| 461 | &web_contents); |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 462 | if (result && web_contents) { |
Andrey Kosyakov | fa3dd0b | 2020-09-19 06:02:05 | [diff] [blame] | 463 | if (!ExtensionMayAttachToWebContents( |
| 464 | *extension(), *web_contents, |
Devlin Cronin | 6ac77aa | 2020-08-05 03:57:37 | [diff] [blame] | 465 | Profile::FromBrowserContext(browser_context()), error)) { |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 466 | return false; |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 467 | } |
Devlin Cronin | 4dafc317 | 2018-06-22 23:16:31 | [diff] [blame] | 468 | |
[email protected] | 02a67fd | 2014-03-28 15:29:55 | [diff] [blame] | 469 | agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents); |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 470 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 471 | } else if (debuggee_.extension_id) { |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 472 | ExtensionHost* extension_host = |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 473 | ProcessManager::Get(browser_context()) |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 474 | ->GetBackgroundHostForExtension(*debuggee_.extension_id); |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 475 | if (extension_host) { |
Solomon Kinard | 3565a34 | 2021-04-07 22:29:27 | [diff] [blame] | 476 | const GURL& url = extension_host->GetLastCommittedURL(); |
| 477 | if (extension()->permissions_data()->IsRestrictedUrl(url, error) || |
| 478 | extension()->permissions_data()->IsPolicyBlockedHost(url)) { |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 479 | return false; |
| 480 | } |
[email protected] | b3f957e6 | 2014-08-08 10:09:02 | [diff] [blame] | 481 | agent_host_ = |
| 482 | DevToolsAgentHost::GetOrCreateFor(extension_host->host_contents()); |
[email protected] | 30e246b | 2013-03-28 17:08:48 | [diff] [blame] | 483 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 484 | } else if (debuggee_.target_id) { |
Andrey Kosyakov | fa3dd0b | 2020-09-19 06:02:05 | [diff] [blame] | 485 | scoped_refptr<DevToolsAgentHost> agent_host = |
| 486 | DevToolsAgentHost::GetForId(*debuggee_.target_id); |
| 487 | if (agent_host) { |
| 488 | if (!ExtensionMayAttachToAgentHost( |
| 489 | *extension(), *agent_host, |
| 490 | Profile::FromBrowserContext(browser_context()), error)) { |
rob | 409bf9d6 | 2015-02-10 23:37:07 | [diff] [blame] | 491 | return false; |
| 492 | } |
Andrey Kosyakov | fa3dd0b | 2020-09-19 06:02:05 | [diff] [blame] | 493 | agent_host_ = std::move(agent_host); |
Andrey Kosyakov | ad5d252 | 2020-01-07 22:42:49 | [diff] [blame] | 494 | } else if (*debuggee_.target_id == kBrowserTargetId && |
| 495 | ExtensionMayAttachToBrowser(*extension())) { |
| 496 | // TODO(caseq): get rid of the below code, browser agent host should |
| 497 | // really be a singleton. |
| 498 | // Re-use existing browser agent hosts. |
| 499 | const std::string& extension_id = extension()->id(); |
| 500 | AttachedClientHosts& hosts = g_attached_client_hosts.Get(); |
| 501 | auto it = std::find_if( |
| 502 | hosts.begin(), hosts.end(), |
| 503 | [&extension_id](ExtensionDevToolsClientHost* client_host) { |
| 504 | return client_host->extension_id() == extension_id && |
| 505 | client_host->agent_host() && |
| 506 | client_host->agent_host()->GetType() == |
| 507 | DevToolsAgentHost::kTypeBrowser; |
| 508 | }); |
| 509 | agent_host_ = it != hosts.end() |
| 510 | ? (*it)->agent_host() |
| 511 | : DevToolsAgentHost::CreateForBrowser( |
| 512 | nullptr /* tethering_task_runner */, |
| 513 | DevToolsAgentHost::CreateServerSocketCallback()); |
rob | 409bf9d6 | 2015-02-10 23:37:07 | [diff] [blame] | 514 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 515 | } else { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 516 | *error = debugger_api_constants::kInvalidTargetError; |
[email protected] | 4e023cd | 2013-03-28 15:04:12 | [diff] [blame] | 517 | return false; |
| 518 | } |
[email protected] | 30e246b | 2013-03-28 17:08:48 | [diff] [blame] | 519 | |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 520 | if (!agent_host_.get()) { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 521 | *error = FormatErrorMessage(debugger_api_constants::kNoTargetError); |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 522 | return false; |
| 523 | } |
| 524 | return true; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 525 | } |
| 526 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 527 | bool DebuggerFunction::InitClientHost(std::string* error) { |
| 528 | if (!InitAgentHost(error)) |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 529 | return false; |
| 530 | |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 531 | client_host_ = FindClientHost(); |
| 532 | if (!client_host_) { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 533 | *error = FormatErrorMessage(debugger_api_constants::kNotAttachedError); |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 534 | return false; |
| 535 | } |
| 536 | |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | ExtensionDevToolsClientHost* DebuggerFunction::FindClientHost() { |
| 541 | if (!agent_host_.get()) |
| 542 | return nullptr; |
| 543 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 544 | const std::string& extension_id = extension()->id(); |
| 545 | DevToolsAgentHost* agent_host = agent_host_.get(); |
| 546 | AttachedClientHosts& hosts = g_attached_client_hosts.Get(); |
jdoerrie | 9be0b2b | 2018-10-05 10:10:05 | [diff] [blame] | 547 | auto it = std::find_if( |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 548 | hosts.begin(), hosts.end(), |
| 549 | [&agent_host, &extension_id](ExtensionDevToolsClientHost* client_host) { |
| 550 | return client_host->agent_host() == agent_host && |
| 551 | client_host->extension_id() == extension_id; |
| 552 | }); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 553 | |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 554 | return it == hosts.end() ? nullptr : *it; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 555 | } |
| 556 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 557 | // DebuggerAttachFunction ----------------------------------------------------- |
| 558 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 559 | DebuggerAttachFunction::DebuggerAttachFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 560 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 561 | DebuggerAttachFunction::~DebuggerAttachFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 562 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 563 | ExtensionFunction::ResponseAction DebuggerAttachFunction::Run() { |
Alex Turner | a6dba25 | 2021-08-18 01:29:05 | [diff] [blame] | 564 | std::unique_ptr<Attach::Params> params(Attach::Params::Create(args())); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 565 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 566 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 567 | CopyDebuggee(&debuggee_, params->target); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 568 | std::string error; |
| 569 | if (!InitAgentHost(&error)) |
Devlin Cronin | 12b40764 | 2020-04-24 20:24:22 | [diff] [blame] | 570 | return RespondNow(Error(std::move(error))); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 571 | |
dgozman | 102fee9 | 2015-04-20 15:45:46 | [diff] [blame] | 572 | if (!DevToolsAgentHost::IsSupportedProtocolVersion( |
[email protected] | be3577e | 2013-06-26 21:42:33 | [diff] [blame] | 573 | params->required_version)) { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 574 | return RespondNow(Error(ErrorUtils::FormatErrorMessage( |
oscarjohansson | 7716401 | 2018-06-11 07:00:11 | [diff] [blame] | 575 | debugger_api_constants::kProtocolVersionNotSupportedError, |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 576 | params->required_version))); |
[email protected] | c7580b6 | 2011-10-26 08:46:30 | [diff] [blame] | 577 | } |
| 578 | |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 579 | if (FindClientHost()) { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 580 | return RespondNow(Error( |
| 581 | FormatErrorMessage(debugger_api_constants::kAlreadyAttachedError))); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 582 | } |
| 583 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 584 | auto host = std::make_unique<ExtensionDevToolsClientHost>( |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 585 | Profile::FromBrowserContext(browser_context()), agent_host_.get(), |
| 586 | extension(), debuggee_); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 587 | |
| 588 | if (!host->Attach()) { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 589 | return RespondNow(Error(debugger_api_constants::kRestrictedError)); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | host.release(); // An attached client host manages its own lifetime. |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 593 | return RespondNow(NoArguments()); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 594 | } |
| 595 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 596 | // DebuggerDetachFunction ----------------------------------------------------- |
| 597 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 598 | DebuggerDetachFunction::DebuggerDetachFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 599 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 600 | DebuggerDetachFunction::~DebuggerDetachFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 601 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 602 | ExtensionFunction::ResponseAction DebuggerDetachFunction::Run() { |
Alex Turner | a6dba25 | 2021-08-18 01:29:05 | [diff] [blame] | 603 | std::unique_ptr<Detach::Params> params(Detach::Params::Create(args())); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 604 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 605 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 606 | CopyDebuggee(&debuggee_, params->target); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 607 | std::string error; |
| 608 | if (!InitClientHost(&error)) |
Devlin Cronin | 12b40764 | 2020-04-24 20:24:22 | [diff] [blame] | 609 | return RespondNow(Error(std::move(error))); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 610 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 611 | client_host_->RespondDetachedToPendingRequests(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 612 | client_host_->Close(); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 613 | return RespondNow(NoArguments()); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 616 | // DebuggerSendCommandFunction ------------------------------------------------ |
| 617 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 618 | DebuggerSendCommandFunction::DebuggerSendCommandFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 619 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 620 | DebuggerSendCommandFunction::~DebuggerSendCommandFunction() = default; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 621 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 622 | ExtensionFunction::ResponseAction DebuggerSendCommandFunction::Run() { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 623 | std::unique_ptr<SendCommand::Params> params( |
Alex Turner | a6dba25 | 2021-08-18 01:29:05 | [diff] [blame] | 624 | SendCommand::Params::Create(args())); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 625 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 626 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 627 | CopyDebuggee(&debuggee_, params->target); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 628 | std::string error; |
| 629 | if (!InitClientHost(&error)) |
Devlin Cronin | 12b40764 | 2020-04-24 20:24:22 | [diff] [blame] | 630 | return RespondNow(Error(std::move(error))); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 631 | |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 632 | client_host_->SendMessageToBackend(this, params->method, |
| 633 | params->command_params.get()); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 634 | if (did_respond()) |
| 635 | return AlreadyResponded(); |
| 636 | return RespondLater(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 637 | } |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 638 | |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame] | 639 | void DebuggerSendCommandFunction::SendResponseBody( |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 640 | base::DictionaryValue* response) { |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 641 | base::Value* error_body; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 642 | if (response->Get("error", &error_body)) { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 643 | std::string error; |
| 644 | base::JSONWriter::Write(*error_body, &error); |
Devlin Cronin | 12b40764 | 2020-04-24 20:24:22 | [diff] [blame] | 645 | Respond(Error(std::move(error))); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 646 | return; |
| 647 | } |
| 648 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 649 | base::DictionaryValue* result_body; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 650 | SendCommand::Results::Result result; |
| 651 | if (response->GetDictionary("result", &result_body)) |
| 652 | result.additional_properties.Swap(result_body); |
| 653 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 654 | Respond(ArgumentList(SendCommand::Results::Create(result))); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 655 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 656 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 657 | void DebuggerSendCommandFunction::SendDetachedError() { |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 658 | Respond(Error(debugger_api_constants::kDetachedWhileHandlingError)); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame] | 659 | } |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 660 | |
| 661 | // DebuggerGetTargetsFunction ------------------------------------------------- |
| 662 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 663 | namespace { |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 664 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 665 | const char kTargetIdField[] = "id"; |
| 666 | const char kTargetTypeField[] = "type"; |
| 667 | const char kTargetTitleField[] = "title"; |
| 668 | const char kTargetAttachedField[] = "attached"; |
| 669 | const char kTargetUrlField[] = "url"; |
| 670 | const char kTargetFaviconUrlField[] = "faviconUrl"; |
[email protected] | f80e0be6 | 2013-07-17 14:17:23 | [diff] [blame] | 671 | const char kTargetTabIdField[] = "tabId"; |
| 672 | const char kTargetExtensionIdField[] = "extensionId"; |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 673 | const char kTargetTypePage[] = "page"; |
| 674 | const char kTargetTypeBackgroundPage[] = "background_page"; |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 675 | const char kTargetTypeWorker[] = "worker"; |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 676 | const char kTargetTypeOther[] = "other"; |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 677 | |
dcheng | f1950200 | 2016-09-14 15:18:18 | [diff] [blame] | 678 | std::unique_ptr<base::DictionaryValue> SerializeTarget( |
| 679 | scoped_refptr<DevToolsAgentHost> host) { |
| 680 | std::unique_ptr<base::DictionaryValue> dictionary( |
| 681 | new base::DictionaryValue()); |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 682 | dictionary->SetString(kTargetIdField, host->GetId()); |
| 683 | dictionary->SetString(kTargetTitleField, host->GetTitle()); |
| 684 | dictionary->SetBoolean(kTargetAttachedField, host->IsAttached()); |
| 685 | dictionary->SetString(kTargetUrlField, host->GetURL().spec()); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 686 | |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 687 | std::string type = host->GetType(); |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 688 | std::string target_type = kTargetTypeOther; |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 689 | if (type == DevToolsAgentHost::kTypePage) { |
| 690 | int tab_id = |
| 691 | extensions::ExtensionTabUtil::GetTabId(host->GetWebContents()); |
| 692 | dictionary->SetInteger(kTargetTabIdField, tab_id); |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 693 | target_type = kTargetTypePage; |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 694 | } else if (type == ChromeDevToolsManagerDelegate::kTypeBackgroundPage) { |
| 695 | dictionary->SetString(kTargetExtensionIdField, host->GetURL().host()); |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 696 | target_type = kTargetTypeBackgroundPage; |
| 697 | } else if (type == DevToolsAgentHost::kTypeServiceWorker || |
| 698 | type == DevToolsAgentHost::kTypeSharedWorker) { |
| 699 | target_type = kTargetTypeWorker; |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 700 | } |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 701 | |
Dmitry Gozman | 2731b56 | 2018-09-10 23:19:02 | [diff] [blame] | 702 | dictionary->SetString(kTargetTypeField, target_type); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 703 | |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 704 | GURL favicon_url = host->GetFaviconURL(); |
[email protected] | 06d503f | 2013-10-29 17:29:37 | [diff] [blame] | 705 | if (favicon_url.is_valid()) |
| 706 | dictionary->SetString(kTargetFaviconUrlField, favicon_url.spec()); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 707 | |
| 708 | return dictionary; |
| 709 | } |
| 710 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 711 | } // namespace |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 712 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 713 | DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() = default; |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 714 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 715 | DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() = default; |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 716 | |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 717 | ExtensionFunction::ResponseAction DebuggerGetTargetsFunction::Run() { |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 718 | content::DevToolsAgentHost::List list = DevToolsAgentHost::GetOrCreateAll(); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 719 | std::unique_ptr<base::ListValue> result(new base::ListValue()); |
Anand K. Mistry | d3d86f5 | 2020-03-03 23:23:33 | [diff] [blame] | 720 | for (size_t i = 0; i < list.size(); ++i) |
| 721 | result->Append(SerializeTarget(list[i])); |
| 722 | |
Lei Zhang | 27a5509 | 2020-10-23 22:07:05 | [diff] [blame] | 723 | return RespondNow( |
| 724 | OneArgument(base::Value::FromUniquePtrValue(std::move(result)))); |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 725 | } |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 726 | |
| 727 | } // namespace extensions |