blob: d4728d7b4a61f539bafdb26943d46a1cd149a272 [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
Myles Watson911d1ae2016-11-28 16:44:40 -080027Settings::Settings() : initialized_(false) {}
Arman Uguray065d0f72015-07-16 18:12:13 -070028
Myles Watson911d1ae2016-11-28 16:44:40 -080029Settings::~Settings() {}
Arman Uguray065d0f72015-07-16 18:12:13 -070030
31bool Settings::Init() {
32 CHECK(!initialized_);
33 auto command_line = base::CommandLine::ForCurrentProcess();
Arman Ugurayfe65fb72015-07-24 19:14:42 -070034 const auto& switches = command_line->GetSwitches();
Arman Uguray065d0f72015-07-16 18:12:13 -070035
Arman Ugurayfe65fb72015-07-24 19:14:42 -070036 for (const auto& iter : switches) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070037 if (iter.first == switches::kCreateIPCSocketPath) {
38 // kCreateIPCSocketPath: An optional argument that initializes an IPC
39 // socket path for IPC.
Arman Ugurayfe65fb72015-07-24 19:14:42 -070040 base::FilePath path(iter.second);
41 if (path.empty() || path.EndsWithSeparator()) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070042 LOG(ERROR) << "Invalid IPC create socket path";
Arman Ugurayfe65fb72015-07-24 19:14:42 -070043 return false;
44 }
Arman Uguray065d0f72015-07-16 18:12:13 -070045
Ian Coolidgec6760d82015-07-30 20:51:47 -070046 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;
Arman Uguray503baaa2015-08-20 11:49:00 -070057 }
58 // Check for libbase logging switches. These get processed by
59 // logging::InitLogging directly.
60 else if (iter.first != ::switches::kV) {
Arman Uguray8531fc32015-11-17 19:29:31 -080061 LOG(ERROR) << "Unexpected command-line switches found: " << iter.first;
Arman Ugurayfe65fb72015-07-24 19:14:42 -070062 return false;
63 }
Arman Uguray065d0f72015-07-16 18:12:13 -070064 }
65
Ian Coolidgec6760d82015-07-30 20:51:47 -070066 // Two IPC methods/paths were provided.
Myles Watson911d1ae2016-11-28 16:44:40 -080067 if (!android_ipc_socket_suffix_.empty() && !create_ipc_socket_path_.empty()) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070068 LOG(ERROR) << "Too many IPC methods provided";
69 return false;
70 }
71
Arman Uguray065d0f72015-07-16 18:12:13 -070072 // The daemon has no arguments
73 if (command_line->GetArgs().size()) {
74 LOG(ERROR) << "Unexpected command-line arguments found";
75 return false;
76 }
77
Arman Uguray065d0f72015-07-16 18:12:13 -070078 initialized_ = true;
79 return true;
80}
81
82} // namespace bluetooth