blob: 66235e775c368b3af016a369dc21fac331700541 [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
19#include <base/command_line.h>
Arman Uguray065d0f72015-07-16 18:12:13 -070020#include <base/logging.h>
21
22#include "service/switches.h"
23
24namespace bluetooth {
25
Arman Uguray065d0f72015-07-16 18:12:13 -070026Settings::Settings() : initialized_(false) {
27}
28
29Settings::~Settings() {
30}
31
32bool Settings::Init() {
33 CHECK(!initialized_);
34 auto command_line = base::CommandLine::ForCurrentProcess();
Arman Ugurayfe65fb72015-07-24 19:14:42 -070035 const auto& switches = command_line->GetSwitches();
Arman Uguray065d0f72015-07-16 18:12:13 -070036
Arman Ugurayfe65fb72015-07-24 19:14:42 -070037 for (const auto& iter : switches) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070038 if (iter.first == switches::kCreateIPCSocketPath) {
39 // kCreateIPCSocketPath: An optional argument that initializes an IPC
40 // socket path for IPC.
Arman Ugurayfe65fb72015-07-24 19:14:42 -070041 base::FilePath path(iter.second);
42 if (path.empty() || path.EndsWithSeparator()) {
Ian Coolidgec6760d82015-07-30 20:51:47 -070043 LOG(ERROR) << "Invalid IPC create socket path";
Arman Ugurayfe65fb72015-07-24 19:14:42 -070044 return false;
45 }
Arman Uguray065d0f72015-07-16 18:12:13 -070046
Ian Coolidgec6760d82015-07-30 20:51:47 -070047 create_ipc_socket_path_ = path;
48 } else if (iter.first == switches::kAndroidIPCSocketSuffix) {
49 // kAndroidIPCSocketSuffix: An optional argument used to express
50 // a socket that Android init created for us. We bind to this.
51 const std::string& suffix = iter.second;
52 if (suffix.empty()) {
53 LOG(ERROR) << "Invalid Android socket suffix";
54 return false;
55 }
56
57 android_ipc_socket_suffix_ = suffix;
Arman Ugurayfe65fb72015-07-24 19:14:42 -070058 } else {
59 LOG(ERROR) << "Unexpected command-line switches found";
60 return false;
61 }
Arman Uguray065d0f72015-07-16 18:12:13 -070062 }
63
Ian Coolidgec6760d82015-07-30 20:51:47 -070064 // Two IPC methods/paths were provided.
65 if (!android_ipc_socket_suffix_.empty() &&
66 !create_ipc_socket_path_.empty()) {
67 LOG(ERROR) << "Too many IPC methods provided";
68 return false;
69 }
70
Arman Uguray065d0f72015-07-16 18:12:13 -070071 // The daemon has no arguments
72 if (command_line->GetArgs().size()) {
73 LOG(ERROR) << "Unexpected command-line arguments found";
74 return false;
75 }
76
Arman Uguray065d0f72015-07-16 18:12:13 -070077 initialized_ = true;
78 return true;
79}
80
81} // namespace bluetooth