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.
5 #ifndef REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_
6 #define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_
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"
16 class SequencedTaskRunner
;
26 // This class is used for reading messages from the Native Messaging client
28 class NativeMessagingReader
{
30 typedef base::Callback
<void(scoped_ptr
<base::Value
>)> MessageCallback
;
32 explicit NativeMessagingReader(base::PlatformFile handle
);
33 ~NativeMessagingReader();
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
);
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();
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
55 scoped_ptr
<Core
> core_
;
57 // Caller-supplied message and end-of-file callbacks.
58 MessageCallback message_callback_
;
59 base::Closure eof_callback_
;
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_
;
67 // Allows the reader to be deleted safely even when tasks may be pending on
69 base::WeakPtrFactory
<NativeMessagingReader
> weak_factory_
;
71 DISALLOW_COPY_AND_ASSIGN(NativeMessagingReader
);
74 } // namespace remoting
76 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_