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/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
13 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "content/public/common/service_registry.h" | 15 #include "content/public/common/service_registry.h" |
16 #include "device/power_save_blocker/power_save_blocker.h" | 16 #include "device/power_save_blocker/power_save_blocker.h" |
17 #include "device/power_save_blocker/power_save_blocker_impl.h" | |
18 | 17 |
19 namespace content { | 18 namespace content { |
20 | 19 |
21 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents) | 20 WakeLockServiceContext::WakeLockServiceContext(WebContents* web_contents) |
22 : WebContentsObserver(web_contents), weak_factory_(this) {} | 21 : WebContentsObserver(web_contents), weak_factory_(this) {} |
23 | 22 |
24 WakeLockServiceContext::~WakeLockServiceContext() {} | 23 WakeLockServiceContext::~WakeLockServiceContext() {} |
25 | 24 |
26 void WakeLockServiceContext::CreateService( | 25 void WakeLockServiceContext::CreateService( |
27 int render_process_id, | 26 int render_process_id, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 std::pair<int, int>(render_process_id, render_frame_id)); | 60 std::pair<int, int>(render_process_id, render_frame_id)); |
62 UpdateWakeLock(); | 61 UpdateWakeLock(); |
63 } | 62 } |
64 | 63 |
65 bool WakeLockServiceContext::HasWakeLockForTests() const { | 64 bool WakeLockServiceContext::HasWakeLockForTests() const { |
66 return !!wake_lock_; | 65 return !!wake_lock_; |
67 } | 66 } |
68 | 67 |
69 void WakeLockServiceContext::CreateWakeLock() { | 68 void WakeLockServiceContext::CreateWakeLock() { |
70 DCHECK(!wake_lock_); | 69 DCHECK(!wake_lock_); |
71 wake_lock_ = device::PowerSaveBlocker::CreateWithTaskRunners( | 70 wake_lock_.reset(new device::PowerSaveBlocker( |
72 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 71 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
73 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", | 72 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", |
74 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), | 73 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI), |
75 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 74 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); |
76 | 75 |
77 #if defined(OS_ANDROID) | 76 #if defined(OS_ANDROID) |
78 // On Android, additionaly associate the blocker with this WebContents. | 77 // On Android, additionaly associate the blocker with this WebContents. |
79 DCHECK(web_contents()); | 78 DCHECK(web_contents()); |
80 | 79 |
81 if (web_contents()->GetNativeView()) { | 80 if (web_contents()->GetNativeView()) { |
82 view_weak_factory_.reset(new base::WeakPtrFactory<ui::ViewAndroid>( | 81 view_weak_factory_.reset(new base::WeakPtrFactory<ui::ViewAndroid>( |
83 web_contents()->GetNativeView())); | 82 web_contents()->GetNativeView())); |
84 static_cast<device::PowerSaveBlockerImpl*>(wake_lock_.get()) | 83 wake_lock_.get()->InitDisplaySleepBlocker(view_weak_factory_->GetWeakPtr()); |
85 ->InitDisplaySleepBlocker(view_weak_factory_->GetWeakPtr()); | |
86 } | 84 } |
87 #endif | 85 #endif |
88 } | 86 } |
89 | 87 |
90 void WakeLockServiceContext::RemoveWakeLock() { | 88 void WakeLockServiceContext::RemoveWakeLock() { |
91 DCHECK(wake_lock_); | 89 DCHECK(wake_lock_); |
92 wake_lock_.reset(); | 90 wake_lock_.reset(); |
93 } | 91 } |
94 | 92 |
95 void WakeLockServiceContext::UpdateWakeLock() { | 93 void WakeLockServiceContext::UpdateWakeLock() { |
96 if (!frames_requesting_lock_.empty()) { | 94 if (!frames_requesting_lock_.empty()) { |
97 if (!wake_lock_) | 95 if (!wake_lock_) |
98 CreateWakeLock(); | 96 CreateWakeLock(); |
99 } else { | 97 } else { |
100 if (wake_lock_) | 98 if (wake_lock_) |
101 RemoveWakeLock(); | 99 RemoveWakeLock(); |
102 } | 100 } |
103 } | 101 } |
104 | 102 |
105 } // namespace content | 103 } // namespace content |
OLD | NEW |