Chromium Code Reviews| 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..1ecb70daea191a580c478c5f00f5656e25029909 |
| --- /dev/null |
| +++ b/content/browser/wake_lock/wake_lock_dispatcher_host.cc |
| @@ -0,0 +1,95 @@ |
| +// 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/wake_lock/wake_lock_service_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::OnKeepScreenAwake( |
| + RenderFrameHost* render_frame_host, |
| + bool intention) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + wake_lock_state_.SetFrameLock(render_frame_host, intention); |
| + UpdateBlocker(); |
| +} |
| + |
| +void WakeLockDispatcherHost::UpdateBlocker() { |
| + if (wake_lock_state_.GetLockState()) { |
| + if (!blocker_) { |
| + CreateBlocker(); |
| + } |
| + } else { |
| + RemoveBlocker(); |
| + } |
| + NotifyUpdate(); |
|
mlamouri (slow - plz ping)
2015/05/05 14:08:33
Should this only be called when there is an actual
alogvinov
2015/05/06 14:30:38
The whole purpose of NotifyUpdate and AddObserver/
|
| +} |
| + |
| +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::RenderFrameCreated( |
| + RenderFrameHost* render_frame_host) { |
| + if (auto registry = render_frame_host->GetServiceRegistry()) { |
| + registry->AddService<WakeLockService>( |
| + base::Bind(&WakeLockServiceImpl::CreateService, |
| + base::Bind(&WakeLockDispatcherHost::OnKeepScreenAwake, |
| + 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::NotifyUpdate() { |
| + FOR_EACH_OBSERVER(Observer, observer_list_, OnUpdate()); |
| +} |
| + |
| +} // namespace content |