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 |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "base/i18n/time_formatting.h" | 23 #include "base/i18n/time_formatting.h" |
24 #include "base/logging.h" | 24 #include "base/logging.h" |
25 #include "base/strings/sys_string_conversions.h" | 25 #include "base/strings/sys_string_conversions.h" |
26 #include "net/url_request/url_request_context_getter.h" | 26 #include "net/url_request/url_request_context_getter.h" |
27 #include "remoting/base/oauth_token_getter.h" | 27 #include "remoting/base/oauth_token_getter.h" |
28 #include "remoting/base/oauth_token_getter_impl.h" | 28 #include "remoting/base/oauth_token_getter_impl.h" |
29 | 29 |
30 static NSString* const kCRDAuthenticatedUserEmailKey = | 30 static NSString* const kCRDAuthenticatedUserEmailKey = |
31 @"kCRDAuthenticatedUserEmailKey"; | 31 @"kCRDAuthenticatedUserEmailKey"; |
32 | 32 |
33 NSString* const kHostsDidUpdate = @"kHostsDidUpdate"; | 33 NSString* const kHostListStateDidChange = @"kHostListStateDidChange"; |
34 | 34 |
35 NSString* const kUserDidUpdate = @"kUserDidUpdate"; | 35 NSString* const kUserDidUpdate = @"kUserDidUpdate"; |
36 NSString* const kUserInfo = @"kUserInfo"; | 36 NSString* const kUserInfo = @"kUserInfo"; |
37 | 37 |
38 @interface RemotingService ()<RemotingAuthenticationDelegate> { | 38 @interface RemotingService ()<RemotingAuthenticationDelegate> { |
39 id<RemotingAuthentication> _authentication; | 39 id<RemotingAuthentication> _authentication; |
40 remoting::HostListFetcher* _hostListFetcher; | 40 remoting::HostListFetcher* _hostListFetcher; |
41 remoting::IosClientRuntimeDelegate* _clientRuntimeDelegate; | 41 remoting::IosClientRuntimeDelegate* _clientRuntimeDelegate; |
42 BOOL _isHostListFetching; | |
43 } | 42 } |
44 @end | 43 @end |
45 | 44 |
46 @implementation RemotingService | 45 @implementation RemotingService |
47 | 46 |
48 @synthesize hosts = _hosts; | 47 @synthesize hosts = _hosts; |
| 48 @synthesize hostListState = _hostListState; |
49 | 49 |
50 // RemotingService is a singleton. | 50 // RemotingService is a singleton. |
51 + (RemotingService*)instance { | 51 + (RemotingService*)instance { |
52 static RemotingService* sharedInstance = nil; | 52 static RemotingService* sharedInstance = nil; |
53 static dispatch_once_t guard; | 53 static dispatch_once_t guard; |
54 dispatch_once(&guard, ^{ | 54 dispatch_once(&guard, ^{ |
55 sharedInstance = [[RemotingService alloc] init]; | 55 sharedInstance = [[RemotingService alloc] init]; |
56 }); | 56 }); |
57 return sharedInstance; | 57 return sharedInstance; |
58 } | 58 } |
59 | 59 |
60 - (instancetype)init { | 60 - (instancetype)init { |
61 self = [super init]; | 61 self = [super init]; |
62 if (self) { | 62 if (self) { |
63 _hosts = nil; | 63 _hosts = nil; |
64 _hostListFetcher = nil; | 64 _hostListFetcher = nil; |
65 // TODO(yuweih): Maybe better to just cancel the previous request. | 65 // TODO(yuweih): Maybe better to just cancel the previous request. |
66 _isHostListFetching = NO; | 66 _hostListState = HostListStateNotFetched; |
67 // TODO(nicholss): This might need a pointer back to the service. | 67 // TODO(nicholss): This might need a pointer back to the service. |
68 _clientRuntimeDelegate = | 68 _clientRuntimeDelegate = |
69 new remoting::IosClientRuntimeDelegate(); | 69 new remoting::IosClientRuntimeDelegate(); |
70 [self runtime]->SetDelegate(_clientRuntimeDelegate); | 70 [self runtime]->SetDelegate(_clientRuntimeDelegate); |
71 } | 71 } |
72 return self; | 72 return self; |
73 } | 73 } |
74 | 74 |
75 #pragma mark - RemotingService Implementation | 75 #pragma mark - RemotingService Implementation |
76 | 76 |
77 - (void)startHostListFetchWith:(NSString*)accessToken { | 77 - (void)startHostListFetchWith:(NSString*)accessToken { |
78 if (_isHostListFetching) { | 78 if (_hostListState == HostListStateFetching) { |
79 return; | 79 return; |
80 } | 80 } |
81 _isHostListFetching = YES; | 81 [self setHostListState:HostListStateFetching]; |
82 if (!_hostListFetcher) { | 82 if (!_hostListFetcher) { |
83 _hostListFetcher = new remoting::HostListFetcher( | 83 _hostListFetcher = new remoting::HostListFetcher( |
84 remoting::ChromotingClientRuntime::GetInstance()->url_requester()); | 84 remoting::ChromotingClientRuntime::GetInstance()->url_requester()); |
85 } | 85 } |
86 _hostListFetcher->RetrieveHostlist( | 86 _hostListFetcher->RetrieveHostlist( |
87 base::SysNSStringToUTF8(accessToken), | 87 base::SysNSStringToUTF8(accessToken), |
88 base::BindBlockArc(^(const std::vector<remoting::HostInfo>& hostlist) { | 88 base::BindBlockArc(^(const std::vector<remoting::HostInfo>& hostlist) { |
89 NSMutableArray<HostInfo*>* hosts = | 89 NSMutableArray<HostInfo*>* hosts = |
90 [NSMutableArray arrayWithCapacity:hostlist.size()]; | 90 [NSMutableArray arrayWithCapacity:hostlist.size()]; |
91 std::string status; | 91 std::string status; |
(...skipping 19 matching lines...) Expand all Loading... |
111 base::SysUTF8ToNSString(host_info.host_os_version); | 111 base::SysUTF8ToNSString(host_info.host_os_version); |
112 host.hostVersion = base::SysUTF8ToNSString(host_info.host_version); | 112 host.hostVersion = base::SysUTF8ToNSString(host_info.host_version); |
113 host.jabberId = base::SysUTF8ToNSString(host_info.host_jid); | 113 host.jabberId = base::SysUTF8ToNSString(host_info.host_jid); |
114 host.publicKey = base::SysUTF8ToNSString(host_info.public_key); | 114 host.publicKey = base::SysUTF8ToNSString(host_info.public_key); |
115 host.status = base::SysUTF8ToNSString(status); | 115 host.status = base::SysUTF8ToNSString(status); |
116 host.updatedTime = base::SysUTF16ToNSString( | 116 host.updatedTime = base::SysUTF16ToNSString( |
117 base::TimeFormatShortDateAndTime(host_info.updated_time)); | 117 base::TimeFormatShortDateAndTime(host_info.updated_time)); |
118 [hosts addObject:host]; | 118 [hosts addObject:host]; |
119 } | 119 } |
120 _hosts = hosts; | 120 _hosts = hosts; |
121 [self hostListUpdated]; | 121 [self setHostListState:HostListStateFetched]; |
122 _isHostListFetching = NO; | |
123 })); | 122 })); |
124 } | 123 } |
125 | 124 |
126 #pragma mark - Notifications | 125 - (void)setHostListState:(HostListState)state { |
127 | 126 if (state == _hostListState) { |
128 - (void)hostListUpdated { | 127 return; |
129 [[NSNotificationCenter defaultCenter] postNotificationName:kHostsDidUpdate | 128 } |
130 object:self | 129 _hostListState = state; |
131 userInfo:nil]; | 130 [[NSNotificationCenter defaultCenter] |
| 131 postNotificationName:kHostListStateDidChange |
| 132 object:self |
| 133 userInfo:nil]; |
132 } | 134 } |
133 | 135 |
134 #pragma mark - RemotingAuthenticationDelegate | 136 #pragma mark - RemotingAuthenticationDelegate |
135 | 137 |
136 - (void)userDidUpdate:(UserInfo*)user { | 138 - (void)userDidUpdate:(UserInfo*)user { |
137 NSDictionary* userInfo = nil; | 139 NSDictionary* userInfo = nil; |
| 140 [self setHostListState:HostListStateNotFetched]; |
138 if (user) { | 141 if (user) { |
139 userInfo = [NSDictionary dictionaryWithObject:user forKey:kUserInfo]; | 142 userInfo = [NSDictionary dictionaryWithObject:user forKey:kUserInfo]; |
140 [self requestHostListFetch]; | 143 [self requestHostListFetch]; |
141 } else { | 144 } else { |
142 _hosts = nil; | 145 _hosts = nil; |
143 [self hostListUpdated]; | |
144 } | 146 } |
145 [[NSNotificationCenter defaultCenter] postNotificationName:kUserDidUpdate | 147 [[NSNotificationCenter defaultCenter] postNotificationName:kUserDidUpdate |
146 object:self | 148 object:self |
147 userInfo:userInfo]; | 149 userInfo:userInfo]; |
148 } | 150 } |
149 | 151 |
150 #pragma mark - Properties | 152 #pragma mark - Properties |
151 | 153 |
152 - (NSArray<HostInfo*>*)hosts { | 154 - (NSArray<HostInfo*>*)hosts { |
153 if ([_authentication.user isAuthenticated]) { | 155 if ([_authentication.user isAuthenticated]) { |
(...skipping 21 matching lines...) Expand all Loading... |
175 authentication.delegate = self; | 177 authentication.delegate = self; |
176 _authentication = authentication; | 178 _authentication = authentication; |
177 } | 179 } |
178 | 180 |
179 - (id<RemotingAuthentication>)authentication { | 181 - (id<RemotingAuthentication>)authentication { |
180 DCHECK(_authentication != nil); | 182 DCHECK(_authentication != nil); |
181 return _authentication; | 183 return _authentication; |
182 } | 184 } |
183 | 185 |
184 @end | 186 @end |
OLD | NEW |