atom_api_debugger.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #ifndef SHELL_BROWSER_API_ATOM_API_DEBUGGER_H_
  5. #define SHELL_BROWSER_API_ATOM_API_DEBUGGER_H_
  6. #include <map>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/values.h"
  10. #include "content/public/browser/devtools_agent_host_client.h"
  11. #include "content/public/browser/web_contents_observer.h"
  12. #include "native_mate/handle.h"
  13. #include "shell/browser/api/trackable_object.h"
  14. #include "shell/common/promise_util.h"
  15. namespace content {
  16. class DevToolsAgentHost;
  17. class WebContents;
  18. } // namespace content
  19. namespace mate {
  20. class Arguments;
  21. }
  22. namespace electron {
  23. namespace api {
  24. class Debugger : public mate::TrackableObject<Debugger>,
  25. public content::DevToolsAgentHostClient,
  26. public content::WebContentsObserver {
  27. public:
  28. static mate::Handle<Debugger> Create(v8::Isolate* isolate,
  29. content::WebContents* web_contents);
  30. // mate::TrackableObject:
  31. static void BuildPrototype(v8::Isolate* isolate,
  32. v8::Local<v8::FunctionTemplate> prototype);
  33. protected:
  34. Debugger(v8::Isolate* isolate, content::WebContents* web_contents);
  35. ~Debugger() override;
  36. // content::DevToolsAgentHostClient:
  37. void AgentHostClosed(content::DevToolsAgentHost* agent_host) override;
  38. void DispatchProtocolMessage(content::DevToolsAgentHost* agent_host,
  39. const std::string& message) override;
  40. // content::WebContentsObserver:
  41. void RenderFrameHostChanged(content::RenderFrameHost* old_rfh,
  42. content::RenderFrameHost* new_rfh) override;
  43. private:
  44. using PendingRequestMap = std::map<int, electron::util::Promise>;
  45. void Attach(mate::Arguments* args);
  46. bool IsAttached();
  47. void Detach();
  48. v8::Local<v8::Promise> SendCommand(mate::Arguments* args);
  49. void ClearPendingRequests();
  50. content::WebContents* web_contents_; // Weak Reference.
  51. scoped_refptr<content::DevToolsAgentHost> agent_host_;
  52. PendingRequestMap pending_requests_;
  53. int previous_request_id_ = 0;
  54. DISALLOW_COPY_AND_ASSIGN(Debugger);
  55. };
  56. } // namespace api
  57. } // namespace electron
  58. #endif // SHELL_BROWSER_API_ATOM_API_DEBUGGER_H_