| 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> | |
| 10 #include <utility> | 9 #include <utility> |
| 11 | 10 |
| 12 #include "base/callback.h" | 11 #include "base/callback.h" |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 15 #include "base/sequenced_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
| 16 #include "device/wake_lock/public/interfaces/wake_lock_context.mojom.h" | 14 #include "device/wake_lock/public/interfaces/wake_lock_context.mojom.h" |
| 17 #include "device/wake_lock/wake_lock_service_impl.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" | 15 #include "ui/gfx/native_widget_types.h" |
| 22 | 16 |
| 23 namespace device { | 17 namespace device { |
| 24 | 18 |
| 25 class PowerSaveBlocker; | |
| 26 | |
| 27 // Callback that maps a context ID to the NativeView associated with | 19 // Callback that maps a context ID to the NativeView associated with |
| 28 // that context. This callback is provided to the Device Service by its | 20 // that context. This callback is provided to the Device Service by its |
| 29 // embedder. | 21 // embedder. |
| 30 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; | 22 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; |
| 31 | 23 |
| 32 // Serves requests for WakeLockService connections within a given context. | 24 // 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 { | 25 class WakeLockServiceContext : public mojom::WakeLockContext { |
| 43 public: | 26 public: |
| 44 WakeLockServiceContext( | 27 WakeLockServiceContext( |
| 45 mojom::WakeLockContextRequest request, | |
| 46 int context_id, | 28 int context_id, |
| 47 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 29 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
| 48 const WakeLockContextCallback& native_view_getter); | 30 const WakeLockContextCallback& native_view_getter); |
| 49 ~WakeLockServiceContext() override; | 31 ~WakeLockServiceContext() override; |
| 50 | 32 |
| 51 // mojom::WakeLockContext: | 33 // mojom::WakeLockContext: |
| 52 void GetWakeLock( | 34 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 | 35 |
| 63 private: | 36 private: |
| 64 void CreateWakeLock(); | |
| 65 void RemoveWakeLock(); | |
| 66 void UpdateWakeLock(); | |
| 67 void OnContextBindingError(); | |
| 68 | |
| 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_; | 37 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
| 74 | |
| 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_; | 38 int context_id_; |
| 82 WakeLockContextCallback native_view_getter_; | 39 WakeLockContextCallback native_view_getter_; |
| 83 #endif | |
| 84 | |
| 85 mojo::Binding<mojom::WakeLockContext> context_binding_; | |
| 86 bool context_binding_encountered_error_; | |
| 87 | |
| 88 mojo::StrongBindingSet<mojom::WakeLockService> wake_lock_bindings_; | |
| 89 | 40 |
| 90 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); | 41 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); |
| 91 }; | 42 }; |
| 92 | 43 |
| 93 } // namespace device | 44 } // namespace device |
| 94 | 45 |
| 95 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ | 46 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ |
| OLD | NEW |