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_HOST_H_
6#define REMOTING_HOST_SETUP_NATIVE_MESSAGING_HOST_H_
7
8#include "base/callback_forward.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 "remoting/host/setup/daemon_controller.h"
14#include "remoting/host/setup/native_messaging_reader.h"
15#include "remoting/host/setup/native_messaging_writer.h"
16
17namespace base {
18class DictionaryValue;
19class ListValue;
20class SingleThreadTaskRunner;
21class Value;
22}  // namespace base
23
24namespace remoting {
25
26namespace protocol {
27class PairingRegistry;
28}  // namespace protocol
29
30// Implementation of the native messaging host process.
31class NativeMessagingHost {
32 public:
33  NativeMessagingHost(
34      scoped_ptr<DaemonController> daemon_controller,
35      scoped_refptr<protocol::PairingRegistry> pairing_registry,
36      base::PlatformFile input,
37      base::PlatformFile output,
38      scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
39      const base::Closure& quit_closure);
40  ~NativeMessagingHost();
41
42  // Starts reading and processing messages.
43  void Start();
44
45  // Posts |quit_closure| to |caller_task_runner|. This gets called whenever an
46  // error is encountered during reading and processing messages.
47  void Shutdown();
48
49 private:
50  // Processes a message received from the client app.
51  void ProcessMessage(scoped_ptr<base::Value> message);
52
53  // These "Process.." methods handle specific request types. The |response|
54  // dictionary is pre-filled by ProcessMessage() with the parts of the
55  // response already known ("id" and "type" fields).
56  bool ProcessHello(const base::DictionaryValue& message,
57                    scoped_ptr<base::DictionaryValue> response);
58  bool ProcessClearPairedClients(const base::DictionaryValue& message,
59                                 scoped_ptr<base::DictionaryValue> response);
60  bool ProcessDeletePairedClient(const base::DictionaryValue& message,
61                                 scoped_ptr<base::DictionaryValue> response);
62  bool ProcessGetHostName(const base::DictionaryValue& message,
63                          scoped_ptr<base::DictionaryValue> response);
64  bool ProcessGetPinHash(const base::DictionaryValue& message,
65                         scoped_ptr<base::DictionaryValue> response);
66  bool ProcessGenerateKeyPair(const base::DictionaryValue& message,
67                              scoped_ptr<base::DictionaryValue> response);
68  bool ProcessUpdateDaemonConfig(const base::DictionaryValue& message,
69                                 scoped_ptr<base::DictionaryValue> response);
70  bool ProcessGetDaemonConfig(const base::DictionaryValue& message,
71                              scoped_ptr<base::DictionaryValue> response);
72  bool ProcessGetPairedClients(const base::DictionaryValue& message,
73                               scoped_ptr<base::DictionaryValue> response);
74  bool ProcessGetUsageStatsConsent(const base::DictionaryValue& message,
75                                   scoped_ptr<base::DictionaryValue> response);
76  bool ProcessStartDaemon(const base::DictionaryValue& message,
77                          scoped_ptr<base::DictionaryValue> response);
78  bool ProcessStopDaemon(const base::DictionaryValue& message,
79                         scoped_ptr<base::DictionaryValue> response);
80  bool ProcessGetDaemonState(const base::DictionaryValue& message,
81                             scoped_ptr<base::DictionaryValue> response);
82
83  // Sends a response back to the client app. This can be called on either the
84  // main message loop or the DaemonController's internal thread, so it
85  // PostTask()s to the main thread if necessary.
86  void SendResponse(scoped_ptr<base::DictionaryValue> response);
87
88  // These Send... methods get called on the DaemonController's internal thread,
89  // or on the calling thread if called by the PairingRegistry.
90  // These methods fill in the |response| dictionary from the other parameters,
91  // and pass it to SendResponse().
92  void SendConfigResponse(scoped_ptr<base::DictionaryValue> response,
93                          scoped_ptr<base::DictionaryValue> config);
94  void SendPairedClientsResponse(scoped_ptr<base::DictionaryValue> response,
95                                 scoped_ptr<base::ListValue> pairings);
96  void SendUsageStatsConsentResponse(scoped_ptr<base::DictionaryValue> response,
97                                     bool supported,
98                                     bool allowed,
99                                     bool set_by_policy);
100  void SendAsyncResult(scoped_ptr<base::DictionaryValue> response,
101                       DaemonController::AsyncResult result);
102  void SendBooleanResult(scoped_ptr<base::DictionaryValue> response,
103                         bool result);
104
105  // Callbacks may be invoked by e.g. DaemonController during destruction,
106  // which use |weak_ptr_|, so it's important that it be the last member to be
107  // destroyed.
108  base::WeakPtr<NativeMessagingHost> weak_ptr_;
109
110  scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
111  base::Closure quit_closure_;
112
113  NativeMessagingReader native_messaging_reader_;
114  NativeMessagingWriter native_messaging_writer_;
115
116  // The DaemonController may post tasks to this object during destruction (but
117  // not afterwards), so it needs to be destroyed before other members of this
118  // class (except for |weak_factory_|).
119  scoped_ptr<remoting::DaemonController> daemon_controller_;
120
121  // Used to load and update the paired clients for this host.
122  scoped_refptr<protocol::PairingRegistry> pairing_registry_;
123
124  base::WeakPtrFactory<NativeMessagingHost> weak_factory_;
125
126  DISALLOW_COPY_AND_ASSIGN(NativeMessagingHost);
127};
128
129// Creates a NativeMessagingHost instance, attaches it to stdin/stdout and runs
130// the message loop until NativeMessagingHost signals shutdown.
131int NativeMessagingHostMain();
132
133}  // namespace remoting
134
135#endif  // REMOTING_HOST_SETUP_NATIVE_MESSAGING_HOST_H_
136