blob: 98c69703e24936f7aac0129b42b69de0da1745aa [file] [log] [blame]
// Copyright 2018 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 "third_party/blink/renderer/modules/idle/idle_status.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/platform/bindings/name_client.h"
#include "third_party/blink/renderer/platform/bindings/script_state.h"
#include "third_party/blink/renderer/platform/bindings/trace_wrapper_member.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
namespace blink {
IdleStatus* IdleStatus::Create(ExecutionContext* context,
uint32_t threshold,
mojom::blink::IdleMonitorRequest request) {
auto* status =
MakeGarbageCollected<IdleStatus>(context, threshold, std::move(request));
status->UpdateStateIfNeeded();
return status;
}
IdleStatus::IdleStatus(ExecutionContext* context,
uint32_t threshold,
mojom::blink::IdleMonitorRequest request)
: ContextLifecycleStateObserver(context),
threshold_(threshold),
binding_(this, std::move(request)) {}
void IdleStatus::Init(IdleState state) {
state_ = state;
}
IdleStatus::~IdleStatus() = default;
void IdleStatus::Dispose() {
StopMonitoring();
}
const AtomicString& IdleStatus::InterfaceName() const {
return event_target_names::kIdleStatus;
}
ExecutionContext* IdleStatus::GetExecutionContext() const {
return ContextLifecycleObserver::GetExecutionContext();
}
bool IdleStatus::HasPendingActivity() const {
return binding_.is_bound();
}
void IdleStatus::ContextLifecycleStateChanged(
mojom::FrameLifecycleState state) {
if (state == mojom::FrameLifecycleState::kRunning)
StartMonitoring();
else
StopMonitoring();
}
void IdleStatus::ContextDestroyed(ExecutionContext*) {
StopMonitoring();
}
void IdleStatus::StartMonitoring() {
DCHECK(!binding_.is_bound());
mojom::blink::IdleManagerPtr service;
GetExecutionContext()->GetInterfaceProvider()->GetInterface(
mojo::MakeRequest(&service));
mojom::blink::IdleMonitorPtr monitor_ptr;
binding_.Bind(mojo::MakeRequest(&monitor_ptr));
service->AddMonitor(threshold_, std::move(monitor_ptr),
WTF::Bind(
[](IdleStatus* status, IdleState state) {
if (state != status->state_)
status->Update(state);
},
WrapWeakPersistent(this)));
}
void IdleStatus::StopMonitoring() {
binding_.Close();
}
String IdleStatus::state() const {
switch (state_) {
case IdleState::ACTIVE:
return "active";
case IdleState::IDLE:
return "idle";
case IdleState::LOCKED:
return "locked";
}
}
void IdleStatus::Update(IdleState state) {
DCHECK(binding_.is_bound());
if (!GetExecutionContext() || GetExecutionContext()->IsContextDestroyed())
return;
state_ = state;
DispatchEvent(*Event::Create(event_type_names::kChange));
}
void IdleStatus::Trace(blink::Visitor* visitor) {
EventTargetWithInlineData::Trace(visitor);
ContextLifecycleStateObserver::Trace(visitor);
}
} // namespace blink