Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / host / native_messaging / native_messaging_reader.h
blobed93e8d88e060f9e572e89d9cdb05924eb739b83
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/files/file.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/thread.h"
15 namespace base {
16 class SequencedTaskRunner;
17 class Value;
18 } // namespace base
20 namespace remoting {
22 // This class is used for reading messages from the Native Messaging client
23 // webapp.
24 class NativeMessagingReader {
25 public:
26 typedef base::Callback<void(scoped_ptr<base::Value>)> MessageCallback;
28 explicit NativeMessagingReader(base::File file);
29 ~NativeMessagingReader();
31 // Begin reading messages from the Native Messaging client webapp, calling
32 // |message_callback| for each received message, or |eof_callback| if
33 // 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 // for these callbacks to be invoked right up until this object is destroyed.
36 void Start(MessageCallback message_callback, base::Closure eof_callback);
38 private:
39 class Core;
40 friend class Core;
42 // 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 // has been deleted before they are processed.
45 void InvokeMessageCallback(scoped_ptr<base::Value> message);
46 void InvokeEofCallback();
48 // 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 // class.
51 scoped_ptr<Core> core_;
53 // Caller-supplied message and end-of-file callbacks.
54 MessageCallback message_callback_;
55 base::Closure eof_callback_;
57 // 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 // Windows, it requires the file handle to have been opened for overlapped IO.
60 base::Thread reader_thread_;
61 scoped_refptr<base::SequencedTaskRunner> read_task_runner_;
63 // Allows the reader to be deleted safely even when tasks may be pending on
64 // it.
65 base::WeakPtrFactory<NativeMessagingReader> weak_factory_;
67 DISALLOW_COPY_AND_ASSIGN(NativeMessagingReader);
70 } // namespace remoting
72 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_READER_H_