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" |
19 #include "ui/gfx/native_widget_types.h" | 20 #include "ui/gfx/native_widget_types.h" |
20 | 21 |
21 namespace device { | 22 namespace device { |
22 | 23 |
23 class PowerSaveBlocker; | 24 class PowerSaveBlocker; |
24 | 25 |
25 class WakeLockServiceContext { | 26 // Callback that maps a context ID to the NativeView associated with |
| 27 // that context. This callback is provided to the Device Service by its |
| 28 // embedder. |
| 29 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; |
| 30 |
| 31 // Serves requests for WakeLockService connections within a given context. |
| 32 class WakeLockServiceContext : public mojom::WakeLockContext { |
26 public: | 33 public: |
27 WakeLockServiceContext( | 34 WakeLockServiceContext( |
| 35 mojom::WakeLockContextRequest request, |
| 36 int context_id, |
28 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, | 37 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner, |
29 base::Callback<gfx::NativeView()> native_view_getter); | 38 WakeLockContextCallback native_view_getter); |
30 ~WakeLockServiceContext(); | 39 ~WakeLockServiceContext() override; |
31 | 40 |
32 // Creates a WakeLockServiceImpl that is strongly bound to |request|. | 41 // mojom::WakeLockContext: |
33 void CreateService(mojo::InterfaceRequest<mojom::WakeLockService> request); | 42 void GetWakeLock( |
| 43 mojo::InterfaceRequest<mojom::WakeLockService> request) override; |
| 44 void HasWakeLockForTests( |
| 45 const HasWakeLockForTestsCallback& callback) override; |
34 | 46 |
35 // Requests wake lock. | 47 // Requests wake lock. |
36 void RequestWakeLock(); | 48 void RequestWakeLock(); |
37 | 49 |
38 // Cancels pending wake lock request. | 50 // Cancels pending wake lock request. |
39 void CancelWakeLock(); | 51 void CancelWakeLock(); |
40 | 52 |
41 // Used by tests. | |
42 bool HasWakeLockForTests() const; | |
43 | |
44 private: | 53 private: |
45 void CreateWakeLock(); | 54 void CreateWakeLock(); |
46 void RemoveWakeLock(); | 55 void RemoveWakeLock(); |
47 void UpdateWakeLock(); | 56 void UpdateWakeLock(); |
| 57 void OnConnectionError(); |
48 | 58 |
49 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; | 59 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; |
50 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 60 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; |
51 | 61 |
52 int num_lock_requests_; | 62 int num_lock_requests_; |
53 | 63 |
54 // The actual power save blocker for screen. | 64 // The actual power save blocker for screen. |
55 std::unique_ptr<PowerSaveBlocker> wake_lock_; | 65 std::unique_ptr<PowerSaveBlocker> wake_lock_; |
56 base::Callback<gfx::NativeView()> native_view_getter_; | |
57 | 66 |
58 base::WeakPtrFactory<WakeLockServiceContext> weak_factory_; | 67 mojo::Binding<mojom::WakeLockContext> binding_; |
| 68 |
| 69 #if defined(OS_ANDROID) |
| 70 int context_id_; |
| 71 WakeLockContextCallback native_view_getter_; |
| 72 #endif |
| 73 |
| 74 // The number of connections that the client has in this context, |
| 75 // including both the connection to this implementation and all |
| 76 // connections made via |GetWakeLock()|. When this number reaches |
| 77 // 0 this instance isn't needed anymore and kills itself. |
| 78 int num_connections_; |
59 | 79 |
60 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); | 80 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceContext); |
61 }; | 81 }; |
62 | 82 |
63 } // namespace device | 83 } // namespace device |
64 | 84 |
65 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ | 85 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_CONTEXT_H_ |
OLD | NEW |