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/setup/daemon_controller.h"
15 #include "remoting/host/setup/oauth_client.h"
18 class DictionaryValue
;
23 class GaiaOAuthClient
;
28 const char kElevatingSwitchName
[] = "elevate";
29 const char kInputSwitchName
[] = "input";
30 const char kOutputSwitchName
[] = "output";
33 class PairingRegistry
;
34 } // namespace protocol
36 // Implementation of the me2me native messaging host.
37 class Me2MeNativeMessagingHost
38 : public extensions::NativeMessagingChannel::EventHandler
{
40 Me2MeNativeMessagingHost(
42 intptr_t parent_window_handle
,
43 scoped_ptr
<extensions::NativeMessagingChannel
> channel
,
44 scoped_refptr
<DaemonController
> daemon_controller
,
45 scoped_refptr
<protocol::PairingRegistry
> pairing_registry
,
46 scoped_ptr
<OAuthClient
> oauth_client
);
47 ~Me2MeNativeMessagingHost() override
;
49 void Start(const base::Closure
& quit_closure
);
51 // extensions::NativeMessagingChannel::EventHandler implementation
52 void OnMessage(scoped_ptr
<base::Value
> message
) override
;
53 void OnDisconnect() override
;
56 // These "Process.." methods handle specific request types. The |response|
57 // dictionary is pre-filled by ProcessMessage() with the parts of the
58 // response already known ("id" and "type" fields).
60 scoped_ptr
<base::DictionaryValue
> message
,
61 scoped_ptr
<base::DictionaryValue
> response
);
62 void ProcessClearPairedClients(
63 scoped_ptr
<base::DictionaryValue
> message
,
64 scoped_ptr
<base::DictionaryValue
> response
);
65 void ProcessDeletePairedClient(
66 scoped_ptr
<base::DictionaryValue
> message
,
67 scoped_ptr
<base::DictionaryValue
> response
);
68 void ProcessGetHostName(
69 scoped_ptr
<base::DictionaryValue
> message
,
70 scoped_ptr
<base::DictionaryValue
> response
);
71 void ProcessGetPinHash(
72 scoped_ptr
<base::DictionaryValue
> message
,
73 scoped_ptr
<base::DictionaryValue
> response
);
74 void ProcessGenerateKeyPair(
75 scoped_ptr
<base::DictionaryValue
> message
,
76 scoped_ptr
<base::DictionaryValue
> response
);
77 void ProcessUpdateDaemonConfig(
78 scoped_ptr
<base::DictionaryValue
> message
,
79 scoped_ptr
<base::DictionaryValue
> response
);
80 void ProcessGetDaemonConfig(
81 scoped_ptr
<base::DictionaryValue
> message
,
82 scoped_ptr
<base::DictionaryValue
> response
);
83 void ProcessGetPairedClients(
84 scoped_ptr
<base::DictionaryValue
> message
,
85 scoped_ptr
<base::DictionaryValue
> response
);
86 void ProcessGetUsageStatsConsent(
87 scoped_ptr
<base::DictionaryValue
> message
,
88 scoped_ptr
<base::DictionaryValue
> response
);
89 void ProcessStartDaemon(
90 scoped_ptr
<base::DictionaryValue
> message
,
91 scoped_ptr
<base::DictionaryValue
> response
);
92 void ProcessStopDaemon(
93 scoped_ptr
<base::DictionaryValue
> message
,
94 scoped_ptr
<base::DictionaryValue
> response
);
95 void ProcessGetDaemonState(
96 scoped_ptr
<base::DictionaryValue
> message
,
97 scoped_ptr
<base::DictionaryValue
> response
);
98 void ProcessGetHostClientId(
99 scoped_ptr
<base::DictionaryValue
> message
,
100 scoped_ptr
<base::DictionaryValue
> response
);
101 void ProcessGetCredentialsFromAuthCode(
102 scoped_ptr
<base::DictionaryValue
> message
,
103 scoped_ptr
<base::DictionaryValue
> response
,
104 bool need_user_email
);
106 // These Send... methods get called on the DaemonController's internal thread,
107 // or on the calling thread if called by the PairingRegistry.
108 // These methods fill in the |response| dictionary from the other parameters,
109 // and pass it to SendResponse().
110 void SendConfigResponse(scoped_ptr
<base::DictionaryValue
> response
,
111 scoped_ptr
<base::DictionaryValue
> config
);
112 void SendPairedClientsResponse(scoped_ptr
<base::DictionaryValue
> response
,
113 scoped_ptr
<base::ListValue
> pairings
);
114 void SendUsageStatsConsentResponse(
115 scoped_ptr
<base::DictionaryValue
> response
,
116 const DaemonController::UsageStatsConsent
& consent
);
117 void SendAsyncResult(scoped_ptr
<base::DictionaryValue
> response
,
118 DaemonController::AsyncResult result
);
119 void SendBooleanResult(scoped_ptr
<base::DictionaryValue
> response
,
121 void SendCredentialsResponse(scoped_ptr
<base::DictionaryValue
> response
,
122 const std::string
& user_email
,
123 const std::string
& refresh_token
);
129 // Returns true if the request was successfully delegated to the elevated
130 // host and false otherwise.
131 bool DelegateToElevatedHost(scoped_ptr
<base::DictionaryValue
> message
);
134 class ElevatedChannelEventHandler
135 : public extensions::NativeMessagingChannel::EventHandler
{
137 ElevatedChannelEventHandler(Me2MeNativeMessagingHost
* host
);
139 void OnMessage(scoped_ptr
<base::Value
> message
) override
;
140 void OnDisconnect() override
;
142 Me2MeNativeMessagingHost
* parent_
;
145 // Create and connect to an elevated host process if necessary.
146 // |elevated_channel_| will contain the native messaging channel to the
147 // elevated host if the function succeeds.
148 void Me2MeNativeMessagingHost::EnsureElevatedHostCreated();
150 // Disconnect and shut down the elevated host.
151 void DisconnectElevatedHost();
153 // Native messaging channel used to communicate with the elevated host.
154 scoped_ptr
<extensions::NativeMessagingChannel
> elevated_channel_
;
156 // Native messaging event handler used to process responses from the elevated
158 scoped_ptr
<ElevatedChannelEventHandler
> elevated_channel_event_handler_
;
160 // Timer to control the lifetime of the elevated host.
161 base::OneShotTimer
<Me2MeNativeMessagingHost
> elevated_host_timer_
;
162 #endif // defined(OS_WIN)
164 bool needs_elevation_
;
167 // Handle of the parent window.
168 intptr_t parent_window_handle_
;
169 #endif // defined(OS_WIN)
171 base::Closure quit_closure_
;
173 // Native messaging channel used to communicate with the native message
175 scoped_ptr
<extensions::NativeMessagingChannel
> channel_
;
176 scoped_refptr
<DaemonController
> daemon_controller_
;
178 // Used to load and update the paired clients for this host.
179 scoped_refptr
<protocol::PairingRegistry
> pairing_registry_
;
181 // Used to exchange the service account authorization code for credentials.
182 scoped_ptr
<OAuthClient
> oauth_client_
;
184 base::ThreadChecker thread_checker_
;
186 base::WeakPtr
<Me2MeNativeMessagingHost
> weak_ptr_
;
187 base::WeakPtrFactory
<Me2MeNativeMessagingHost
> weak_factory_
;
189 DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHost
);
192 } // namespace remoting
194 #endif // REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_