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(base::PlatformFile input, base::PlatformFile output); |
52 scoped_ptr<Delegate> delegate, | |
53 base::PlatformFile input, | |
54 base::PlatformFile output); | |
55 ~NativeMessagingChannel(); | 35 ~NativeMessagingChannel(); |
56 | 36 |
57 // Starts reading and processing messages. | 37 // Starts reading and processing messages. |
58 void Start(const base::Closure& quit_closure); | 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); |
59 | 43 |
60 private: | 44 private: |
61 // Processes a message received from the client app. | 45 // Processes a message received from the client app. |
62 void ProcessMessage(scoped_ptr<base::Value> message); | 46 void ProcessMessage(scoped_ptr<base::Value> message); |
63 | 47 |
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 | 48 // Initiates shutdown and runs |quit_closure| if there are no pending requests |
68 // left. | 49 // left. |
69 void Shutdown(); | 50 void Shutdown(); |
70 | 51 |
71 base::Closure quit_closure_; | 52 base::Closure quit_closure_; |
72 | 53 |
73 NativeMessagingReader native_messaging_reader_; | 54 NativeMessagingReader native_messaging_reader_; |
74 scoped_ptr<NativeMessagingWriter> native_messaging_writer_; | 55 scoped_ptr<NativeMessagingWriter> native_messaging_writer_; |
75 | 56 |
76 // |delegate_| may post tasks to this object during destruction (but not | 57 // The callback to invoke when a message is received. |
77 // afterwards), so it needs to be destroyed before other members of this class | 58 SendMessageCallback received_message_; |
78 // (except for |weak_factory_|). | |
79 scoped_ptr<Delegate> delegate_; | |
80 | 59 |
81 base::WeakPtr<NativeMessagingChannel> weak_ptr_; | 60 base::WeakPtr<NativeMessagingChannel> weak_ptr_; |
82 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_; | 61 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_; |
83 | 62 |
84 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel); | 63 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel); |
85 }; | 64 }; |
86 | 65 |
87 } // namespace remoting | 66 } // namespace remoting |
88 | 67 |
89 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ | 68 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_ |
OLD | NEW |