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_dispatcher_host.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/browser/web_contents/web_contents_impl.h" | |
11 #include "content/browser/web_contents/web_contents_view.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/power_save_blocker.h" | |
14 #include "content/public/browser/render_frame_host.h" | |
15 #include "content/public/common/service_registry.h" | |
16 | |
17 namespace content { | |
18 | |
19 WakeLockDispatcherHost::WakeLockDispatcherHost(WebContents* web_contents) | |
20 : WebContentsObserver(web_contents), | |
21 weak_factory_(this) { | |
22 } | |
23 | |
24 WakeLockDispatcherHost::~WakeLockDispatcherHost() { | |
25 } | |
26 | |
27 void WakeLockDispatcherHost::OnKeepScreenAwake( | |
28 RenderFrameHost* render_frame_host, | |
29 bool intention) { | |
30 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
31 wake_lock_state_.SetFrameLock(render_frame_host, intention); | |
32 UpdateBlocker(); | |
33 } | |
34 | |
35 void WakeLockDispatcherHost::UpdateBlocker() { | |
36 if (wake_lock_state_.GetLockState()) { | |
37 if (!blocker_) { | |
38 CreateBlocker(); | |
39 } | |
40 } else { | |
41 RemoveBlocker(); | |
42 } | |
43 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/
| |
44 } | |
45 | |
46 void WakeLockDispatcherHost::CreateBlocker() { | |
47 DCHECK(!blocker_); | |
48 blocker_ = PowerSaveBlocker::Create( | |
49 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
50 PowerSaveBlocker::kReasonOther, | |
51 "WakeLock API"); | |
52 // On Android, additionaly associate the blocker with this WebContents' | |
53 // NativeView. | |
54 #if defined(OS_ANDROID) | |
55 WebContentsImpl* web_contents_impl = | |
56 static_cast<WebContentsImpl*>(web_contents()); | |
57 DCHECK(web_contents_impl); | |
58 static_cast<PowerSaveBlockerImpl*>(blocker_.get())->InitDisplaySleepBlocker( | |
59 web_contents_impl->GetView()->GetNativeView()); | |
60 #endif | |
61 } | |
62 | |
63 void WakeLockDispatcherHost::RemoveBlocker() { | |
64 blocker_.reset(); | |
65 } | |
66 | |
67 void WakeLockDispatcherHost::RenderFrameCreated( | |
68 RenderFrameHost* render_frame_host) { | |
69 if (auto registry = render_frame_host->GetServiceRegistry()) { | |
70 registry->AddService<WakeLockService>( | |
71 base::Bind(&WakeLockServiceImpl::CreateService, | |
72 base::Bind(&WakeLockDispatcherHost::OnKeepScreenAwake, | |
73 weak_factory_.GetWeakPtr(), | |
74 base::Unretained(render_frame_host)))); | |
75 } | |
76 } | |
77 | |
78 void WakeLockDispatcherHost::RenderFrameDeleted( | |
79 RenderFrameHost* render_frame_host) { | |
80 wake_lock_state_.RemoveFrame(render_frame_host); | |
81 UpdateBlocker(); | |
82 } | |
83 | |
84 void WakeLockDispatcherHost::RenderFrameHostChanged( | |
85 RenderFrameHost* old_host, | |
86 RenderFrameHost* new_host) { | |
87 wake_lock_state_.RemoveFrame(old_host); | |
88 UpdateBlocker(); | |
89 } | |
90 | |
91 void WakeLockDispatcherHost::NotifyUpdate() { | |
92 FOR_EACH_OBSERVER(Observer, observer_list_, OnUpdate()); | |
93 } | |
94 | |
95 } // namespace content | |
OLD | NEW |