[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 | |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 16 | #include "base/command_line.h" |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 17 | #include "base/json/json_reader.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 18 | #include "base/json/json_writer.h" |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 19 | #include "base/lazy_instance.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 20 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 21 | #include "base/memory/singleton.h" |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 22 | #include "base/scoped_observer.h" |
[email protected] | 06d503f | 2013-10-29 17:29:37 | [diff] [blame] | 23 | #include "base/stl_util.h" |
[email protected] | 3ea1b18 | 2013-02-08 22:38:41 | [diff] [blame] | 24 | #include "base/strings/string_number_conversions.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 25 | #include "base/values.h" |
[email protected] | 49a01e64 | 2013-07-12 00:29:45 | [diff] [blame] | 26 | #include "chrome/browser/chrome_notification_types.h" |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 27 | #include "chrome/browser/devtools/chrome_devtools_manager_delegate.h" |
[email protected] | 1eb8f2f | 2012-07-12 02:04:49 | [diff] [blame] | 28 | #include "chrome/browser/extensions/api/debugger/debugger_api_constants.h" |
Peter Kasting | 37db76f8 | 2018-01-06 02:39:16 | [diff] [blame] | 29 | #include "chrome/browser/extensions/api/debugger/extension_dev_tools_infobar.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 30 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | ac84431b | 2011-09-27 17:26:11 | [diff] [blame] | 31 | #include "chrome/browser/extensions/extension_tab_util.h" |
[email protected] | 4a8adfa0 | 2013-03-19 22:37:46 | [diff] [blame] | 32 | #include "chrome/browser/infobars/infobar_service.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 33 | #include "chrome/browser/profiles/profile.h" |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 34 | #include "chrome/browser/ui/browser.h" |
[email protected] | 863f70a | 2012-01-27 02:05:50 | [diff] [blame] | 35 | #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 36 | #include "chrome/common/chrome_switches.h" |
[email protected] | 051655ad | 2014-04-18 15:09:41 | [diff] [blame] | 37 | #include "components/infobars/core/infobar.h" |
dgozman | cb088d0 | 2015-04-28 11:57:45 | [diff] [blame] | 38 | #include "content/public/browser/browser_thread.h" |
[email protected] | 98f6611 | 2012-12-25 12:59:36 | [diff] [blame] | 39 | #include "content/public/browser/devtools_agent_host.h" |
[email protected] | 1c3bd20 | 2011-12-23 05:48:24 | [diff] [blame] | 40 | #include "content/public/browser/notification_service.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 41 | #include "content/public/browser/notification_source.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 42 | #include "content/public/browser/render_process_host.h" |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 43 | #include "content/public/browser/render_widget_host.h" |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 44 | #include "content/public/browser/web_contents.h" |
[email protected] | 744c2a2 | 2012-03-15 18:42:04 | [diff] [blame] | 45 | #include "content/public/common/content_client.h" |
[email protected] | 3b366ae | 2013-05-17 21:16:58 | [diff] [blame] | 46 | #include "content/public/common/url_utils.h" |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 47 | #include "extensions/browser/event_router.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 48 | #include "extensions/browser/extension_host.h" |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 49 | #include "extensions/browser/extension_registry.h" |
| 50 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 51 | #include "extensions/common/constants.h" |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 52 | #include "extensions/common/error_utils.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 53 | #include "extensions/common/extension.h" |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 54 | #include "extensions/common/manifest_constants.h" |
| 55 | #include "extensions/common/permissions/permissions_data.h" |
| 56 | #include "extensions/common/switches.h" |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 57 | |
[email protected] | 0e12d7d | 2011-12-01 16:21:44 | [diff] [blame] | 58 | using content::DevToolsAgentHost; |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 59 | using content::RenderProcessHost; |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 60 | using content::RenderWidgetHost; |
[email protected] | 7320a44e | 2012-05-22 17:01:11 | [diff] [blame] | 61 | using content::WebContents; |
[email protected] | 0e12d7d | 2011-12-01 16:21:44 | [diff] [blame] | 62 | |
[email protected] | 1eb8f2f | 2012-07-12 02:04:49 | [diff] [blame] | 63 | namespace keys = debugger_api_constants; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 64 | namespace Attach = extensions::api::debugger::Attach; |
| 65 | namespace Detach = extensions::api::debugger::Detach; |
| 66 | namespace OnDetach = extensions::api::debugger::OnDetach; |
| 67 | namespace OnEvent = extensions::api::debugger::OnEvent; |
| 68 | namespace SendCommand = extensions::api::debugger::SendCommand; |
[email protected] | ef9572e | 2012-01-04 22:14:12 | [diff] [blame] | 69 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 70 | namespace extensions { |
| 71 | class ExtensionRegistry; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 72 | class ExtensionDevToolsClientHost; |
[email protected] | 19bcf9d | 2013-07-18 22:00:59 | [diff] [blame] | 73 | |
| 74 | namespace { |
| 75 | |
| 76 | // Helpers -------------------------------------------------------------------- |
| 77 | |
| 78 | void CopyDebuggee(Debuggee* dst, const Debuggee& src) { |
| 79 | if (src.tab_id) |
| 80 | dst->tab_id.reset(new int(*src.tab_id)); |
| 81 | if (src.extension_id) |
| 82 | dst->extension_id.reset(new std::string(*src.extension_id)); |
| 83 | if (src.target_id) |
| 84 | dst->target_id.reset(new std::string(*src.target_id)); |
| 85 | } |
| 86 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 87 | } // namespace |
| 88 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 89 | // ExtensionDevToolsClientHost ------------------------------------------------ |
| 90 | |
| 91 | using AttachedClientHosts = std::set<ExtensionDevToolsClientHost*>; |
| 92 | base::LazyInstance<AttachedClientHosts>::Leaky g_attached_client_hosts = |
| 93 | LAZY_INSTANCE_INITIALIZER; |
| 94 | |
| 95 | class ExtensionDevToolsClientHost : public content::DevToolsAgentHostClient, |
| 96 | public content::NotificationObserver, |
| 97 | public ExtensionRegistryObserver { |
| 98 | public: |
| 99 | ExtensionDevToolsClientHost(Profile* profile, |
| 100 | DevToolsAgentHost* agent_host, |
| 101 | const std::string& extension_id, |
| 102 | const std::string& extension_name, |
| 103 | const Debuggee& debuggee); |
| 104 | |
| 105 | ~ExtensionDevToolsClientHost() override; |
| 106 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 107 | bool Attach(); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 108 | const std::string& extension_id() { return extension_id_; } |
| 109 | DevToolsAgentHost* agent_host() { return agent_host_.get(); } |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 110 | void RespondDetachedToPendingRequests(); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 111 | void Close(); |
| 112 | void SendMessageToBackend(DebuggerSendCommandFunction* function, |
| 113 | const std::string& method, |
| 114 | SendCommand::Params::CommandParams* command_params); |
| 115 | |
| 116 | // Closes connection as terminated by the user. |
| 117 | void InfoBarDismissed(); |
| 118 | |
| 119 | // DevToolsAgentHostClient interface. |
Pavel Feldman | a344d93 | 2017-10-31 20:24:52 | [diff] [blame] | 120 | void AgentHostClosed(DevToolsAgentHost* agent_host) override; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 121 | void DispatchProtocolMessage(DevToolsAgentHost* agent_host, |
| 122 | const std::string& message) override; |
| 123 | |
| 124 | private: |
| 125 | using PendingRequests = |
| 126 | std::map<int, scoped_refptr<DebuggerSendCommandFunction>>; |
| 127 | |
| 128 | void SendDetachedEvent(); |
| 129 | |
| 130 | // content::NotificationObserver implementation. |
| 131 | void Observe(int type, |
| 132 | const content::NotificationSource& source, |
| 133 | const content::NotificationDetails& details) override; |
| 134 | |
| 135 | // ExtensionRegistryObserver implementation. |
| 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; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 139 | |
| 140 | Profile* profile_; |
| 141 | scoped_refptr<DevToolsAgentHost> agent_host_; |
| 142 | std::string extension_id_; |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 143 | std::string extension_name_; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 144 | Debuggee debuggee_; |
| 145 | content::NotificationRegistrar registrar_; |
| 146 | int last_request_id_; |
| 147 | PendingRequests pending_requests_; |
dgozman | 798856d | 2015-11-19 02:27:22 | [diff] [blame] | 148 | ExtensionDevToolsInfoBar* infobar_; |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 149 | api::debugger::DetachReason detach_reason_; |
| 150 | |
| 151 | // Listen to extension unloaded notification. |
| 152 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 153 | extension_registry_observer_; |
| 154 | |
| 155 | DISALLOW_COPY_AND_ASSIGN(ExtensionDevToolsClientHost); |
| 156 | }; |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 157 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 158 | ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 159 | Profile* profile, |
| 160 | DevToolsAgentHost* agent_host, |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 161 | const std::string& extension_id, |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 162 | const std::string& extension_name, |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 163 | const Debuggee& debuggee) |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 164 | : profile_(profile), |
| 165 | agent_host_(agent_host), |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 166 | extension_id_(extension_id), |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 167 | extension_name_(extension_name), |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 168 | last_request_id_(0), |
dgozman | 798856d | 2015-11-19 02:27:22 | [diff] [blame] | 169 | infobar_(nullptr), |
rdevlin.cronin | 00f1fc2 | 2015-04-06 17:19:18 | [diff] [blame] | 170 | detach_reason_(api::debugger::DETACH_REASON_TARGET_CLOSED), |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 171 | extension_registry_observer_(this) { |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 172 | CopyDebuggee(&debuggee_, debuggee); |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 173 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 174 | g_attached_client_hosts.Get().insert(this); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 175 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 176 | // ExtensionRegistryObserver listen extension unloaded and detach debugger |
| 177 | // from there. |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 178 | extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 179 | |
[email protected] | 02ff8ad | 2013-04-12 13:54:35 | [diff] [blame] | 180 | // RVH-based agents disconnect from their clients when the app is terminating |
| 181 | // but shared worker-based agents do not. |
| 182 | // Disconnect explicitly to make sure that |this| observer is not leaked. |
| 183 | registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| 184 | content::NotificationService::AllSources()); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 185 | } |
[email protected] | 02ff8ad | 2013-04-12 13:54:35 | [diff] [blame] | 186 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 187 | bool ExtensionDevToolsClientHost::Attach() { |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 188 | // Attach to debugger and tell it we are ready. |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 189 | if (!agent_host_->AttachRestrictedClient(this)) |
| 190 | return false; |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 191 | |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 192 | if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 193 | ::switches::kSilentDebuggerExtensionAPI)) { |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 194 | return true; |
[email protected] | bde1160 | 2012-05-04 19:05:42 | [diff] [blame] | 195 | } |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 196 | |
| 197 | // We allow policy-installed extensions to circumvent the normal |
| 198 | // infobar warning. See crbug.com/693621. |
| 199 | const Extension* extension = |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 200 | ExtensionRegistry::Get(profile_)->enabled_extensions().GetByID( |
| 201 | extension_id_); |
| 202 | // TODO(dgozman): null-checking |extension| below is sketchy. |
| 203 | // We probably should not allow debugging in this case. Or maybe |
| 204 | // it's never null? |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 205 | if (extension && Manifest::IsPolicyLocation(extension->location())) |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 206 | return true; |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 207 | |
| 208 | infobar_ = ExtensionDevToolsInfoBar::Create( |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 209 | extension_id_, extension_name_, this, |
pfeldman | c64e1a6 | 2017-02-24 01:22:50 | [diff] [blame] | 210 | base::Bind(&ExtensionDevToolsClientHost::InfoBarDismissed, |
| 211 | base::Unretained(this))); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 212 | return true; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | ExtensionDevToolsClientHost::~ExtensionDevToolsClientHost() { |
pfeldman | d3a885c | 2015-10-22 23:28:26 | [diff] [blame] | 216 | if (infobar_) |
dgozman | 798856d | 2015-11-19 02:27:22 | [diff] [blame] | 217 | infobar_->Remove(this); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 218 | g_attached_client_hosts.Get().erase(this); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 221 | // DevToolsAgentHostClient implementation. |
| 222 | void ExtensionDevToolsClientHost::AgentHostClosed( |
Pavel Feldman | a344d93 | 2017-10-31 20:24:52 | [diff] [blame] | 223 | DevToolsAgentHost* agent_host) { |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 224 | DCHECK(agent_host == agent_host_.get()); |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 225 | RespondDetachedToPendingRequests(); |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 226 | SendDetachedEvent(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 227 | delete this; |
| 228 | } |
| 229 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 230 | void ExtensionDevToolsClientHost::Close() { |
pfeldman | fb8e747 | 2016-06-08 21:13:37 | [diff] [blame] | 231 | agent_host_->DetachClient(this); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 232 | delete this; |
| 233 | } |
| 234 | |
| 235 | void ExtensionDevToolsClientHost::SendMessageToBackend( |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame] | 236 | DebuggerSendCommandFunction* function, |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 237 | const std::string& method, |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 238 | SendCommand::Params::CommandParams* command_params) { |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 239 | base::DictionaryValue protocol_request; |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 240 | int request_id = ++last_request_id_; |
| 241 | pending_requests_[request_id] = function; |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 242 | protocol_request.SetInteger("id", request_id); |
| 243 | protocol_request.SetString("method", method); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 244 | if (command_params) { |
vabr | 9984ea6 | 2017-04-10 11:33:49 | [diff] [blame] | 245 | protocol_request.Set( |
| 246 | "params", command_params->additional_properties.CreateDeepCopy()); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 247 | } |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 248 | |
| 249 | std::string json_args; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 250 | base::JSONWriter::Write(protocol_request, &json_args); |
pfeldman | fb8e747 | 2016-06-08 21:13:37 | [diff] [blame] | 251 | agent_host_->DispatchProtocolMessage(this, json_args); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 252 | } |
| 253 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 254 | void ExtensionDevToolsClientHost::InfoBarDismissed() { |
rdevlin.cronin | 00f1fc2 | 2015-04-06 17:19:18 | [diff] [blame] | 255 | detach_reason_ = api::debugger::DETACH_REASON_CANCELED_BY_USER; |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 256 | RespondDetachedToPendingRequests(); |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 257 | SendDetachedEvent(); |
| 258 | Close(); |
[email protected] | 87d8a63 | 2012-09-20 13:40:39 | [diff] [blame] | 259 | } |
| 260 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 261 | void ExtensionDevToolsClientHost::RespondDetachedToPendingRequests() { |
| 262 | for (const auto& it : pending_requests_) |
| 263 | it.second->SendDetachedError(); |
| 264 | pending_requests_.clear(); |
| 265 | } |
| 266 | |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 267 | void ExtensionDevToolsClientHost::SendDetachedEvent() { |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 268 | if (!EventRouter::Get(profile_)) |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 269 | return; |
| 270 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 271 | std::unique_ptr<base::ListValue> args( |
| 272 | OnDetach::Create(debuggee_, detach_reason_)); |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 273 | auto event = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 274 | std::make_unique<Event>(events::DEBUGGER_ON_DETACH, OnDetach::kEventName, |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 275 | std::move(args), profile_); |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 276 | EventRouter::Get(profile_) |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 277 | ->DispatchEventToExtension(extension_id_, std::move(event)); |
[email protected] | c99c766 | 2011-12-23 12:05:37 | [diff] [blame] | 278 | } |
| 279 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 280 | void ExtensionDevToolsClientHost::OnExtensionUnloaded( |
| 281 | content::BrowserContext* browser_context, |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 282 | const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 283 | UnloadedExtensionReason reason) { |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 284 | if (extension->id() == extension_id_) |
| 285 | Close(); |
| 286 | } |
| 287 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 288 | void ExtensionDevToolsClientHost::Observe( |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 289 | int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 290 | const content::NotificationSource& source, |
| 291 | const content::NotificationDetails& details) { |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 292 | DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); |
| 293 | Close(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 294 | } |
| 295 | |
[email protected] | b50452f | 2014-08-18 12:31:44 | [diff] [blame] | 296 | void ExtensionDevToolsClientHost::DispatchProtocolMessage( |
| 297 | DevToolsAgentHost* agent_host, const std::string& message) { |
| 298 | DCHECK(agent_host == agent_host_.get()); |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 299 | if (!EventRouter::Get(profile_)) |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 300 | return; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 301 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 302 | std::unique_ptr<base::Value> result = base::JSONReader::Read(message); |
jdoerrie | 1f536b2 | 2017-10-23 17:15:11 | [diff] [blame] | 303 | if (!result || !result->is_dict()) |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 304 | return; |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 305 | base::DictionaryValue* dictionary = |
| 306 | static_cast<base::DictionaryValue*>(result.get()); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 307 | |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 308 | int id; |
| 309 | if (!dictionary->GetInteger("id", &id)) { |
| 310 | std::string method_name; |
| 311 | if (!dictionary->GetString("method", &method_name)) |
[email protected] | 7d713ec | 2011-04-01 14:22:02 | [diff] [blame] | 312 | return; |
| 313 | |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 314 | OnEvent::Params params; |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 315 | base::DictionaryValue* params_value; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 316 | if (dictionary->GetDictionary("params", ¶ms_value)) |
| 317 | params.additional_properties.Swap(params_value); |
| 318 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 319 | std::unique_ptr<base::ListValue> args( |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 320 | OnEvent::Create(debuggee_, method_name, params)); |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 321 | auto event = |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 322 | std::make_unique<Event>(events::DEBUGGER_ON_EVENT, OnEvent::kEventName, |
lazyboy | 59155a4 | 2017-05-24 22:23:35 | [diff] [blame] | 323 | std::move(args), profile_); |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 324 | EventRouter::Get(profile_) |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 325 | ->DispatchEventToExtension(extension_id_, std::move(event)); |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 326 | } else { |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 327 | DebuggerSendCommandFunction* function = pending_requests_[id].get(); |
[email protected] | ac31010 | 2011-04-08 14:08:33 | [diff] [blame] | 328 | if (!function) |
| 329 | return; |
| 330 | |
| 331 | function->SendResponseBody(dictionary); |
| 332 | pending_requests_.erase(id); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 336 | |
| 337 | // DebuggerFunction ----------------------------------------------------------- |
| 338 | |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 339 | DebuggerFunction::DebuggerFunction() |
[email protected] | 19bcf9d | 2013-07-18 22:00:59 | [diff] [blame] | 340 | : client_host_(NULL) { |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | DebuggerFunction::~DebuggerFunction() { |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 346 | void DebuggerFunction::FormatErrorMessage(const std::string& format) { |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 347 | if (debuggee_.tab_id) |
| 348 | error_ = ErrorUtils::FormatErrorMessage( |
| 349 | format, keys::kTabTargetType, base::IntToString(*debuggee_.tab_id)); |
| 350 | else if (debuggee_.extension_id) |
| 351 | error_ = ErrorUtils::FormatErrorMessage( |
| 352 | format, keys::kBackgroundPageTargetType, *debuggee_.extension_id); |
| 353 | else |
| 354 | error_ = ErrorUtils::FormatErrorMessage( |
| 355 | format, keys::kOpaqueTargetType, *debuggee_.target_id); |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 356 | } |
| 357 | |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 358 | bool DebuggerFunction::InitAgentHost() { |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 359 | if (debuggee_.tab_id) { |
| 360 | WebContents* web_contents = NULL; |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 361 | bool result = ExtensionTabUtil::GetTabById(*debuggee_.tab_id, |
| 362 | GetProfile(), |
| 363 | include_incognito(), |
| 364 | NULL, |
| 365 | NULL, |
| 366 | &web_contents, |
| 367 | NULL); |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 368 | if (result && web_contents) { |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 369 | // TODO(rdevlin.cronin) This should definitely be GetLastCommittedURL(). |
| 370 | GURL url = web_contents->GetVisibleURL(); |
rdevlin.cronin | f994d1e | 2015-06-03 22:28:19 | [diff] [blame] | 371 | if (PermissionsData::IsRestrictedUrl(url, extension(), &error_)) |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 372 | return false; |
[email protected] | 02a67fd | 2014-03-28 15:29:55 | [diff] [blame] | 373 | agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents); |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 374 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 375 | } else if (debuggee_.extension_id) { |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 376 | ExtensionHost* extension_host = |
reillyg | 0ea3fa90 | 2014-10-28 15:30:23 | [diff] [blame] | 377 | ProcessManager::Get(GetProfile()) |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 378 | ->GetBackgroundHostForExtension(*debuggee_.extension_id); |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 379 | if (extension_host) { |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 380 | if (PermissionsData::IsRestrictedUrl(extension_host->GetURL(), |
[email protected] | eba8f7d | 2014-07-28 22:09:23 | [diff] [blame] | 381 | extension(), |
[email protected] | 684a212a | 2014-06-27 17:14:50 | [diff] [blame] | 382 | &error_)) { |
| 383 | return false; |
| 384 | } |
[email protected] | b3f957e6 | 2014-08-08 10:09:02 | [diff] [blame] | 385 | agent_host_ = |
| 386 | DevToolsAgentHost::GetOrCreateFor(extension_host->host_contents()); |
[email protected] | 30e246b | 2013-03-28 17:08:48 | [diff] [blame] | 387 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 388 | } else if (debuggee_.target_id) { |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 389 | agent_host_ = DevToolsAgentHost::GetForId(*debuggee_.target_id); |
rob | 409bf9d6 | 2015-02-10 23:37:07 | [diff] [blame] | 390 | if (agent_host_.get()) { |
| 391 | if (PermissionsData::IsRestrictedUrl(agent_host_->GetURL(), |
rob | 409bf9d6 | 2015-02-10 23:37:07 | [diff] [blame] | 392 | extension(), |
| 393 | &error_)) { |
| 394 | agent_host_ = nullptr; |
| 395 | return false; |
| 396 | } |
| 397 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 398 | } else { |
| 399 | error_ = keys::kInvalidTargetError; |
[email protected] | 4e023cd | 2013-03-28 15:04:12 | [diff] [blame] | 400 | return false; |
| 401 | } |
[email protected] | 30e246b | 2013-03-28 17:08:48 | [diff] [blame] | 402 | |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 403 | if (!agent_host_.get()) { |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 404 | FormatErrorMessage(keys::kNoTargetError); |
| 405 | return false; |
| 406 | } |
| 407 | return true; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 408 | } |
| 409 | |
[email protected] | c7580b6 | 2011-10-26 08:46:30 | [diff] [blame] | 410 | bool DebuggerFunction::InitClientHost() { |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 411 | if (!InitAgentHost()) |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 412 | return false; |
| 413 | |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 414 | client_host_ = FindClientHost(); |
| 415 | if (!client_host_) { |
| 416 | FormatErrorMessage(keys::kNotAttachedError); |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | ExtensionDevToolsClientHost* DebuggerFunction::FindClientHost() { |
| 424 | if (!agent_host_.get()) |
| 425 | return nullptr; |
| 426 | |
dgozman | d06efdf | 2015-08-27 02:28:38 | [diff] [blame] | 427 | const std::string& extension_id = extension()->id(); |
| 428 | DevToolsAgentHost* agent_host = agent_host_.get(); |
| 429 | AttachedClientHosts& hosts = g_attached_client_hosts.Get(); |
| 430 | AttachedClientHosts::iterator it = std::find_if( |
| 431 | hosts.begin(), hosts.end(), |
| 432 | [&agent_host, &extension_id](ExtensionDevToolsClientHost* client_host) { |
| 433 | return client_host->agent_host() == agent_host && |
| 434 | client_host->extension_id() == extension_id; |
| 435 | }); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 436 | |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 437 | return it == hosts.end() ? nullptr : *it; |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 440 | // DebuggerAttachFunction ----------------------------------------------------- |
| 441 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 442 | DebuggerAttachFunction::DebuggerAttachFunction() { |
| 443 | } |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 444 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 445 | DebuggerAttachFunction::~DebuggerAttachFunction() { |
| 446 | } |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 447 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 448 | bool DebuggerAttachFunction::RunAsync() { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 449 | std::unique_ptr<Attach::Params> params(Attach::Params::Create(*args_)); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 450 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 451 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 452 | CopyDebuggee(&debuggee_, params->target); |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 453 | if (!InitAgentHost()) |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 454 | return false; |
| 455 | |
dgozman | 102fee9 | 2015-04-20 15:45:46 | [diff] [blame] | 456 | if (!DevToolsAgentHost::IsSupportedProtocolVersion( |
[email protected] | be3577e | 2013-06-26 21:42:33 | [diff] [blame] | 457 | params->required_version)) { |
[email protected] | e9f541a | 2012-11-19 21:52:31 | [diff] [blame] | 458 | error_ = ErrorUtils::FormatErrorMessage( |
[email protected] | c7580b6 | 2011-10-26 08:46:30 | [diff] [blame] | 459 | keys::kProtocolVersionNotSupportedError, |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 460 | params->required_version); |
[email protected] | c7580b6 | 2011-10-26 08:46:30 | [diff] [blame] | 461 | return false; |
| 462 | } |
| 463 | |
Dmitry Gozman | ed41e02 | 2017-08-24 20:12:10 | [diff] [blame] | 464 | if (FindClientHost()) { |
[email protected] | 2fefdb3 | 2013-02-26 14:28:10 | [diff] [blame] | 465 | FormatErrorMessage(keys::kAlreadyAttachedError); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 466 | return false; |
| 467 | } |
| 468 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 469 | auto host = std::make_unique<ExtensionDevToolsClientHost>( |
| 470 | GetProfile(), agent_host_.get(), extension()->id(), extension()->name(), |
| 471 | debuggee_); |
| 472 | |
| 473 | if (!host->Attach()) { |
| 474 | FormatErrorMessage(keys::kRestrictedError); |
| 475 | return false; |
| 476 | } |
| 477 | |
| 478 | host.release(); // An attached client host manages its own lifetime. |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 479 | SendResponse(true); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 483 | |
| 484 | // DebuggerDetachFunction ----------------------------------------------------- |
| 485 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 486 | DebuggerDetachFunction::DebuggerDetachFunction() { |
| 487 | } |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 488 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 489 | DebuggerDetachFunction::~DebuggerDetachFunction() { |
| 490 | } |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 491 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 492 | bool DebuggerDetachFunction::RunAsync() { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 493 | std::unique_ptr<Detach::Params> params(Detach::Params::Create(*args_)); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 494 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 495 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 496 | CopyDebuggee(&debuggee_, params->target); |
[email protected] | c7580b6 | 2011-10-26 08:46:30 | [diff] [blame] | 497 | if (!InitClientHost()) |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 498 | return false; |
| 499 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 500 | client_host_->RespondDetachedToPendingRequests(); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 501 | client_host_->Close(); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 502 | SendResponse(true); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 503 | return true; |
| 504 | } |
| 505 | |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 506 | |
| 507 | // DebuggerSendCommandFunction ------------------------------------------------ |
| 508 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 509 | DebuggerSendCommandFunction::DebuggerSendCommandFunction() { |
| 510 | } |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 511 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 512 | DebuggerSendCommandFunction::~DebuggerSendCommandFunction() { |
| 513 | } |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 514 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 515 | bool DebuggerSendCommandFunction::RunAsync() { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 516 | std::unique_ptr<SendCommand::Params> params( |
| 517 | SendCommand::Params::Create(*args_)); |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 518 | EXTENSION_FUNCTION_VALIDATE(params.get()); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 519 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 520 | CopyDebuggee(&debuggee_, params->target); |
[email protected] | c7580b6 | 2011-10-26 08:46:30 | [diff] [blame] | 521 | if (!InitClientHost()) |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 522 | return false; |
| 523 | |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 524 | client_host_->SendMessageToBackend(this, params->method, |
| 525 | params->command_params.get()); |
[email protected] | 91ba331 | 2011-03-17 20:39:22 | [diff] [blame] | 526 | return true; |
| 527 | } |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 528 | |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame] | 529 | void DebuggerSendCommandFunction::SendResponseBody( |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 530 | base::DictionaryValue* response) { |
[email protected] | 5bcdd99d | 2013-12-23 18:28:30 | [diff] [blame] | 531 | base::Value* error_body; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 532 | if (response->Get("error", &error_body)) { |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 533 | base::JSONWriter::Write(*error_body, &error_); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 534 | SendResponse(false); |
| 535 | return; |
| 536 | } |
| 537 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 538 | base::DictionaryValue* result_body; |
[email protected] | 752960d | 2012-07-23 19:06:11 | [diff] [blame] | 539 | SendCommand::Results::Result result; |
| 540 | if (response->GetDictionary("result", &result_body)) |
| 541 | result.additional_properties.Swap(result_body); |
| 542 | |
| 543 | results_ = SendCommand::Results::Create(result); |
[email protected] | ff31a8a | 2011-03-30 14:13:59 | [diff] [blame] | 544 | SendResponse(true); |
| 545 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 546 | |
Dmitry Gozman | 2aec794f | 2018-03-05 20:27:54 | [diff] [blame^] | 547 | void DebuggerSendCommandFunction::SendDetachedError() { |
| 548 | error_ = keys::kDetachedWhileHandlingError; |
| 549 | SendResponse(false); |
| 550 | } |
[email protected] | 49c71ac | 2013-05-03 01:36:22 | [diff] [blame] | 551 | |
| 552 | // DebuggerGetTargetsFunction ------------------------------------------------- |
| 553 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 554 | namespace { |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 555 | |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 556 | const char kTargetIdField[] = "id"; |
| 557 | const char kTargetTypeField[] = "type"; |
| 558 | const char kTargetTitleField[] = "title"; |
| 559 | const char kTargetAttachedField[] = "attached"; |
| 560 | const char kTargetUrlField[] = "url"; |
| 561 | const char kTargetFaviconUrlField[] = "faviconUrl"; |
[email protected] | f80e0be6 | 2013-07-17 14:17:23 | [diff] [blame] | 562 | const char kTargetTabIdField[] = "tabId"; |
| 563 | const char kTargetExtensionIdField[] = "extensionId"; |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 564 | const char kTargetTypeWorker[] = "worker"; |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 565 | |
dcheng | f1950200 | 2016-09-14 15:18:18 | [diff] [blame] | 566 | std::unique_ptr<base::DictionaryValue> SerializeTarget( |
| 567 | scoped_refptr<DevToolsAgentHost> host) { |
| 568 | std::unique_ptr<base::DictionaryValue> dictionary( |
| 569 | new base::DictionaryValue()); |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 570 | dictionary->SetString(kTargetIdField, host->GetId()); |
| 571 | dictionary->SetString(kTargetTitleField, host->GetTitle()); |
| 572 | dictionary->SetBoolean(kTargetAttachedField, host->IsAttached()); |
| 573 | dictionary->SetString(kTargetUrlField, host->GetURL().spec()); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 574 | |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 575 | std::string type = host->GetType(); |
| 576 | if (type == DevToolsAgentHost::kTypePage) { |
| 577 | int tab_id = |
| 578 | extensions::ExtensionTabUtil::GetTabId(host->GetWebContents()); |
| 579 | dictionary->SetInteger(kTargetTabIdField, tab_id); |
| 580 | } else if (type == ChromeDevToolsManagerDelegate::kTypeBackgroundPage) { |
| 581 | dictionary->SetString(kTargetExtensionIdField, host->GetURL().host()); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 582 | } |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 583 | |
| 584 | if (type == DevToolsAgentHost::kTypeServiceWorker || |
| 585 | type == DevToolsAgentHost::kTypeSharedWorker) { |
| 586 | type = kTargetTypeWorker; |
| 587 | } |
| 588 | |
[email protected] | 06d503f | 2013-10-29 17:29:37 | [diff] [blame] | 589 | dictionary->SetString(kTargetTypeField, type); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 590 | |
pfeldman | a9e7dda | 2016-08-26 14:35:17 | [diff] [blame] | 591 | GURL favicon_url = host->GetFaviconURL(); |
[email protected] | 06d503f | 2013-10-29 17:29:37 | [diff] [blame] | 592 | if (favicon_url.is_valid()) |
| 593 | dictionary->SetString(kTargetFaviconUrlField, favicon_url.spec()); |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 594 | |
| 595 | return dictionary; |
| 596 | } |
| 597 | |
[email protected] | 4edaf7ec | 2014-05-07 13:54:15 | [diff] [blame] | 598 | } // namespace |
[email protected] | 28b05ef0 | 2013-07-10 22:28:32 | [diff] [blame] | 599 | |
| 600 | DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() { |
| 601 | } |
| 602 | |
| 603 | DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() { |
| 604 | } |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 605 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 606 | bool DebuggerGetTargetsFunction::RunAsync() { |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 607 | content::DevToolsAgentHost::List list = DevToolsAgentHost::GetOrCreateAll(); |
dgozman | cb088d0 | 2015-04-28 11:57:45 | [diff] [blame] | 608 | content::BrowserThread::PostTask( |
tzik | 8d880ee | 2017-04-20 19:46:24 | [diff] [blame] | 609 | content::BrowserThread::UI, FROM_HERE, |
| 610 | base::BindOnce(&DebuggerGetTargetsFunction::SendTargetList, this, list)); |
[email protected] | 56cc3804 | 2013-04-03 04:42:47 | [diff] [blame] | 611 | return true; |
| 612 | } |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 613 | |
[email protected] | fe38b1e | 2013-10-02 23:43:34 | [diff] [blame] | 614 | void DebuggerGetTargetsFunction::SendTargetList( |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 615 | const content::DevToolsAgentHost::List& target_list) { |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 616 | std::unique_ptr<base::ListValue> result(new base::ListValue()); |
[email protected] | 06d503f | 2013-10-29 17:29:37 | [diff] [blame] | 617 | for (size_t i = 0; i < target_list.size(); ++i) |
pfeldman | bc5d788 | 2016-08-26 18:55:46 | [diff] [blame] | 618 | result->Append(SerializeTarget(target_list[i])); |
dcheng | 85f24da | 2016-05-20 22:20:26 | [diff] [blame] | 619 | SetResult(std::move(result)); |
[email protected] | ac3851f | 2013-04-05 15:51:59 | [diff] [blame] | 620 | SendResponse(true); |
| 621 | } |
[email protected] | d32a9265 | 2014-05-09 11:06:15 | [diff] [blame] | 622 | |
| 623 | } // namespace extensions |