Index: device/wake_lock/wake_lock_service_context.cc |
diff --git a/device/wake_lock/wake_lock_service_context.cc b/device/wake_lock/wake_lock_service_context.cc |
index d30f8cdaadd5f8a7224a237932bfb02bc12e1467..3a4ce26f823d484d34f15a00baac2265d6f5fc4d 100644 |
--- a/device/wake_lock/wake_lock_service_context.cc |
+++ b/device/wake_lock/wake_lock_service_context.cc |
@@ -16,21 +16,33 @@ |
namespace device { |
WakeLockServiceContext::WakeLockServiceContext( |
+ mojom::WakeLockContextRequest request, |
+ int context_id, |
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
- base::Callback<gfx::NativeView()> native_view_getter) |
+ 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.
|
: main_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
file_task_runner_(file_task_runner), |
dcheng
2017/03/17 06:55:26
Nit: std::move
blundell
2017/03/17 12:28:22
Done.
|
num_lock_requests_(0), |
+ binding_(this, std::move(request)), |
+#if defined(OS_ANDROID) |
+ context_id_(context_id), |
native_view_getter_(native_view_getter), |
- weak_factory_(this) {} |
+#endif |
+ num_connections_(1) { |
+ binding_.set_connection_error_handler(base::Bind( |
+ &WakeLockServiceContext::OnConnectionError, base::Unretained(this))); |
+} |
WakeLockServiceContext::~WakeLockServiceContext() {} |
-void WakeLockServiceContext::CreateService( |
+void WakeLockServiceContext::GetWakeLock( |
mojo::InterfaceRequest<mojom::WakeLockService> request) { |
- mojo::MakeStrongBinding( |
- base::MakeUnique<WakeLockServiceImpl>(weak_factory_.GetWeakPtr()), |
- std::move(request)); |
+ num_connections_++; |
+ mojo::StrongBindingPtr<mojom::WakeLockService> binding = |
+ mojo::MakeStrongBinding(base::MakeUnique<WakeLockServiceImpl>(this), |
+ std::move(request)); |
+ binding->set_connection_error_handler(base::Bind( |
+ &WakeLockServiceContext::OnConnectionError, base::Unretained(this))); |
} |
void WakeLockServiceContext::RequestWakeLock() { |
@@ -45,8 +57,9 @@ void WakeLockServiceContext::CancelWakeLock() { |
UpdateWakeLock(); |
} |
-bool WakeLockServiceContext::HasWakeLockForTests() const { |
- return !!wake_lock_; |
+void WakeLockServiceContext::HasWakeLockForTests( |
+ const HasWakeLockForTestsCallback& callback) { |
+ callback.Run(!!wake_lock_); |
} |
void WakeLockServiceContext::CreateWakeLock() { |
@@ -57,7 +70,7 @@ void WakeLockServiceContext::CreateWakeLock() { |
main_task_runner_, file_task_runner_)); |
#if defined(OS_ANDROID) |
- gfx::NativeView native_view = native_view_getter_.Run(); |
+ gfx::NativeView native_view = native_view_getter_.Run(context_id_); |
if (native_view) { |
wake_lock_.get()->InitDisplaySleepBlocker(native_view); |
} |
@@ -80,4 +93,16 @@ void WakeLockServiceContext::UpdateWakeLock() { |
} |
} |
+void WakeLockServiceContext::OnConnectionError() { |
+ DCHECK(num_connections_ > 0); |
dcheng
2017/03/17 06:55:26
Nit: DCHECK_GT
blundell
2017/03/17 12:28:22
Done.
|
+ num_connections_--; |
+ |
+ // Delete this instance when there are no more live connections to it. |
+ // However, ensure that this instance stays alive throughout the destructor of |
+ // a WakeLockServiceImpl instance that might be triggering this callback by |
+ // posting the deletion as a task. |
+ if (num_connections_ == 0) |
+ 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
|
+} |
+ |
} // namespace device |