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 "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" 16 17namespace base { 18class DictionaryValue; 19class ListValue; 20} // namespace base 21 22namespace gaia { 23class GaiaOAuthClient; 24} // namespace gaia 25 26namespace remoting { 27 28const char kElevatingSwitchName[] = "elevate"; 29const char kInputSwitchName[] = "input"; 30const char kOutputSwitchName[] = "output"; 31 32namespace protocol { 33class PairingRegistry; 34} // namespace protocol 35 36// Implementation of the me2me native messaging host. 37class Me2MeNativeMessagingHost 38 : public extensions::NativeMessagingChannel::EventHandler { 39 public: 40 Me2MeNativeMessagingHost( 41 bool needs_elevation, 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 virtual ~Me2MeNativeMessagingHost(); 48 49 void Start(const base::Closure& quit_closure); 50 51 // extensions::NativeMessagingChannel::EventHandler implementation 52 virtual void OnMessage(scoped_ptr<base::Value> message) OVERRIDE; 53 virtual void OnDisconnect() OVERRIDE; 54 55 private: 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). 59 void ProcessHello( 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 105 // These Send... methods get called on the DaemonController's internal thread, 106 // or on the calling thread if called by the PairingRegistry. 107 // These methods fill in the |response| dictionary from the other parameters, 108 // and pass it to SendResponse(). 109 void SendConfigResponse(scoped_ptr<base::DictionaryValue> response, 110 scoped_ptr<base::DictionaryValue> config); 111 void SendPairedClientsResponse(scoped_ptr<base::DictionaryValue> response, 112 scoped_ptr<base::ListValue> pairings); 113 void SendUsageStatsConsentResponse( 114 scoped_ptr<base::DictionaryValue> response, 115 const DaemonController::UsageStatsConsent& consent); 116 void SendAsyncResult(scoped_ptr<base::DictionaryValue> response, 117 DaemonController::AsyncResult result); 118 void SendBooleanResult(scoped_ptr<base::DictionaryValue> response, 119 bool result); 120 void SendCredentialsResponse(scoped_ptr<base::DictionaryValue> response, 121 const std::string& user_email, 122 const std::string& refresh_token); 123 124 void OnError(); 125 126 void Stop(); 127 128 // Returns true if the request was successfully delegated to the elevated 129 // host and false otherwise. 130 bool DelegateToElevatedHost(scoped_ptr<base::DictionaryValue> message); 131 132#if defined(OS_WIN) 133 class ElevatedChannelEventHandler 134 : public extensions::NativeMessagingChannel::EventHandler { 135 public: 136 ElevatedChannelEventHandler(Me2MeNativeMessagingHost* host); 137 138 virtual void OnMessage(scoped_ptr<base::Value> message) OVERRIDE; 139 virtual void OnDisconnect() OVERRIDE; 140 private: 141 Me2MeNativeMessagingHost* parent_; 142 }; 143 144 // Create and connect to an elevated host process if necessary. 145 // |elevated_channel_| will contain the native messaging channel to the 146 // elevated host if the function succeeds. 147 void Me2MeNativeMessagingHost::EnsureElevatedHostCreated(); 148 149 // Disconnect and shut down the elevated host. 150 void DisconnectElevatedHost(); 151 152 // Native messaging channel used to communicate with the elevated host. 153 scoped_ptr<extensions::NativeMessagingChannel> elevated_channel_; 154 155 // Native messaging event handler used to process responses from the elevated 156 // host. 157 scoped_ptr<ElevatedChannelEventHandler> elevated_channel_event_handler_; 158 159 // Timer to control the lifetime of the elevated host. 160 base::OneShotTimer<Me2MeNativeMessagingHost> elevated_host_timer_; 161#endif // defined(OS_WIN) 162 163 bool needs_elevation_; 164 165 // Handle of the parent window. 166 intptr_t parent_window_handle_; 167 168 base::Closure quit_closure_; 169 170 // Native messaging channel used to communicate with the native message 171 // client. 172 scoped_ptr<extensions::NativeMessagingChannel> channel_; 173 scoped_refptr<DaemonController> daemon_controller_; 174 175 // Used to load and update the paired clients for this host. 176 scoped_refptr<protocol::PairingRegistry> pairing_registry_; 177 178 // Used to exchange the service account authorization code for credentials. 179 scoped_ptr<OAuthClient> oauth_client_; 180 181 base::ThreadChecker thread_checker_; 182 183 base::WeakPtr<Me2MeNativeMessagingHost> weak_ptr_; 184 base::WeakPtrFactory<Me2MeNativeMessagingHost> weak_factory_; 185 186 DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHost); 187}; 188 189} // namespace remoting 190 191#endif // REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_ 192