| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 6 #error "This file requires ARC support." | |
| 7 #endif | |
| 8 | |
| 9 #import "remoting/client/ios/session/remoting_client.h" | |
| 10 | |
| 11 #include <memory> | |
| 12 | |
| 13 #import "base/mac/bind_objc_block.h" | |
| 14 #import "ios/third_party/material_components_ios/src/components/Dialogs/src/Mate
rialDialogs.h" | |
| 15 #import "remoting/client/ios/display/gl_display_handler.h" | |
| 16 #import "remoting/client/ios/domain/client_session_details.h" | |
| 17 #import "remoting/client/ios/domain/host_info.h" | |
| 18 | |
| 19 #include "base/strings/sys_string_conversions.h" | |
| 20 #include "remoting/client/chromoting_client_runtime.h" | |
| 21 #include "remoting/client/chromoting_session.h" | |
| 22 #include "remoting/client/connect_to_host_info.h" | |
| 23 #include "remoting/client/gesture_interpreter.h" | |
| 24 #include "remoting/client/ios/session/remoting_client_session_delegate.h" | |
| 25 #include "remoting/protocol/session.h" | |
| 26 #include "remoting/protocol/video_renderer.h" | |
| 27 | |
| 28 NSString* const kHostSessionStatusChanged = @"kHostSessionStatusChanged"; | |
| 29 NSString* const kHostSessionPinProvided = @"kHostSessionPinProvided"; | |
| 30 | |
| 31 NSString* const kSessionDetails = @"kSessionDetails"; | |
| 32 NSString* const kSessonStateErrorCode = @"kSessonStateErrorCode"; | |
| 33 NSString* const kHostSessionPin = @"kHostSessionPin"; | |
| 34 | |
| 35 @interface RemotingClient () { | |
| 36 remoting::ChromotingClientRuntime* _runtime; | |
| 37 std::unique_ptr<remoting::ChromotingSession> _session; | |
| 38 remoting::RemotingClientSessonDelegate* _sessonDelegate; | |
| 39 ClientSessionDetails* _sessionDetails; | |
| 40 // Call _secretFetchedCallback on the network thread. | |
| 41 remoting::protocol::SecretFetchedCallback _secretFetchedCallback; | |
| 42 std::unique_ptr<remoting::GestureInterpreter> _gestureInterpreter; | |
| 43 } | |
| 44 @end | |
| 45 | |
| 46 @implementation RemotingClient | |
| 47 | |
| 48 @synthesize displayHandler = _displayHandler; | |
| 49 | |
| 50 - (instancetype)init { | |
| 51 self = [super init]; | |
| 52 if (self) { | |
| 53 _runtime = remoting::ChromotingClientRuntime::GetInstance(); | |
| 54 _sessonDelegate = new remoting::RemotingClientSessonDelegate(self); | |
| 55 _sessionDetails = [[ClientSessionDetails alloc] init]; | |
| 56 | |
| 57 [[NSNotificationCenter defaultCenter] | |
| 58 addObserver:self | |
| 59 selector:@selector(hostSessionPinProvided:) | |
| 60 name:kHostSessionPinProvided | |
| 61 object:nil]; | |
| 62 } | |
| 63 return self; | |
| 64 } | |
| 65 | |
| 66 - (void)connectToHost:(HostInfo*)hostInfo | |
| 67 username:(NSString*)username | |
| 68 accessToken:(NSString*)accessToken { | |
| 69 DCHECK(_runtime->ui_task_runner()->BelongsToCurrentThread()); | |
| 70 DCHECK(hostInfo); | |
| 71 DCHECK(hostInfo.jabberId); | |
| 72 DCHECK(hostInfo.hostId); | |
| 73 DCHECK(hostInfo.publicKey); | |
| 74 | |
| 75 _sessionDetails.hostInfo = hostInfo; | |
| 76 | |
| 77 remoting::ConnectToHostInfo info; | |
| 78 info.username = base::SysNSStringToUTF8(username); | |
| 79 info.auth_token = base::SysNSStringToUTF8(accessToken); | |
| 80 info.host_jid = base::SysNSStringToUTF8(hostInfo.jabberId); | |
| 81 info.host_id = base::SysNSStringToUTF8(hostInfo.hostId); | |
| 82 info.host_pubkey = base::SysNSStringToUTF8(hostInfo.publicKey); | |
| 83 // TODO(nicholss): If iOS supports pairing, pull the stored data and | |
| 84 // insert it here. | |
| 85 info.pairing_id = ""; | |
| 86 info.pairing_secret = ""; | |
| 87 | |
| 88 // TODO(nicholss): I am not sure about the following fields yet. | |
| 89 // info.capabilities = | |
| 90 // info.flags = | |
| 91 // info.host_version = | |
| 92 // info.host_os = | |
| 93 // info.host_os_version = | |
| 94 | |
| 95 remoting::protocol::ClientAuthenticationConfig client_auth_config; | |
| 96 client_auth_config.host_id = info.host_id; | |
| 97 client_auth_config.pairing_client_id = info.pairing_id; | |
| 98 client_auth_config.pairing_secret = info.pairing_secret; | |
| 99 client_auth_config.fetch_secret_callback = base::BindBlockArc( | |
| 100 ^(bool pairing_supported, const remoting::protocol::SecretFetchedCallback& | |
| 101 secret_fetched_callback) { | |
| 102 _secretFetchedCallback = secret_fetched_callback; | |
| 103 _sessionDetails.state = SessionPinPrompt; | |
| 104 [[NSNotificationCenter defaultCenter] | |
| 105 postNotificationName:kHostSessionStatusChanged | |
| 106 object:self | |
| 107 userInfo:[NSDictionary | |
| 108 dictionaryWithObject:_sessionDetails | |
| 109 forKey:kSessionDetails]]; | |
| 110 }); | |
| 111 | |
| 112 // TODO(nicholss): Add audio support to iOS. | |
| 113 base::WeakPtr<remoting::protocol::AudioStub> audioPlayer = nullptr; | |
| 114 | |
| 115 _displayHandler = [[GlDisplayHandler alloc] init]; | |
| 116 _displayHandler.delegate = self; | |
| 117 | |
| 118 _session.reset(new remoting::ChromotingSession( | |
| 119 _sessonDelegate->GetWeakPtr(), [_displayHandler CreateCursorShapeStub], | |
| 120 [_displayHandler CreateVideoRenderer], audioPlayer, info, | |
| 121 client_auth_config)); | |
| 122 _session->Connect(); | |
| 123 | |
| 124 __weak GlDisplayHandler* weakDisplayHandler = _displayHandler; | |
| 125 _gestureInterpreter.reset(new remoting::GestureInterpreter( | |
| 126 base::BindBlockArc(^(const remoting::ViewMatrix& matrix) { | |
| 127 [weakDisplayHandler onPixelTransformationChanged:matrix]; | |
| 128 }), | |
| 129 _session.get())); | |
| 130 } | |
| 131 | |
| 132 - (void)disconnectFromHost { | |
| 133 if (_session) { | |
| 134 _session->Disconnect(); | |
| 135 } | |
| 136 // TODO(nicholss): Do we need to cleanup more? | |
| 137 } | |
| 138 | |
| 139 #pragma mark - Eventing | |
| 140 | |
| 141 - (void)hostSessionPinProvided:(NSNotification*)notification { | |
| 142 NSString* pin = [[notification userInfo] objectForKey:kHostSessionPin]; | |
| 143 if (_secretFetchedCallback) { | |
| 144 _runtime->network_task_runner()->PostTask( | |
| 145 FROM_HERE, base::BindBlockArc(^{ | |
| 146 _secretFetchedCallback.Run(base::SysNSStringToUTF8(pin)); | |
| 147 })); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 #pragma mark - Properties | |
| 152 | |
| 153 - (HostInfo*)hostInfo { | |
| 154 return _sessionDetails.hostInfo; | |
| 155 } | |
| 156 | |
| 157 - (remoting::GestureInterpreter*)gestureInterpreter { | |
| 158 return _gestureInterpreter.get(); | |
| 159 } | |
| 160 | |
| 161 #pragma mark - ChromotingSession::Delegate | |
| 162 | |
| 163 - (void)onConnectionState:(remoting::protocol::ConnectionToHost::State)state | |
| 164 error:(remoting::protocol::ErrorCode)error { | |
| 165 switch (state) { | |
| 166 case remoting::protocol::ConnectionToHost::INITIALIZING: | |
| 167 NSLog(@"State --> INITIALIZING"); | |
| 168 _sessionDetails.state = SessionInitializing; | |
| 169 break; | |
| 170 case remoting::protocol::ConnectionToHost::CONNECTING: | |
| 171 NSLog(@"State --> CONNECTING"); | |
| 172 _sessionDetails.state = SessionConnecting; | |
| 173 break; | |
| 174 case remoting::protocol::ConnectionToHost::AUTHENTICATED: | |
| 175 NSLog(@"State --> AUTHENTICATED"); | |
| 176 _sessionDetails.state = SessionAuthenticated; | |
| 177 break; | |
| 178 case remoting::protocol::ConnectionToHost::CONNECTED: | |
| 179 NSLog(@"State --> CONNECTED"); | |
| 180 _sessionDetails.state = SessionConnected; | |
| 181 break; | |
| 182 case remoting::protocol::ConnectionToHost::FAILED: | |
| 183 NSLog(@"State --> FAILED"); | |
| 184 _sessionDetails.state = SessionFailed; | |
| 185 break; | |
| 186 case remoting::protocol::ConnectionToHost::CLOSED: | |
| 187 NSLog(@"State --> CLOSED"); | |
| 188 _sessionDetails.state = SessionClosed; | |
| 189 break; | |
| 190 default: | |
| 191 LOG(ERROR) << "onConnectionState, unknown state: " << state; | |
| 192 } | |
| 193 | |
| 194 // TODO(nicholss): Send along the error code when we know what to do about it. | |
| 195 [[NSNotificationCenter defaultCenter] | |
| 196 postNotificationName:kHostSessionStatusChanged | |
| 197 object:self | |
| 198 userInfo:[NSDictionary dictionaryWithObject:_sessionDetails | |
| 199 forKey:kSessionDetails]]; | |
| 200 } | |
| 201 | |
| 202 - (void)commitPairingCredentialsForHost:(NSString*)host | |
| 203 id:(NSString*)id | |
| 204 secret:(NSString*)secret { | |
| 205 NSLog(@"TODO(nicholss): implement this, commitPairingCredentialsForHost."); | |
| 206 } | |
| 207 | |
| 208 - (void)fetchThirdPartyTokenForUrl:(NSString*)tokenUrl | |
| 209 clientId:(NSString*)clientId | |
| 210 scope:(NSString*)scope { | |
| 211 NSLog(@"TODO(nicholss): implement this, fetchThirdPartyTokenForUrl."); | |
| 212 } | |
| 213 | |
| 214 - (void)setCapabilities:(NSString*)capabilities { | |
| 215 NSLog(@"TODO(nicholss): implement this, setCapabilities."); | |
| 216 } | |
| 217 | |
| 218 - (void)handleExtensionMessageOfType:(NSString*)type | |
| 219 message:(NSString*)message { | |
| 220 NSLog(@"TODO(nicholss): implement this, handleExtensionMessageOfType %@:%@.", | |
| 221 type, message); | |
| 222 } | |
| 223 | |
| 224 - (void)surfaceChanged:(const CGRect&)frame { | |
| 225 [_displayHandler onSurfaceChanged:frame]; | |
| 226 _gestureInterpreter->OnSurfaceSizeChanged(frame.size.width, | |
| 227 frame.size.height); | |
| 228 } | |
| 229 | |
| 230 #pragma mark - GlDisplayHandlerDelegate | |
| 231 | |
| 232 - (void)canvasSizeChanged:(CGSize)size { | |
| 233 if (_gestureInterpreter) { | |
| 234 _gestureInterpreter->OnDesktopSizeChanged(size.width, size.height); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 @end | |
| OLD | NEW |