OLD | NEW |
| (Empty) |
1 // Copyright 2013 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 REMOTING_HOST_SETUP_NATIVE_MESSAGING_READER_H_ | |
6 #define REMOTING_HOST_SETUP_NATIVE_MESSAGING_READER_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/callback.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/platform_file.h" | |
13 #include "base/threading/thread.h" | |
14 | |
15 namespace base { | |
16 class SequencedTaskRunner; | |
17 class Value; | |
18 } // namespace base | |
19 | |
20 namespace net { | |
21 class FileStream; | |
22 } // namespace net | |
23 | |
24 namespace remoting { | |
25 | |
26 // This class is used for reading messages from the Native Messaging client | |
27 // webapp. | |
28 class NativeMessagingReader { | |
29 public: | |
30 typedef base::Callback<void(scoped_ptr<base::Value>)> MessageCallback; | |
31 | |
32 explicit NativeMessagingReader(base::PlatformFile handle); | |
33 ~NativeMessagingReader(); | |
34 | |
35 // Begin reading messages from the Native Messaging client webapp, calling | |
36 // |message_callback| for each received message, or |eof_callback| if | |
37 // EOF or error is encountered. This method is asynchronous - the callbacks | |
38 // will be run on the same thread via PostTask. The caller should be prepared | |
39 // for these callbacks to be invoked right up until this object is destroyed. | |
40 void Start(MessageCallback message_callback, base::Closure eof_callback); | |
41 | |
42 private: | |
43 class Core; | |
44 friend class Core; | |
45 | |
46 // Wrappers posted to by the read thread to trigger the message and EOF | |
47 // callbacks on the caller thread, and have them safely dropped if the reader | |
48 // has been deleted before they are processed. | |
49 void InvokeMessageCallback(scoped_ptr<base::Value> message); | |
50 void InvokeEofCallback(); | |
51 | |
52 // Holds the information that the read thread needs to access, such as the | |
53 // FileStream, and the TaskRunner used for posting notifications back to this | |
54 // class. | |
55 scoped_ptr<Core> core_; | |
56 | |
57 // Caller-supplied message and end-of-file callbacks. | |
58 MessageCallback message_callback_; | |
59 base::Closure eof_callback_; | |
60 | |
61 // Separate thread used to read from the stream without blocking the main | |
62 // thread. net::FileStream's async API cannot be used here because, on | |
63 // Windows, it requires the file handle to have been opened for overlapped IO. | |
64 base::Thread reader_thread_; | |
65 scoped_refptr<base::SequencedTaskRunner> read_task_runner_; | |
66 | |
67 // Allows the reader to be deleted safely even when tasks may be pending on | |
68 // it. | |
69 base::WeakPtrFactory<NativeMessagingReader> weak_factory_; | |
70 | |
71 DISALLOW_COPY_AND_ASSIGN(NativeMessagingReader); | |
72 }; | |
73 | |
74 } // namespace remoting | |
75 | |
76 #endif // REMOTING_HOST_SETUP_NATIVE_MESSAGING_READER_H_ | |
OLD | NEW |