Home | History | Annotate | Download | only in bridge
      1 // Copyright 2014 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 #import "remoting/ios/bridge/host_proxy.h"
      6 
      7 #import "remoting/ios/data_store.h"
      8 #import "remoting/ios/host_preferences.h"
      9 #import "remoting/ios/bridge/client_instance.h"
     10 #import "remoting/ios/bridge/client_proxy.h"
     11 
     12 @implementation HostProxy
     13 
     14 // Override default constructor and initialize internals
     15 - (id)init {
     16   self = [super init];
     17   if (self) {
     18     _isConnected = false;
     19   }
     20   return self;
     21 }
     22 
     23 // Override default destructor
     24 - (void)dealloc {
     25   if (_isConnected) {
     26     [self disconnectFromHost];
     27   }
     28 
     29   [super dealloc];
     30 }
     31 
     32 - (BOOL)isConnected {
     33   return _isConnected;
     34 }
     35 
     36 - (void)connectToHost:(NSString*)username
     37             authToken:(NSString*)token
     38              jabberId:(NSString*)jid
     39                hostId:(NSString*)hostId
     40             publicKey:(NSString*)hostPublicKey
     41              delegate:(id<ClientProxyDelegate>)delegate {
     42   // Implicitly, if currently connected, discard the connection and begin a new
     43   // connection.
     44   [self disconnectFromHost];
     45 
     46   NSString* pairId = @"";
     47   NSString* pairSecret = @"";
     48 
     49   const HostPreferences* hostPrefs =
     50       [[DataStore sharedStore] getHostForId:hostId];
     51 
     52   // Use the pairing id and secret when known
     53   if (hostPrefs && hostPrefs.pairId && hostPrefs.pairSecret) {
     54     pairId = [hostPrefs.pairId copy];
     55     pairSecret = [hostPrefs.pairSecret copy];
     56   }
     57 
     58   _hostToClientChannel.reset(new remoting::ClientProxy(
     59       [ClientProxyDelegateWrapper wrapDelegate:delegate]));
     60 
     61   DCHECK(!_clientToHostChannel);
     62   _clientToHostChannel =
     63       new remoting::ClientInstance(_hostToClientChannel->AsWeakPtr(),
     64                                    [username UTF8String],
     65                                    [token UTF8String],
     66                                    [jid UTF8String],
     67                                    [hostId UTF8String],
     68                                    [hostPublicKey UTF8String],
     69                                    [pairId UTF8String],
     70                                    [pairSecret UTF8String]);
     71 
     72   _clientToHostChannel->Start();
     73   _isConnected = YES;
     74 }
     75 
     76 - (void)authenticationResponse:(NSString*)pin createPair:(BOOL)createPair {
     77   if (_isConnected) {
     78     _clientToHostChannel->ProvideSecret([pin UTF8String], createPair);
     79   }
     80 }
     81 
     82 - (void)disconnectFromHost {
     83   if (_isConnected) {
     84     VLOG(1) << "Disconnecting from Host";
     85 
     86     // |_clientToHostChannel| must be closed before releasing
     87     // |_hostToClientChannel|
     88 
     89     // |_clientToHostChannel| owns several objects that have references to
     90     // itself.  These objects need to be cleaned up before we can release
     91     // |_clientToHostChannel|.
     92     _clientToHostChannel->Cleanup();
     93     // All other references to |_clientToHostChannel| should now be free.  When
     94     // the next statement is executed the destructor is called automatically.
     95     _clientToHostChannel = NULL;
     96 
     97     _hostToClientChannel.reset();
     98 
     99     _isConnected = NO;
    100   }
    101 }
    102 
    103 - (void)mouseAction:(const webrtc::DesktopVector&)position
    104          wheelDelta:(const webrtc::DesktopVector&)wheelDelta
    105         whichButton:(NSInteger)buttonPressed
    106          buttonDown:(BOOL)buttonIsDown {
    107   if (_isConnected) {
    108     _clientToHostChannel->PerformMouseAction(
    109         position, wheelDelta, buttonPressed, buttonIsDown);
    110   }
    111 }
    112 
    113 - (void)keyboardAction:(NSInteger)keyCode keyDown:(BOOL)keyIsDown {
    114   if (_isConnected) {
    115     _clientToHostChannel->PerformKeyboardAction(keyCode, keyIsDown);
    116   }
    117 }
    118 
    119 @end
    120