This source file includes following definitions.
- GetServiceProcessSharedMemName
- GetServiceProcessRunningState
- GetServiceProcessScopedName
- GetServiceProcessScopedVersionedName
- GetServiceProcessData
- SignalStopped
- Initialize
- HandleOtherVersion
- CreateSharedData
- GetServiceProcessChannel
- CreateAutoRunCommandLine
#include <algorithm>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/sha1.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/version.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/service_process_util.h"
#include "content/public/common/content_paths.h"
#if !defined(OS_MACOSX)
namespace {
const uint32 kMaxVersionStringLength = 256;
struct ServiceProcessSharedData {
char service_process_version[kMaxVersionStringLength];
base::ProcessId service_process_pid;
};
std::string GetServiceProcessSharedMemName() {
return GetServiceProcessScopedName("_service_shmem");
}
enum ServiceProcessRunningState {
SERVICE_NOT_RUNNING,
SERVICE_OLDER_VERSION_RUNNING,
SERVICE_SAME_VERSION_RUNNING,
SERVICE_NEWER_VERSION_RUNNING,
};
ServiceProcessRunningState GetServiceProcessRunningState(
std::string* service_version_out, base::ProcessId* pid_out) {
std::string version;
if (!GetServiceProcessData(&version, pid_out))
return SERVICE_NOT_RUNNING;
#if defined(OS_POSIX)
if (!CheckServiceProcessReady()) {
return SERVICE_NOT_RUNNING;
}
#endif
if (service_version_out)
*service_version_out = version;
Version service_version(version);
if (!service_version.IsValid())
return SERVICE_OLDER_VERSION_RUNNING;
chrome::VersionInfo version_info;
if (!version_info.is_valid()) {
NOTREACHED() << "Failed to get current file version";
return SERVICE_NEWER_VERSION_RUNNING;
}
Version running_version(version_info.Version());
if (!running_version.IsValid()) {
NOTREACHED() << "Failed to parse version info";
return SERVICE_NEWER_VERSION_RUNNING;
}
if (running_version.CompareTo(service_version) > 0) {
return SERVICE_OLDER_VERSION_RUNNING;
} else if (service_version.CompareTo(running_version) > 0) {
return SERVICE_NEWER_VERSION_RUNNING;
}
return SERVICE_SAME_VERSION_RUNNING;
}
}
std::string GetServiceProcessScopedName(const std::string& append_str) {
base::FilePath user_data_dir;
PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
#if defined(OS_WIN)
std::string user_data_dir_path = base::WideToUTF8(user_data_dir.value());
#elif defined(OS_POSIX)
std::string user_data_dir_path = user_data_dir.value();
#endif
std::string hash = base::SHA1HashString(user_data_dir_path);
std::string hex_hash = base::HexEncode(hash.c_str(), hash.length());
return hex_hash + "." + append_str;
}
std::string GetServiceProcessScopedVersionedName(
const std::string& append_str) {
std::string versioned_str;
chrome::VersionInfo version_info;
DCHECK(version_info.is_valid());
versioned_str.append(version_info.Version());
versioned_str.append(append_str);
return GetServiceProcessScopedName(versioned_str);
}
bool GetServiceProcessData(std::string* version, base::ProcessId* pid) {
scoped_ptr<base::SharedMemory> shared_mem_service_data;
shared_mem_service_data.reset(new base::SharedMemory());
ServiceProcessSharedData* service_data = NULL;
if (shared_mem_service_data.get() &&
shared_mem_service_data->Open(GetServiceProcessSharedMemName(), true) &&
shared_mem_service_data->Map(sizeof(ServiceProcessSharedData))) {
service_data = reinterpret_cast<ServiceProcessSharedData*>(
shared_mem_service_data->memory());
if (version && memchr(service_data->service_process_version, '\0',
sizeof(service_data->service_process_version)))
*version = service_data->service_process_version;
if (pid)
*pid = service_data->service_process_pid;
return true;
}
return false;
}
#endif
ServiceProcessState::ServiceProcessState() : state_(NULL) {
CreateAutoRunCommandLine();
CreateState();
}
ServiceProcessState::~ServiceProcessState() {
#if !defined(OS_MACOSX)
if (shared_mem_service_data_.get()) {
shared_mem_service_data_->Delete(GetServiceProcessSharedMemName());
}
#endif
TearDownState();
}
void ServiceProcessState::SignalStopped() {
TearDownState();
shared_mem_service_data_.reset();
}
#if !defined(OS_MACOSX)
bool ServiceProcessState::Initialize() {
if (!TakeSingletonLock()) {
return false;
}
if (!HandleOtherVersion())
return false;
return CreateSharedData();
}
bool ServiceProcessState::HandleOtherVersion() {
std::string running_version;
base::ProcessId process_id = 0;
ServiceProcessRunningState state =
GetServiceProcessRunningState(&running_version, &process_id);
switch (state) {
case SERVICE_SAME_VERSION_RUNNING:
case SERVICE_NEWER_VERSION_RUNNING:
return false;
case SERVICE_OLDER_VERSION_RUNNING:
ForceServiceProcessShutdown(running_version, process_id);
break;
case SERVICE_NOT_RUNNING:
break;
}
return true;
}
bool ServiceProcessState::CreateSharedData() {
chrome::VersionInfo version_info;
if (!version_info.is_valid()) {
NOTREACHED() << "Failed to get current file version";
return false;
}
if (version_info.Version().length() >= kMaxVersionStringLength) {
NOTREACHED() << "Version string length is << " <<
version_info.Version().length() << "which is longer than" <<
kMaxVersionStringLength;
return false;
}
scoped_ptr<base::SharedMemory> shared_mem_service_data(
new base::SharedMemory());
if (!shared_mem_service_data.get())
return false;
uint32 alloc_size = sizeof(ServiceProcessSharedData);
if (!shared_mem_service_data->CreateNamedDeprecated
(GetServiceProcessSharedMemName(), true, alloc_size))
return false;
if (!shared_mem_service_data->Map(alloc_size))
return false;
memset(shared_mem_service_data->memory(), 0, alloc_size);
ServiceProcessSharedData* shared_data =
reinterpret_cast<ServiceProcessSharedData*>(
shared_mem_service_data->memory());
memcpy(shared_data->service_process_version, version_info.Version().c_str(),
version_info.Version().length());
shared_data->service_process_pid = base::GetCurrentProcId();
shared_mem_service_data_.reset(shared_mem_service_data.release());
return true;
}
IPC::ChannelHandle ServiceProcessState::GetServiceProcessChannel() {
return ::GetServiceProcessChannel();
}
#endif
void ServiceProcessState::CreateAutoRunCommandLine() {
base::FilePath exe_path;
PathService::Get(content::CHILD_PROCESS_EXE, &exe_path);
DCHECK(!exe_path.empty()) << "Unable to get service process binary name.";
autorun_command_line_.reset(new CommandLine(exe_path));
autorun_command_line_->AppendSwitchASCII(switches::kProcessType,
switches::kServiceProcess);
const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
base::FilePath user_data_dir =
browser_command_line.GetSwitchValuePath(switches::kUserDataDir);
if (!user_data_dir.empty())
autorun_command_line_->AppendSwitchPath(switches::kUserDataDir,
user_data_dir);
}