Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #include "chrome/installer/setup/setup_singleton.h" | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/strings/string_number_conversions.h" | |
| 10 #include "base/time/time.h" | |
| 11 #include "chrome/installer/util/installation_state.h" | |
| 12 #include "chrome/installer/util/installer_state.h" | |
| 13 | |
| 14 namespace installer { | |
| 15 | |
| 16 std::unique_ptr<SetupSingleton> SetupSingleton::Acquire( | |
| 17 const base::CommandLine& command_line, | |
| 18 const MasterPreferences& master_preferences, | |
| 19 InstallationState* original_state, | |
| 20 InstallerState* installer_state) { | |
| 21 DCHECK(original_state); | |
| 22 DCHECK(installer_state); | |
| 23 | |
| 24 const base::string16 sync_primitive_name_suffix( | |
| 25 base::SizeTToString16(std::hash<base::FilePath::StringType>()( | |
| 26 installer_state->target_path().value()))); | |
| 27 | |
| 28 std::unique_ptr<SetupSingleton> setup_singleton( | |
| 29 new SetupSingleton(sync_primitive_name_suffix)); | |
| 30 | |
| 31 { | |
| 32 // Acquire a mutex to ensure that a single call to SetupSingleton::Acquire() | |
| 33 // signals |exit_event_| and waits for |setup_mutex_| to be released at a | |
| 34 // time. | |
| 35 base::win::ScopedHandle exit_event_mutex(::CreateMutex( | |
| 36 nullptr, FALSE, | |
| 37 (L"Global\\ChromeSetupExitEventMutex_" + sync_primitive_name_suffix) | |
| 38 .c_str())); | |
| 39 DCHECK(exit_event_mutex.IsValid()); | |
| 40 ScopedHoldMutex scoped_hold_exit_event_mutex; | |
| 41 if (!scoped_hold_exit_event_mutex.Acquire(exit_event_mutex.Get())) | |
| 42 return nullptr; | |
| 43 | |
| 44 // Signal |exit_event_|. This causes any call to WaitForInterrupt() on a | |
| 45 // SetupSingleton bound to the same Chrome installation to return | |
| 46 // immediately. | |
| 47 setup_singleton->exit_event_.Signal(); | |
| 48 | |
| 49 // Acquire |setup_mutex_|. | |
| 50 if (!setup_singleton->scoped_hold_setup_mutex_.Acquire( | |
| 51 setup_singleton->setup_mutex_.Get())) { | |
| 52 return nullptr; | |
| 53 } | |
| 54 setup_singleton->exit_event_.Reset(); | |
| 55 } | |
| 56 | |
| 57 // Update |original_state| and |installer_state|. | |
| 58 original_state->Initialize(); | |
| 59 installer_state->Initialize(command_line, master_preferences, | |
| 60 *original_state); | |
| 61 | |
| 62 return setup_singleton; | |
| 63 } | |
| 64 | |
| 65 SetupSingleton::~SetupSingleton() = default; | |
| 66 | |
| 67 bool SetupSingleton::WaitForInterrupt(const base::TimeDelta& max_time) { | |
| 68 const bool exit_event_signaled = exit_event_.TimedWait(max_time); | |
| 69 return exit_event_signaled; | |
| 70 } | |
| 71 | |
| 72 SetupSingleton::ScopedHoldMutex::ScopedHoldMutex() = default; | |
| 73 | |
| 74 SetupSingleton::ScopedHoldMutex::~ScopedHoldMutex() { | |
| 75 if (mutex_ != INVALID_HANDLE_VALUE) | |
| 76 ::ReleaseMutex(mutex_); | |
| 77 } | |
| 78 | |
| 79 bool SetupSingleton::ScopedHoldMutex::Acquire(HANDLE mutex) { | |
| 80 DCHECK_NE(INVALID_HANDLE_VALUE, mutex); | |
| 81 DCHECK_EQ(INVALID_HANDLE_VALUE, mutex_); | |
| 82 | |
| 83 const DWORD wait_return_value = ::WaitForSingleObject( | |
| 84 mutex, | |
| 85 static_cast<DWORD>(base::TimeDelta::FromSeconds(1).InMilliseconds())); | |
|
grt (UTC plus 2)
2016/09/01 21:22:41
did you mean to change this to 1 second?
fdoray
2016/09/06 17:29:39
Yes. I think we should wait long enough to allow a
grt (UTC plus 2)
2016/09/06 20:22:10
sgtm
| |
| 86 if (wait_return_value == WAIT_ABANDONED || | |
| 87 wait_return_value == WAIT_OBJECT_0) { | |
| 88 mutex_ = mutex; | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 DPCHECK(wait_return_value != WAIT_FAILED); | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 SetupSingleton::SetupSingleton(const base::string16& sync_primitive_name_suffix) | |
| 97 : setup_mutex_(::CreateMutex( | |
| 98 nullptr, | |
| 99 FALSE, | |
| 100 (L"Global\\ChromeSetupMutex_" + sync_primitive_name_suffix).c_str())), | |
| 101 exit_event_(base::win::ScopedHandle(::CreateEvent( | |
| 102 nullptr, | |
| 103 TRUE, | |
| 104 FALSE, | |
| 105 (L"Global\\ChromeSetupExitEvent_" + sync_primitive_name_suffix) | |
| 106 .c_str()))) { | |
| 107 DCHECK(setup_mutex_.IsValid()); | |
| 108 } | |
| 109 | |
| 110 } // namespace installer | |
| OLD | NEW |