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 <utility> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "build/build_config.h" | |
12 #include "device/power_save_blocker/power_save_blocker.h" | |
13 #include "mojo/public/cpp/bindings/strong_binding.h" | |
14 | |
15 namespace content { | |
16 | |
17 WakeLockServiceContext::WakeLockServiceContext( | |
18 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | |
19 base::Callback<gfx::NativeView()> native_view_getter) | |
20 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
21 file_task_runner_(file_task_runner), | |
22 num_lock_requests_(0), | |
23 native_view_getter_(native_view_getter), | |
24 weak_factory_(this) {} | |
25 | |
26 WakeLockServiceContext::~WakeLockServiceContext() {} | |
27 | |
28 void WakeLockServiceContext::CreateService( | |
29 mojo::InterfaceRequest<blink::mojom::WakeLockService> request) { | |
30 mojo::MakeStrongBinding( | |
31 base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()), | |
32 std::move(request)); | |
33 } | |
34 | |
35 void WakeLockServiceContext::RequestWakeLock() { | |
36 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
37 num_lock_requests_++; | |
38 UpdateWakeLock(); | |
39 } | |
40 | |
41 void WakeLockServiceContext::CancelWakeLock() { | |
42 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | |
43 num_lock_requests_--; | |
44 UpdateWakeLock(); | |
45 } | |
46 | |
47 bool WakeLockServiceContext::HasWakeLockForTests() const { | |
48 return !!wake_lock_; | |
49 } | |
50 | |
51 void WakeLockServiceContext::CreateWakeLock() { | |
52 DCHECK(!wake_lock_); | |
53 wake_lock_.reset(new device::PowerSaveBlocker( | |
54 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | |
55 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", | |
56 main_task_runner_, file_task_runner_)); | |
57 | |
58 #if defined(OS_ANDROID) | |
59 gfx::NativeView native_view = native_view_getter_.Run(); | |
60 if (native_view) { | |
61 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | |
62 } | |
63 #endif | |
64 } | |
65 | |
66 void WakeLockServiceContext::RemoveWakeLock() { | |
67 DCHECK(wake_lock_); | |
68 wake_lock_.reset(); | |
69 } | |
70 | |
71 void WakeLockServiceContext::UpdateWakeLock() { | |
72 DCHECK(num_lock_requests_ >= 0); | |
73 if (num_lock_requests_) { | |
74 if (!wake_lock_) | |
75 CreateWakeLock(); | |
76 } else { | |
77 if (wake_lock_) | |
78 RemoveWakeLock(); | |
79 } | |
80 } | |
81 | |
82 } // namespace content | |
OLD | NEW |