| Index: content/browser/wake_lock/wake_lock_dispatcher_host.cc
|
| diff --git a/content/browser/wake_lock/wake_lock_dispatcher_host.cc b/content/browser/wake_lock/wake_lock_dispatcher_host.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..38e6ff5310ae972c70f2836f57f8a4744fb10a08
|
| --- /dev/null
|
| +++ b/content/browser/wake_lock/wake_lock_dispatcher_host.cc
|
| @@ -0,0 +1,99 @@
|
| +// Copyright 2015 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 "content/browser/wake_lock/wake_lock_dispatcher_host.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "content/browser/power_save_blocker_impl.h"
|
| +#include "content/browser/web_contents/web_contents_impl.h"
|
| +#include "content/browser/web_contents/web_contents_view.h"
|
| +#include "content/public/browser/browser_thread.h"
|
| +#include "content/public/browser/power_save_blocker.h"
|
| +#include "content/public/browser/render_frame_host.h"
|
| +#include "content/public/common/service_registry.h"
|
| +
|
| +namespace content {
|
| +
|
| +WakeLockDispatcherHost::WakeLockDispatcherHost(WebContents* web_contents)
|
| + : WebContentsObserver(web_contents),
|
| + weak_factory_(this) {
|
| +}
|
| +
|
| +WakeLockDispatcherHost::~WakeLockDispatcherHost() {
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::RenderFrameCreated(
|
| + RenderFrameHost* render_frame_host) {
|
| + if (auto registry = render_frame_host->GetServiceRegistry()) {
|
| + registry->AddService<WakeLockService>(
|
| + base::Bind(&WakeLockServiceImpl::CreateService,
|
| + weak_factory_.GetWeakPtr(),
|
| + base::Unretained(render_frame_host)));
|
| + }
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::RenderFrameDeleted(
|
| + RenderFrameHost* render_frame_host) {
|
| + wake_lock_state_.RemoveFrame(render_frame_host);
|
| + UpdateBlocker();
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::RenderFrameHostChanged(
|
| + RenderFrameHost* old_host,
|
| + RenderFrameHost* new_host) {
|
| + wake_lock_state_.RemoveFrame(old_host);
|
| + UpdateBlocker();
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::RequestWakeLock(
|
| + RenderFrameHost* render_frame_host) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| + wake_lock_state_.AddFrame(render_frame_host);
|
| + UpdateBlocker();
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::CancelWakeLock(
|
| + RenderFrameHost* render_frame_host) {
|
| + DCHECK_CURRENTLY_ON(BrowserThread::UI);
|
| + wake_lock_state_.RemoveFrame(render_frame_host);
|
| + UpdateBlocker();
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::CreateBlocker() {
|
| + DCHECK(!blocker_);
|
| + blocker_ = PowerSaveBlocker::Create(
|
| + PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep,
|
| + PowerSaveBlocker::kReasonOther,
|
| + "WakeLock API");
|
| + // On Android, additionaly associate the blocker with this WebContents'
|
| + // NativeView.
|
| +#if defined(OS_ANDROID)
|
| + WebContentsImpl* web_contents_impl =
|
| + static_cast<WebContentsImpl*>(web_contents());
|
| + DCHECK(web_contents_impl);
|
| + static_cast<PowerSaveBlockerImpl*>(blocker_.get())->InitDisplaySleepBlocker(
|
| + web_contents_impl->GetView()->GetNativeView());
|
| +#endif
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::RemoveBlocker() {
|
| + blocker_.reset();
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::UpdateBlocker() {
|
| + if (wake_lock_state_.GetLockState()) {
|
| + if (!blocker_) {
|
| + CreateBlocker();
|
| + }
|
| + } else {
|
| + RemoveBlocker();
|
| + }
|
| + NotifyUpdate();
|
| +}
|
| +
|
| +void WakeLockDispatcherHost::NotifyUpdate() {
|
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnUpdate());
|
| +}
|
| +
|
| +} // namespace content
|
|
|