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_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_
6 #define REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_
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 "base/timer/timer.h"
13 #include "extensions/browser/api/messaging/native_messaging_channel.h"
14 #include "remoting/host/native_messaging/log_message_handler.h"
15 #include "remoting/host/setup/daemon_controller.h"
16 #include "remoting/host/setup/oauth_client.h"
19 class DictionaryValue
;
24 class GaiaOAuthClient
;
29 const char kElevatingSwitchName
[] = "elevate";
30 const char kInputSwitchName
[] = "input";
31 const char kOutputSwitchName
[] = "output";
34 class PairingRegistry
;
35 } // namespace protocol
37 // Implementation of the me2me native messaging host.
38 class Me2MeNativeMessagingHost
39 : public extensions::NativeMessagingChannel::EventHandler
{
41 Me2MeNativeMessagingHost(
43 intptr_t parent_window_handle
,
44 scoped_ptr
<extensions::NativeMessagingChannel
> channel
,
45 scoped_refptr
<DaemonController
> daemon_controller
,
46 scoped_refptr
<protocol::PairingRegistry
> pairing_registry
,
47 scoped_ptr
<OAuthClient
> oauth_client
);
48 ~Me2MeNativeMessagingHost() override
;
50 void Start(const base::Closure
& quit_closure
);
52 // extensions::NativeMessagingChannel::EventHandler implementation
53 void OnMessage(scoped_ptr
<base::Value
> message
) override
;
54 void OnDisconnect() override
;
57 // These "Process.." methods handle specific request types. The |response|
58 // dictionary is pre-filled by ProcessMessage() with the parts of the
59 // response already known ("id" and "type" fields).
61 scoped_ptr
<base::DictionaryValue
> message
,
62 scoped_ptr
<base::DictionaryValue
> response
);
63 void ProcessClearPairedClients(
64 scoped_ptr
<base::DictionaryValue
> message
,
65 scoped_ptr
<base::DictionaryValue
> response
);
66 void ProcessDeletePairedClient(
67 scoped_ptr
<base::DictionaryValue
> message
,
68 scoped_ptr
<base::DictionaryValue
> response
);
69 void ProcessGetHostName(
70 scoped_ptr
<base::DictionaryValue
> message
,
71 scoped_ptr
<base::DictionaryValue
> response
);
72 void ProcessGetPinHash(
73 scoped_ptr
<base::DictionaryValue
> message
,
74 scoped_ptr
<base::DictionaryValue
> response
);
75 void ProcessGenerateKeyPair(
76 scoped_ptr
<base::DictionaryValue
> message
,
77 scoped_ptr
<base::DictionaryValue
> response
);
78 void ProcessUpdateDaemonConfig(
79 scoped_ptr
<base::DictionaryValue
> message
,
80 scoped_ptr
<base::DictionaryValue
> response
);
81 void ProcessGetDaemonConfig(
82 scoped_ptr
<base::DictionaryValue
> message
,
83 scoped_ptr
<base::DictionaryValue
> response
);
84 void ProcessGetPairedClients(
85 scoped_ptr
<base::DictionaryValue
> message
,
86 scoped_ptr
<base::DictionaryValue
> response
);
87 void ProcessGetUsageStatsConsent(
88 scoped_ptr
<base::DictionaryValue
> message
,
89 scoped_ptr
<base::DictionaryValue
> response
);
90 void ProcessStartDaemon(
91 scoped_ptr
<base::DictionaryValue
> message
,
92 scoped_ptr
<base::DictionaryValue
> response
);
93 void ProcessStopDaemon(
94 scoped_ptr
<base::DictionaryValue
> message
,
95 scoped_ptr
<base::DictionaryValue
> response
);
96 void ProcessGetDaemonState(
97 scoped_ptr
<base::DictionaryValue
> message
,
98 scoped_ptr
<base::DictionaryValue
> response
);
99 void ProcessGetHostClientId(
100 scoped_ptr
<base::DictionaryValue
> message
,
101 scoped_ptr
<base::DictionaryValue
> response
);
102 void ProcessGetCredentialsFromAuthCode(
103 scoped_ptr
<base::DictionaryValue
> message
,
104 scoped_ptr
<base::DictionaryValue
> response
,
105 bool need_user_email
);
107 // These Send... methods get called on the DaemonController's internal thread,
108 // or on the calling thread if called by the PairingRegistry.
109 // These methods fill in the |response| dictionary from the other parameters,
110 // and pass it to SendResponse().
111 void SendConfigResponse(scoped_ptr
<base::DictionaryValue
> response
,
112 scoped_ptr
<base::DictionaryValue
> config
);
113 void SendPairedClientsResponse(scoped_ptr
<base::DictionaryValue
> response
,
114 scoped_ptr
<base::ListValue
> pairings
);
115 void SendUsageStatsConsentResponse(
116 scoped_ptr
<base::DictionaryValue
> response
,
117 const DaemonController::UsageStatsConsent
& consent
);
118 void SendAsyncResult(scoped_ptr
<base::DictionaryValue
> response
,
119 DaemonController::AsyncResult result
);
120 void SendBooleanResult(scoped_ptr
<base::DictionaryValue
> response
,
122 void SendCredentialsResponse(scoped_ptr
<base::DictionaryValue
> response
,
123 const std::string
& user_email
,
124 const std::string
& refresh_token
);
130 // Returns true if the request was successfully delegated to the elevated
131 // host and false otherwise.
132 bool DelegateToElevatedHost(scoped_ptr
<base::DictionaryValue
> message
);
135 class ElevatedChannelEventHandler
136 : public extensions::NativeMessagingChannel::EventHandler
{
138 ElevatedChannelEventHandler(Me2MeNativeMessagingHost
* host
);
140 void OnMessage(scoped_ptr
<base::Value
> message
) override
;
141 void OnDisconnect() override
;
143 Me2MeNativeMessagingHost
* parent_
;
146 // Create and connect to an elevated host process if necessary.
147 // |elevated_channel_| will contain the native messaging channel to the
148 // elevated host if the function succeeds.
149 void EnsureElevatedHostCreated();
151 // Disconnect and shut down the elevated host.
152 void DisconnectElevatedHost();
154 // Native messaging channel used to communicate with the elevated host.
155 scoped_ptr
<extensions::NativeMessagingChannel
> elevated_channel_
;
157 // Native messaging event handler used to process responses from the elevated
159 scoped_ptr
<ElevatedChannelEventHandler
> elevated_channel_event_handler_
;
161 // Timer to control the lifetime of the elevated host.
162 base::OneShotTimer
<Me2MeNativeMessagingHost
> elevated_host_timer_
;
163 #endif // defined(OS_WIN)
165 bool needs_elevation_
;
168 // Handle of the parent window.
169 intptr_t parent_window_handle_
;
170 #endif // defined(OS_WIN)
172 base::Closure quit_closure_
;
174 // Native messaging channel used to communicate with the native message
176 scoped_ptr
<extensions::NativeMessagingChannel
> channel_
;
178 LogMessageHandler log_message_handler_
;
180 scoped_refptr
<DaemonController
> daemon_controller_
;
182 // Used to load and update the paired clients for this host.
183 scoped_refptr
<protocol::PairingRegistry
> pairing_registry_
;
185 // Used to exchange the service account authorization code for credentials.
186 scoped_ptr
<OAuthClient
> oauth_client_
;
188 base::ThreadChecker thread_checker_
;
190 base::WeakPtr
<Me2MeNativeMessagingHost
> weak_ptr_
;
191 base::WeakPtrFactory
<Me2MeNativeMessagingHost
> weak_factory_
;
193 DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHost
);
196 } // namespace remoting
198 #endif // REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_