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 17 #include "service/settings.h" 18 19 #include <base/command_line.h> 20 #include <base/logging.h> 21 22 #include "service/switches.h" 23 24 namespace bluetooth { 25 26 namespace switches { 27 // Verbose switch that should be part of base_switches but doesn't seem to be 28 // exported as part of libbase. 29 const char kV[] = "v"; 30 }; // namespace switches 31 32 Settings::Settings() : initialized_(false), enable_on_start_(false) {} 33 34 Settings::~Settings() {} 35 36 bool Settings::Init() { 37 CHECK(!initialized_); 38 auto command_line = base::CommandLine::ForCurrentProcess(); 39 const auto& switches = command_line->GetSwitches(); 40 41 for (const auto& iter : switches) { 42 if (iter.first == switches::kCreateIPCSocketPath) { 43 // kCreateIPCSocketPath: An optional argument that initializes an IPC 44 // socket path for IPC. 45 base::FilePath path(iter.second); 46 if (path.empty() || path.EndsWithSeparator()) { 47 LOG(ERROR) << "Invalid IPC create socket path"; 48 return false; 49 } 50 51 create_ipc_socket_path_ = path; 52 } else if (iter.first == switches::kAndroidIPCSocketSuffix) { 53 // kAndroidIPCSocketSuffix: An optional argument used to express 54 // a socket that Android init created for us. We bind to this. 55 const std::string& suffix = iter.second; 56 if (suffix.empty()) { 57 LOG(ERROR) << "Invalid Android socket suffix"; 58 return false; 59 } 60 61 android_ipc_socket_suffix_ = suffix; 62 } else if (iter.first == switches::kEnableOnStart) { 63 if (iter.second == "true") { 64 enable_on_start_ = true; 65 } else if (iter.second == "false") { 66 enable_on_start_ = false; 67 } else { 68 LOG(ERROR) << "Invalid value for " << switches::kEnableOnStart << ": " 69 << iter.second << ". Expect 'true' or 'false'"; 70 return false; 71 } 72 } 73 // Check for libbase logging switches. These get processed by 74 // logging::InitLogging directly. 75 else if (iter.first != switches::kV) { 76 LOG(ERROR) << "Unexpected command-line switches found: " << iter.first; 77 return false; 78 } 79 } 80 81 // Two IPC methods/paths were provided. 82 if (!android_ipc_socket_suffix_.empty() && !create_ipc_socket_path_.empty()) { 83 LOG(ERROR) << "Too many IPC methods provided"; 84 return false; 85 } 86 87 // The daemon has no arguments 88 if (command_line->GetArgs().size()) { 89 LOG(ERROR) << "Unexpected command-line arguments found"; 90 return false; 91 } 92 93 initialized_ = true; 94 return true; 95 } 96 97 } // namespace bluetooth 98