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