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/metrics/histogram_macros.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 namespace { |
| 18 |
| 19 enum SetupSingletonAcquisitionResult { |
| 20 // The setup singleton was acquired successfully. |
| 21 SETUP_SINGLETON_ACQUISITION_SUCCESS = 0, |
| 22 // Acquisition of the exit event mutex timed out. |
| 23 SETUP_SINGLETON_ACQUISITION_EXIT_EVENT_MUTEX_TIMEOUT = 1, |
| 24 // Acquisition of the setup mutex timed out. |
| 25 SETUP_SINGLETON_ACQUISITION_SETUP_MUTEX_TIMEOUT = 2, |
| 26 SETUP_SINGLETON_ACQUISITION_RESULT_COUNT, |
| 27 }; |
| 28 |
| 29 void RecordSetupSingletonAcquisitionResultHistogram( |
| 30 SetupSingletonAcquisitionResult result) { |
| 31 UMA_HISTOGRAM_ENUMERATION("Setup.Install.SingletonAcquisitionResult", result, |
| 32 SETUP_SINGLETON_ACQUISITION_RESULT_COUNT); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 std::unique_ptr<SetupSingleton> SetupSingleton::Acquire( |
| 38 const base::CommandLine& command_line, |
| 39 const MasterPreferences& master_preferences, |
| 40 InstallationState* original_state, |
| 41 InstallerState* installer_state) { |
| 42 DCHECK(original_state); |
| 43 DCHECK(installer_state); |
| 44 |
| 45 const base::string16 sync_primitive_name_suffix( |
| 46 base::SizeTToString16(std::hash<base::FilePath::StringType>()( |
| 47 installer_state->target_path().value()))); |
| 48 |
| 49 std::unique_ptr<SetupSingleton> setup_singleton( |
| 50 new SetupSingleton(sync_primitive_name_suffix)); |
| 51 |
| 52 { |
| 53 // Acquire a mutex to ensure that a single call to SetupSingleton::Acquire() |
| 54 // signals |exit_event_| and waits for |setup_mutex_| to be released at a |
| 55 // time. |
| 56 base::win::ScopedHandle exit_event_mutex(::CreateMutex( |
| 57 nullptr, FALSE, |
| 58 (L"Global\\ChromeSetupExitEventMutex_" + sync_primitive_name_suffix) |
| 59 .c_str())); |
| 60 DCHECK(exit_event_mutex.IsValid()); |
| 61 ScopedHoldMutex scoped_hold_exit_event_mutex; |
| 62 if (!scoped_hold_exit_event_mutex.Acquire(exit_event_mutex.Get())) { |
| 63 RecordSetupSingletonAcquisitionResultHistogram( |
| 64 SETUP_SINGLETON_ACQUISITION_EXIT_EVENT_MUTEX_TIMEOUT); |
| 65 return nullptr; |
| 66 } |
| 67 |
| 68 // Signal |exit_event_|. This causes any call to WaitForInterrupt() on a |
| 69 // SetupSingleton bound to the same Chrome installation to return |
| 70 // immediately. |
| 71 setup_singleton->exit_event_.Signal(); |
| 72 |
| 73 // Acquire |setup_mutex_|. |
| 74 if (!setup_singleton->scoped_hold_setup_mutex_.Acquire( |
| 75 setup_singleton->setup_mutex_.Get())) { |
| 76 RecordSetupSingletonAcquisitionResultHistogram( |
| 77 SETUP_SINGLETON_ACQUISITION_SETUP_MUTEX_TIMEOUT); |
| 78 return nullptr; |
| 79 } |
| 80 setup_singleton->exit_event_.Reset(); |
| 81 } |
| 82 |
| 83 // Update |original_state| and |installer_state|. |
| 84 original_state->Initialize(); |
| 85 installer_state->Initialize(command_line, master_preferences, |
| 86 *original_state); |
| 87 |
| 88 RecordSetupSingletonAcquisitionResultHistogram( |
| 89 SETUP_SINGLETON_ACQUISITION_SUCCESS); |
| 90 return setup_singleton; |
| 91 } |
| 92 |
| 93 SetupSingleton::~SetupSingleton() = default; |
| 94 |
| 95 bool SetupSingleton::WaitForInterrupt(const base::TimeDelta& max_time) { |
| 96 const bool exit_event_signaled = exit_event_.TimedWait(max_time); |
| 97 return exit_event_signaled; |
| 98 } |
| 99 |
| 100 SetupSingleton::ScopedHoldMutex::ScopedHoldMutex() = default; |
| 101 |
| 102 SetupSingleton::ScopedHoldMutex::~ScopedHoldMutex() { |
| 103 if (mutex_ != INVALID_HANDLE_VALUE) |
| 104 ::ReleaseMutex(mutex_); |
| 105 } |
| 106 |
| 107 bool SetupSingleton::ScopedHoldMutex::Acquire(HANDLE mutex) { |
| 108 DCHECK_NE(INVALID_HANDLE_VALUE, mutex); |
| 109 DCHECK_EQ(INVALID_HANDLE_VALUE, mutex_); |
| 110 |
| 111 const DWORD wait_return_value = ::WaitForSingleObject( |
| 112 mutex, |
| 113 static_cast<DWORD>(base::TimeDelta::FromSeconds(5).InMilliseconds())); |
| 114 if (wait_return_value == WAIT_ABANDONED || |
| 115 wait_return_value == WAIT_OBJECT_0) { |
| 116 mutex_ = mutex; |
| 117 return true; |
| 118 } |
| 119 |
| 120 DPCHECK(wait_return_value != WAIT_FAILED); |
| 121 return false; |
| 122 } |
| 123 |
| 124 SetupSingleton::SetupSingleton(const base::string16& sync_primitive_name_suffix) |
| 125 : setup_mutex_(::CreateMutex( |
| 126 nullptr, |
| 127 FALSE, |
| 128 (L"Global\\ChromeSetupMutex_" + sync_primitive_name_suffix).c_str())), |
| 129 exit_event_(base::win::ScopedHandle(::CreateEvent( |
| 130 nullptr, |
| 131 TRUE, |
| 132 FALSE, |
| 133 (L"Global\\ChromeSetupExitEvent_" + sync_primitive_name_suffix) |
| 134 .c_str()))) { |
| 135 DCHECK(setup_mutex_.IsValid()); |
| 136 } |
| 137 |
| 138 } // namespace installer |
OLD | NEW |