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" | |
15 | 14 |
16 namespace device { | 15 namespace device { |
17 | 16 |
18 WakeLockServiceContext::WakeLockServiceContext( | 17 WakeLockServiceContext::WakeLockServiceContext( |
18 mojom::WakeLockContextRequest request, | |
dcheng
2017/03/22 10:09:55
Sorry, one more lifetime question...
Is it possib
blundell
2017/03/22 13:03:40
Any messages sent from the other end of the pipe b
Ken Rockot(use gerrit already)
2017/03/22 18:25:05
+1 to all of that. A pipe being disconnected is no
dcheng
2017/03/22 20:07:32
OK, so the transition to the disconnected state do
| |
19 int context_id, | |
19 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 20 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
20 base::Callback<gfx::NativeView()> native_view_getter) | 21 const WakeLockContextCallback& native_view_getter) |
21 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 22 : main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
22 file_task_runner_(file_task_runner), | 23 file_task_runner_(std::move(file_task_runner)), |
23 num_lock_requests_(0), | 24 num_lock_requests_(0), |
25 #if defined(OS_ANDROID) | |
26 context_id_(context_id), | |
24 native_view_getter_(native_view_getter), | 27 native_view_getter_(native_view_getter), |
25 weak_factory_(this) {} | 28 #endif |
29 context_binding_(this, std::move(request)), | |
30 context_binding_encountered_error_(false) { | |
31 context_binding_.set_connection_error_handler(base::Bind( | |
32 &WakeLockServiceContext::OnContextBindingError, base::Unretained(this))); | |
33 wake_lock_bindings_.set_connection_error_handler( | |
34 base::Bind(&WakeLockServiceContext::DestroyIfNoLongerNeeded, | |
35 base::Unretained(this))); | |
36 } | |
26 | 37 |
27 WakeLockServiceContext::~WakeLockServiceContext() {} | 38 WakeLockServiceContext::~WakeLockServiceContext() {} |
28 | 39 |
29 void WakeLockServiceContext::CreateService( | 40 void WakeLockServiceContext::GetWakeLock( |
30 mojo::InterfaceRequest<mojom::WakeLockService> request) { | 41 mojo::InterfaceRequest<mojom::WakeLockService> request) { |
31 mojo::MakeStrongBinding( | 42 wake_lock_bindings_.AddBinding(base::MakeUnique<WakeLockServiceImpl>(this), |
32 base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()), | 43 std::move(request)); |
33 std::move(request)); | |
34 } | 44 } |
35 | 45 |
36 void WakeLockServiceContext::RequestWakeLock() { | 46 void WakeLockServiceContext::RequestWakeLock() { |
37 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 47 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
38 num_lock_requests_++; | 48 num_lock_requests_++; |
39 UpdateWakeLock(); | 49 UpdateWakeLock(); |
40 } | 50 } |
41 | 51 |
42 void WakeLockServiceContext::CancelWakeLock() { | 52 void WakeLockServiceContext::CancelWakeLock() { |
43 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); | 53 DCHECK(main_task_runner_->RunsTasksOnCurrentThread()); |
44 num_lock_requests_--; | 54 num_lock_requests_--; |
45 UpdateWakeLock(); | 55 UpdateWakeLock(); |
46 } | 56 } |
47 | 57 |
48 bool WakeLockServiceContext::HasWakeLockForTests() const { | 58 void WakeLockServiceContext::HasWakeLockForTests( |
49 return !!wake_lock_; | 59 const HasWakeLockForTestsCallback& callback) { |
60 callback.Run(!!wake_lock_); | |
50 } | 61 } |
51 | 62 |
52 void WakeLockServiceContext::CreateWakeLock() { | 63 void WakeLockServiceContext::CreateWakeLock() { |
53 DCHECK(!wake_lock_); | 64 DCHECK(!wake_lock_); |
54 wake_lock_.reset(new device::PowerSaveBlocker( | 65 wake_lock_.reset(new device::PowerSaveBlocker( |
55 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, | 66 device::PowerSaveBlocker::kPowerSaveBlockPreventDisplaySleep, |
56 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", | 67 device::PowerSaveBlocker::kReasonOther, "Wake Lock API", |
57 main_task_runner_, file_task_runner_)); | 68 main_task_runner_, file_task_runner_)); |
58 | 69 |
59 #if defined(OS_ANDROID) | 70 #if defined(OS_ANDROID) |
60 gfx::NativeView native_view = native_view_getter_.Run(); | 71 gfx::NativeView native_view = native_view_getter_.Run(context_id_); |
61 if (native_view) { | 72 if (native_view) { |
62 wake_lock_.get()->InitDisplaySleepBlocker(native_view); | 73 wake_lock_.get()->InitDisplaySleepBlocker(native_view); |
63 } | 74 } |
64 #endif | 75 #endif |
65 } | 76 } |
66 | 77 |
67 void WakeLockServiceContext::RemoveWakeLock() { | 78 void WakeLockServiceContext::RemoveWakeLock() { |
68 DCHECK(wake_lock_); | 79 DCHECK(wake_lock_); |
69 wake_lock_.reset(); | 80 wake_lock_.reset(); |
70 } | 81 } |
71 | 82 |
72 void WakeLockServiceContext::UpdateWakeLock() { | 83 void WakeLockServiceContext::UpdateWakeLock() { |
73 DCHECK(num_lock_requests_ >= 0); | 84 DCHECK(num_lock_requests_ >= 0); |
74 if (num_lock_requests_) { | 85 if (num_lock_requests_) { |
75 if (!wake_lock_) | 86 if (!wake_lock_) |
76 CreateWakeLock(); | 87 CreateWakeLock(); |
77 } else { | 88 } else { |
78 if (wake_lock_) | 89 if (wake_lock_) |
79 RemoveWakeLock(); | 90 RemoveWakeLock(); |
80 } | 91 } |
81 } | 92 } |
82 | 93 |
94 void WakeLockServiceContext::OnContextBindingError() { | |
95 context_binding_encountered_error_ = true; | |
96 DestroyIfNoLongerNeeded(); | |
97 } | |
98 | |
99 void WakeLockServiceContext::DestroyIfNoLongerNeeded() { | |
100 if (context_binding_encountered_error_ && wake_lock_bindings_.empty()) { | |
101 // Delete this instance once there are no more live connections to it. | |
102 // However, ensure that this instance stays alive throughout the destructor | |
103 // of a WakeLockServiceImpl instance that might be triggering this callback. | |
104 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
105 } | |
106 } | |
107 | |
83 } // namespace device | 108 } // namespace device |
OLD | NEW |