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_ME2ME_NATIVE_MESSAGING_HOST_H_
6#define REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/weak_ptr.h"
11#include "base/threading/thread_checker.h"
12#include "remoting/host/native_messaging/native_messaging_channel.h"
13#include "remoting/host/setup/daemon_controller.h"
14#include "remoting/host/setup/oauth_client.h"
15
16namespace base {
17class DictionaryValue;
18class ListValue;
19}  // namespace base
20
21namespace gaia {
22class GaiaOAuthClient;
23}  // namespace gaia
24
25namespace remoting {
26
27namespace protocol {
28class PairingRegistry;
29}  // namespace protocol
30
31// Implementation of the me2me native messaging host.
32class Me2MeNativeMessagingHost {
33 public:
34  typedef NativeMessagingChannel::SendMessageCallback SendMessageCallback;
35
36  Me2MeNativeMessagingHost(
37      scoped_ptr<NativeMessagingChannel> channel,
38      scoped_refptr<DaemonController> daemon_controller,
39      scoped_refptr<protocol::PairingRegistry> pairing_registry,
40      scoped_ptr<OAuthClient> oauth_client);
41  virtual ~Me2MeNativeMessagingHost();
42
43  void Start(const base::Closure& quit_closure);
44
45 private:
46  void ProcessMessage(scoped_ptr<base::DictionaryValue> message);
47
48  // These "Process.." methods handle specific request types. The |response|
49  // dictionary is pre-filled by ProcessMessage() with the parts of the
50  // response already known ("id" and "type" fields).
51  bool ProcessHello(
52      const base::DictionaryValue& message,
53      scoped_ptr<base::DictionaryValue> response);
54  bool ProcessClearPairedClients(
55      const base::DictionaryValue& message,
56      scoped_ptr<base::DictionaryValue> response);
57  bool ProcessDeletePairedClient(
58      const base::DictionaryValue& message,
59      scoped_ptr<base::DictionaryValue> response);
60  bool ProcessGetHostName(
61      const base::DictionaryValue& message,
62      scoped_ptr<base::DictionaryValue> response);
63  bool ProcessGetPinHash(
64      const base::DictionaryValue& message,
65      scoped_ptr<base::DictionaryValue> response);
66  bool ProcessGenerateKeyPair(
67      const base::DictionaryValue& message,
68      scoped_ptr<base::DictionaryValue> response);
69  bool ProcessUpdateDaemonConfig(
70      const base::DictionaryValue& message,
71      scoped_ptr<base::DictionaryValue> response);
72  bool ProcessGetDaemonConfig(
73      const base::DictionaryValue& message,
74      scoped_ptr<base::DictionaryValue> response);
75  bool ProcessGetPairedClients(
76      const base::DictionaryValue& message,
77      scoped_ptr<base::DictionaryValue> response);
78  bool ProcessGetUsageStatsConsent(
79      const base::DictionaryValue& message,
80      scoped_ptr<base::DictionaryValue> response);
81  bool ProcessStartDaemon(
82      const base::DictionaryValue& message,
83      scoped_ptr<base::DictionaryValue> response);
84  bool ProcessStopDaemon(
85      const base::DictionaryValue& message,
86      scoped_ptr<base::DictionaryValue> response);
87  bool ProcessGetDaemonState(
88      const base::DictionaryValue& message,
89      scoped_ptr<base::DictionaryValue> response);
90  bool ProcessGetHostClientId(
91      const base::DictionaryValue& message,
92      scoped_ptr<base::DictionaryValue> response);
93  bool ProcessGetCredentialsFromAuthCode(
94      const base::DictionaryValue& message,
95      scoped_ptr<base::DictionaryValue> response);
96
97  // These Send... methods get called on the DaemonController's internal thread,
98  // or on the calling thread if called by the PairingRegistry.
99  // These methods fill in the |response| dictionary from the other parameters,
100  // and pass it to SendResponse().
101  void SendConfigResponse(scoped_ptr<base::DictionaryValue> response,
102                          scoped_ptr<base::DictionaryValue> config);
103  void SendPairedClientsResponse(scoped_ptr<base::DictionaryValue> response,
104                                 scoped_ptr<base::ListValue> pairings);
105  void SendUsageStatsConsentResponse(
106      scoped_ptr<base::DictionaryValue> response,
107      const DaemonController::UsageStatsConsent& consent);
108  void SendAsyncResult(scoped_ptr<base::DictionaryValue> response,
109                       DaemonController::AsyncResult result);
110  void SendBooleanResult(scoped_ptr<base::DictionaryValue> response,
111                         bool result);
112  void SendCredentialsResponse(scoped_ptr<base::DictionaryValue> response,
113                               const std::string& user_email,
114                               const std::string& refresh_token);
115
116  scoped_ptr<NativeMessagingChannel> channel_;
117  scoped_refptr<DaemonController> daemon_controller_;
118
119  // Used to load and update the paired clients for this host.
120  scoped_refptr<protocol::PairingRegistry> pairing_registry_;
121
122  // Used to exchange the service account authorization code for credentials.
123  scoped_ptr<OAuthClient> oauth_client_;
124
125  base::ThreadChecker thread_checker_;
126
127  base::WeakPtr<Me2MeNativeMessagingHost> weak_ptr_;
128  base::WeakPtrFactory<Me2MeNativeMessagingHost> weak_factory_;
129
130  DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHost);
131};
132
133// Creates a Me2MeNativeMessagingHost instance, attaches it to stdin/stdout and
134// runs the message loop until Me2MeNativeMessagingHost signals shutdown.
135int Me2MeNativeMessagingHostMain();
136
137}  // namespace remoting
138
139#endif  // REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_
140