blob: dee0e69cb3905de4b8a090d4ee38365488e90a96 [file] [log] [blame]
Arman Uguray065d0f72015-07-16 18:12:13 -07001//
2// Copyright (C) 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
Arman Uguray503baaa2015-08-20 11:49:00 -070019#include <base/base_switches.h>
Arman Uguray065d0f72015-07-16 18:12:13 -070020#include <base/command_line.h>
Arman Uguray065d0f72015-07-16 18:12:13 -070021#include <base/logging.h>
22
23#include "service/switches.h"
24
25namespace bluetooth {
26
Arman Ugurayb2286f32015-08-05 21:19:02 -070027Settings::Settings()
28 : initialized_(false) {
Arman Uguray065d0f72015-07-16 18:12:13 -070029}
30
31Settings::~Settings() {
32}
33
34bool Settings::Init() {
35 CHECK(!initialized_);
36 auto command_line = base::CommandLine::ForCurrentProcess();
Arman Ugurayfe65fb72015-07-24 19:14:42 -070037 const auto& switches = command_line->GetSwitches();
Arman Uguray065d0f72015-07-16 18:12:13 -070038
Arman Ugurayfe65fb72015-07-24 19:14:42 -070039 for (const auto& iter : switches) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070040 if (iter.first == switches::kCreateIPCSocketPath) {
41 // kCreateIPCSocketPath: An optional argument that initializes an IPC
42 // socket path for IPC.
Arman Ugurayfe65fb72015-07-24 19:14:42 -070043 base::FilePath path(iter.second);
44 if (path.empty() || path.EndsWithSeparator()) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070045 LOG(ERROR) << "Invalid IPC create socket path";
Arman Ugurayfe65fb72015-07-24 19:14:42 -070046 return false;
47 }
Arman Uguray065d0f72015-07-16 18:12:13 -070048
Ian Coolidgec6760d82015-07-30 20:51:47 -070049 create_ipc_socket_path_ = path;
50 } else if (iter.first == switches::kAndroidIPCSocketSuffix) {
51 // kAndroidIPCSocketSuffix: An optional argument used to express
52 // a socket that Android init created for us. We bind to this.
53 const std::string& suffix = iter.second;
54 if (suffix.empty()) {
55 LOG(ERROR) << "Invalid Android socket suffix";
56 return false;
57 }
58
59 android_ipc_socket_suffix_ = suffix;
Arman Uguray503baaa2015-08-20 11:49:00 -070060 }
61 // Check for libbase logging switches. These get processed by
62 // logging::InitLogging directly.
63 else if (iter.first != ::switches::kV) {
Arman Uguray8531fc32015-11-17 19:29:31 -080064 LOG(ERROR) << "Unexpected command-line switches found: " << iter.first;
Arman Ugurayfe65fb72015-07-24 19:14:42 -070065 return false;
66 }
Arman Uguray065d0f72015-07-16 18:12:13 -070067 }
68
Ian Coolidgec6760d82015-07-30 20:51:47 -070069 // Two IPC methods/paths were provided.
70 if (!android_ipc_socket_suffix_.empty() &&
71 !create_ipc_socket_path_.empty()) {
72 LOG(ERROR) << "Too many IPC methods provided";
73 return false;
74 }
75
Arman Uguray065d0f72015-07-16 18:12:13 -070076 // The daemon has no arguments
77 if (command_line->GetArgs().size()) {
78 LOG(ERROR) << "Unexpected command-line arguments found";
79 return false;
80 }
81
Arman Uguray065d0f72015-07-16 18:12:13 -070082 initialized_ = true;
83 return true;
84}
85
86} // namespace bluetooth