Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / remoting / ios / bridge / client_proxy.mm
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 #if !defined(__has_feature) || !__has_feature(objc_arc)
6 #error "This file requires ARC support."
7 #endif
8
9 #include "remoting/ios/bridge/client_proxy.h"
10
11 #import "remoting/ios/data_store.h"
12 #import "remoting/ios/host_preferences.h"
13 #import "remoting/ios/bridge/client_proxy_delegate_wrapper.h"
14
15 namespace {
16 // The value indicating a successful connection has been established via a call
17 // to ReportConnectionStatus
18 const static int kSuccessfulConnection = 3;
19
20 // Translate a connection status code integer to a NSString description
21 NSString* GetStatusMsgFromInteger(NSInteger code) {
22   switch (code) {
23     case 0:  // INITIALIZING
24       return @"Initializing connection";
25     case 1:  // CONNECTING
26       return @"Connecting";
27     case 2:  // AUTHENTICATED
28       return @"Authenticated";
29     case 3:  // CONNECTED
30       return @"Connected";
31     case 4:  // FAILED
32       return @"Connection Failed";
33     case 5:  // CLOSED
34       return @"Connection closed";
35     default:
36       return @"Unknown connection state";
37   }
38 }
39
40 // Translate a connection error code integer to a NSString description
41 NSString* GetErrorMsgFromInteger(NSInteger code) {
42   switch (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 "
61               "from opening.";
62     default:
63       return @"An unknown error code has occurred.";
64   }
65 }
66
67 }  // namespace
68
69 namespace remoting {
70
71 ClientProxy::ClientProxy(ClientProxyDelegateWrapper* wrapper) {
72   delegate_ = [wrapper delegate];
73 }
74
75 void ClientProxy::ReportConnectionStatus(
76     protocol::ConnectionToHost::State state,
77     protocol::ErrorCode error) {
78   DCHECK(delegate_);
79   if (state <= kSuccessfulConnection && error == protocol::ErrorCode::OK) {
80     // Report Progress
81     [delegate_ connectionStatus:GetStatusMsgFromInteger(state)];
82
83     if (state == kSuccessfulConnection) {
84       [delegate_ connected];
85     }
86   } else {
87     [delegate_ connectionStatus:GetStatusMsgFromInteger(state)];
88     if (error != protocol::ErrorCode::OK) {
89       [delegate_ connectionFailed:GetErrorMsgFromInteger(error)];
90     }
91   }
92 }
93
94 void ClientProxy::DisplayAuthenticationPrompt(bool pairing_supported) {
95   DCHECK(delegate_);
96   [delegate_ requestHostPin:pairing_supported];
97 }
98
99 void ClientProxy::CommitPairingCredentials(const std::string& hostId,
100                                            const std::string& pairId,
101                                            const std::string& pairSecret) {
102   DCHECK(delegate_);
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()];
107
108   const HostPreferences* hostPrefs =
109       [[DataStore sharedStore] getHostForId:nsHostId];
110   if (hostPrefs == nil) {
111     hostPrefs = [[DataStore sharedStore] createHost:nsHostId];
112   }
113   if (hostPrefs) {
114     hostPrefs.pairId = nsPairId;
115     hostPrefs.pairSecret = nsPairSecret;
116
117     [[DataStore sharedStore] saveChanges];
118   }
119 }
120
121 void ClientProxy::RedrawCanvas(const webrtc::DesktopSize& view_size,
122                                webrtc::DesktopFrame* buffer,
123                                const webrtc::DesktopRegion& region) {
124   DCHECK(delegate_);
125   std::vector<webrtc::DesktopRect> regions;
126
127   for (webrtc::DesktopRegion::Iterator i(region); !i.IsAtEnd(); i.Advance()) {
128     const webrtc::DesktopRect& rect(i.rect());
129
130     regions.push_back(webrtc::DesktopRect::MakeXYWH(
131         rect.left(), rect.top(), rect.width(), rect.height()));
132   }
133
134   [delegate_ applyFrame:view_size
135                  stride:buffer->stride()
136                    data:buffer->data()
137                 regions:regions];
138 }
139
140 void ClientProxy::UpdateCursorShape(
141     const protocol::CursorShapeInfo& cursor_shape) {
142   DCHECK(delegate_);
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()];
148 }
149
150 }  // namespace remoting