Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame^] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 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 | |
Arman Uguray | 503baaa | 2015-08-20 11:49:00 -0700 | [diff] [blame] | 19 | #include <base/base_switches.h> |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 20 | #include <base/command_line.h> |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 21 | #include <base/logging.h> |
| 22 | |
| 23 | #include "service/switches.h" |
| 24 | |
| 25 | namespace bluetooth { |
| 26 | |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 27 | Settings::Settings() : initialized_(false), enable_on_start_(false) {} |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 28 | |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 29 | Settings::~Settings() {} |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 30 | |
| 31 | bool Settings::Init() { |
| 32 | CHECK(!initialized_); |
| 33 | auto command_line = base::CommandLine::ForCurrentProcess(); |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 34 | const auto& switches = command_line->GetSwitches(); |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 35 | |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 36 | for (const auto& iter : switches) { |
Ian Coolidge | c6760d8 | 2015-07-30 20:51:47 -0700 | [diff] [blame] | 37 | if (iter.first == switches::kCreateIPCSocketPath) { |
| 38 | // kCreateIPCSocketPath: An optional argument that initializes an IPC |
| 39 | // socket path for IPC. |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 40 | base::FilePath path(iter.second); |
| 41 | if (path.empty() || path.EndsWithSeparator()) { |
Ian Coolidge | c6760d8 | 2015-07-30 20:51:47 -0700 | [diff] [blame] | 42 | LOG(ERROR) << "Invalid IPC create socket path"; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 45 | |
Ian Coolidge | c6760d8 | 2015-07-30 20:51:47 -0700 | [diff] [blame] | 46 | create_ipc_socket_path_ = path; |
| 47 | } else if (iter.first == switches::kAndroidIPCSocketSuffix) { |
| 48 | // kAndroidIPCSocketSuffix: An optional argument used to express |
| 49 | // a socket that Android init created for us. We bind to this. |
| 50 | const std::string& suffix = iter.second; |
| 51 | if (suffix.empty()) { |
| 52 | LOG(ERROR) << "Invalid Android socket suffix"; |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | android_ipc_socket_suffix_ = suffix; |
Bailey Forrest | e8e6c6b | 2017-06-07 14:50:08 -0700 | [diff] [blame] | 57 | } else if (iter.first == switches::kEnableOnStart) { |
| 58 | if (iter.second == "true") { |
| 59 | enable_on_start_ = true; |
| 60 | } else if (iter.second == "false") { |
| 61 | enable_on_start_ = false; |
| 62 | } else { |
| 63 | LOG(ERROR) << "Invalid value for " << switches::kEnableOnStart << ": " |
| 64 | << iter.second << ". Expect 'true' or 'false'"; |
| 65 | return false; |
| 66 | } |
Arman Uguray | 503baaa | 2015-08-20 11:49:00 -0700 | [diff] [blame] | 67 | } |
| 68 | // Check for libbase logging switches. These get processed by |
| 69 | // logging::InitLogging directly. |
| 70 | else if (iter.first != ::switches::kV) { |
Arman Uguray | 8531fc3 | 2015-11-17 19:29:31 -0800 | [diff] [blame] | 71 | LOG(ERROR) << "Unexpected command-line switches found: " << iter.first; |
Arman Uguray | fe65fb7 | 2015-07-24 19:14:42 -0700 | [diff] [blame] | 72 | return false; |
| 73 | } |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Ian Coolidge | c6760d8 | 2015-07-30 20:51:47 -0700 | [diff] [blame] | 76 | // Two IPC methods/paths were provided. |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 77 | if (!android_ipc_socket_suffix_.empty() && !create_ipc_socket_path_.empty()) { |
Ian Coolidge | c6760d8 | 2015-07-30 20:51:47 -0700 | [diff] [blame] | 78 | LOG(ERROR) << "Too many IPC methods provided"; |
| 79 | return false; |
| 80 | } |
| 81 | |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 82 | // The daemon has no arguments |
| 83 | if (command_line->GetArgs().size()) { |
| 84 | LOG(ERROR) << "Unexpected command-line arguments found"; |
| 85 | return false; |
| 86 | } |
| 87 | |
Arman Uguray | 065d0f7 | 2015-07-16 18:12:13 -0700 | [diff] [blame] | 88 | initialized_ = true; |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | } // namespace bluetooth |