Chromium Code Reviews| 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 #ifndef DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ | 5 #ifndef DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ |
| 6 #define DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ | 6 #define DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequenced_task_runner.h" | 15 #include "base/sequenced_task_runner.h" |
| 16 #include "device/wake_lock/public/interfaces/wake_lock_context.mojom.h" | 16 #include "device/wake_lock/public/interfaces/wake_lock_context.mojom.h" |
| 17 #include "device/wake_lock/wake_lock_service_impl.h" | 17 #include "device/wake_lock/wake_lock_service_impl.h" |
| 18 #include "mojo/public/cpp/bindings/binding.h" | 18 #include "mojo/public/cpp/bindings/binding.h" |
| 19 #include "mojo/public/cpp/bindings/interface_request.h" | |
| 20 #include "mojo/public/cpp/bindings/strong_binding_set.h" | |
| 21 #include "ui/gfx/native_widget_types.h" | 19 #include "ui/gfx/native_widget_types.h" |
| 22 | 20 |
| 23 namespace device { | 21 namespace device { |
| 24 | 22 |
| 25 class PowerSaveBlocker; | |
| 26 | |
| 27 // Callback that maps a context ID to the NativeView associated with | 23 // Callback that maps a context ID to the NativeView associated with |
| 28 // that context. This callback is provided to the Device Service by its | 24 // that context. This callback is provided to the Device Service by its |
| 29 // embedder. | 25 // embedder. |
| 30 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; | 26 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; |
| 31 | 27 |
| 32 // Serves requests for WakeLockService connections within a given context. | 28 // Serves requests for WakeLockService connections within a given context. |
| 33 | |
| 34 // Note that the lifetime model of WakeLockContext is somewhat complex: It must | |
| 35 // stay alive as long as either | |
| 36 // (1) Its Mojo connection is still valid (as the client might make future | |
| 37 // GetWakeLock() calls) OR | |
| 38 // (2) There are still live WakeLock instances that it has instantiated (since | |
| 39 // they call into it when they receive Mojo requests from *their* clients). | |
| 40 // Consequently, WakeLockContext monitors the state of the connections described | |
| 41 // in (1) and (2), dying only when *all* of those connections go away. | |
| 42 class WakeLockServiceContext : public mojom::WakeLockContext { | 29 class WakeLockServiceContext : public mojom::WakeLockContext { |
| 43 public: | 30 public: |
| 44 WakeLockServiceContext( | 31 WakeLockServiceContext( |
| 45 mojom::WakeLockContextRequest request, | 32 mojom::WakeLockContextRequest request, |
| 46 int context_id, | 33 int context_id, |
| 47 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 34 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 48 const WakeLockContextCallback& native_view_getter); | 35 const WakeLockContextCallback& native_view_getter); |
| 49 ~WakeLockServiceContext() override; | 36 ~WakeLockServiceContext() override; |
| 50 | 37 |
| 51 // mojom::WakeLockContext: | 38 // mojom::WakeLockContext: |
| 52 void GetWakeLock( | 39 void GetWakeLock(mojom::WakeLockServiceRequest request) override; |
| 53 mojo::InterfaceRequest<mojom::WakeLockService> request) override; | |
| 54 void HasWakeLockForTests( | |
| 55 const HasWakeLockForTestsCallback& callback) override; | |
| 56 | |
| 57 // Requests wake lock. | |
| 58 void RequestWakeLock(); | |
| 59 | |
| 60 // Cancels pending wake lock request. | |
| 61 void CancelWakeLock(); | |
| 62 | 40 |
| 63 private: | 41 private: |
| 64 void CreateWakeLock(); | |
| 65 void RemoveWakeLock(); | |
| 66 void UpdateWakeLock(); | |
| 67 void OnContextBindingError(); | 42 void OnContextBindingError(); |
| 68 | 43 |
| 69 // Checks whether this instance is still needed, and if not, destroys it. | |
| 70 void DestroyIfNoLongerNeeded(); | |
| 71 | |
| 72 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; | |
| 73 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 44 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 74 | 45 |
| 75 int num_lock_requests_; | |
| 76 | |
| 77 // The actual power save blocker for screen. | |
| 78 std::unique_ptr<PowerSaveBlocker> wake_lock_; | |
| 79 | |
| 80 #if defined(OS_ANDROID) | |
| 81 int context_id_; | 46 int context_id_; |
| 82 WakeLockContextCallback native_view_getter_; | 47 WakeLockContextCallback native_view_getter_; |
| 83 #endif | |
| 84 | 48 |
| 85 mojo::Binding<mojom::WakeLockContext> context_binding_; | 49 mojo::Binding<mojom::WakeLockContext> context_binding_; |
|
blundell
2017/05/04 15:40:50
We can just make this a StrongBinding now.
ke.he
2017/05/05 08:58:00
Yes. and OnContextBindingError() is not needed eit
| |
| 86 bool context_binding_encountered_error_; | |
| 87 | |
| 88 mojo::StrongBindingSet<mojom::WakeLockService> wake_lock_bindings_; | |
| 89 | 50 |
| 90 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); | 51 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); |
| 91 }; | 52 }; |
| 92 | 53 |
| 93 } // namespace device | 54 } // namespace device |
| 94 | 55 |
| 95 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ | 56 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ |
| OLD | NEW |