1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/wake_lock/wake_lock_service_context.h"
8 #include "content/browser/frame_host/frame_tree_node.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h"
10 #include "content/browser/power_save_blocker_impl.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/power_save_blocker.h"
13 #include "content/public/common/service_registry.h"
17 WakeLockServiceContext::WakeLockServiceContext(WebContents
* web_contents
)
18 : WebContentsObserver(web_contents
),
22 WakeLockServiceContext::~WakeLockServiceContext() {
25 void WakeLockServiceContext::CreateService(
27 mojo::InterfaceRequest
<WakeLockService
> request
) {
28 new WakeLockServiceImpl(weak_factory_
.GetWeakPtr(),
33 void WakeLockServiceContext::RenderFrameDeleted(
34 RenderFrameHost
* render_frame_host
) {
35 RenderFrameHostImpl
* rfh_impl
=
36 static_cast<RenderFrameHostImpl
*>(render_frame_host
);
37 CancelWakeLock(rfh_impl
->frame_tree_node()->frame_tree_node_id());
40 void WakeLockServiceContext::RequestWakeLock(int frame_id
) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
42 frames_requesting_lock_
.insert(frame_id
);
46 void WakeLockServiceContext::CancelWakeLock(int frame_id
) {
47 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
48 frames_requesting_lock_
.erase(frame_id
);
52 bool WakeLockServiceContext::HasWakeLockForTests() const {
56 void WakeLockServiceContext::CreateWakeLock() {
58 wake_lock_
= PowerSaveBlocker::Create(
59 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep
,
60 PowerSaveBlocker::kReasonOther
,
63 // On Android, additionaly associate the blocker with this WebContents.
64 #if defined(OS_ANDROID)
65 DCHECK(web_contents());
67 static_cast<PowerSaveBlockerImpl
*>(wake_lock_
.get())->InitDisplaySleepBlocker(
72 void WakeLockServiceContext::RemoveWakeLock() {
77 void WakeLockServiceContext::UpdateWakeLock() {
78 if (!frames_requesting_lock_
.empty()) {
87 } // namespace content