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_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ 6 #define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ 7 8 #include "base/callback.h" 9 #include "base/memory/ref_counted.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/non_thread_safe.h" 14 #include "remoting/host/native_messaging/native_messaging_reader.h" 15 #include "remoting/host/native_messaging/native_messaging_writer.h" 16 17 namespace base { 18 class DictionaryValue; 19 class Value; 20 } // namespace base 21 22 namespace remoting { 23 24 // Implements reading messages and sending responses across the native messaging 25 // host pipe. 26 class NativeMessagingChannel : public base::NonThreadSafe { 27 public: 28 // Used to send a message to the client app. 29 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> message)> 30 SendMessageCallback; 31 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::PlatformFile input, base::PlatformFile output); 35 ~NativeMessagingChannel(); 36 37 // Starts reading and processing messages. 38 void Start(const SendMessageCallback& received_message, 39 const base::Closure& quit_closure); 40 41 // Sends a message to the client app. 42 void SendMessage(scoped_ptr<base::DictionaryValue> message); 43 44 private: 45 // Processes a message received from the client app. 46 void ProcessMessage(scoped_ptr<base::Value> message); 47 48 // Initiates shutdown and runs |quit_closure| if there are no pending requests 49 // left. 50 void Shutdown(); 51 52 base::Closure quit_closure_; 53 54 NativeMessagingReader native_messaging_reader_; 55 scoped_ptr<NativeMessagingWriter> native_messaging_writer_; 56 57 // The callback to invoke when a message is received. 58 SendMessageCallback received_message_; 59 60 base::WeakPtr<NativeMessagingChannel> weak_ptr_; 61 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_; 62 63 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel); 64 }; 65 66 } // namespace remoting 67 68 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ 69