Commit 195179f0 authored by Minoru Chikamune's avatar Minoru Chikamune Committed by Commit Bot

Migrate WakeLockManager to use GC mojo wrappers.

No behavior change. This CL reduces potential risks of use-after-free bugs.

Bug: 1049056
Change-Id: Id2cbcd0dafb470fb368722f3b1eed9e22a39df3f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2108212
Commit-Queue: Minoru Chikamune <chikamune@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarKouhei Ueno <kouhei@chromium.org>
Reviewed-by: default avatarAlexander Timin <altimin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#754409}
parent 4d33ef97
......@@ -17,7 +17,9 @@ namespace blink {
WakeLockManager::WakeLockManager(ExecutionContext* execution_context,
WakeLockType type)
: wake_lock_type_(type), execution_context_(execution_context) {
: wake_lock_(execution_context),
wake_lock_type_(type),
execution_context_(execution_context) {
DCHECK_NE(execution_context, nullptr);
}
......@@ -36,15 +38,16 @@ void WakeLockManager::AcquireWakeLock(ScriptPromiseResolver* resolver) {
// document and type.
// 4.3. Add lock to record.[[ActiveLocks]].
// 5. Return active.
if (!wake_lock_) {
if (!wake_lock_.is_bound()) {
mojo::Remote<mojom::blink::WakeLockService> wake_lock_service;
execution_context_->GetBrowserInterfaceBroker().GetInterface(
wake_lock_service.BindNewPipeAndPassReceiver());
wake_lock_service->GetWakeLock(ToMojomWakeLockType(wake_lock_type_),
device::mojom::blink::WakeLockReason::kOther,
"Blink Wake Lock",
wake_lock_.BindNewPipeAndPassReceiver());
wake_lock_service->GetWakeLock(
ToMojomWakeLockType(wake_lock_type_),
device::mojom::blink::WakeLockReason::kOther, "Blink Wake Lock",
wake_lock_.BindNewPipeAndPassReceiver(
execution_context_->GetTaskRunner(TaskType::kMiscPlatformAPI)));
wake_lock_.set_disconnect_handler(WTF::Bind(
&WakeLockManager::OnWakeLockConnectionError, WrapWeakPersistent(this)));
wake_lock_->RequestWakeLock();
......@@ -99,6 +102,7 @@ void WakeLockManager::OnWakeLockConnectionError() {
void WakeLockManager::Trace(Visitor* visitor) {
visitor->Trace(execution_context_);
visitor->Trace(wake_lock_sentinels_);
visitor->Trace(wake_lock_);
}
} // namespace blink
......@@ -6,11 +6,12 @@
#define THIRD_PARTY_BLINK_RENDERER_MODULES_WAKE_LOCK_WAKE_LOCK_MANAGER_H_
#include "base/gtest_prod_util.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/device/public/mojom/wake_lock.mojom-blink-forward.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/modules/wake_lock/wake_lock_type.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
namespace blink {
......@@ -42,7 +43,9 @@ class MODULES_EXPORT WakeLockManager final
// An actual platform WakeLock. If bound, it means there is an active wake
// lock for a given type.
mojo::Remote<device::mojom::blink::WakeLock> wake_lock_;
HeapMojoRemote<device::mojom::blink::WakeLock,
HeapMojoWrapperMode::kWithoutContextObserver>
wake_lock_;
WakeLockType wake_lock_type_;
// ExecutionContext from which we will connect to |wake_lock_service_|.
......
......@@ -194,6 +194,7 @@ TEST(WakeLockManagerTest, WakeLockConnectionError) {
context.WaitForPromiseFulfillment(promise1);
context.WaitForPromiseFulfillment(promise2);
EXPECT_TRUE(manager->wake_lock_.is_bound());
EXPECT_EQ(2U, manager->wake_lock_sentinels_.size());
// Unbind and wait for the disconnection to reach |wake_lock_|'s
......@@ -202,7 +203,7 @@ TEST(WakeLockManagerTest, WakeLockConnectionError) {
manager->wake_lock_.FlushForTesting();
EXPECT_EQ(0U, manager->wake_lock_sentinels_.size());
EXPECT_FALSE(manager->wake_lock_);
EXPECT_FALSE(manager->wake_lock_.is_bound());
EXPECT_FALSE(system_lock.is_acquired());
}
......
......@@ -53,6 +53,7 @@ class HeapMojoRemote {
DCHECK(task_runner);
wrapper_->remote().Bind(std::move(pending_remote), std::move(task_runner));
}
void FlushForTesting() { return wrapper_->remote().FlushForTesting(); }
void Trace(Visitor* visitor) { visitor->Trace(wrapper_); }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment