Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / common / service_process_util.cc
blob9bb3464ca8865b58d6f523880e24bbab1de3d90f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/service_process_util.h"
7 #include <algorithm>
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "base/memory/singleton.h"
12 #include "base/path_service.h"
13 #include "base/sha1.h"
14 #include "base/strings/string16.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/version.h"
19 #include "chrome/common/chrome_constants.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "components/cloud_devices/common/cloud_devices_switches.h"
23 #include "components/version_info/version_info.h"
24 #include "content/public/common/content_paths.h"
25 #include "google_apis/gaia/gaia_switches.h"
26 #include "ui/base/ui_base_switches.h"
28 #if !defined(OS_MACOSX)
30 namespace {
32 // This should be more than enough to hold a version string assuming each part
33 // of the version string is an int64.
34 const uint32 kMaxVersionStringLength = 256;
36 // The structure that gets written to shared memory.
37 struct ServiceProcessSharedData {
38 char service_process_version[kMaxVersionStringLength];
39 base::ProcessId service_process_pid;
42 // Gets the name of the shared memory used by the service process to write its
43 // version. The name is not versioned.
44 std::string GetServiceProcessSharedMemName() {
45 return GetServiceProcessScopedName("_service_shmem");
48 enum ServiceProcessRunningState {
49 SERVICE_NOT_RUNNING,
50 SERVICE_OLDER_VERSION_RUNNING,
51 SERVICE_SAME_VERSION_RUNNING,
52 SERVICE_NEWER_VERSION_RUNNING,
55 ServiceProcessRunningState GetServiceProcessRunningState(
56 std::string* service_version_out, base::ProcessId* pid_out) {
57 std::string version;
58 if (!GetServiceProcessData(&version, pid_out))
59 return SERVICE_NOT_RUNNING;
61 #if defined(OS_POSIX)
62 // We only need to check for service running on POSIX because Windows cleans
63 // up shared memory files when an app crashes, so there isn't a chance of
64 // us reading bogus data from shared memory for an app that has died.
65 if (!CheckServiceProcessReady()) {
66 return SERVICE_NOT_RUNNING;
68 #endif // defined(OS_POSIX)
70 // At this time we have a version string. Set the out param if it exists.
71 if (service_version_out)
72 *service_version_out = version;
74 Version service_version(version);
75 // If the version string is invalid, treat it like an older version.
76 if (!service_version.IsValid())
77 return SERVICE_OLDER_VERSION_RUNNING;
79 // Get the version of the currently *running* instance of Chrome.
80 Version running_version(version_info::GetVersionNumber());
81 if (!running_version.IsValid()) {
82 NOTREACHED() << "Failed to parse version info";
83 // Our own version is invalid. This is an error case. Pretend that we
84 // are out of date.
85 return SERVICE_NEWER_VERSION_RUNNING;
88 if (running_version.CompareTo(service_version) > 0) {
89 return SERVICE_OLDER_VERSION_RUNNING;
90 } else if (service_version.CompareTo(running_version) > 0) {
91 return SERVICE_NEWER_VERSION_RUNNING;
93 return SERVICE_SAME_VERSION_RUNNING;
96 } // namespace
99 // Return a name that is scoped to this instance of the service process. We
100 // use the user-data-dir and the version as a scoping prefix.
101 std::string GetServiceProcessScopedVersionedName(
102 const std::string& append_str) {
103 std::string versioned_str;
104 versioned_str.append(version_info::GetVersionNumber());
105 versioned_str.append(append_str);
106 return GetServiceProcessScopedName(versioned_str);
109 // Reads the named shared memory to get the shared data. Returns false if no
110 // matching shared memory was found.
111 bool GetServiceProcessData(std::string* version, base::ProcessId* pid) {
112 scoped_ptr<base::SharedMemory> shared_mem_service_data;
113 shared_mem_service_data.reset(new base::SharedMemory());
114 ServiceProcessSharedData* service_data = NULL;
115 if (shared_mem_service_data.get() &&
116 shared_mem_service_data->Open(GetServiceProcessSharedMemName(), true) &&
117 shared_mem_service_data->Map(sizeof(ServiceProcessSharedData))) {
118 service_data = reinterpret_cast<ServiceProcessSharedData*>(
119 shared_mem_service_data->memory());
120 // Make sure the version in shared memory is null-terminated. If it is not,
121 // treat it as invalid.
122 if (version && memchr(service_data->service_process_version, '\0',
123 sizeof(service_data->service_process_version)))
124 *version = service_data->service_process_version;
125 if (pid)
126 *pid = service_data->service_process_pid;
127 return true;
129 return false;
132 #endif // !OS_MACOSX
134 // Return a name that is scoped to this instance of the service process. We
135 // use the hash of the user-data-dir as a scoping prefix. We can't use
136 // the user-data-dir itself as we have limits on the size of the lock names.
137 std::string GetServiceProcessScopedName(const std::string& append_str) {
138 base::FilePath user_data_dir;
139 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
140 #if defined(OS_WIN)
141 std::string user_data_dir_path = base::WideToUTF8(user_data_dir.value());
142 #elif defined(OS_POSIX)
143 std::string user_data_dir_path = user_data_dir.value();
144 #endif // defined(OS_WIN)
145 std::string hash = base::SHA1HashString(user_data_dir_path);
146 std::string hex_hash = base::HexEncode(hash.c_str(), hash.length());
147 return hex_hash + "." + append_str;
150 scoped_ptr<base::CommandLine> CreateServiceProcessCommandLine() {
151 base::FilePath exe_path;
152 PathService::Get(content::CHILD_PROCESS_EXE, &exe_path);
153 DCHECK(!exe_path.empty()) << "Unable to get service process binary name.";
154 scoped_ptr<base::CommandLine> command_line(new base::CommandLine(exe_path));
155 command_line->AppendSwitchASCII(switches::kProcessType,
156 switches::kServiceProcess);
157 static const char* const kSwitchesToCopy[] = {
158 switches::kCloudPrintSetupProxy,
159 switches::kCloudPrintURL,
160 switches::kCloudPrintXmppEndpoint,
161 #if defined(OS_WIN)
162 switches::kEnableCloudPrintXps,
163 #endif
164 switches::kEnableLogging,
165 switches::kIgnoreUrlFetcherCertRequests,
166 switches::kLang,
167 switches::kLoggingLevel,
168 switches::kLsoUrl,
169 switches::kNoServiceAutorun,
170 switches::kUserDataDir,
171 switches::kV,
172 switches::kVModule,
173 switches::kWaitForDebugger,
176 command_line->CopySwitchesFrom(*base::CommandLine::ForCurrentProcess(),
177 kSwitchesToCopy,
178 arraysize(kSwitchesToCopy));
179 return command_line.Pass();
182 ServiceProcessState::ServiceProcessState() : state_(NULL) {
183 autorun_command_line_ = CreateServiceProcessCommandLine();
184 CreateState();
187 ServiceProcessState::~ServiceProcessState() {
188 #if !defined(OS_MACOSX)
189 if (shared_mem_service_data_.get()) {
190 shared_mem_service_data_->Delete(GetServiceProcessSharedMemName());
192 #endif // !OS_MACOSX
193 TearDownState();
196 void ServiceProcessState::SignalStopped() {
197 TearDownState();
198 shared_mem_service_data_.reset();
201 #if !defined(OS_MACOSX)
202 bool ServiceProcessState::Initialize() {
203 if (!TakeSingletonLock()) {
204 return false;
206 // Now that we have the singleton, take care of killing an older version, if
207 // it exists.
208 if (!HandleOtherVersion())
209 return false;
211 // Write the version we are using to shared memory. This can be used by a
212 // newer service to signal us to exit.
213 return CreateSharedData();
216 bool ServiceProcessState::HandleOtherVersion() {
217 std::string running_version;
218 base::ProcessId process_id = 0;
219 ServiceProcessRunningState state =
220 GetServiceProcessRunningState(&running_version, &process_id);
221 switch (state) {
222 case SERVICE_SAME_VERSION_RUNNING:
223 case SERVICE_NEWER_VERSION_RUNNING:
224 return false;
225 case SERVICE_OLDER_VERSION_RUNNING:
226 // If an older version is running, kill it.
227 ForceServiceProcessShutdown(running_version, process_id);
228 break;
229 case SERVICE_NOT_RUNNING:
230 break;
232 return true;
235 bool ServiceProcessState::CreateSharedData() {
236 if (version_info::GetVersionNumber().length() >= kMaxVersionStringLength) {
237 NOTREACHED() << "Version string length is << "
238 << version_info::GetVersionNumber().length()
239 << " which is longer than" << kMaxVersionStringLength;
240 return false;
243 scoped_ptr<base::SharedMemory> shared_mem_service_data(
244 new base::SharedMemory());
245 if (!shared_mem_service_data.get())
246 return false;
248 uint32 alloc_size = sizeof(ServiceProcessSharedData);
249 // TODO(viettrungluu): Named shared memory is deprecated (crbug.com/345734).
250 if (!shared_mem_service_data->CreateNamedDeprecated
251 (GetServiceProcessSharedMemName(), true, alloc_size))
252 return false;
254 if (!shared_mem_service_data->Map(alloc_size))
255 return false;
257 memset(shared_mem_service_data->memory(), 0, alloc_size);
258 ServiceProcessSharedData* shared_data =
259 reinterpret_cast<ServiceProcessSharedData*>(
260 shared_mem_service_data->memory());
261 memcpy(shared_data->service_process_version,
262 version_info::GetVersionNumber().c_str(),
263 version_info::GetVersionNumber().length());
264 shared_data->service_process_pid = base::GetCurrentProcId();
265 shared_mem_service_data_.reset(shared_mem_service_data.release());
266 return true;
269 IPC::ChannelHandle ServiceProcessState::GetServiceProcessChannel() {
270 return ::GetServiceProcessChannel();
273 #endif // !OS_MACOSX