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 "remoting/host/native_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
{
39 typedef NativeMessagingChannel::SendMessageCallback SendMessageCallback
;
41 Me2MeNativeMessagingHost(
43 intptr_t parent_window_handle
,
44 scoped_ptr
<NativeMessagingChannel
> channel
,
45 scoped_refptr
<DaemonController
> daemon_controller
,
46 scoped_refptr
<protocol::PairingRegistry
> pairing_registry
,
47 scoped_ptr
<OAuthClient
> oauth_client
);
48 virtual ~Me2MeNativeMessagingHost();
50 void Start(const base::Closure
& quit_closure
);
53 // Callback to process messages from the native messaging client through
55 void ProcessRequest(scoped_ptr
<base::DictionaryValue
> message
);
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
);
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 // Create and connect to an elevated host process if necessary.
135 // |elevated_channel_| will contain the native messaging channel to the
136 // elevated host if the function succeeds.
137 void Me2MeNativeMessagingHost::EnsureElevatedHostCreated();
139 // Callback to process messages from the elevated host through
140 // |elevated_channel_|.
141 void ProcessDelegateResponse(scoped_ptr
<base::DictionaryValue
> message
);
143 // Disconnect and shut down the elevated host.
144 void DisconnectElevatedHost();
146 // Native messaging channel used to communicate with the elevated host.
147 scoped_ptr
<NativeMessagingChannel
> elevated_channel_
;
149 // Timer to control the lifetime of the elevated host.
150 base::OneShotTimer
<Me2MeNativeMessagingHost
> elevated_host_timer_
;
151 #endif // defined(OS_WIN)
153 bool needs_elevation_
;
155 // Handle of the parent window.
156 intptr_t parent_window_handle_
;
158 base::Closure quit_closure_
;
160 // Native messaging channel used to communicate with the native message
162 scoped_ptr
<NativeMessagingChannel
> channel_
;
163 scoped_refptr
<DaemonController
> daemon_controller_
;
165 // Used to load and update the paired clients for this host.
166 scoped_refptr
<protocol::PairingRegistry
> pairing_registry_
;
168 // Used to exchange the service account authorization code for credentials.
169 scoped_ptr
<OAuthClient
> oauth_client_
;
171 base::ThreadChecker thread_checker_
;
173 base::WeakPtr
<Me2MeNativeMessagingHost
> weak_ptr_
;
174 base::WeakPtrFactory
<Me2MeNativeMessagingHost
> weak_factory_
;
176 DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHost
);
179 } // namespace remoting
181 #endif // REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_