electron_api_command_line.cc 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2019 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 "base/command_line.h"
  5. #include "base/files/file_path.h"
  6. #include "services/network/public/cpp/network_switches.h"
  7. #include "shell/common/gin_converters/base_converter.h"
  8. #include "shell/common/gin_converters/file_path_converter.h"
  9. #include "shell/common/gin_helper/dictionary.h"
  10. #include "shell/common/node_includes.h"
  11. namespace {
  12. bool HasSwitch(const std::string& name) {
  13. return base::CommandLine::ForCurrentProcess()->HasSwitch(name);
  14. }
  15. base::CommandLine::StringType GetSwitchValue(const std::string& name) {
  16. return base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(name);
  17. }
  18. void AppendSwitch(const std::string& switch_string,
  19. gin_helper::Arguments* args) {
  20. auto* command_line = base::CommandLine::ForCurrentProcess();
  21. if (base::EndsWith(switch_string, "-path",
  22. base::CompareCase::INSENSITIVE_ASCII) ||
  23. switch_string == network::switches::kLogNetLog) {
  24. base::FilePath path;
  25. args->GetNext(&path);
  26. command_line->AppendSwitchPath(switch_string, path);
  27. return;
  28. }
  29. base::CommandLine::StringType value;
  30. if (args->GetNext(&value))
  31. command_line->AppendSwitchNative(switch_string, value);
  32. else
  33. command_line->AppendSwitch(switch_string);
  34. }
  35. void RemoveSwitch(const std::string& switch_string) {
  36. auto* command_line = base::CommandLine::ForCurrentProcess();
  37. command_line->RemoveSwitch(switch_string);
  38. }
  39. void AppendArg(const std::string& arg) {
  40. auto* command_line = base::CommandLine::ForCurrentProcess();
  41. command_line->AppendArg(arg);
  42. }
  43. void Initialize(v8::Local<v8::Object> exports,
  44. v8::Local<v8::Value> unused,
  45. v8::Local<v8::Context> context,
  46. void* priv) {
  47. gin_helper::Dictionary dict(context->GetIsolate(), exports);
  48. dict.SetMethod("hasSwitch", &HasSwitch);
  49. dict.SetMethod("getSwitchValue", &GetSwitchValue);
  50. dict.SetMethod("appendSwitch", &AppendSwitch);
  51. dict.SetMethod("removeSwitch", &RemoveSwitch);
  52. dict.SetMethod("appendArgument", &AppendArg);
  53. }
  54. } // namespace
  55. NODE_LINKED_BINDING_CONTEXT_AWARE(electron_common_command_line, Initialize)