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/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"
18 class DictionaryValue;
24 // Implements reading messages and sending responses across the native messaging
25 // host pipe. Delegates processing of received messages to Delegate.
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 {
33 // Used to sends a response back to the client app.
34 typedef base::Callback<void(scoped_ptr<base::DictionaryValue> response)>
39 virtual ~Delegate() {}
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;
47 // Constructs an object taking the ownership of |input| and |output|. Closes
48 // |input| and |output| to prevent the caller from using them.
49 NativeMessagingChannel(
50 scoped_ptr<Delegate> delegate,
51 base::PlatformFile input,
52 base::PlatformFile output);
53 ~NativeMessagingChannel();
55 // Starts reading and processing messages.
56 void Start(const base::Closure& quit_closure);
59 // Processes a message received from the client app.
60 void ProcessMessage(scoped_ptr<base::Value> message);
62 // Sends a response back to the client app.
63 void SendResponse(scoped_ptr<base::DictionaryValue> response);
65 // Initiates shutdown and runs |quit_closure| if there are no pending requests
69 base::Closure quit_closure_;
71 NativeMessagingReader native_messaging_reader_;
72 scoped_ptr<NativeMessagingWriter> native_messaging_writer_;
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_;
79 // Keeps track of pending requests. Used to delay shutdown until all responses
81 int pending_requests_;
83 // True if Shutdown() has been called.
86 base::WeakPtr<NativeMessagingChannel> weak_ptr_;
87 base::WeakPtrFactory<NativeMessagingChannel> weak_factory_;
89 DISALLOW_COPY_AND_ASSIGN(NativeMessagingChannel);
92 } // namespace remoting
94 #endif // REMOTING_HOST_NATIVE_MESSAGING_NATIVE_MESSAGING_CHANNEL_H_