blob: 02fbf52bf1c4d4ff2a3d88145cd4b98619a7f982 [file] [log] [blame]
// Copyright 2020 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/updater/prefs_impl.h"
#include <windows.h>
#include <memory>
#include "base/time/time.h"
#include "base/win/scoped_handle.h"
#include "chrome/updater/win/constants.h"
#include "chrome/updater/win/util.h"
namespace updater {
class ScopedPrefsLockImpl {
public:
ScopedPrefsLockImpl() = default;
ScopedPrefsLockImpl(const ScopedPrefsLockImpl&) = delete;
ScopedPrefsLockImpl& operator=(const ScopedPrefsLockImpl&) = delete;
~ScopedPrefsLockImpl();
bool Initialize(bool is_machine, base::TimeDelta timeout);
private:
base::win::ScopedHandle mutex_;
};
ScopedPrefsLock::ScopedPrefsLock(std::unique_ptr<ScopedPrefsLockImpl> impl)
: impl_(std::move(impl)) {}
ScopedPrefsLock::~ScopedPrefsLock() = default;
std::unique_ptr<ScopedPrefsLock> AcquireGlobalPrefsLock(
base::TimeDelta timeout) {
auto lock = std::make_unique<ScopedPrefsLockImpl>();
// TODO(crbug.com/1096654): need to pass is_machine instead of 'false' here.
if (!lock->Initialize(false, timeout))
return nullptr;
return std::make_unique<ScopedPrefsLock>(std::move(lock));
}
bool ScopedPrefsLockImpl::Initialize(bool is_machine, base::TimeDelta timeout) {
NamedObjectAttributes lock_attr;
GetNamedObjectAttributes(kPrefsAccessMutex, is_machine, &lock_attr);
mutex_.Set(::CreateMutex(&lock_attr.sa, false, lock_attr.name.c_str()));
if (!mutex_.IsValid())
return false;
DWORD ret = ::WaitForSingleObject(mutex_.Get(), timeout.InMilliseconds());
return ret == WAIT_OBJECT_0 || ret == WAIT_ABANDONED;
}
ScopedPrefsLockImpl::~ScopedPrefsLockImpl() {
if (mutex_.IsValid())
::ReleaseMutex(mutex_.Get());
}
} // namespace updater