summaryrefslogtreecommitdiffstats
path: root/chrome/common/multi_process_lock_mac.cc
diff options
context:
space:
mode:
authordmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-16 21:14:58 +0000
committerdmaclach@chromium.org <dmaclach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-11-16 21:14:58 +0000
commit293fd7a9eb27dac7599a752e847c5b15f449519f (patch)
treeacc6e4a4c1d5d2ea551a64b92e8223dc94bad728 /chrome/common/multi_process_lock_mac.cc
parentc18215ab75f2f05e1da3f901fd385393a5d8def6 (diff)
downloadchromium_src-293fd7a9eb27dac7599a752e847c5b15f449519f.zip
chromium_src-293fd7a9eb27dac7599a752e847c5b15f449519f.tar.gz
chromium_src-293fd7a9eb27dac7599a752e847c5b15f449519f.tar.bz2
Add multi_process_lock to base
Platform abstraction for a shared lock between processes. The process that owns the lock will release it on exit even if exit is due to a crash. For cloud-print and remoting we want to be able to have a singleton service-process that can run independently of the browser process. This service process will communicate with the browser process via the standard IAC channels, but we want to have a way of signaling to other processes that a) there is a service-process running and b) that it is in a state where it is ready to be communicated with. The multi_process_lock class is intended to work as a simple flag that can be queried from multiple processes. If the service-process should crash, we would like the flag to be cleared automatically so that there is never confusion about the state of the service-process. Other approaches considered for some Unix/Mac: - Standard unix domain sockets depend on the file system and don't clean up properly in the case of a crash. - Shared memory on unix depend on the file system and don't clean up properly in the case of a crash. - System V semaphores on unix again depend on the file system. - named_mach_ports on Mac OS. Bootstrap_register_name has been deprecated on 10.6, so we are doing essentially the same thing using CFMessagePort. On Windows it is implemented as an event. On Mac it is implemented using a CFMessagePort name. On Linux it is implement using an abstract name port socket. TEST=none BUG=none Review URL: http://codereview.chromium.org/4721001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66323 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/multi_process_lock_mac.cc')
-rw-r--r--chrome/common/multi_process_lock_mac.cc54
1 files changed, 54 insertions, 0 deletions
diff --git a/chrome/common/multi_process_lock_mac.cc b/chrome/common/multi_process_lock_mac.cc
new file mode 100644
index 0000000..9a16d71
--- /dev/null
+++ b/chrome/common/multi_process_lock_mac.cc
@@ -0,0 +1,54 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/multi_process_lock.h"
+
+#include "base/logging.h"
+#include "base/mac/scoped_cftyperef.h"
+#include "base/sys_string_conversions.h"
+
+class MultiProcessLockMac : public MultiProcessLock {
+ public:
+ explicit MultiProcessLockMac(const std::string& name) : name_(name) { }
+
+ virtual ~MultiProcessLockMac() {
+ if (port_ != NULL) {
+ Unlock();
+ }
+ }
+
+ virtual bool TryLock() {
+ if (port_ != NULL) {
+ DLOG(ERROR) << "MultiProcessLock is already locked - " << name_;
+ return true;
+ }
+
+ if (name_.length() > MULTI_PROCESS_LOCK_NAME_MAX_LEN) {
+ LOG(ERROR) << "Socket name too long - " << name_;
+ return false;
+ }
+
+ CFStringRef cf_name(base::SysUTF8ToCFStringRef(name_));
+ base::mac::ScopedCFTypeRef<CFStringRef> scoped_cf_name(cf_name);
+ port_.reset(CFMessagePortCreateLocal(NULL, cf_name, NULL, NULL, NULL));
+ return port_ != NULL;
+ }
+
+ virtual void Unlock() {
+ if (port_ == NULL) {
+ DLOG(ERROR) << "Over-unlocked MultiProcessLock - " << name_;
+ return;
+ }
+ port_.reset();
+ }
+
+ private:
+ std::string name_;
+ base::mac::ScopedCFTypeRef<CFMessagePortRef> port_;
+ DISALLOW_COPY_AND_ASSIGN(MultiProcessLockMac);
+};
+
+MultiProcessLock* MultiProcessLock::Create(const std::string &name) {
+ return new MultiProcessLockMac(name);
+}