| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/wake_lock/wake_lock_service_context.h" | 5 #include "content/browser/wake_lock/wake_lock_service_context.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "content/browser/power_save_blocker_impl.h" | 11 #include "content/browser/power_save_blocker_factory.h" |
| 12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/power_save_blocker_factory.h" | |
| 14 #include "content/public/browser/render_frame_host.h" | 13 #include "content/public/browser/render_frame_host.h" |
| 15 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/common/service_registry.h" | 16 #include "content/public/common/service_registry.h" |
| 17 #include "device/power_save_blocker/power_save_blocker_impl.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 | 20 |
| 21 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents) | 21 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents) |
| 22 : WebContentsObserver(web_contents), weak_factory_(this) {} | 22 : WebContentsObserver(web_contents), weak_factory_(this) {} |
| 23 | 23 |
| 24 WakeLockServiceContext::~WakeLockServiceContext() {} | 24 WakeLockServiceContext::~WakeLockServiceContext() {} |
| 25 | 25 |
| 26 void WakeLockServiceContext::CreateService( | 26 void WakeLockServiceContext::CreateService( |
| 27 int render_process_id, | 27 int render_process_id, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 UpdateWakeLock(); | 62 UpdateWakeLock(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 bool WakeLockServiceContext::HasWakeLockForTests() const { | 65 bool WakeLockServiceContext::HasWakeLockForTests() const { |
| 66 return !!wake_lock_; | 66 return !!wake_lock_; |
| 67 } | 67 } |
| 68 | 68 |
| 69 void WakeLockServiceContext::CreateWakeLock() { | 69 void WakeLockServiceContext::CreateWakeLock() { |
| 70 DCHECK(!wake_lock_); | 70 DCHECK(!wake_lock_); |
| 71 wake_lock_ = CreatePowerSaveBlocker( | 71 wake_lock_ = CreatePowerSaveBlocker( |
| 72 PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 72 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
| 73 PowerSaveBlocker::kReasonOther, "Wake Lock API"); | 73 device::PowerSaveBlocker::kReasonOther, "Wake Lock API"); |
| 74 | 74 |
| 75 #if defined(OS_ANDROID) | 75 #if defined(OS_ANDROID) |
| 76 // On Android, additionaly associate the blocker with this WebContents. | 76 // On Android, additionaly associate the blocker with this WebContents. |
| 77 DCHECK(web_contents()); | 77 DCHECK(web_contents()); |
| 78 | 78 |
| 79 if (web_contents()->GetNativeView()) { | 79 if (web_contents()->GetNativeView()) { |
| 80 view_weak_factory_.reset(new base::WeakPtrFactory<ui::ViewAndroid>( | 80 view_weak_factory_.reset(new base::WeakPtrFactory<ui::ViewAndroid>( |
| 81 web_contents()->GetNativeView())); | 81 web_contents()->GetNativeView())); |
| 82 static_cast<PowerSaveBlockerImpl*>(wake_lock_.get()) | 82 static_cast<device::PowerSaveBlockerImpl*>(wake_lock_.get()) |
| 83 ->InitDisplaySleepBlocker(view_weak_factory_->GetWeakPtr()); | 83 ->InitDisplaySleepBlocker(view_weak_factory_->GetWeakPtr()); |
| 84 } | 84 } |
| 85 #endif | 85 #endif |
| 86 } | 86 } |
| 87 | 87 |
| 88 void WakeLockServiceContext::RemoveWakeLock() { | 88 void WakeLockServiceContext::RemoveWakeLock() { |
| 89 DCHECK(wake_lock_); | 89 DCHECK(wake_lock_); |
| 90 wake_lock_.reset(); | 90 wake_lock_.reset(); |
| 91 } | 91 } |
| 92 | 92 |
| 93 void WakeLockServiceContext::UpdateWakeLock() { | 93 void WakeLockServiceContext::UpdateWakeLock() { |
| 94 if (!frames_requesting_lock_.empty()) { | 94 if (!frames_requesting_lock_.empty()) { |
| 95 if (!wake_lock_) | 95 if (!wake_lock_) |
| 96 CreateWakeLock(); | 96 CreateWakeLock(); |
| 97 } else { | 97 } else { |
| 98 if (wake_lock_) | 98 if (wake_lock_) |
| 99 RemoveWakeLock(); | 99 RemoveWakeLock(); |
| 100 } | 100 } |
| 101 } | 101 } |
| 102 | 102 |
| 103 } // namespace content | 103 } // namespace content |
| OLD | NEW |