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/numerics/safe_conversions.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "chrome/installer/util/installation_state.h" | |
| 13 #include "chrome/installer/util/installer_state.h" | |
| 14 | |
| 15 namespace installer { | |
| 16 | |
| 17 std::unique_ptr<SetupSingleton> SetupSingleton::Acquire( | |
| 18 const base::CommandLine& command_line, | |
| 19 const MasterPreferences& master_preferences, | |
| 20 InstallerState* installer_state, | |
| 21 InstallationState* original_state) { | |
| 22 DCHECK(installer_state); | |
| 23 DCHECK(original_state); | |
| 24 | |
| 25 const base::string16 sync_primitive_name_suffix( | |
| 26 base::SizeTToString16(std::hash<base::FilePath::StringType>()( | |
| 27 installer_state->target_path().value()))); | |
| 28 | |
| 29 std::unique_ptr<SetupSingleton> setup_singleton( | |
| 30 new SetupSingleton(sync_primitive_name_suffix)); | |
| 31 | |
| 32 // This mutex is acquired before signaling |exit_event_| and released after | |
| 33 // acquiring |setup_mutex_|. It ensures that a single thread signals | |
| 34 // |exit_event_| and waits for |setup_mutex_| to be released at a 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 | |
| 41 // Signal |exit_event_|. This causes any call to Wait() on a SetupSingleton | |
|
grt (UTC plus 2)
2016/09/01 12:05:39
this comment applies to line 48, no? please move i
fdoray
2016/09/01 15:45:22
Done.
| |
| 42 // bound to the same Chrome installation as |setup_singleton| to return | |
| 43 // immediately. | |
| 44 std::unique_ptr<ScopedHoldMutex> scoped_hold_exit_event_mutex( | |
|
grt (UTC plus 2)
2016/09/01 12:05:39
rather than using a unique_ptr that you release()
fdoray
2016/09/01 15:45:22
Done.
| |
| 45 new ScopedHoldMutex); | |
| 46 if (!scoped_hold_exit_event_mutex->Acquire(exit_event_mutex.Get())) | |
| 47 return nullptr; | |
| 48 setup_singleton->exit_event_.Signal(); | |
| 49 | |
| 50 // Acquire |setup_mutex_|. | |
| 51 if (!setup_singleton->scoped_hold_setup_mutex_.Acquire( | |
| 52 setup_singleton->setup_mutex_.Get())) { | |
| 53 return nullptr; | |
| 54 } | |
| 55 setup_singleton->exit_event_.Reset(); | |
| 56 scoped_hold_exit_event_mutex.reset(nullptr); | |
| 57 | |
| 58 // Update |installer_state| and |original_state|. | |
|
grt (UTC plus 2)
2016/09/01 12:05:39
nit: swap these in the comment so that they match
fdoray
2016/09/01 15:45:23
Done.
| |
| 59 original_state->Initialize(); | |
| 60 installer_state->Initialize(command_line, master_preferences, | |
| 61 *original_state); | |
| 62 | |
| 63 return setup_singleton; | |
| 64 } | |
| 65 | |
| 66 SetupSingleton::~SetupSingleton() { | |
| 67 ::ReleaseMutex(setup_mutex_.Get()); | |
|
grt (UTC plus 2)
2016/09/01 12:05:39
remove this? doesn't scoped_hold_setup_mutex_ auto
fdoray
2016/09/01 15:45:23
Done.
| |
| 68 } | |
| 69 | |
| 70 bool SetupSingleton::Wait(const base::TimeDelta& max_time) { | |
| 71 const bool exit_event_signaled = exit_event_.TimedWait(max_time); | |
| 72 return exit_event_signaled; | |
| 73 } | |
| 74 | |
| 75 SetupSingleton::ScopedHoldMutex::ScopedHoldMutex() = default; | |
| 76 | |
| 77 SetupSingleton::ScopedHoldMutex::~ScopedHoldMutex() { | |
| 78 if (mutex_ != INVALID_HANDLE_VALUE) | |
| 79 ::ReleaseMutex(mutex_); | |
| 80 } | |
| 81 | |
| 82 bool SetupSingleton::ScopedHoldMutex::Acquire(HANDLE mutex) { | |
| 83 DCHECK_NE(INVALID_HANDLE_VALUE, mutex); | |
| 84 DCHECK_EQ(INVALID_HANDLE_VALUE, mutex_); | |
| 85 | |
| 86 const DWORD wait_return_value = ::WaitForSingleObject( | |
| 87 mutex, base::saturated_cast<DWORD>( | |
|
grt (UTC plus 2)
2016/09/01 12:05:39
since you control the input, i think a static_cast
fdoray
2016/09/01 15:45:23
Done.
| |
| 88 base::TimeDelta::FromSeconds(30).InMilliseconds())); | |
| 89 if (wait_return_value == WAIT_ABANDONED || | |
| 90 wait_return_value == WAIT_OBJECT_0) { | |
| 91 mutex_ = mutex; | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 DCHECK_EQ(static_cast<DWORD>(WAIT_TIMEOUT), wait_return_value); | |
|
grt (UTC plus 2)
2016/09/01 12:05:39
if you change this this to:
DPCHECK(wait_return_
fdoray
2016/09/01 15:45:23
Done.
| |
| 96 return false; | |
| 97 } | |
| 98 | |
| 99 SetupSingleton::SetupSingleton(const base::string16& sync_primitive_name_suffix) | |
| 100 : setup_mutex_(::CreateMutex( | |
| 101 nullptr, | |
| 102 FALSE, | |
| 103 (L"Global\\ChromeSetupMutex_" + sync_primitive_name_suffix).c_str())), | |
| 104 exit_event_(base::win::ScopedHandle(::CreateEvent( | |
| 105 nullptr, | |
| 106 TRUE, | |
| 107 FALSE, | |
| 108 (L"Global\\ChromeSetupExitEvent_" + sync_primitive_name_suffix) | |
| 109 .c_str()))) { | |
| 110 DCHECK(setup_mutex_.IsValid()); | |
| 111 } | |
| 112 | |
| 113 } // namespace installer | |
| OLD | NEW |