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