OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014 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/lock.h" |
| 6 |
| 7 #include "content/browser/web_contents/web_contents_impl.h" |
| 8 #include "content/browser/web_contents/web_contents_view_guest.h" |
| 9 #include "content/public/browser/power_save_blocker.h" |
| 10 #include "content/public/browser/render_view_host.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 Lock::Lock(int render_process_id, int routed_id) |
| 15 : render_process_id_(render_process_id), |
| 16 routed_id_(routed_id), |
| 17 unlocked_force_(false) { |
| 18 LockResource(); |
| 19 } |
| 20 |
| 21 Lock::~Lock() { |
| 22 UnlockResource(); |
| 23 } |
| 24 |
| 25 void Lock::LockResource() { |
| 26 if (blocker_.get()) |
| 27 return; |
| 28 |
| 29 blocker_ = PowerSaveBlocker::Create( |
| 30 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, "WakeLock API"); |
| 31 #if defined(OS_ANDROID) |
| 32 const RenderViewHost* rvh = RenderViewHost::FromID( |
| 33 render_process_id_, routed_id_); |
| 34 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); |
| 35 if (WebContentsImpl* web_contents_impl = |
| 36 static_cast<WebContentsImpl*>(web_contents)) { |
| 37 PowerSaveBlockerImpl* blocker = |
| 38 static_cast<PowerSaveBlockerImpl*>(blocker_.get()); |
| 39 blocker->InitDisplaySleepBlocker( |
| 40 web_contents_impl->GetView()->GetNativeView()); |
| 41 } |
| 42 #endif |
| 43 } |
| 44 |
| 45 void Lock::UnlockResource() { |
| 46 if (blocker_.get()) |
| 47 blocker_.reset(); |
| 48 } |
| 49 |
| 50 void Lock::ForceLockResource() { |
| 51 if (unlocked_force_ == true) |
| 52 LockResource(); |
| 53 unlocked_force_ = false; |
| 54 } |
| 55 |
| 56 void Lock::ForceUnlockResource() { |
| 57 if (unlocked_force_ == false) |
| 58 UnlockResource(); |
| 59 unlocked_force_ = true; |
| 60 } |
| 61 } |
OLD | NEW |