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.
5 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
9 #include "remoting/ios/bridge/client_proxy.h"
11 #import "remoting/ios/data_store.h"
12 #import "remoting/ios/host_preferences.h"
13 #import "remoting/ios/bridge/client_proxy_delegate_wrapper.h"
16 // The value indicating a successful connection has been established via a call
17 // to ReportConnectionStatus
18 const static int kSuccessfulConnection = 3;
20 // Translate a connection status code integer to a NSString description
21 NSString* GetStatusMsgFromInteger(NSInteger code) {
23 case 0: // INITIALIZING
24 return @"Initializing connection";
27 case 2: // AUTHENTICATED
28 return @"Authenticated";
32 return @"Connection Failed";
34 return @"Connection closed";
36 return @"Unknown connection state";
40 // Translate a connection error code integer to a NSString description
41 NSString* GetErrorMsgFromInteger(NSInteger code) {
43 case 1: // PEER_IS_OFFLINE
44 return @"Requested host is offline.";
45 case 2: // SESSION_REJECTED
46 return @"Session was rejected by the host.";
47 case 3: // INCOMPATIBLE_PROTOCOL
48 return @"Incompatible Protocol.";
49 case 4: // AUTHENTICATION_FAILED
50 return @"Authentication Failed.";
51 case 5: // CHANNEL_CONNECTION_ERROR
52 return @"Channel Connection Error";
53 case 6: // SIGNALING_ERROR
54 return @"Signaling Error";
55 case 7: // SIGNALING_TIMEOUT
56 return @"Signaling Timeout";
57 case 8: // HOST_OVERLOAD
58 return @"Host Overload";
59 case 9: // UNKNOWN_ERROR
60 return @"An unknown error has occurred, preventing the session "
63 return @"An unknown error code has occurred.";
71 ClientProxy::ClientProxy(ClientProxyDelegateWrapper* wrapper) {
72 delegate_ = [wrapper delegate];
75 void ClientProxy::ReportConnectionStatus(
76 protocol::ConnectionToHost::State state,
77 protocol::ErrorCode error) {
79 if (state <= kSuccessfulConnection && error == protocol::ErrorCode::OK) {
81 [delegate_ connectionStatus:GetStatusMsgFromInteger(state)];
83 if (state == kSuccessfulConnection) {
84 [delegate_ connected];
87 [delegate_ connectionStatus:GetStatusMsgFromInteger(state)];
88 if (error != protocol::ErrorCode::OK) {
89 [delegate_ connectionFailed:GetErrorMsgFromInteger(error)];
94 void ClientProxy::DisplayAuthenticationPrompt(bool pairing_supported) {
96 [delegate_ requestHostPin:pairing_supported];
99 void ClientProxy::CommitPairingCredentials(const std::string& hostId,
100 const std::string& pairId,
101 const std::string& pairSecret) {
103 NSString* nsHostId = [[NSString alloc] initWithUTF8String:hostId.c_str()];
104 NSString* nsPairId = [[NSString alloc] initWithUTF8String:pairId.c_str()];
105 NSString* nsPairSecret =
106 [[NSString alloc] initWithUTF8String:pairSecret.c_str()];
108 const HostPreferences* hostPrefs =
109 [[DataStore sharedStore] getHostForId:nsHostId];
110 if (hostPrefs == nil) {
111 hostPrefs = [[DataStore sharedStore] createHost:nsHostId];
114 hostPrefs.pairId = nsPairId;
115 hostPrefs.pairSecret = nsPairSecret;
117 [[DataStore sharedStore] saveChanges];
121 void ClientProxy::RedrawCanvas(const webrtc::DesktopSize& view_size,
122 webrtc::DesktopFrame* buffer,
123 const webrtc::DesktopRegion& region) {
125 std::vector<webrtc::DesktopRect> regions;
127 for (webrtc::DesktopRegion::Iterator i(region); !i.IsAtEnd(); i.Advance()) {
128 const webrtc::DesktopRect& rect(i.rect());
130 regions.push_back(webrtc::DesktopRect::MakeXYWH(
131 rect.left(), rect.top(), rect.width(), rect.height()));
134 [delegate_ applyFrame:view_size
135 stride:buffer->stride()
140 void ClientProxy::UpdateCursorShape(
141 const protocol::CursorShapeInfo& cursor_shape) {
143 [delegate_ applyCursor:webrtc::DesktopSize(cursor_shape.width(),
144 cursor_shape.height())
145 hotspot:webrtc::DesktopVector(cursor_shape.hotspot_x(),
146 cursor_shape.hotspot_y())
147 cursorData:(uint8_t*)cursor_shape.data().c_str()];
150 } // namespace remoting