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
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/device_posture/device_posture.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_device_posture_type.h"
#include "third_party/blink/renderer/core/dom/events/event.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/modules/event_target_modules.h"
namespace blink {
namespace {
V8DevicePostureType::Enum PostureToV8Enum(
mojom::blink::DevicePostureType posture) {
switch (posture) {
case mojom::blink::DevicePostureType::kContinuous:
return V8DevicePostureType::Enum::kContinuous;
case mojom::blink::DevicePostureType::kFolded:
return V8DevicePostureType::Enum::kFolded;
}
NOTREACHED();
}
} // namespace
DevicePosture::DevicePosture(LocalDOMWindow* window)
: ExecutionContextClient(window), receiver_(this, GetExecutionContext()) {}
DevicePosture::~DevicePosture() = default;
V8DevicePostureType DevicePosture::type() {
EnsureServiceConnection();
return V8DevicePostureType(PostureToV8Enum(posture_));
}
void DevicePosture::OnPostureChanged(mojom::blink::DevicePostureType posture) {
if (posture_ == posture)
return;
posture_ = posture;
DispatchEvent(*Event::CreateBubble(event_type_names::kChange));
}
void DevicePosture::EnsureServiceConnection() {
LocalDOMWindow* window = DomWindow();
if (!window) {
return;
}
if (receiver_.is_bound()) {
return;
}
mojom::blink::DevicePostureProvider* service =
window->GetFrame()->GetDevicePostureProvider();
auto task_runner =
GetExecutionContext()->GetTaskRunner(TaskType::kMiscPlatformAPI);
service->AddListenerAndGetCurrentPosture(
receiver_.BindNewPipeAndPassRemote(task_runner),
WTF::BindOnce(&DevicePosture::OnPostureChanged, WrapPersistent(this)));
}
void DevicePosture::AddedEventListener(const AtomicString& event_type,
RegisteredEventListener& listener) {
EventTarget::AddedEventListener(event_type, listener);
if (event_type != event_type_names::kChange)
return;
ExecutionContext* context = GetExecutionContext();
if (!context)
return;
EnsureServiceConnection();
}
ExecutionContext* DevicePosture::GetExecutionContext() const {
return ExecutionContextClient::GetExecutionContext();
}
const AtomicString& DevicePosture::InterfaceName() const {
return event_target_names::kDevicePosture;
}
void DevicePosture::Trace(blink::Visitor* visitor) const {
visitor->Trace(receiver_);
EventTarget::Trace(visitor);
ExecutionContextClient::Trace(visitor);
}
} // namespace blink
|