diff options
author | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-31 19:23:14 +0000 |
---|---|---|
committer | hclam@chromium.org <hclam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-08-31 19:23:14 +0000 |
commit | 1e1fd49cfd5b770a7bdba08bfbba321748325f31 (patch) | |
tree | a7ff5c758fd389beaac2c7ac8ffc4b96635a383f | |
parent | b21d691e4e486071ac8601fee7867c29729faca6 (diff) | |
download | chromium_src-1e1fd49cfd5b770a7bdba08bfbba321748325f31.zip chromium_src-1e1fd49cfd5b770a7bdba08bfbba321748325f31.tar.gz chromium_src-1e1fd49cfd5b770a7bdba08bfbba321748325f31.tar.bz2 |
Use named events instead of lock files for service process
Use named events to signal that a service process is running.
BUG=52891
TEST=browser_tests --gtest_filter=ServiceProcess*
Review URL: http://codereview.chromium.org/3268003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@58041 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/service/service_process_control.cc | 6 | ||||
-rw-r--r-- | chrome/common/service_process_util.cc | 56 | ||||
-rw-r--r-- | chrome/common/service_process_util.h | 20 | ||||
-rw-r--r-- | chrome/service/service_process.cc | 16 |
4 files changed, 62 insertions, 36 deletions
diff --git a/chrome/browser/service/service_process_control.cc b/chrome/browser/service/service_process_control.cc index 97e277b..79e8675 100644 --- a/chrome/browser/service/service_process_control.cc +++ b/chrome/browser/service/service_process_control.cc @@ -50,13 +50,7 @@ class ServiceProcessControl::Launcher } void DoDetectLaunched(Task* task) { - // TODO(hclam): We need to improve the method we are using to connect to - // the service process. The approach we are using here is to check for - // the existence of the service process lock file created after the service - // process is fully launched. if (CheckServiceProcessRunning(kServiceProcessCloudPrint)) { - // After the process is launched we listen on the file system for the - // service process lock file to detect the service process has launched. ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(this, &Launcher::Notify, task)); return; diff --git a/chrome/common/service_process_util.cc b/chrome/common/service_process_util.cc index 4be6991..fe600fc 100644 --- a/chrome/common/service_process_util.cc +++ b/chrome/common/service_process_util.cc @@ -10,6 +10,11 @@ #include "chrome/common/chrome_paths.h" #include "chrome/common/service_process_util.h" +#if defined(OS_WIN) +#include "base/scoped_handle_win.h" +#endif + +// TODO(hclam): Split this file for different platforms. // TODO(hclam): |type| is not used at all. Different types of service process // should have a different instance of process and channel. std::string GetServiceProcessChannelName(ServiceProcessType type) { @@ -32,24 +37,63 @@ std::string GetServiceProcessChannelName(ServiceProcessType type) { static FilePath GetServiceProcessLockFilePath(ServiceProcessType type) { FilePath user_data_dir; PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); - return user_data_dir.Append(FILE_PATH_LITERAL("Service Process.Lock")); + return user_data_dir.Append(FILE_PATH_LITERAL("Service Process Lock")); +} + +#if defined(OS_WIN) +static std::wstring GetServiceProcessEventName( + ServiceProcessType type) { + FilePath path = GetServiceProcessLockFilePath(type); + std::wstring event_name = path.value(); + std::replace(event_name.begin(), event_name.end(), '\\', '!'); + return event_name; } -bool CreateServiceProcessLockFile(ServiceProcessType type) { +// An event that is signaled when a service process is running. This +// variable is used only in the service process. +static ScopedHandle service_process_running_event; +#endif + +void SignalServiceProcessRunning(ServiceProcessType type) { +#if defined(OS_WIN) + std::wstring event_name = GetServiceProcessEventName(type); + service_process_running_event.Set( + CreateEvent(NULL, true, true, event_name.c_str())); + DCHECK(service_process_running_event.IsValid()); +#else + // TODO(hclam): Implement better mechanism for these platform. const FilePath path = GetServiceProcessLockFilePath(type); FILE* file = file_util::OpenFile(path, "wb+"); if (!file) - return false; + return; LOG(INFO) << "Created Service Process lock file: " << path.value(); - return file_util::TruncateFile(file) && file_util::CloseFile(file); + file_util::TruncateFile(file) && file_util::CloseFile(file); +#endif } -bool DeleteServiceProcessLockFile(ServiceProcessType type) { +void SignalServiceProcessStopped(ServiceProcessType type) { +#if defined(OS_WIN) + // Close the handle to the event. + service_process_running_event.Close(); +#else + // TODO(hclam): Implement better mechanism for these platform. const FilePath path = GetServiceProcessLockFilePath(type); - return file_util::Delete(path, false); + file_util::Delete(path, false); +#endif } bool CheckServiceProcessRunning(ServiceProcessType type) { +#if defined(OS_WIN) + std::wstring event_name = GetServiceProcessEventName(type); + ScopedHandle event( + OpenEvent(SYNCHRONIZE | READ_CONTROL, false, event_name.c_str())); + if (!event.IsValid()) + return false; + // Check if the event is signaled. + return WaitForSingleObject(event, 0) == WAIT_OBJECT_0; +#else + // TODO(hclam): Implement better mechanism for these platform. const FilePath path = GetServiceProcessLockFilePath(type); return file_util::PathExists(path); +#endif } diff --git a/chrome/common/service_process_util.h b/chrome/common/service_process_util.h index 9894b1e..4ea6106 100644 --- a/chrome/common/service_process_util.h +++ b/chrome/common/service_process_util.h @@ -20,24 +20,14 @@ std::string GetServiceProcessChannelName(ServiceProcessType type); // The following methods are used as a mechanism to signal a service process // is running properly and all initialized. // -// The way it works is that we will create a file on the file system to indicate -// that service process is running. This way the browser process will know that -// it can connect to it without problem. -// -// When the service process this lock file is deleted. - +// Signal that the service process is running. // This method is called when the service process is running and initialized. -// Return true if lock file was created. -bool CreateServiceProcessLockFile(ServiceProcessType type); +void SignalServiceProcessRunning(ServiceProcessType type); -// This method deletes the lock file created by this above method. -// Return true if lock file was deleted. -bool DeleteServiceProcessLockFile(ServiceProcessType type); +// Signal that the service process is stopped. +void SignalServiceProcessStopped(ServiceProcessType type); -// This method checks that if the service process lock file exists. This means -// that the service process is running. -// TODO(hclam): We should use a better mechanism to detect that a service -// process is running. +// This method checks that if the service process is running. bool CheckServiceProcessRunning(ServiceProcessType type); #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_ diff --git a/chrome/service/service_process.cc b/chrome/service/service_process.cc index 9ebed07..4fd8f70 100644 --- a/chrome/service/service_process.cc +++ b/chrome/service/service_process.cc @@ -72,7 +72,7 @@ bool ServiceProcess::Initialize(MessageLoop* message_loop) { // Check if remoting host is already enabled. if (values->GetBoolean(prefs::kRemotingHostEnabled, &remoting_host_enabled) && - remoting_host_enabled) { + remoting_host_enabled) { // If true then we start the host. StartChromotingHost(); } @@ -85,11 +85,10 @@ bool ServiceProcess::Initialize(MessageLoop* message_loop) { GetServiceProcessChannelName(kServiceProcessCloudPrint))); ipc_server_->Init(); - // After the IPC server has started we can create the lock file to indicate - // that we have started. - bool ret = CreateServiceProcessLockFile(kServiceProcessCloudPrint); - DCHECK(ret) << "Failed to create service process lock file."; - return ret; + // After the IPC server has started we signal that the service process is + // running. + SignalServiceProcessRunning(kServiceProcessCloudPrint); + return true; } bool ServiceProcess::Teardown() { @@ -114,9 +113,8 @@ bool ServiceProcess::Teardown() { network_change_notifier_.reset(); // Delete the service process lock file when it shuts down. - bool ret = DeleteServiceProcessLockFile(kServiceProcessCloudPrint); - DCHECK(ret) << "Failed to delete service process lock file."; - return ret; + SignalServiceProcessStopped(kServiceProcessCloudPrint); + return true; } void ServiceProcess::Shutdown() { |