| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/file.h> | 7 #include <sys/file.h> |
| 8 | 8 |
| 9 #include "chrome/browser/process_singleton.h" | 9 #include "chrome/browser/process_singleton.h" |
| 10 | 10 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 lock_path_(user_data_dir.Append(chrome::kSingletonLockFilename)), | 44 lock_path_(user_data_dir.Append(chrome::kSingletonLockFilename)), |
| 45 lock_fd_(-1) { | 45 lock_fd_(-1) { |
| 46 } | 46 } |
| 47 | 47 |
| 48 ProcessSingleton::~ProcessSingleton() { | 48 ProcessSingleton::~ProcessSingleton() { |
| 49 // Make sure the lock is released. Process death will also release | 49 // Make sure the lock is released. Process death will also release |
| 50 // it, even if this is not called. | 50 // it, even if this is not called. |
| 51 Cleanup(); | 51 Cleanup(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { | |
| 55 // This space intentionally left blank. | |
| 56 return PROCESS_NONE; | |
| 57 } | |
| 58 | |
| 59 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate( | 54 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate( |
| 60 const NotificationCallback& notification_callback) { | 55 const NotificationCallback& notification_callback) { |
| 61 // Windows tries NotifyOtherProcess() first. | 56 // Windows tries NotifyOtherProcess() first. |
| 62 return Create(notification_callback) ? PROCESS_NONE : PROFILE_IN_USE; | 57 return Create(notification_callback) ? PROCESS_NONE : PROFILE_IN_USE; |
| 63 } | 58 } |
| 64 | 59 |
| 60 void ProcessSingleton::Cleanup() { |
| 61 // Closing the file also releases the lock. |
| 62 if (lock_fd_ != -1) { |
| 63 int rc = HANDLE_EINTR(close(lock_fd_)); |
| 64 DPCHECK(!rc) << "Closing lock_fd_:"; |
| 65 } |
| 66 lock_fd_ = -1; |
| 67 } |
| 68 |
| 69 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { |
| 70 // This space intentionally left blank. |
| 71 return PROCESS_NONE; |
| 72 } |
| 73 |
| 65 // Attempt to acquire an exclusive lock on an empty file in the | 74 // Attempt to acquire an exclusive lock on an empty file in the |
| 66 // profile directory. Returns |true| if it gets the lock. Returns | 75 // profile directory. Returns |true| if it gets the lock. Returns |
| 67 // |false| if the lock is held, or if there is an error. | 76 // |false| if the lock is held, or if there is an error. |
| 68 // |notification_callback| is not actually used. See the comments at the top of | 77 // |notification_callback| is not actually used. See the comments at the top of |
| 69 // this file for details. | 78 // this file for details. |
| 70 // TODO(shess): Rather than logging failures, popup an alert. Punting | 79 // TODO(shess): Rather than logging failures, popup an alert. Punting |
| 71 // that for now because it would require confidence that this code is | 80 // that for now because it would require confidence that this code is |
| 72 // never called in a situation where an alert wouldn't work. | 81 // never called in a situation where an alert wouldn't work. |
| 73 // http://crbug.com/59061 | 82 // http://crbug.com/59061 |
| 74 bool ProcessSingleton::Create( | 83 bool ProcessSingleton::Create( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 102 return false; | 111 return false; |
| 103 } | 112 } |
| 104 | 113 |
| 105 // The file is open by another process and locked. | 114 // The file is open by another process and locked. |
| 106 LOG(ERROR) << "Unable to obtain profile lock."; | 115 LOG(ERROR) << "Unable to obtain profile lock."; |
| 107 return false; | 116 return false; |
| 108 } | 117 } |
| 109 | 118 |
| 110 return true; | 119 return true; |
| 111 } | 120 } |
| 112 | |
| 113 void ProcessSingleton::Cleanup() { | |
| 114 // Closing the file also releases the lock. | |
| 115 if (lock_fd_ != -1) { | |
| 116 int rc = HANDLE_EINTR(close(lock_fd_)); | |
| 117 DPCHECK(!rc) << "Closing lock_fd_:"; | |
| 118 } | |
| 119 lock_fd_ = -1; | |
| 120 } | |
| OLD | NEW |