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/browser/wake_lock/wake_lock_service_impl.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "content/public/browser/power_save_blocker.h" | |
12 #include "content/public/browser/render_frame_host.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::RenderFrameCreated( | |
26 RenderFrameHost* render_frame_host) { | |
27 if (auto registry = render_frame_host->GetServiceRegistry()) { | |
28 registry->AddService<WakeLockService>( | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
Could you do that in RenderFrameHostImpl::Register
alogvinov
2015/09/02 17:06:22
Done.
| |
29 base::Bind(&WakeLockServiceImpl::CreateService, | |
30 weak_factory_.GetWeakPtr(), | |
31 base::Unretained(render_frame_host))); | |
32 } | |
33 } | |
34 | |
35 void WakeLockServiceContext::RenderFrameDeleted( | |
36 RenderFrameHost* render_frame_host) { | |
37 frames_requesting_lock_.erase(render_frame_host); | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
Call ::CancelWakeLock() instead.
alogvinov
2015/09/02 17:06:22
Done.
| |
38 UpdateWakeLock(); | |
39 } | |
40 | |
41 void WakeLockServiceContext::RenderFrameHostChanged( | |
42 RenderFrameHost* old_host, | |
43 RenderFrameHost* new_host) { | |
44 frames_requesting_lock_.erase(old_host); | |
45 UpdateWakeLock(); | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
ditto
alogvinov
2015/09/02 17:06:22
Done.
| |
46 } | |
47 | |
48 void WakeLockServiceContext::RequestWakeLock( | |
49 RenderFrameHost* render_frame_host) { | |
50 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
51 frames_requesting_lock_.insert(render_frame_host); | |
52 UpdateWakeLock(); | |
53 } | |
54 | |
55 void WakeLockServiceContext::CancelWakeLock( | |
56 RenderFrameHost* render_frame_host) { | |
57 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
58 frames_requesting_lock_.erase(render_frame_host); | |
59 UpdateWakeLock(); | |
60 } | |
61 | |
62 void WakeLockServiceContext::CreateWakeLock() { | |
63 DCHECK(!wake_lock_); | |
64 wake_lock_ = PowerSaveBlocker::Create( | |
65 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
66 PowerSaveBlocker::kReasonOther, | |
67 "Wake Lock API"); | |
68 // On Android, additionaly associate the blocker with this WebContents' | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
nit: add empty line before this comment and anothe
alogvinov
2015/09/02 17:06:22
Done.
| |
69 // NativeView. | |
70 #if defined(OS_ANDROID) | |
71 DCHECK(web_contents()); | |
72 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get())->InitDisplaySleepBlocker( | |
73 web_contents()); | |
74 #endif | |
75 } | |
76 | |
77 void WakeLockServiceContext::RemoveWakeLock() { | |
78 wake_lock_.reset(); | |
mlamouri (slow - plz ping)
2015/08/31 14:59:15
nit: DCHECK(wake_lock_);
alogvinov
2015/09/02 17:06:22
Done.
| |
79 } | |
80 | |
81 void WakeLockServiceContext::UpdateWakeLock() { | |
82 if (!frames_requesting_lock_.empty()) { | |
83 if (!wake_lock_) { | |
84 CreateWakeLock(); | |
85 } | |
86 } else { | |
87 RemoveWakeLock(); | |
88 } | |
89 } | |
90 | |
91 } // namespace content | |
OLD | NEW |