blob: 874fc4e529a226efeedd1aaebe80e121fe5de042 [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>
20#include <base/lazy_instance.h>
21#include <base/logging.h>
22
23#include "service/switches.h"
24
25namespace bluetooth {
26
27namespace {
28
29// The global settings instance. We use a LazyInstance here so that we can
30// lazily initialize the instance AND guarantee that it will be cleaned up at
31// exit time without violating the Google C++ style guide.
32base::LazyInstance<Settings> g_settings = LAZY_INSTANCE_INITIALIZER;
33
34void LogRequiredOption(const std::string& option) {
35 LOG(ERROR) << "Required option: \"" << option << "\"";
36}
37
38} // namespace
39
40// static
41bool Settings::Initialize() {
42 return g_settings.Get().Init();
43}
44
45// static
46const Settings& Settings::Get() {
47 CHECK(g_settings.Get().initialized_);
48 return g_settings.Get();
49}
50
51Settings::Settings() : initialized_(false) {
52}
53
54Settings::~Settings() {
55}
56
57bool Settings::Init() {
58 CHECK(!initialized_);
59 auto command_line = base::CommandLine::ForCurrentProcess();
60
61 // Since we have only one meaningful command-line flag for now, it's OK to
62 // hard-code this here. As we add more switches, we should process this in a
63 // more meaningful way.
64 if (command_line->GetSwitches().size() > 1) {
65 LOG(ERROR) << "Unexpected command-line switches found";
66 return false;
67 }
68
69 if (!command_line->HasSwitch(switches::kIPCSocketPath)) {
70 LogRequiredOption(switches::kIPCSocketPath);
71 return false;
72 }
73
74 base::FilePath path = command_line->GetSwitchValuePath(
75 switches::kIPCSocketPath);
76 if (path.value().empty() || path.EndsWithSeparator()) {
77 LOG(ERROR) << "Invalid IPC socket path";
78 return false;
79 }
80
81 // The daemon has no arguments
82 if (command_line->GetArgs().size()) {
83 LOG(ERROR) << "Unexpected command-line arguments found";
84 return false;
85 }
86
87 ipc_socket_path_ = path;
88
89 initialized_ = true;
90 return true;
91}
92
93} // namespace bluetooth