OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef BLIMP_NET_BLIMP_MESSAGE_THREAD_PIPE_H_ | |
6 #define BLIMP_NET_BLIMP_MESSAGE_THREAD_PIPE_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "blimp/net/blimp_net_export.h" | |
13 | |
14 namespace base { | |
15 class SequencedTaskRunner; | |
16 } | |
17 | |
18 namespace blimp { | |
19 | |
20 class BlimpMessageProcessor; | |
21 | |
22 // Uni-directional MessageProcessor "pipe" that accepts messages on | |
23 // one thread and dispatches them to a MessageProcessor on a different | |
24 // thread. | |
25 // | |
26 // Typical usage involves: | |
27 // 1. Create the pipe on the "main" thread, specifying the target thread's | |
28 // task runner. | |
29 // 2. Take one or more MessageProcessor proxies from it. | |
30 // 3. Post a task to the target thread to set the target MessageProcessor. | |
31 // 4. Start using the MessageProcessor proxy(/ies) on the main thread. | |
32 // 5. When the target MessageProcessor is about to be destroyed on the | |
33 // target thread, destroy the pipe instance immediately beforehand. | |
34 // Any messages that are subsequently passed to a proxy, or are already | |
35 // in-flight to the pipe, will be silently dropped. | |
36 class BLIMP_NET_EXPORT BlimpMessageThreadPipe { | |
37 public: | |
38 explicit BlimpMessageThreadPipe( | |
39 const scoped_refptr<base::SequencedTaskRunner>& task_runner); | |
40 ~BlimpMessageThreadPipe(); | |
41 | |
42 // Creates a proxy MessageProcessor that routes messages to | |
43 // the outgoing MessageProcessor on |task_runner|. | |
44 // Proxies are safe to create before the outgoing MessageProcessor | |
45 // has been set, but cannot be used until it has after been set - | |
46 // see the class-level comment on usage. | |
47 // Proxies must be deleted on the thread on which they are used. | |
48 std::unique_ptr<BlimpMessageProcessor> CreateProxy(); | |
49 | |
50 // Sets/gets the target MessageProcessor on the target thread. | |
51 void set_target_processor(BlimpMessageProcessor* processor); | |
52 BlimpMessageProcessor* target_processor() const; | |
53 | |
54 private: | |
55 // Target MessageProcessor & TaskRunner to process messages with. | |
56 BlimpMessageProcessor* target_processor_ = nullptr; | |
57 scoped_refptr<base::SequencedTaskRunner> target_task_runner_; | |
58 | |
59 // Allows |this| to be safely detached from existing proxies on deletion. | |
60 base::WeakPtrFactory<BlimpMessageThreadPipe> weak_factory_; | |
61 | |
62 DISALLOW_COPY_AND_ASSIGN(BlimpMessageThreadPipe); | |
63 }; | |
64 | |
65 } // namespace blimp | |
66 | |
67 #endif // BLIMP_NET_BLIMP_MESSAGE_THREAD_PIPE_H_ | |
OLD | NEW |