atom_api_debugger.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/browser/api/atom_api_debugger.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "atom/common/native_mate_converters/callback.h"
  9. #include "atom/common/native_mate_converters/value_converter.h"
  10. #include "atom/common/node_includes.h"
  11. #include "base/json/json_reader.h"
  12. #include "base/json/json_writer.h"
  13. #include "content/public/browser/devtools_agent_host.h"
  14. #include "content/public/browser/web_contents.h"
  15. #include "native_mate/dictionary.h"
  16. using content::DevToolsAgentHost;
  17. namespace atom {
  18. namespace api {
  19. Debugger::Debugger(v8::Isolate* isolate, content::WebContents* web_contents)
  20. : content::WebContentsObserver(web_contents), web_contents_(web_contents) {
  21. Init(isolate);
  22. }
  23. Debugger::~Debugger() {}
  24. void Debugger::AgentHostClosed(DevToolsAgentHost* agent_host) {
  25. DCHECK(agent_host == agent_host_);
  26. agent_host_ = nullptr;
  27. ClearPendingRequests();
  28. Emit("detach", "target closed");
  29. }
  30. void Debugger::DispatchProtocolMessage(DevToolsAgentHost* agent_host,
  31. const std::string& message) {
  32. DCHECK(agent_host == agent_host_);
  33. v8::Locker locker(isolate());
  34. v8::HandleScope handle_scope(isolate());
  35. std::unique_ptr<base::Value> parsed_message =
  36. base::JSONReader::ReadDeprecated(message);
  37. if (!parsed_message || !parsed_message->is_dict())
  38. return;
  39. base::DictionaryValue* dict =
  40. static_cast<base::DictionaryValue*>(parsed_message.get());
  41. int id;
  42. if (!dict->GetInteger("id", &id)) {
  43. std::string method;
  44. if (!dict->GetString("method", &method))
  45. return;
  46. base::DictionaryValue* params_value = nullptr;
  47. base::DictionaryValue params;
  48. if (dict->GetDictionary("params", &params_value))
  49. params.Swap(params_value);
  50. Emit("message", method, params);
  51. } else {
  52. auto it = pending_requests_.find(id);
  53. if (it == pending_requests_.end())
  54. return;
  55. atom::util::Promise promise = std::move(it->second);
  56. pending_requests_.erase(it);
  57. base::DictionaryValue* error = nullptr;
  58. if (dict->GetDictionary("error", &error)) {
  59. std::string message;
  60. error->GetString("message", &message);
  61. promise.RejectWithErrorMessage(message);
  62. } else {
  63. base::DictionaryValue* result_body = nullptr;
  64. base::DictionaryValue result;
  65. if (dict->GetDictionary("result", &result_body)) {
  66. result.Swap(result_body);
  67. }
  68. promise.Resolve(result);
  69. }
  70. }
  71. }
  72. void Debugger::RenderFrameHostChanged(content::RenderFrameHost* old_rfh,
  73. content::RenderFrameHost* new_rfh) {
  74. if (agent_host_) {
  75. agent_host_->DisconnectWebContents();
  76. auto* web_contents = content::WebContents::FromRenderFrameHost(new_rfh);
  77. agent_host_->ConnectWebContents(web_contents);
  78. }
  79. }
  80. void Debugger::Attach(mate::Arguments* args) {
  81. std::string protocol_version;
  82. args->GetNext(&protocol_version);
  83. if (agent_host_) {
  84. args->ThrowError("Debugger is already attached to the target");
  85. return;
  86. }
  87. if (!protocol_version.empty() &&
  88. !DevToolsAgentHost::IsSupportedProtocolVersion(protocol_version)) {
  89. args->ThrowError("Requested protocol version is not supported");
  90. return;
  91. }
  92. agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents_);
  93. if (!agent_host_) {
  94. args->ThrowError("No target available");
  95. return;
  96. }
  97. agent_host_->AttachClient(this);
  98. }
  99. bool Debugger::IsAttached() {
  100. return agent_host_ && agent_host_->IsAttached();
  101. }
  102. void Debugger::Detach() {
  103. if (!agent_host_)
  104. return;
  105. agent_host_->DetachClient(this);
  106. AgentHostClosed(agent_host_.get());
  107. }
  108. v8::Local<v8::Promise> Debugger::SendCommand(mate::Arguments* args) {
  109. atom::util::Promise promise(isolate());
  110. v8::Local<v8::Promise> handle = promise.GetHandle();
  111. if (!agent_host_) {
  112. promise.RejectWithErrorMessage("No target available");
  113. return handle;
  114. }
  115. std::string method;
  116. if (!args->GetNext(&method)) {
  117. promise.RejectWithErrorMessage("Invalid method");
  118. return handle;
  119. }
  120. base::DictionaryValue command_params;
  121. args->GetNext(&command_params);
  122. base::DictionaryValue request;
  123. int request_id = ++previous_request_id_;
  124. pending_requests_.emplace(request_id, std::move(promise));
  125. request.SetInteger("id", request_id);
  126. request.SetString("method", method);
  127. if (!command_params.empty())
  128. request.Set("params",
  129. base::Value::ToUniquePtrValue(command_params.Clone()));
  130. std::string json_args;
  131. base::JSONWriter::Write(request, &json_args);
  132. agent_host_->DispatchProtocolMessage(this, json_args);
  133. return handle;
  134. }
  135. void Debugger::ClearPendingRequests() {
  136. for (auto& it : pending_requests_)
  137. it.second.RejectWithErrorMessage("target closed while handling command");
  138. }
  139. // static
  140. mate::Handle<Debugger> Debugger::Create(v8::Isolate* isolate,
  141. content::WebContents* web_contents) {
  142. return mate::CreateHandle(isolate, new Debugger(isolate, web_contents));
  143. }
  144. // static
  145. void Debugger::BuildPrototype(v8::Isolate* isolate,
  146. v8::Local<v8::FunctionTemplate> prototype) {
  147. prototype->SetClassName(mate::StringToV8(isolate, "Debugger"));
  148. mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
  149. .SetMethod("attach", &Debugger::Attach)
  150. .SetMethod("isAttached", &Debugger::IsAttached)
  151. .SetMethod("detach", &Debugger::Detach)
  152. .SetMethod("sendCommand", &Debugger::SendCommand);
  153. }
  154. } // namespace api
  155. } // namespace atom
  156. namespace {
  157. using atom::api::Debugger;
  158. void Initialize(v8::Local<v8::Object> exports,
  159. v8::Local<v8::Value> unused,
  160. v8::Local<v8::Context> context,
  161. void* priv) {
  162. v8::Isolate* isolate = context->GetIsolate();
  163. mate::Dictionary(isolate, exports)
  164. .Set("Debugger", Debugger::GetConstructor(isolate)
  165. ->GetFunction(context)
  166. .ToLocalChecked());
  167. }
  168. } // namespace
  169. NODE_LINKED_MODULE_CONTEXT_AWARE(atom_browser_debugger, Initialize)