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/facade/remoting_service.h" | |
10 | |
11 #import <Foundation/Foundation.h> | |
12 #import <Security/Security.h> | |
13 | |
14 #import "base/mac/bind_objc_block.h" | |
15 #import "remoting/client/ios/domain/host_info.h" | |
16 #import "remoting/client/ios/domain/user_info.h" | |
17 #import "remoting/client/ios/facade/host_info.h" | |
18 #import "remoting/client/ios/facade/host_list_fetcher.h" | |
19 #import "remoting/client/ios/facade/ios_client_runtime_delegate.h" | |
20 #import "remoting/client/ios/facade/remoting_authentication.h" | |
21 #import "remoting/client/ios/facade/remoting_service.h" | |
22 #import "remoting/client/ios/keychain_wrapper.h" | |
23 | |
24 #include "base/logging.h" | |
25 #include "base/strings/sys_string_conversions.h" | |
26 #include "net/url_request/url_request_context_getter.h" | |
27 #include "remoting/base/oauth_token_getter.h" | |
28 #include "remoting/base/oauth_token_getter_impl.h" | |
29 | |
30 static NSString* const kCRDAuthenticatedUserEmailKey = | |
31 @"kCRDAuthenticatedUserEmailKey"; | |
32 | |
33 NSString* const kHostsDidUpdate = @"kHostsDidUpdate"; | |
34 | |
35 NSString* const kUserDidUpdate = @"kUserDidUpdate"; | |
36 NSString* const kUserInfo = @"kUserInfo"; | |
37 | |
38 @interface RemotingService ()<RemotingAuthenticationDelegate> { | |
39 std::unique_ptr<remoting::OAuthTokenGetter> _tokenGetter; | |
40 remoting::HostListFetcher* _hostListFetcher; | |
41 remoting::IosClientRuntimeDelegate* _clientRuntimeDelegate; | |
42 } | |
43 @end | |
44 | |
45 @implementation RemotingService | |
46 | |
47 @synthesize authentication = _authentication; | |
48 @synthesize hosts = _hosts; | |
49 | |
50 // RemotingService is a singleton. | |
51 + (RemotingService*)SharedInstance { | |
52 static RemotingService* sharedInstance = nil; | |
53 static dispatch_once_t guard; | |
54 dispatch_once(&guard, ^{ | |
55 sharedInstance = [[RemotingService alloc] init]; | |
56 }); | |
57 return sharedInstance; | |
58 } | |
59 | |
60 - (instancetype)init { | |
61 self = [super init]; | |
62 if (self) { | |
63 _authentication = [[RemotingAuthentication alloc] init]; | |
64 _authentication.delegate = self; | |
65 _hosts = nil; | |
66 _hostListFetcher = nil; | |
67 // TODO(nicholss): This might need a pointer back to the service. | |
68 _clientRuntimeDelegate = | |
69 new remoting::IosClientRuntimeDelegate(); | |
70 [self runtime]->SetDelegate(_clientRuntimeDelegate); | |
71 } | |
72 return self; | |
73 } | |
74 | |
75 #pragma mark - RemotingService Implementation | |
76 | |
77 - (void)startHostListFetchWith:(NSString*)accessToken { | |
78 if (!_hostListFetcher) { | |
79 _hostListFetcher = new remoting::HostListFetcher( | |
80 remoting::ChromotingClientRuntime::GetInstance()->url_requester()); | |
81 } | |
82 _hostListFetcher->RetrieveHostlist( | |
83 base::SysNSStringToUTF8(accessToken), | |
84 base::BindBlockArc(^(const std::vector<remoting::HostInfo>& hostlist) { | |
85 NSMutableArray<HostInfo*>* hosts = | |
86 [NSMutableArray arrayWithCapacity:hostlist.size()]; | |
87 std::string status; | |
88 for (const remoting::HostInfo& host_info : hostlist) { | |
89 remoting::HostStatus host_status = host_info.status; | |
90 switch (host_status) { | |
91 case remoting::kHostStatusOnline: | |
92 status = "ONLINE"; | |
93 break; | |
94 case remoting::kHostStatusOffline: | |
95 status = "OFFLINE"; | |
96 break; | |
97 default: | |
98 NOTREACHED(); | |
99 } | |
100 // TODO(nicholss): Not yet integrated: createdTime, hostVersion, | |
101 // kind, offlineReason. Add them as the app will need this info. | |
102 HostInfo* host = [[HostInfo alloc] init]; | |
103 host.hostId = | |
104 [NSString stringWithCString:host_info.host_id.c_str() | |
105 encoding:[NSString defaultCStringEncoding]]; | |
106 host.hostName = | |
107 [NSString stringWithCString:host_info.host_name.c_str() | |
108 encoding:[NSString defaultCStringEncoding]]; | |
109 host.jabberId = | |
110 [NSString stringWithCString:host_info.host_jid.c_str() | |
111 encoding:[NSString defaultCStringEncoding]]; | |
112 host.publicKey = | |
113 [NSString stringWithCString:host_info.public_key.c_str() | |
114 encoding:[NSString defaultCStringEncoding]]; | |
115 host.status = | |
116 [NSString stringWithCString:status.c_str() | |
117 encoding:[NSString defaultCStringEncoding]]; | |
118 [hosts addObject:host]; | |
119 } | |
120 _hosts = hosts; | |
121 [self hostListUpdated]; | |
122 })); | |
123 } | |
124 | |
125 #pragma mark - Notifications | |
126 | |
127 - (void)hostListUpdated { | |
128 [[NSNotificationCenter defaultCenter] postNotificationName:kHostsDidUpdate | |
129 object:self | |
130 userInfo:nil]; | |
131 } | |
132 | |
133 #pragma mark - RemotingAuthenticationDelegate | |
134 | |
135 - (void)userDidUpdate:(UserInfo*)user { | |
136 NSDictionary* userInfo = nil; | |
137 if (user) { | |
138 userInfo = [NSDictionary dictionaryWithObject:user forKey:kUserInfo]; | |
139 } else { | |
140 _hosts = nil; | |
141 [self hostListUpdated]; | |
142 } | |
143 [[NSNotificationCenter defaultCenter] postNotificationName:kUserDidUpdate | |
144 object:self | |
145 userInfo:userInfo]; | |
146 } | |
147 | |
148 #pragma mark - Properties | |
149 | |
150 - (NSArray<HostInfo*>*)hosts { | |
151 if ([_authentication.user isAuthenticated]) { | |
152 return _hosts; | |
153 } | |
154 return nil; | |
155 } | |
156 | |
157 - (remoting::ChromotingClientRuntime*)runtime { | |
158 return remoting::ChromotingClientRuntime::GetInstance(); | |
159 } | |
160 | |
161 #pragma mark - Implementation | |
162 | |
163 - (void)requestHostListFetch { | |
164 [_authentication | |
165 callbackWithAccessToken:base::BindBlockArc(^( | |
166 remoting::OAuthTokenGetter::Status status, | |
167 const std::string& user_email, | |
168 const std::string& access_token) { | |
169 NSString* accessToken = | |
170 [NSString stringWithCString:access_token.c_str() | |
171 encoding:[NSString defaultCStringEncoding]]; | |
172 [self startHostListFetchWith:accessToken]; | |
173 })]; | |
174 } | |
175 | |
176 @end | |
OLD | NEW |