OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/wake_lock/wake_lock_service_context.h" | |
6 | |
7 #include "base/bind.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" | |
14 | |
15 namespace content { | |
16 | |
17 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents) | |
18 : WebContentsObserver(web_contents), | |
19 weak_factory_(this) { | |
20 } | |
21 | |
22 WakeLockServiceContext::~WakeLockServiceContext() { | |
23 } | |
24 | |
25 void WakeLockServiceContext::CreateService( | |
26 int frame_id, | |
27 mojo::InterfaceRequest<WakeLockService> request) { | |
28 new WakeLockServiceImpl(weak_factory_.GetWeakPtr(), | |
29 frame_id, | |
30 request.Pass()); | |
31 } | |
32 | |
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()); | |
38 } | |
39 | |
40 void WakeLockServiceContext::RequestWakeLock(int frame_id) { | |
41 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
42 frames_requesting_lock_.insert(frame_id); | |
43 UpdateWakeLock(); | |
44 } | |
45 | |
46 void WakeLockServiceContext::CancelWakeLock(int frame_id) { | |
47 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
48 frames_requesting_lock_.erase(frame_id); | |
49 UpdateWakeLock(); | |
50 } | |
51 | |
52 bool WakeLockServiceContext::HasWakeLockForTests() const { | |
53 return wake_lock_; | |
54 } | |
55 | |
56 void WakeLockServiceContext::CreateWakeLock() { | |
57 DCHECK(!wake_lock_); | |
58 wake_lock_ = PowerSaveBlocker::Create( | |
59 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
60 PowerSaveBlocker::kReasonOther, | |
61 "Wake Lock API"); | |
62 | |
63 // On Android, additionaly associate the blocker with this WebContents. | |
64 #if defined(OS_ANDROID) | |
65 DCHECK(web_contents()); | |
66 | |
67 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get())->InitDisplaySleepBlocker( | |
68 web_contents()); | |
69 #endif | |
70 } | |
71 | |
72 void WakeLockServiceContext::RemoveWakeLock() { | |
73 DCHECK(wake_lock_); | |
74 wake_lock_.reset(); | |
75 } | |
76 | |
77 void WakeLockServiceContext::UpdateWakeLock() { | |
78 if (!frames_requesting_lock_.empty()) { | |
79 if (!wake_lock_) | |
80 CreateWakeLock(); | |
81 } else { | |
82 if (wake_lock_) | |
83 RemoveWakeLock(); | |
84 } | |
85 } | |
86 | |
87 } // namespace content | |
OLD | NEW |