OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #if !defined(__has_feature) || !__has_feature(objc_arc) | 5 #if !defined(__has_feature) || !__has_feature(objc_arc) |
6 #error "This file requires ARC support." | 6 #error "This file requires ARC support." |
7 #endif | 7 #endif |
8 | 8 |
9 #import "remoting/ios/facade/remoting_service.h" | 9 #import "remoting/ios/facade/remoting_service.h" |
10 | 10 |
11 #import <Foundation/Foundation.h> | 11 #import <Foundation/Foundation.h> |
12 #import <Security/Security.h> | 12 #import <Security/Security.h> |
13 | 13 |
14 #import "base/mac/bind_objc_block.h" | 14 #import "base/mac/bind_objc_block.h" |
15 #import "remoting/ios/domain/host_info.h" | 15 #import "remoting/ios/domain/host_info.h" |
16 #import "remoting/ios/domain/user_info.h" | 16 #import "remoting/ios/domain/user_info.h" |
17 #import "remoting/ios/facade/host_info.h" | 17 #import "remoting/ios/facade/host_info.h" |
18 #import "remoting/ios/facade/host_list_fetcher.h" | 18 #import "remoting/ios/facade/host_list_fetcher.h" |
19 #import "remoting/ios/facade/ios_client_runtime_delegate.h" | 19 #import "remoting/ios/facade/ios_client_runtime_delegate.h" |
20 #import "remoting/ios/facade/remoting_authentication.h" | 20 #import "remoting/ios/facade/remoting_authentication.h" |
21 #import "remoting/ios/facade/remoting_service.h" | |
22 #import "remoting/ios/keychain_wrapper.h" | 21 #import "remoting/ios/keychain_wrapper.h" |
23 | 22 |
24 #include "base/i18n/time_formatting.h" | 23 #include "base/i18n/time_formatting.h" |
25 #include "base/logging.h" | 24 #include "base/logging.h" |
26 #include "base/strings/sys_string_conversions.h" | 25 #include "base/strings/sys_string_conversions.h" |
27 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
28 #include "remoting/base/oauth_token_getter.h" | 27 #include "remoting/base/oauth_token_getter.h" |
29 #include "remoting/base/oauth_token_getter_impl.h" | 28 #include "remoting/base/oauth_token_getter_impl.h" |
30 | 29 |
31 static NSString* const kCRDAuthenticatedUserEmailKey = | 30 static NSString* const kCRDAuthenticatedUserEmailKey = |
32 @"kCRDAuthenticatedUserEmailKey"; | 31 @"kCRDAuthenticatedUserEmailKey"; |
33 | 32 |
34 NSString* const kHostsDidUpdate = @"kHostsDidUpdate"; | 33 NSString* const kHostsDidUpdate = @"kHostsDidUpdate"; |
35 | 34 |
36 NSString* const kUserDidUpdate = @"kUserDidUpdate"; | 35 NSString* const kUserDidUpdate = @"kUserDidUpdate"; |
37 NSString* const kUserInfo = @"kUserInfo"; | 36 NSString* const kUserInfo = @"kUserInfo"; |
38 | 37 |
39 @interface RemotingService ()<RemotingAuthenticationDelegate> { | 38 @interface RemotingService ()<RemotingAuthenticationDelegate> { |
40 std::unique_ptr<remoting::OAuthTokenGetter> _tokenGetter; | 39 id<RemotingAuthentication> _authentication; |
41 remoting::HostListFetcher* _hostListFetcher; | 40 remoting::HostListFetcher* _hostListFetcher; |
42 remoting::IosClientRuntimeDelegate* _clientRuntimeDelegate; | 41 remoting::IosClientRuntimeDelegate* _clientRuntimeDelegate; |
| 42 BOOL _isHostListFetching; |
43 } | 43 } |
44 @end | 44 @end |
45 | 45 |
46 @implementation RemotingService | 46 @implementation RemotingService |
47 | 47 |
48 @synthesize authentication = _authentication; | |
49 @synthesize hosts = _hosts; | 48 @synthesize hosts = _hosts; |
50 | 49 |
51 // RemotingService is a singleton. | 50 // RemotingService is a singleton. |
52 + (RemotingService*)SharedInstance { | 51 + (RemotingService*)instance { |
53 static RemotingService* sharedInstance = nil; | 52 static RemotingService* sharedInstance = nil; |
54 static dispatch_once_t guard; | 53 static dispatch_once_t guard; |
55 dispatch_once(&guard, ^{ | 54 dispatch_once(&guard, ^{ |
56 sharedInstance = [[RemotingService alloc] init]; | 55 sharedInstance = [[RemotingService alloc] init]; |
57 }); | 56 }); |
58 return sharedInstance; | 57 return sharedInstance; |
59 } | 58 } |
60 | 59 |
61 - (instancetype)init { | 60 - (instancetype)init { |
62 self = [super init]; | 61 self = [super init]; |
63 if (self) { | 62 if (self) { |
64 _authentication = [[RemotingAuthentication alloc] init]; | |
65 _authentication.delegate = self; | |
66 _hosts = nil; | 63 _hosts = nil; |
67 _hostListFetcher = nil; | 64 _hostListFetcher = nil; |
| 65 // TODO(yuweih): Maybe better to just cancel the previous request. |
| 66 _isHostListFetching = NO; |
68 // TODO(nicholss): This might need a pointer back to the service. | 67 // TODO(nicholss): This might need a pointer back to the service. |
69 _clientRuntimeDelegate = | 68 _clientRuntimeDelegate = |
70 new remoting::IosClientRuntimeDelegate(); | 69 new remoting::IosClientRuntimeDelegate(); |
71 [self runtime]->SetDelegate(_clientRuntimeDelegate); | 70 [self runtime]->SetDelegate(_clientRuntimeDelegate); |
72 } | 71 } |
73 return self; | 72 return self; |
74 } | 73 } |
75 | 74 |
76 #pragma mark - RemotingService Implementation | 75 #pragma mark - RemotingService Implementation |
77 | 76 |
78 - (void)startHostListFetchWith:(NSString*)accessToken { | 77 - (void)startHostListFetchWith:(NSString*)accessToken { |
| 78 if (_isHostListFetching) { |
| 79 return; |
| 80 } |
| 81 _isHostListFetching = YES; |
79 if (!_hostListFetcher) { | 82 if (!_hostListFetcher) { |
80 _hostListFetcher = new remoting::HostListFetcher( | 83 _hostListFetcher = new remoting::HostListFetcher( |
81 remoting::ChromotingClientRuntime::GetInstance()->url_requester()); | 84 remoting::ChromotingClientRuntime::GetInstance()->url_requester()); |
82 } | 85 } |
83 _hostListFetcher->RetrieveHostlist( | 86 _hostListFetcher->RetrieveHostlist( |
84 base::SysNSStringToUTF8(accessToken), | 87 base::SysNSStringToUTF8(accessToken), |
85 base::BindBlockArc(^(const std::vector<remoting::HostInfo>& hostlist) { | 88 base::BindBlockArc(^(const std::vector<remoting::HostInfo>& hostlist) { |
86 NSMutableArray<HostInfo*>* hosts = | 89 NSMutableArray<HostInfo*>* hosts = |
87 [NSMutableArray arrayWithCapacity:hostlist.size()]; | 90 [NSMutableArray arrayWithCapacity:hostlist.size()]; |
88 std::string status; | 91 std::string status; |
(...skipping 20 matching lines...) Expand all Loading... |
109 host.hostVersion = base::SysUTF8ToNSString(host_info.host_version); | 112 host.hostVersion = base::SysUTF8ToNSString(host_info.host_version); |
110 host.jabberId = base::SysUTF8ToNSString(host_info.host_jid); | 113 host.jabberId = base::SysUTF8ToNSString(host_info.host_jid); |
111 host.publicKey = base::SysUTF8ToNSString(host_info.public_key); | 114 host.publicKey = base::SysUTF8ToNSString(host_info.public_key); |
112 host.status = base::SysUTF8ToNSString(status); | 115 host.status = base::SysUTF8ToNSString(status); |
113 host.updatedTime = base::SysUTF16ToNSString( | 116 host.updatedTime = base::SysUTF16ToNSString( |
114 base::TimeFormatShortDateAndTime(host_info.updated_time)); | 117 base::TimeFormatShortDateAndTime(host_info.updated_time)); |
115 [hosts addObject:host]; | 118 [hosts addObject:host]; |
116 } | 119 } |
117 _hosts = hosts; | 120 _hosts = hosts; |
118 [self hostListUpdated]; | 121 [self hostListUpdated]; |
| 122 _isHostListFetching = NO; |
119 })); | 123 })); |
120 } | 124 } |
121 | 125 |
122 #pragma mark - Notifications | 126 #pragma mark - Notifications |
123 | 127 |
124 - (void)hostListUpdated { | 128 - (void)hostListUpdated { |
125 [[NSNotificationCenter defaultCenter] postNotificationName:kHostsDidUpdate | 129 [[NSNotificationCenter defaultCenter] postNotificationName:kHostsDidUpdate |
126 object:self | 130 object:self |
127 userInfo:nil]; | 131 userInfo:nil]; |
128 } | 132 } |
129 | 133 |
130 #pragma mark - RemotingAuthenticationDelegate | 134 #pragma mark - RemotingAuthenticationDelegate |
131 | 135 |
132 - (void)userDidUpdate:(UserInfo*)user { | 136 - (void)userDidUpdate:(UserInfo*)user { |
133 NSDictionary* userInfo = nil; | 137 NSDictionary* userInfo = nil; |
134 if (user) { | 138 if (user) { |
135 userInfo = [NSDictionary dictionaryWithObject:user forKey:kUserInfo]; | 139 userInfo = [NSDictionary dictionaryWithObject:user forKey:kUserInfo]; |
| 140 [self requestHostListFetch]; |
136 } else { | 141 } else { |
137 _hosts = nil; | 142 _hosts = nil; |
138 [self hostListUpdated]; | 143 [self hostListUpdated]; |
139 } | 144 } |
140 [[NSNotificationCenter defaultCenter] postNotificationName:kUserDidUpdate | 145 [[NSNotificationCenter defaultCenter] postNotificationName:kUserDidUpdate |
141 object:self | 146 object:self |
142 userInfo:userInfo]; | 147 userInfo:userInfo]; |
143 } | 148 } |
144 | 149 |
145 #pragma mark - Properties | 150 #pragma mark - Properties |
146 | 151 |
147 - (NSArray<HostInfo*>*)hosts { | 152 - (NSArray<HostInfo*>*)hosts { |
148 if ([_authentication.user isAuthenticated]) { | 153 if ([_authentication.user isAuthenticated]) { |
149 return _hosts; | 154 return _hosts; |
150 } | 155 } |
151 return nil; | 156 return nil; |
152 } | 157 } |
153 | 158 |
154 - (remoting::ChromotingClientRuntime*)runtime { | 159 - (remoting::ChromotingClientRuntime*)runtime { |
155 return remoting::ChromotingClientRuntime::GetInstance(); | 160 return remoting::ChromotingClientRuntime::GetInstance(); |
156 } | 161 } |
157 | 162 |
158 #pragma mark - Implementation | 163 #pragma mark - Implementation |
159 | 164 |
160 - (void)requestHostListFetch { | 165 - (void)requestHostListFetch { |
161 [_authentication | 166 [_authentication |
162 callbackWithAccessToken:base::BindBlockArc(^( | 167 callbackWithAccessToken:^(RemotingAuthenticationStatus status, |
163 remoting::OAuthTokenGetter::Status status, | 168 NSString* userEmail, NSString* accessToken) { |
164 const std::string& user_email, | |
165 const std::string& access_token) { | |
166 NSString* accessToken = base::SysUTF8ToNSString(access_token); | |
167 [self startHostListFetchWith:accessToken]; | 169 [self startHostListFetchWith:accessToken]; |
168 })]; | 170 }]; |
| 171 } |
| 172 |
| 173 - (void)setAuthentication:(id<RemotingAuthentication>)authentication { |
| 174 DCHECK(_authentication == nil); |
| 175 authentication.delegate = self; |
| 176 _authentication = authentication; |
| 177 } |
| 178 |
| 179 - (id<RemotingAuthentication>)authentication { |
| 180 DCHECK(_authentication != nil); |
| 181 return _authentication; |
169 } | 182 } |
170 | 183 |
171 @end | 184 @end |
OLD | NEW |