OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef SERVICES_MEDIA_FRAMEWORK_MOJO_PUBLISHER_H_ | |
6 #define SERVICES_MEDIA_FRAMEWORK_MOJO_PUBLISHER_H_ | |
7 | |
8 #include <functional> | |
9 #include <vector> | |
10 | |
11 namespace mojo { | |
12 namespace media { | |
13 | |
14 // Implements pull mode publishing (e.g. MediaPlayer::GetStatus). | |
15 template <typename TCallback> | |
16 class MojoPublisher { | |
17 public: | |
18 using CallbackRunner = std::function<void(const TCallback&, uint64_t)>; | |
19 | |
20 // Sets the callback runner. This method must be called before calling Get | |
21 // or Updated. The callback runner calls a single callback using current | |
22 // information. | |
23 void SetCallbackRunner(const CallbackRunner& callback_runner) { | |
24 DCHECK(callback_runner); | |
25 callback_runner_ = callback_runner; | |
26 } | |
27 | |
28 // Handles a get request from the client. This method should be called from | |
29 // the mojo 'get' method (e.g. MediaPlayer::GetStatus). | |
30 void Get(uint64_t version_last_seen, const TCallback& callback) { | |
31 DCHECK(callback_runner_); | |
32 | |
33 if (version_last_seen < version_) { | |
34 callback_runner_(callback, version_); | |
35 } else { | |
36 pending_callbacks_.push_back(callback); | |
37 } | |
38 } | |
39 | |
40 // Increments the version number and runs pending callbacks. This method | |
41 // should be called whenever the published information should be sent to | |
42 // subscribing clients. | |
43 void SendUpdates() { | |
44 DCHECK(callback_runner_); | |
45 | |
46 ++version_; | |
47 | |
48 std::vector<TCallback> pending_callbacks; | |
49 pending_callbacks_.swap(pending_callbacks); | |
50 | |
51 for (const TCallback& pending_callback : pending_callbacks) { | |
52 callback_runner_(pending_callback, version_); | |
53 } | |
54 } | |
55 | |
56 private: | |
57 uint64_t version_ = 1u; | |
58 std::vector<TCallback> pending_callbacks_; | |
59 CallbackRunner callback_runner_; | |
60 }; | |
61 | |
62 } // namespace media | |
63 } // namespace mojo | |
64 | |
65 #endif // SERVICES_MEDIA_FRAMEWORK_MOJO_MOJO_ALLOCATOR_H_ | |
OLD | NEW |