forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup_singleton.cc
130 lines (106 loc) · 4.26 KB
/
setup_singleton.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2016 The Chromium Authors
// 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 <functional>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "chrome/installer/setup/installer_state.h"
#include "chrome/installer/util/installation_state.h"
namespace installer {
std::unique_ptr<SetupSingleton> SetupSingleton::Acquire(
const base::CommandLine& command_line,
const InitialPreferences& initial_preferences,
InstallationState* original_state,
InstallerState* installer_state) {
DCHECK(original_state);
DCHECK(installer_state);
const std::wstring sync_primitive_name_suffix(
base::NumberToWString(std::hash<base::FilePath::StringType>()(
installer_state->target_path().value())));
base::win::ScopedHandle setup_mutex(::CreateMutex(
nullptr, FALSE,
(L"Global\\ChromeSetupMutex_" + sync_primitive_name_suffix).c_str()));
if (!setup_mutex.IsValid()) {
// UMA data indicates that this happens 0.03 % of the time.
return nullptr;
}
base::win::ScopedHandle exit_event(::CreateEvent(
nullptr, TRUE, FALSE,
(L"Global\\ChromeSetupExitEvent_" + sync_primitive_name_suffix).c_str()));
if (!exit_event.IsValid()) {
// UMA data indicates that this happens < 0.01 % of the time.
return nullptr;
}
auto setup_singleton = base::WrapUnique(
new SetupSingleton(std::move(setup_mutex), std::move(exit_event)));
{
// Acquire a mutex to ensure that a single call to SetupSingleton::Acquire()
// 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()));
if (!exit_event_mutex.IsValid()) {
// UMA data indicates that this happens < 0.01 % of the time.
return nullptr;
}
ScopedHoldMutex scoped_hold_exit_event_mutex;
if (!scoped_hold_exit_event_mutex.Acquire(exit_event_mutex.Get())) {
// UMA data indicates that this happens < 0.01 % of the time.
return nullptr;
}
// Signal |exit_event_|. This causes any call to WaitForInterrupt() on a
// SetupSingleton bound to the same Chrome installation to return
// immediately.
setup_singleton->exit_event_.Signal();
// Acquire |setup_mutex_|.
if (!setup_singleton->scoped_hold_setup_mutex_.Acquire(
setup_singleton->setup_mutex_.Get())) {
// UMA data indicates that this happens 0.84 % of the time.
return nullptr;
}
setup_singleton->exit_event_.Reset();
}
// Update |original_state| and |installer_state|.
original_state->Initialize();
installer_state->Initialize(command_line, initial_preferences,
*original_state);
// UMA data indicates that this method succeeds > 99% of the time.
return setup_singleton;
}
SetupSingleton::~SetupSingleton() = default;
bool SetupSingleton::WaitForInterrupt(const base::TimeDelta& max_time) const {
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, static_cast<DWORD>(base::Seconds(5).InMilliseconds()));
if (wait_return_value == WAIT_ABANDONED ||
wait_return_value == WAIT_OBJECT_0) {
mutex_ = mutex;
return true;
}
DPCHECK(wait_return_value != WAIT_FAILED);
return false;
}
SetupSingleton::SetupSingleton(base::win::ScopedHandle setup_mutex,
base::win::ScopedHandle exit_event)
: setup_mutex_(std::move(setup_mutex)), exit_event_(std::move(exit_event)) {
DCHECK(setup_mutex_.IsValid());
}
} // namespace installer