Index: chrome/installer/setup/setup_singleton.cc |
diff --git a/chrome/installer/setup/setup_singleton.cc b/chrome/installer/setup/setup_singleton.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f6c02431a6ce7a8279505368b7b121381404a7d0 |
--- /dev/null |
+++ b/chrome/installer/setup/setup_singleton.cc |
@@ -0,0 +1,113 @@ |
+// Copyright 2016 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/installer/setup/setup_singleton.h" |
+ |
+#include "base/files/file_path.h" |
+#include "base/logging.h" |
+#include "base/numerics/safe_conversions.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/time/time.h" |
+#include "chrome/installer/util/installation_state.h" |
+#include "chrome/installer/util/installer_state.h" |
+ |
+namespace installer { |
+ |
+std::unique_ptr<SetupSingleton> SetupSingleton::Acquire( |
+ const base::CommandLine& command_line, |
+ const MasterPreferences& master_preferences, |
+ InstallerState* installer_state, |
+ InstallationState* original_state) { |
+ DCHECK(installer_state); |
+ DCHECK(original_state); |
+ |
+ const base::string16 sync_primitive_name_suffix( |
+ base::SizeTToString16(std::hash<base::FilePath::StringType>()( |
+ installer_state->target_path().value()))); |
+ |
+ std::unique_ptr<SetupSingleton> setup_singleton( |
+ new SetupSingleton(sync_primitive_name_suffix)); |
+ |
+ // This mutex is acquired before signaling |exit_event_| and released after |
+ // acquiring |setup_mutex_|. It ensures that a single thread signals |
+ // |exit_event_| and waits for |setup_mutex_| to be released at a time. |
+ base::win::ScopedHandle exit_event_mutex(::CreateMutex( |
+ nullptr, FALSE, |
+ (L"Global\\ChromeSetupExitEventMutex_" + sync_primitive_name_suffix) |
+ .c_str())); |
+ DCHECK(exit_event_mutex.IsValid()); |
+ |
+ // 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.
|
+ // bound to the same Chrome installation as |setup_singleton| to return |
+ // immediately. |
+ 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.
|
+ new ScopedHoldMutex); |
+ if (!scoped_hold_exit_event_mutex->Acquire(exit_event_mutex.Get())) |
+ return nullptr; |
+ setup_singleton->exit_event_.Signal(); |
+ |
+ // Acquire |setup_mutex_|. |
+ if (!setup_singleton->scoped_hold_setup_mutex_.Acquire( |
+ setup_singleton->setup_mutex_.Get())) { |
+ return nullptr; |
+ } |
+ setup_singleton->exit_event_.Reset(); |
+ scoped_hold_exit_event_mutex.reset(nullptr); |
+ |
+ // 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.
|
+ original_state->Initialize(); |
+ installer_state->Initialize(command_line, master_preferences, |
+ *original_state); |
+ |
+ return setup_singleton; |
+} |
+ |
+SetupSingleton::~SetupSingleton() { |
+ ::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.
|
+} |
+ |
+bool SetupSingleton::Wait(const base::TimeDelta& max_time) { |
+ const bool exit_event_signaled = exit_event_.TimedWait(max_time); |
+ return exit_event_signaled; |
+} |
+ |
+SetupSingleton::ScopedHoldMutex::ScopedHoldMutex() = default; |
+ |
+SetupSingleton::ScopedHoldMutex::~ScopedHoldMutex() { |
+ if (mutex_ != INVALID_HANDLE_VALUE) |
+ ::ReleaseMutex(mutex_); |
+} |
+ |
+bool SetupSingleton::ScopedHoldMutex::Acquire(HANDLE mutex) { |
+ DCHECK_NE(INVALID_HANDLE_VALUE, mutex); |
+ DCHECK_EQ(INVALID_HANDLE_VALUE, mutex_); |
+ |
+ const DWORD wait_return_value = ::WaitForSingleObject( |
+ 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.
|
+ base::TimeDelta::FromSeconds(30).InMilliseconds())); |
+ if (wait_return_value == WAIT_ABANDONED || |
+ wait_return_value == WAIT_OBJECT_0) { |
+ mutex_ = mutex; |
+ return true; |
+ } |
+ |
+ 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.
|
+ return false; |
+} |
+ |
+SetupSingleton::SetupSingleton(const base::string16& sync_primitive_name_suffix) |
+ : setup_mutex_(::CreateMutex( |
+ nullptr, |
+ FALSE, |
+ (L"Global\\ChromeSetupMutex_" + sync_primitive_name_suffix).c_str())), |
+ exit_event_(base::win::ScopedHandle(::CreateEvent( |
+ nullptr, |
+ TRUE, |
+ FALSE, |
+ (L"Global\\ChromeSetupExitEvent_" + sync_primitive_name_suffix) |
+ .c_str()))) { |
+ DCHECK(setup_mutex_.IsValid()); |
+} |
+ |
+} // namespace installer |