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 "device/wake_lock/wake_lock_service_context.h" | 5 #include "device/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 "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "device/power_save_blocker/power_save_blocker.h" | 13 #include "device/power_save_blocker/power_save_blocker.h" |
14 #include "mojo/public/cpp/bindings/strong_binding.h" | 14 #include "mojo/public/cpp/bindings/strong_binding.h" |
15 | 15 |
16 namespace device { | 16 namespace device { |
17 | 17 |
18 WakeLockServiceContext::WakeLockServiceContext( | 18 WakeLockServiceContext::WakeLockServiceContext( |
19 mojom::WakeLockContextRequest request, | |
20 int context_id, | |
19 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 21 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
20 base::Callback<gfx::NativeView()> native_view_getter) | 22 WakeLockContextCallback native_view_getter) |
dcheng
2017/03/17 06:55:26
Nit: either move or pass by const ref (pass by con
blundell
2017/03/17 12:28:22
Done.
| |
21 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 23 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
22 file_task_runner_(file_task_runner), | 24 file_task_runner_(file_task_runner), |
dcheng
2017/03/17 06:55:26
Nit: std::move
blundell
2017/03/17 12:28:22
Done.
| |
23 num_lock_requests_(0), | 25 num_lock_requests_(0), |
26 binding_(this, std::move(request)), | |
27 #if defined(OS_ANDROID) | |
28 context_id_(context_id), | |
24 native_view_getter_(native_view_getter), | 29 native_view_getter_(native_view_getter), |
25 weak_factory_(this) {} | 30 #endif |
31 num_connections_(1) { | |
32 binding_.set_connection_error_handler(base::Bind( | |
33 &WakeLockServiceContext::OnConnectionError, base::Unretained(this))); | |
34 } | |
26 | 35 |
27 WakeLockServiceContext::~WakeLockServiceContext() {} | 36 WakeLockServiceContext::~WakeLockServiceContext() {} |
28 | 37 |
29 void WakeLockServiceContext::CreateService( | 38 void WakeLockServiceContext::GetWakeLock( |
30 mojo::InterfaceRequest<mojom::WakeLockService> request) { | 39 mojo::InterfaceRequest<mojom::WakeLockService> request) { |
31 mojo::MakeStrongBinding( | 40 num_connections_++; |
32 base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()), | 41 mojo::StrongBindingPtr<mojom::WakeLockService> binding = |
33 std::move(request)); | 42 mojo::MakeStrongBinding(base::MakeUnique<WakeLockServiceImpl>(this), |
43 std::move(request)); | |
44 binding->set_connection_error_handler(base::Bind( | |
45 &WakeLockServiceContext::OnConnectionError, base::Unretained(this))); | |
34 } | 46 } |
35 | 47 |
36 void WakeLockServiceContext::RequestWakeLock() { | 48 void WakeLockServiceContext::RequestWakeLock() { |
37 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 49 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
38 num_lock_requests_++; | 50 num_lock_requests_++; |
39 UpdateWakeLock(); | 51 UpdateWakeLock(); |
40 } | 52 } |
41 | 53 |
42 void WakeLockServiceContext::CancelWakeLock() { | 54 void WakeLockServiceContext::CancelWakeLock() { |
43 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 55 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
44 num_lock_requests_--; | 56 num_lock_requests_--; |
45 UpdateWakeLock(); | 57 UpdateWakeLock(); |
46 } | 58 } |
47 | 59 |
48 bool WakeLockServiceContext::HasWakeLockForTests() const { | 60 void WakeLockServiceContext::HasWakeLockForTests( |
49 return !!wake_lock_; | 61 const HasWakeLockForTestsCallback& callback) { |
62 callback.Run(!!wake_lock_); | |
50 } | 63 } |
51 | 64 |
52 void WakeLockServiceContext::CreateWakeLock() { | 65 void WakeLockServiceContext::CreateWakeLock() { |
53 DCHECK(!wake_lock_); | 66 DCHECK(!wake_lock_); |
54 wake_lock_.reset(new device::PowerSaveBlocker( | 67 wake_lock_.reset(new device::PowerSaveBlocker( |
55 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 68 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
56 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", | 69 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", |
57 main_task_runner_, file_task_runner_)); | 70 main_task_runner_, file_task_runner_)); |
58 | 71 |
59 #if defined(OS_ANDROID) | 72 #if defined(OS_ANDROID) |
60 gfx::NativeView native_view = native_view_getter_.Run(); | 73 gfx::NativeView native_view = native_view_getter_.Run(context_id_); |
61 if (native_view) { | 74 if (native_view) { |
62 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | 75 wake_lock_.get()->InitDisplaySleepBlocker(native_view); |
63 } | 76 } |
64 #endif | 77 #endif |
65 } | 78 } |
66 | 79 |
67 void WakeLockServiceContext::RemoveWakeLock() { | 80 void WakeLockServiceContext::RemoveWakeLock() { |
68 DCHECK(wake_lock_); | 81 DCHECK(wake_lock_); |
69 wake_lock_.reset(); | 82 wake_lock_.reset(); |
70 } | 83 } |
71 | 84 |
72 void WakeLockServiceContext::UpdateWakeLock() { | 85 void WakeLockServiceContext::UpdateWakeLock() { |
73 DCHECK(num_lock_requests_ >= 0); | 86 DCHECK(num_lock_requests_ >= 0); |
74 if (num_lock_requests_) { | 87 if (num_lock_requests_) { |
75 if (!wake_lock_) | 88 if (!wake_lock_) |
76 CreateWakeLock(); | 89 CreateWakeLock(); |
77 } else { | 90 } else { |
78 if (wake_lock_) | 91 if (wake_lock_) |
79 RemoveWakeLock(); | 92 RemoveWakeLock(); |
80 } | 93 } |
81 } | 94 } |
82 | 95 |
96 void WakeLockServiceContext::OnConnectionError() { | |
97 DCHECK(num_connections_ > 0); | |
dcheng
2017/03/17 06:55:26
Nit: DCHECK_GT
blundell
2017/03/17 12:28:22
Done.
| |
98 num_connections_--; | |
99 | |
100 // Delete this instance when there are no more live connections to it. | |
101 // However, ensure that this instance stays alive throughout the destructor of | |
102 // a WakeLockServiceImpl instance that might be triggering this callback by | |
103 // posting the deletion as a task. | |
104 if (num_connections_ == 0) | |
105 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
Ken Rockot(use gerrit already)
2017/03/16 17:48:40
I haven't understood why this is necessary, i.e. w
blundell
2017/03/17 12:28:22
WakeLockServiceImpl calls back into this class on
| |
106 } | |
107 | |
83 } // namespace device | 108 } // namespace device |
OLD | NEW |