settings.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Copyright 2015 Google, Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at:
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "service/settings.h"
  17. #include <base/base_switches.h>
  18. #include <base/command_line.h>
  19. #include <base/logging.h>
  20. #include "service/switches.h"
  21. namespace bluetooth {
  22. Settings::Settings() : initialized_(false), enable_on_start_(false) {}
  23. Settings::~Settings() {}
  24. bool Settings::Init() {
  25. CHECK(!initialized_);
  26. auto command_line = base::CommandLine::ForCurrentProcess();
  27. const auto& switches = command_line->GetSwitches();
  28. for (const auto& iter : switches) {
  29. if (iter.first == switches::kCreateIPCSocketPath) {
  30. // kCreateIPCSocketPath: An optional argument that initializes an IPC
  31. // socket path for IPC.
  32. base::FilePath path(iter.second);
  33. if (path.empty() || path.EndsWithSeparator()) {
  34. LOG(ERROR) << "Invalid IPC create socket path";
  35. return false;
  36. }
  37. create_ipc_socket_path_ = path;
  38. } else if (iter.first == switches::kAndroidIPCSocketSuffix) {
  39. // kAndroidIPCSocketSuffix: An optional argument used to express
  40. // a socket that Android init created for us. We bind to this.
  41. const std::string& suffix = iter.second;
  42. if (suffix.empty()) {
  43. LOG(ERROR) << "Invalid Android socket suffix";
  44. return false;
  45. }
  46. android_ipc_socket_suffix_ = suffix;
  47. } else if (iter.first == switches::kEnableOnStart) {
  48. if (iter.second == "true") {
  49. enable_on_start_ = true;
  50. } else if (iter.second == "false") {
  51. enable_on_start_ = false;
  52. } else {
  53. LOG(ERROR) << "Invalid value for " << switches::kEnableOnStart << ": "
  54. << iter.second << ". Expect 'true' or 'false'";
  55. return false;
  56. }
  57. }
  58. // Check for libbase logging switches. These get processed by
  59. // logging::InitLogging directly.
  60. else if (iter.first != ::switches::kV) {
  61. LOG(ERROR) << "Unexpected command-line switches found: " << iter.first;
  62. return false;
  63. }
  64. }
  65. // Two IPC methods/paths were provided.
  66. if (!android_ipc_socket_suffix_.empty() && !create_ipc_socket_path_.empty()) {
  67. LOG(ERROR) << "Too many IPC methods provided";
  68. return false;
  69. }
  70. // The daemon has no arguments
  71. if (command_line->GetArgs().size()) {
  72. LOG(ERROR) << "Unexpected command-line arguments found";
  73. return false;
  74. }
  75. initialized_ = true;
  76. return true;
  77. }
  78. } // namespace bluetooth