summaryrefslogtreecommitdiff
path: root/remoting/host/setup/native_messaging_host.h
blob: 309d63cad67cfbdb5b1a0c894b018e4f8cd9899e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_SETUP_NATIVE_MESSAGING_HOST_H_
#define REMOTING_HOST_SETUP_NATIVE_MESSAGING_HOST_H_

#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/platform_file.h"
#include "remoting/host/setup/daemon_controller.h"
#include "remoting/host/setup/native_messaging_reader.h"
#include "remoting/host/setup/native_messaging_writer.h"

namespace base {
class DictionaryValue;
class ListValue;
class SingleThreadTaskRunner;
class Value;
}  // namespace base

namespace remoting {

namespace protocol {
class PairingRegistry;
}  // namespace protocol

// Implementation of the native messaging host process.
class NativeMessagingHost {
 public:
  NativeMessagingHost(
      scoped_ptr<DaemonController> daemon_controller,
      scoped_refptr<protocol::PairingRegistry> pairing_registry,
      base::PlatformFile input,
      base::PlatformFile output,
      scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner,
      const base::Closure& quit_closure);
  ~NativeMessagingHost();

  // Starts reading and processing messages.
  void Start();

  // Posts |quit_closure| to |caller_task_runner|. This gets called whenever an
  // error is encountered during reading and processing messages.
  void Shutdown();

 private:
  // Processes a message received from the client app.
  void ProcessMessage(scoped_ptr<base::Value> message);

  // These "Process.." methods handle specific request types. The |response|
  // dictionary is pre-filled by ProcessMessage() with the parts of the
  // response already known ("id" and "type" fields).
  bool ProcessHello(const base::DictionaryValue& message,
                    scoped_ptr<base::DictionaryValue> response);
  bool ProcessClearPairedClients(const base::DictionaryValue& message,
                                 scoped_ptr<base::DictionaryValue> response);
  bool ProcessDeletePairedClient(const base::DictionaryValue& message,
                                 scoped_ptr<base::DictionaryValue> response);
  bool ProcessGetHostName(const base::DictionaryValue& message,
                          scoped_ptr<base::DictionaryValue> response);
  bool ProcessGetPinHash(const base::DictionaryValue& message,
                         scoped_ptr<base::DictionaryValue> response);
  bool ProcessGenerateKeyPair(const base::DictionaryValue& message,
                              scoped_ptr<base::DictionaryValue> response);
  bool ProcessUpdateDaemonConfig(const base::DictionaryValue& message,
                                 scoped_ptr<base::DictionaryValue> response);
  bool ProcessGetDaemonConfig(const base::DictionaryValue& message,
                              scoped_ptr<base::DictionaryValue> response);
  bool ProcessGetPairedClients(const base::DictionaryValue& message,
                               scoped_ptr<base::DictionaryValue> response);
  bool ProcessGetUsageStatsConsent(const base::DictionaryValue& message,
                                   scoped_ptr<base::DictionaryValue> response);
  bool ProcessStartDaemon(const base::DictionaryValue& message,
                          scoped_ptr<base::DictionaryValue> response);
  bool ProcessStopDaemon(const base::DictionaryValue& message,
                         scoped_ptr<base::DictionaryValue> response);
  bool ProcessGetDaemonState(const base::DictionaryValue& message,
                             scoped_ptr<base::DictionaryValue> response);

  // Sends a response back to the client app. This can be called on either the
  // main message loop or the DaemonController's internal thread, so it
  // PostTask()s to the main thread if necessary.
  void SendResponse(scoped_ptr<base::DictionaryValue> response);

  // These Send... methods get called on the DaemonController's internal thread,
  // or on the calling thread if called by the PairingRegistry.
  // These methods fill in the |response| dictionary from the other parameters,
  // and pass it to SendResponse().
  void SendConfigResponse(scoped_ptr<base::DictionaryValue> response,
                          scoped_ptr<base::DictionaryValue> config);
  void SendPairedClientsResponse(scoped_ptr<base::DictionaryValue> response,
                                 scoped_ptr<base::ListValue> pairings);
  void SendUsageStatsConsentResponse(scoped_ptr<base::DictionaryValue> response,
                                     bool supported,
                                     bool allowed,
                                     bool set_by_policy);
  void SendAsyncResult(scoped_ptr<base::DictionaryValue> response,
                       DaemonController::AsyncResult result);
  void SendBooleanResult(scoped_ptr<base::DictionaryValue> response,
                         bool result);

  // Callbacks may be invoked by e.g. DaemonController during destruction,
  // which use |weak_ptr_|, so it's important that it be the last member to be
  // destroyed.
  base::WeakPtr<NativeMessagingHost> weak_ptr_;

  scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
  base::Closure quit_closure_;

  NativeMessagingReader native_messaging_reader_;
  NativeMessagingWriter native_messaging_writer_;

  // The DaemonController may post tasks to this object during destruction (but
  // not afterwards), so it needs to be destroyed before other members of this
  // class (except for |weak_factory_|).
  scoped_ptr<remoting::DaemonController> daemon_controller_;

  // Used to load and update the paired clients for this host.
  scoped_refptr<protocol::PairingRegistry> pairing_registry_;

  base::WeakPtrFactory<NativeMessagingHost> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(NativeMessagingHost);
};

// Creates a NativeMessagingHost instance, attaches it to stdin/stdout and runs
// the message loop until NativeMessagingHost signals shutdown.
int NativeMessagingHostMain();

}  // namespace remoting

#endif  // REMOTING_HOST_SETUP_NATIVE_MESSAGING_HOST_H_