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_IMPL_H_ | 5 #ifndef DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_IMPL_H_ |
6 #define DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_IMPL_H_ | 6 #define DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <map> | |
9 | |
8 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/ref_counted.h" | |
12 #include "device/power_save_blocker/power_save_blocker.h" | |
9 #include "device/wake_lock/public/interfaces/wake_lock_service.mojom.h" | 13 #include "device/wake_lock/public/interfaces/wake_lock_service.mojom.h" |
10 #include "mojo/public/cpp/bindings/interface_request.h" | 14 #include "mojo/public/cpp/bindings/binding_set.h" |
15 #include "ui/gfx/native_widget_types.h" | |
11 | 16 |
12 namespace device { | 17 namespace device { |
13 | 18 |
14 class WakeLockServiceContext; | 19 class WakeLockServiceContext; |
15 | 20 |
21 // Callback that maps a context ID to the NativeView associated with | |
22 // that context. This callback is provided to the Device Service by its | |
23 // embedder. | |
24 using WakeLockContextCallback = base::Callback<gfx::NativeView(int)>; | |
25 | |
16 class WakeLockServiceImpl : public mojom::WakeLockService { | 26 class WakeLockServiceImpl : public mojom::WakeLockService { |
17 public: | 27 public: |
18 explicit WakeLockServiceImpl(WakeLockServiceContext* context); | 28 WakeLockServiceImpl( |
29 WakeLockServiceContext* context, | |
30 device::PowerSaveBlocker::PowerSaveBlockerType type, | |
31 device::PowerSaveBlocker::Reason reason, | |
32 const std::string& description, | |
33 int context_id, | |
34 WakeLockContextCallback native_view_getter, | |
35 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | |
19 ~WakeLockServiceImpl() override; | 36 ~WakeLockServiceImpl() override; |
20 | 37 |
21 // WakeLockSevice implementation. | 38 // WakeLockSevice implementation. |
22 void RequestWakeLock() override; | 39 void RequestWakeLock() override; |
23 void CancelWakeLock() override; | 40 void CancelWakeLock() override; |
41 void AddClient(mojom::WakeLockServiceRequest request) override; | |
24 | 42 |
25 private: | 43 private: |
44 void UpdateWakeLock(); | |
45 void CreateWakeLock(); | |
46 void RemoveWakeLock(); | |
47 void OnConnectionError(); | |
48 | |
26 // Will outlive this instance. | 49 // Will outlive this instance. |
27 WakeLockServiceContext* context_; | 50 WakeLockServiceContext* context_; |
28 bool wake_lock_request_outstanding_; | 51 |
52 device::PowerSaveBlocker::PowerSaveBlockerType type_; | |
53 device::PowerSaveBlocker::Reason reason_; | |
54 std::unique_ptr<std::string> description_; | |
55 int num_lock_requests_; | |
56 | |
57 #if defined(OS_ANDROID) | |
58 int context_id_; | |
59 WakeLockContextCallback native_view_getter_; | |
60 #endif | |
61 | |
62 scoped_refptr<base::SequencedTaskRunner> main_task_runner_; | |
63 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
64 | |
65 // The actual power save blocker for screen. | |
66 std::unique_ptr<PowerSaveBlocker> wake_lock_; | |
67 | |
68 // Wake lock requests from frames are coalesced into |frames_binding_set_|, | |
69 // While other requests *strongbinds* this WakeLockServiceImpl and don't use | |
70 // the |frames_binding_set_| at all. So we have to use a |outstandings_| map | |
71 // and a |outstanding_| to help handle both cases. | |
72 | |
73 // TODO(heke): Split WakeLockServiceImpl into two derived classes that both | |
74 // implements mojom::WakeLockService interface. One to handle requests from | |
75 // frames, another to handle other kinds requests. | |
76 mojo::BindingSet<mojom::WakeLockService, int> frames_binding_set_; | |
blundell
2017/05/02 11:27:43
See my comment on wake_lock_service_context.h. I t
ke.he
2017/05/03 06:25:37
If we stuff all the requests into the BindingSet,
blundell
2017/05/03 09:13:13
I'm confused: what prevents the client just using
ke.he
2017/05/04 11:54:14
Understand after the mail discussion, Done.
| |
77 std::map<int, bool> outstandings_; | |
78 bool outstanding_; | |
29 | 79 |
30 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceImpl); | 80 DISALLOW_COPY_AND_ASSIGN(WakeLockServiceImpl); |
31 }; | 81 }; |
32 | 82 |
33 } // namespace device | 83 } // namespace device |
34 | 84 |
35 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_IMPL_H_ | 85 #endif // DEVICE_WAKE_LOCK_WAKE_LOCK_SERVICE_IMPL_H_ |
OLD | NEW |