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_CHANNEL_H_
6 #define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_
8 #include "base/callback.h"
9 #include "base/files/file.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "remoting/host/native_messaging/native_messaging_reader.h"
15 #include "remoting/host/native_messaging/native_messaging_writer.h"
18 class DictionaryValue
;
24 // Implements reading messages and sending responses across the native messaging
26 class NativeMessagingChannel
: public base::NonThreadSafe
{
28 // Used to send a message to the client app.
29 typedef base::Callback
<void(scoped_ptr
<base::DictionaryValue
> message
)>
32 // Constructs an object taking the ownership of |input| and |output|. Closes
33 // |input| and |output| to prevent the caller from using them.
34 NativeMessagingChannel(base::File input
, base::File output
);
35 ~NativeMessagingChannel();
37 // Starts reading and processing messages.
38 void Start(const SendMessageCallback
& received_message
,
39 const base::Closure
& quit_closure
);
41 // Sends a message to the client app.
42 void SendMessage(scoped_ptr
<base::DictionaryValue
> message
);
45 // Processes a message received from the client app.
46 void ProcessMessage(scoped_ptr
<base::Value
> message
);
48 // Initiates shutdown and runs |quit_closure| if there are no pending requests
52 base::Closure quit_closure_
;
54 NativeMessagingReader native_messaging_reader_
;
55 scoped_ptr
<NativeMessagingWriter
> native_messaging_writer_
;
57 // The callback to invoke when a message is received.
58 SendMessageCallback received_message_
;
60 base::WeakPtr
<NativeMessagingChannel
> weak_ptr_
;
61 base::WeakPtrFactory
<NativeMessagingChannel
> weak_factory_
;
63 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel
);
66 } // namespace remoting
68 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_