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 CancelWakeLockForRFH(render_frame_host); |
| 36 } |
| 37 |
| 38 void WakeLockServiceContext::RenderFrameHostChanged(RenderFrameHost* old_host, |
| 39 RenderFrameHost* new_host) { |
| 40 CancelWakeLockForRFH(old_host); |
| 41 } |
| 42 |
| 43 void WakeLockServiceContext::RequestWakeLock(int frame_id) { |
| 44 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 45 frames_requesting_lock_.insert(frame_id); |
| 46 UpdateWakeLock(); |
| 47 } |
| 48 |
| 49 void WakeLockServiceContext::CancelWakeLock(int frame_id) { |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 51 frames_requesting_lock_.erase(frame_id); |
| 52 UpdateWakeLock(); |
| 53 } |
| 54 |
| 55 bool WakeLockServiceContext::HasWakeLockForTests() const { |
| 56 return wake_lock_; |
| 57 } |
| 58 |
| 59 void WakeLockServiceContext::CancelWakeLockForRFH( |
| 60 RenderFrameHost* render_frame_host) { |
| 61 if (!render_frame_host) |
| 62 return; |
| 63 |
| 64 RenderFrameHostImpl* rfh_impl = |
| 65 static_cast<RenderFrameHostImpl*>(render_frame_host); |
| 66 CancelWakeLock(rfh_impl->frame_tree_node()->frame_tree_node_id()); |
| 67 } |
| 68 |
| 69 void WakeLockServiceContext::CreateWakeLock() { |
| 70 DCHECK(!wake_lock_); |
| 71 wake_lock_ = PowerSaveBlocker::Create( |
| 72 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
| 73 PowerSaveBlocker::kReasonOther, |
| 74 "Wake Lock API"); |
| 75 |
| 76 // On Android, additionaly associate the blocker with this WebContents. |
| 77 #if defined(OS_ANDROID) |
| 78 DCHECK(web_contents()); |
| 79 |
| 80 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get())->InitDisplaySleepBlocker( |
| 81 web_contents()); |
| 82 #endif |
| 83 } |
| 84 |
| 85 void WakeLockServiceContext::RemoveWakeLock() { |
| 86 DCHECK(wake_lock_); |
| 87 wake_lock_.reset(); |
| 88 } |
| 89 |
| 90 void WakeLockServiceContext::UpdateWakeLock() { |
| 91 if (!frames_requesting_lock_.empty()) { |
| 92 if (!wake_lock_) |
| 93 CreateWakeLock(); |
| 94 } else { |
| 95 if (wake_lock_) |
| 96 RemoveWakeLock(); |
| 97 } |
| 98 } |
| 99 |
| 100 } // namespace content |
OLD | NEW |