node_debugger.cc 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) 2014 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 "shell/browser/node_debugger.h"
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "base/command_line.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_util.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "libplatform/libplatform.h"
  13. #include "native_mate/dictionary.h"
  14. #include "shell/common/node_includes.h"
  15. namespace electron {
  16. NodeDebugger::NodeDebugger(node::Environment* env) : env_(env) {}
  17. NodeDebugger::~NodeDebugger() {}
  18. void NodeDebugger::Start() {
  19. auto* inspector = env_->inspector_agent();
  20. if (inspector == nullptr)
  21. return;
  22. std::vector<std::string> args;
  23. for (auto& arg : base::CommandLine::ForCurrentProcess()->argv()) {
  24. #if defined(OS_WIN)
  25. args.push_back(base::UTF16ToUTF8(arg));
  26. #else
  27. args.push_back(arg);
  28. #endif
  29. }
  30. node::DebugOptions options;
  31. std::vector<std::string> exec_args;
  32. std::vector<std::string> v8_args;
  33. std::vector<std::string> errors;
  34. node::options_parser::Parse(&args, &exec_args, &v8_args, &options,
  35. node::options_parser::kDisallowedInEnvironment,
  36. &errors);
  37. if (!errors.empty()) {
  38. // TODO(jeremy): what's the appropriate behaviour here?
  39. LOG(ERROR) << "Error parsing node options: "
  40. << base::JoinString(errors, " ");
  41. }
  42. const char* path = "";
  43. if (inspector->Start(path, options,
  44. std::make_shared<node::HostPort>(options.host_port),
  45. true /* is_main */))
  46. DCHECK(env_->inspector_agent()->IsListening());
  47. }
  48. void NodeDebugger::Stop() {
  49. auto* inspector = env_->inspector_agent();
  50. if (inspector && inspector->IsListening()) {
  51. inspector->WaitForDisconnect();
  52. inspector->Stop();
  53. }
  54. }
  55. } // namespace electron