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