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/memory/weak_ptr.h" | |
16 #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" |
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/interface_request.h" | 19 #include "mojo/public/cpp/bindings/interface_request.h" |
| 20 #include "mojo/public/cpp/bindings/strong_binding_set.h" |
19 #include "ui/gfx/native_widget_types.h" | 21 #include "ui/gfx/native_widget_types.h" |
20 | 22 |
21 namespace device { | 23 namespace device { |
22 | 24 |
23 class PowerSaveBlocker; | 25 class PowerSaveBlocker; |
24 | 26 |
25 class WakeLockServiceContext { | 27 // Callback that maps a context ID to the NativeView associated with |
| 28 // that context. This callback is provided to the Device Service by its |
| 29 // embedder. |
| 30 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; |
| 31 |
| 32 // 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 { |
26 public: | 43 public: |
27 WakeLockServiceContext( | 44 WakeLockServiceContext( |
| 45 mojom::WakeLockContextRequest request, |
| 46 int context_id, |
28 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 47 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
29 base::Callback<gfx::NativeView()> native_view_getter); | 48 const WakeLockContextCallback& native_view_getter); |
30 ~WakeLockServiceContext(); | 49 ~WakeLockServiceContext() override; |
31 | 50 |
32 // Creates a WakeLockServiceImpl that is strongly bound to |request|. | 51 // mojom::WakeLockContext: |
33 void CreateService(mojo::InterfaceRequest<mojom::WakeLockService> request); | 52 void GetWakeLock( |
| 53 mojo::InterfaceRequest<mojom::WakeLockService> request) override; |
| 54 void HasWakeLockForTests( |
| 55 const HasWakeLockForTestsCallback& callback) override; |
34 | 56 |
35 // Requests wake lock. | 57 // Requests wake lock. |
36 void RequestWakeLock(); | 58 void RequestWakeLock(); |
37 | 59 |
38 // Cancels pending wake lock request. | 60 // Cancels pending wake lock request. |
39 void CancelWakeLock(); | 61 void CancelWakeLock(); |
40 | 62 |
41 // Used by tests. | |
42 bool HasWakeLockForTests() const; | |
43 | |
44 private: | 63 private: |
45 void CreateWakeLock(); | 64 void CreateWakeLock(); |
46 void RemoveWakeLock(); | 65 void RemoveWakeLock(); |
47 void UpdateWakeLock(); | 66 void UpdateWakeLock(); |
| 67 void OnContextBindingError(); |
| 68 |
| 69 // Checks whether this instance is still needed, and if not, destroys it. |
| 70 void DestroyIfNoLongerNeeded(); |
48 | 71 |
49 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; | 72 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; |
50 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 73 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
51 | 74 |
52 int num_lock_requests_; | 75 int num_lock_requests_; |
53 | 76 |
54 // The actual power save blocker for screen. | 77 // The actual power save blocker for screen. |
55 std::unique_ptr<PowerSaveBlocker> wake_lock_; | 78 std::unique_ptr<PowerSaveBlocker> wake_lock_; |
56 base::Callback<gfx::NativeView()> native_view_getter_; | |
57 | 79 |
58 base::WeakPtrFactory<WakeLockServiceContext> weak_factory_; | 80 #if defined(OS_ANDROID) |
| 81 int context_id_; |
| 82 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_; |
59 | 89 |
60 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); | 90 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); |
61 }; | 91 }; |
62 | 92 |
63 } // namespace device | 93 } // namespace device |
64 | 94 |
65 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ | 95 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ |
OLD | NEW |