Home | History | Annotate | Download | only in setup
      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 "remoting/host/native_messaging/native_messaging_channel.h"
     14 #include "remoting/host/setup/daemon_controller.h"
     15 #include "remoting/host/setup/oauth_client.h"
     16 
     17 namespace base {
     18 class DictionaryValue;
     19 class ListValue;
     20 }  // namespace base
     21 
     22 namespace gaia {
     23 class GaiaOAuthClient;
     24 }  // namespace gaia
     25 
     26 namespace remoting {
     27 
     28 const char kElevatingSwitchName[] = "elevate";
     29 const char kInputSwitchName[] = "input";
     30 const char kOutputSwitchName[] = "output";
     31 
     32 namespace protocol {
     33 class PairingRegistry;
     34 }  // namespace protocol
     35 
     36 // Implementation of the me2me native messaging host.
     37 class Me2MeNativeMessagingHost {
     38  public:
     39   typedef NativeMessagingChannel::SendMessageCallback SendMessageCallback;
     40 
     41   Me2MeNativeMessagingHost(
     42       bool needs_elevation,
     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();
     49 
     50   void Start(const base::Closure& quit_closure);
     51 
     52  private:
     53   // Callback to process messages from the native messaging client through
     54   // |channel_|.
     55   void ProcessRequest(scoped_ptr<base::DictionaryValue> message);
     56 
     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).
     60   void ProcessHello(
     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 
    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,
    120                          bool result);
    121   void SendCredentialsResponse(scoped_ptr<base::DictionaryValue> response,
    122                                const std::string& user_email,
    123                                const std::string& refresh_token);
    124 
    125   void OnError();
    126 
    127   void Stop();
    128 
    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);
    132 
    133 #if defined(OS_WIN)
    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();
    138 
    139   // Callback to process messages from the elevated host through
    140   // |elevated_channel_|.
    141   void ProcessDelegateResponse(scoped_ptr<base::DictionaryValue> message);
    142 
    143   // Disconnect and shut down the elevated host.
    144   void DisconnectElevatedHost();
    145 
    146   // Native messaging channel used to communicate with the elevated host.
    147   scoped_ptr<NativeMessagingChannel> elevated_channel_;
    148 
    149   // Timer to control the lifetime of the elevated host.
    150   base::OneShotTimer<Me2MeNativeMessagingHost> elevated_host_timer_;
    151 #endif  // defined(OS_WIN)
    152 
    153   bool needs_elevation_;
    154 
    155   // Handle of the parent window.
    156   intptr_t parent_window_handle_;
    157 
    158   base::Closure quit_closure_;
    159 
    160   // Native messaging channel used to communicate with the native message
    161   // client.
    162   scoped_ptr<NativeMessagingChannel> channel_;
    163   scoped_refptr<DaemonController> daemon_controller_;
    164 
    165   // Used to load and update the paired clients for this host.
    166   scoped_refptr<protocol::PairingRegistry> pairing_registry_;
    167 
    168   // Used to exchange the service account authorization code for credentials.
    169   scoped_ptr<OAuthClient> oauth_client_;
    170 
    171   base::ThreadChecker thread_checker_;
    172 
    173   base::WeakPtr<Me2MeNativeMessagingHost> weak_ptr_;
    174   base::WeakPtrFactory<Me2MeNativeMessagingHost> weak_factory_;
    175 
    176   DISALLOW_COPY_AND_ASSIGN(Me2MeNativeMessagingHost);
    177 };
    178 
    179 // Creates a Me2MeNativeMessagingHost instance, attaches it to stdin/stdout and
    180 // runs the message loop until Me2MeNativeMessagingHost signals shutdown.
    181 int Me2MeNativeMessagingHostMain();
    182 
    183 }  // namespace remoting
    184 
    185 #endif  // REMOTING_HOST_SETUP_ME2ME_NATIVE_MESSAGING_HOST_H_
    186