Chromium Code Reviews| 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_CHANNEL_H_ | 5 #ifndef REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ |
| 6 #define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ | 6 #define REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ |
| 7 | 7 |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/platform_file.h" | 12 #include "base/platform_file.h" |
| 13 #include "base/threading/non_thread_safe.h" | 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "remoting/host/native_messaging/native_messaging_reader.h" | 14 #include "remoting/host/native_messaging/native_messaging_reader.h" |
| 15 #include "remoting/host/native_messaging/native_messaging_writer.h" | 15 #include "remoting/host/native_messaging/native_messaging_writer.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 class DictionaryValue; | 18 class DictionaryValue; |
| 19 class Value; | 19 class Value; |
| 20 } // namespace base | 20 } // namespace base |
| 21 | 21 |
| 22 namespace remoting { | 22 namespace remoting { |
| 23 | 23 |
| 24 // Implements reading messages and sending responses across the native messaging | 24 // Implements reading messages and sending responses across the native messaging |
| 25 // host pipe. Delegates processing of received messages to Delegate. | 25 // host pipe. |
| 26 // | |
| 27 // TODO(alexeypa): Add ability to switch between different |delegate_| pointers | |
| 28 // on the fly. This is useful for implementing UAC-style elevation on Windows - | |
| 29 // an unprivileged delegate could be replaced with another delegate that | |
| 30 // forwards messages to the elevated instance of the native messaging host. | |
| 31 class NativeMessagingChannel : public base::NonThreadSafe { | 26 class NativeMessagingChannel : public base::NonThreadSafe { |
| 32 public: | 27 public: |
| 33 // Used to send a message to the client app. | 28 // Used to send a message to the client app. |
| 34 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> message)> | 29 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> message)> |
| 35 SendMessageCallback; | 30 SendMessageCallback; |
| 36 | 31 |
| 37 class Delegate { | |
| 38 public: | |
| 39 virtual ~Delegate() {} | |
| 40 | |
| 41 // Sets the callback the delegate can use to send a message to the client. | |
| 42 virtual void SetSendMessageCallback( | |
| 43 const SendMessageCallback& send_message) = 0; | |
| 44 | |
| 45 // Processes a message received from the client app. | |
| 46 virtual void ProcessMessage(scoped_ptr<base::DictionaryValue> message) = 0; | |
| 47 }; | |
| 48 | |
| 49 // Constructs an object taking the ownership of |input| and |output|. Closes | 32 // Constructs an object taking the ownership of |input| and |output|. Closes |
| 50 // |input| and |output| to prevent the caller from using them. | 33 // |input| and |output| to prevent the caller from using them. |
| 51 NativeMessagingChannel( | 34 NativeMessagingChannel( |
| 52 scoped_ptr<Delegate> delegate, | 35 SendMessageCallback received_message, |
|
Sergey Ulanov
2013/12/12 19:10:28
Would it be better to pass this to Start()?
weitao
2013/12/12 23:33:48
Done.
| |
| 53 base::PlatformFile input, | 36 base::PlatformFile input, |
| 54 base::PlatformFile output); | 37 base::PlatformFile output); |
| 55 ~NativeMessagingChannel(); | 38 ~NativeMessagingChannel(); |
| 56 | 39 |
| 57 // Starts reading and processing messages. | 40 // Starts reading and processing messages. |
| 58 void Start(const base::Closure& quit_closure); | 41 void Start(const base::Closure& quit_closure); |
| 59 | 42 |
| 43 // Sends a message to the client app. | |
| 44 void SendMessage(scoped_ptr<base::DictionaryValue> message); | |
| 45 | |
| 60 private: | 46 private: |
| 61 // Processes a message received from the client app. | 47 // Processes a message received from the client app. |
| 62 void ProcessMessage(scoped_ptr<base::Value> message); | 48 void ProcessMessage(scoped_ptr<base::Value> message); |
| 63 | 49 |
| 64 // Sends a message to the client app. | |
| 65 void SendMessage(scoped_ptr<base::DictionaryValue> message); | |
| 66 | |
| 67 // Initiates shutdown and runs |quit_closure| if there are no pending requests | 50 // Initiates shutdown and runs |quit_closure| if there are no pending requests |
| 68 // left. | 51 // left. |
| 69 void Shutdown(); | 52 void Shutdown(); |
| 70 | 53 |
| 71 base::Closure quit_closure_; | 54 base::Closure quit_closure_; |
| 72 | 55 |
| 73 NativeMessagingReader native_messaging_reader_; | 56 NativeMessagingReader native_messaging_reader_; |
| 74 scoped_ptr<NativeMessagingWriter> native_messaging_writer_; | 57 scoped_ptr<NativeMessagingWriter> native_messaging_writer_; |
| 75 | 58 |
| 76 // |delegate_| may post tasks to this object during destruction (but not | 59 // The callback to invoke when a message is received. |
| 77 // afterwards), so it needs to be destroyed before other members of this class | 60 SendMessageCallback received_message_; |
| 78 // (except for |weak_factory_|). | |
| 79 scoped_ptr<Delegate> delegate_; | |
| 80 | 61 |
| 81 base::WeakPtr<NativeMessagingChannel> weak_ptr_; | 62 base::WeakPtr<NativeMessagingChannel> weak_ptr_; |
| 82 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_; | 63 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_; |
| 83 | 64 |
| 84 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel); | 65 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel); |
| 85 }; | 66 }; |
| 86 | 67 |
| 87 } // namespace remoting | 68 } // namespace remoting |
| 88 | 69 |
| 89 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ | 70 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ |
| OLD | NEW |