Chromium Code Reviews| Index: remoting/ios/bridge/client_controller.mm |
| diff --git a/remoting/ios/bridge/client_controller.mm b/remoting/ios/bridge/client_controller.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..46bccd8789ee54da9dea62bd70668339b0e486d9 |
| --- /dev/null |
| +++ b/remoting/ios/bridge/client_controller.mm |
| @@ -0,0 +1,222 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "remoting/ios/bridge/client_controller.h" |
| + |
| +#import "remoting/ios/data_store.h" |
| +#import "remoting/ios/host_preferences.h" |
| +#import "remoting/ios/bridge/client_bridge.h" |
| + |
| +namespace { |
| +// The value indicateing a successful connection has been established via a call |
|
dcaiafa
2014/03/19 01:14:15
nit: spelling
aboone
2014/03/21 16:42:07
Done.
|
| +// to reportConnectionStatus |
| +const static int kSuccessfulConnection = 3; |
| +} // namespace |
| + |
| +@interface ClientController (Private) |
| ++ (NSString*)getStatusMsgFromInteger:(NSInteger)code; |
| ++ (NSString*)getErrorMsgFromInteger:(NSInteger)code; |
| +@end |
| + |
| +// In this file CLIENT and HOST are used to refer to the iOS Applicaition |
| +// written in OBJ_C++ and the Chromium C++ code base, respectively. This class |
| +// is the CLIENT side of the bridge. Most functions simply pass the information |
| +// to the UI, or to the next layer of the bridge. |
| +@implementation ClientController |
|
dcaiafa
2014/03/19 01:14:15
nit: move class description to header.
aboone
2014/03/21 16:42:07
Done.
|
| + |
| +// Override default contructor and initialize internals |
| +- (id)init { |
| + self = [super init]; |
| + if (self) { |
| + _isConnected = false; |
| + _bridge = new remoting::ClientBridge(); |
| + } |
| + return self; |
| +} |
| + |
| +// Credentials are received from the CLIENT and sent to the HOST. If currently |
| +// connected, discard the connection and begin a new connection. |
| +- (void)connectToHost:(NSString*)username |
| + authToken:(NSString*)token |
| + jabberId:(NSString*)jid |
| + hostId:(NSString*)hostId |
| + publicKey:(NSString*)hostPublicKey |
| + delegate:(id<ClientControllerDelegate>)delegate { |
| + if (_isConnected) { |
| + [self disconnectFromHost]; |
| + } |
| + _delegate = delegate; |
| + |
| + NSString* pairId = @""; |
| + NSString* pairSecret = @""; |
| + |
| + const HostPreferences* hostPrefs = |
| + [[DataStore sharedStore] getHostForId:hostId]; |
| + |
| + // Use the pairing id and secret when known |
| + if (hostPrefs && hostPrefs.pairId && hostPrefs.secret) { |
| + pairId = [hostPrefs.pairId copy]; |
| + pairSecret = [hostPrefs.secret copy]; |
| + } |
| + |
| + _bridge->ConnectToHost([username UTF8String], |
| + [token UTF8String], |
| + [jid UTF8String], |
| + [hostId UTF8String], |
| + [hostPublicKey UTF8String], |
| + [pairId UTF8String], |
| + [pairSecret UTF8String], |
| + self); |
| + |
| + _isConnected = YES; |
| +} |
| + |
| +// CLIENT is asking to close the connection |
| +- (void)disconnectFromHost { |
| + if (!_isConnected) |
| + return; |
| + |
| + _bridge->DisconnectFromHost(); |
| + |
| + _delegate = nil; |
| + _isConnected = NO; |
| +} |
| + |
| +// GET _isConnected |
| +- (BOOL)isConnected { |
| + return _isConnected; |
| +} |
| + |
| +// HOST request for User's PIN |
| +- (void)displayAuthenticationPrompt:(BOOL)pairingSupported { |
| + [_delegate requestHostPin:pairingSupported]; |
| +} |
| + |
| +// HOST requests to save User's connection authorization |
| +- (void)commitPairinedentials:(NSString*)hostId |
|
dcaiafa
2014/03/19 01:14:15
method name spelling
aboone
2014/03/21 16:42:07
Done.
|
| + pairId:(NSString*)pairId |
| + secret:(NSString*)hostSecret { |
| + const HostPreferences* hostPrefs = |
| + [[DataStore sharedStore] getHostForId:hostId]; |
| + if (hostPrefs == nil) { |
| + hostPrefs = [[DataStore sharedStore] createHost:hostId]; |
| + } |
| + if (hostPrefs) { |
| + hostPrefs.pairId = pairId; |
| + hostPrefs.secret = hostSecret; |
| + |
| + [[DataStore sharedStore] saveChanges]; |
| + } |
| +} |
| + |
| +// Translate a connection status code integer to a NSString description |
| ++ (NSString*)getStatusMsgFromInteger:(NSInteger)code { |
| + switch (code) { |
| + case 0: // INITIALIZING |
| + return @"Initializing connection"; |
| + case 1: // CONNECTING |
| + return @"Connecting"; |
| + case 2: // AUTHENTICATED |
| + return @"Authenticated"; |
| + case 3: // CONNECTED |
| + return @"Connected"; |
| + case 4: // FAILED |
| + return @"Connection Failed"; |
| + case 5: // CLOSED |
| + return @"Connection closed"; |
| + default: |
| + return @"Unknown connection state"; |
| + } |
| +} |
| + |
| +// Translate a connection error code integer to a NSString description |
| ++ (NSString*)getErrorMsgFromInteger:(NSInteger)code { |
| + switch (code) { |
| + case 1: // PEER_IS_OFFLINE |
| + return @"Requested host is offline."; |
| + case 2: // SESSION_REJECTED |
| + return @"Session was rejected by the host."; |
| + case 3: // INCOMPATIBLE_PROTOCOL |
| + return @"Incompatible Protocol."; |
| + case 4: // AUTHENTICATION_FAILED |
| + return @"Authentication Failed."; |
| + case 5: // CHANNEL_CONNECTION_ERROR |
| + return @"Channel Connection Error"; |
| + case 6: // SIGNALING_ERROR |
| + return @"Signaling Error"; |
| + case 7: // SIGNALING_TIMEOUT |
| + return @"Signaling Timeout"; |
| + case 8: // HOST_OVERLOAD |
| + return @"Host Overload"; |
| + case 9: // UNKNOWN_ERROR |
| + return @"An unknown error has occurred, preventing the session " |
| + "from opening."; |
| + default: |
| + return @"An unknown error code has occurred."; |
| + } |
| +} |
| + |
| +// HOST reporting connection status. Translate the |state| and |error| into a |
|
dcaiafa
2014/03/19 01:14:15
Please move method descriptions to header.
aboone
2014/03/21 16:42:07
Done.
|
| +// string report or an error condition for the CLIENT UI. |
| +- (void)reportConnectionStatus:(NSInteger)state error:(NSInteger)error { |
| + if (state <= kSuccessfulConnection && error == 0) { |
| + // Report Progress |
| + [_delegate |
| + connectionStatus:[ClientController getStatusMsgFromInteger:state]]; |
| + |
| + if (state == kSuccessfulConnection) { |
| + [_delegate connected]; |
| + } |
| + } else { |
| + [_delegate |
| + connectionStatus:[ClientController getErrorMsgFromInteger:error]]; |
| + [_delegate |
| + connectionFailed:[ClientController getErrorMsgFromInteger:error]]; |
| + } |
| +} |
| + |
| +// CLIENT reporting a mouse input. Only passed on when a connection is |
| +// currently active. |
| +- (void)mouseAction:(const webrtc::DesktopVector&)position |
| + wheelDelta:(const webrtc::DesktopVector&)wheelDelta |
| + whichButton:(NSInteger)buttonPressed |
| + buttonDown:(BOOL)buttonIsDown { |
| + if (_isConnected) { |
| + if (buttonPressed >= 0 && buttonPressed < 5) { |
| + _bridge->session()->PerformMouseAction( |
| + position, wheelDelta, buttonPressed, buttonIsDown); |
| + } |
| + } |
| +} |
| + |
| +// CLIENT reporting a keyboard input. Only passed on when a connection is |
| +// currently active. |
| +- (void)keyboardAction:(NSInteger)keyCode keyDown:(BOOL)keyIsDown { |
| + if (_isConnected) { |
| + _bridge->session()->PerformKeyboardAction(keyCode, keyIsDown); |
| + } |
| +} |
| + |
| +// CLIENT reporting the User's input for their PIN, if needed create a new |
| +// pairing |
| +- (void)authenticationResponse:(NSString*)pin createPair:(BOOL)createPair { |
| + _bridge->session()->ProvideSecret([pin UTF8String], createPair); |
| +} |
| + |
| +// HOST reporting a change in Cursor (mouse) |
| +- (void)updateCursorShape:(const webrtc::DesktopSize&)size |
| + hotspot:(const webrtc::DesktopVector&)hotspot |
| + cursorData:(uint8_t*)data { |
| + [_delegate applyCursor:size hotspot:hotspot cursorData:data]; |
| +} |
| + |
| +// HOST reporting a change in canvas (desktop) |
| +- (void)updateImageBuffer:(const webrtc::DesktopSize&)size |
| + stride:(NSInteger)stride |
| + data:(uint8_t*)data |
| + regions:(const std::vector<webrtc::DesktopRect>&)regions { |
| + [_delegate applyFrame:size stride:stride data:data regions:regions]; |
| +} |
| + |
| +@end |