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/power_save_blocker_impl.h" | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "content/public/browser/power_save_blocker.h" | |
11 #include "content/public/browser/render_frame_host.h" | |
12 #include "content/public/common/service_registry.h" | |
13 | |
14 namespace content { | |
15 | |
16 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents) | |
17 : WebContentsObserver(web_contents), | |
18 weak_factory_(this) { | |
19 } | |
20 | |
21 WakeLockServiceContext::~WakeLockServiceContext() { | |
22 } | |
23 | |
24 void WakeLockServiceContext::CreateService( | |
25 const RenderFrameHost* render_frame_host, | |
26 mojo::InterfaceRequest<WakeLockService> request) { | |
27 new WakeLockServiceImpl(weak_factory_.GetWeakPtr(), | |
28 render_frame_host, | |
29 request.Pass()); | |
30 } | |
31 | |
32 void WakeLockServiceContext::RenderFrameDeleted( | |
33 RenderFrameHost* render_frame_host) { | |
34 CancelWakeLock(render_frame_host); | |
35 } | |
36 | |
37 void WakeLockServiceContext::RenderFrameHostChanged( | |
38 RenderFrameHost* old_host, | |
39 RenderFrameHost* new_host) { | |
40 CancelWakeLock(old_host); | |
41 } | |
42 | |
43 void WakeLockServiceContext::RequestWakeLock( | |
44 const RenderFrameHost* render_frame_host) { | |
45 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
46 frames_requesting_lock_.insert(render_frame_host); | |
47 UpdateWakeLock(); | |
48 } | |
49 | |
50 void WakeLockServiceContext::CancelWakeLock( | |
51 const RenderFrameHost* render_frame_host) { | |
52 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
53 frames_requesting_lock_.erase(render_frame_host); | |
54 UpdateWakeLock(); | |
55 } | |
56 | |
57 bool WakeLockServiceContext::HasWakeLockForTests() const { | |
58 return wake_lock_; | |
59 } | |
60 | |
61 void WakeLockServiceContext::CreateWakeLock() { | |
62 DCHECK(!wake_lock_); | |
63 wake_lock_ = PowerSaveBlocker::Create( | |
64 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
65 PowerSaveBlocker::kReasonOther, | |
66 "Wake Lock API"); | |
67 | |
68 // On Android, additionaly associate the blocker with this WebContents' | |
69 // NativeView. | |
70 #if defined(OS_ANDROID) | |
71 DCHECK(web_contents()); | |
72 | |
73 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get())->InitDisplaySleepBlocker( | |
74 web_contents()); | |
75 #endif | |
76 } | |
77 | |
78 void WakeLockServiceContext::RemoveWakeLock() { | |
79 DCHECK(wake_lock_); | |
80 wake_lock_.reset(); | |
81 } | |
82 | |
83 void WakeLockServiceContext::UpdateWakeLock() { | |
84 if (!frames_requesting_lock_.empty()) { | |
85 if (!wake_lock_) { | |
nasko
2015/09/02 22:40:15
nit: No need for {} for single line if statements.
alogvinov
2015/09/03 16:25:00
Done.
| |
86 CreateWakeLock(); | |
87 } | |
88 } else { | |
89 if (wake_lock_) { | |
nasko
2015/09/02 22:40:15
Same here.
alogvinov
2015/09/03 16:25:00
Done.
| |
90 RemoveWakeLock(); | |
91 } | |
92 } | |
93 } | |
94 | |
95 } // namespace content | |
OLD | NEW |