OLD | NEW |
| (Empty) |
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_SETUP_NATIVE_MESSAGING_CHANNEL_H_ | |
6 #define REMOTING_HOST_SETUP_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/setup/native_messaging_reader.h" | |
15 #include "remoting/host/setup/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. Delegates processing of received messages to Delegate. | |
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 { | |
32 public: | |
33 // Used to sends a response back to the client app. | |
34 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> response)> | |
35 SendResponseCallback; | |
36 | |
37 class Delegate { | |
38 public: | |
39 virtual ~Delegate() {} | |
40 | |
41 // Processes a message received from the client app. Invokes |done| to send | |
42 // a response back to the client app. | |
43 virtual void ProcessMessage(scoped_ptr<base::DictionaryValue> message, | |
44 const SendResponseCallback& done) = 0; | |
45 }; | |
46 | |
47 // Constructs an object taking the ownership of |input| and |output|. Closes | |
48 // |input| and |output| to prevent the caller frmo using them. | |
49 NativeMessagingChannel( | |
50 scoped_ptr<Delegate> delegate, | |
51 base::PlatformFile input, | |
52 base::PlatformFile output); | |
53 ~NativeMessagingChannel(); | |
54 | |
55 // Starts reading and processing messages. | |
56 void Start(const base::Closure& quit_closure); | |
57 | |
58 private: | |
59 // Processes a message received from the client app. | |
60 void ProcessMessage(scoped_ptr<base::Value> message); | |
61 | |
62 // Sends a response back to the client app. | |
63 void SendResponse(scoped_ptr<base::DictionaryValue> response); | |
64 | |
65 // Initiates shutdown and runs |quit_closure| if there are no pending requests | |
66 // left. | |
67 void Shutdown(); | |
68 | |
69 base::Closure quit_closure_; | |
70 | |
71 NativeMessagingReader native_messaging_reader_; | |
72 scoped_ptr<NativeMessagingWriter> native_messaging_writer_; | |
73 | |
74 // |delegate_| may post tasks to this object during destruction (but not | |
75 // afterwards), so it needs to be destroyed before other members of this class | |
76 // (except for |weak_factory_|). | |
77 scoped_ptr<Delegate> delegate_; | |
78 | |
79 // Keeps track of pending requests. Used to delay shutdown until all responses | |
80 // have been sent. | |
81 int pending_requests_; | |
82 | |
83 // True if Shutdown() has been called. | |
84 bool shutdown_; | |
85 | |
86 base::WeakPtr<NativeMessagingChannel> weak_ptr_; | |
87 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel); | |
90 }; | |
91 | |
92 } // namespace remoting | |
93 | |
94 #endif // REMOTING_HOST_SETUP_NATIVE_MESSAGING_CHANNEL_H_ | |
OLD | NEW |