blob: bf5e67ee5bd102c35b0c5b5d832915fb366eaf97 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
#import "remoting/ios/host_refresh.h"
#import "remoting/ios/authorize.h"
#import "remoting/ios/host.h"
#import "remoting/ios/utility.h"
namespace {
NSString* kDefaultErrorMessage = @"The Host list refresh is not available at "
@"this time. Please try again later.";
} // namespace
@interface HostRefresh (Private)
- (void)authentication:(GTMOAuth2Authentication*)auth
request:(NSMutableURLRequest*)request
error:(NSError*)error;
- (void)formatErrorMessage:(NSString*)error;
- (void)notifyDelegate;
@end
// Logic flow begins with refreshHostList, and continues until an error occurs,
// or the host list is returned to the delegate
@implementation HostRefresh
@synthesize jsonData = _jsonData;
@synthesize errorMessage = _errorMessage;
@synthesize delegate = _delegate;
// Override default constructor and initialize internals
- (id)init {
self = [super init];
if (self) {
_jsonData = [[NSMutableData alloc] init];
}
return self;
}
// Begin the authentication and authorization process. Begin the process by
// creating an oAuth2 request to google api's including the needed scopes to
// fetch the users host list.
- (void)refreshHostList:(GTMOAuth2Authentication*)authReq
delegate:(id<HostRefreshDelegate>)delegate {
CHECK(_delegate == nil); // Do not reuse an instance of this class
_delegate = delegate;
[Authorize beginRequest:authReq
delegate:self
didFinishSelector:@selector(authentication:request:error:)];
}
// Handle completion of the authorization process. Append service credentials
// for jabber. If an error occurred, notify user.
- (void)authentication:(NSObject*)auth
request:(NSMutableURLRequest*)request
error:(NSError*)error {
if (error != nil) {
[self formatErrorMessage:error.localizedDescription];
} else {
// Add credentials for service
[Authorize appendCredentials:request];
// Begin connection, the returned reference is not useful right now and
// marked as __unused
__unused NSURLConnection* connection =
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
}
// @protocol NSURLConnectionDelegate, handle any error during connection
- (void)connection:(NSURLConnection*)connection
didFailWithError:(NSError*)error {
[self formatErrorMessage:[error localizedDescription]];
[self notifyDelegate];
}
// @protocol NSURLConnectionDataDelegate, may be called async multiple times.
// Each call appends the new data to the known data until completed.
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data {
[_jsonData appendData:data];
}
// @protocol NSURLConnectionDataDelegate
// Ensure connection succeeded: HTTP 200 OK
- (void)connection:(NSURLConnection*)connection
didReceiveResponse:(NSURLResponse*)response {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSNumber* responseCode =
[[NSNumber alloc] initWithInteger:[httpResponse statusCode]];
if (responseCode.intValue != 200) {
[self formatErrorMessage:[NSString
stringWithFormat:@"HTTP STATUS CODE: %d",
[httpResponse statusCode]]];
}
}
}
// @protocol NSURLConnectionDataDelegate handle a completed connection, parse
// received data, and return host list to delegate
- (void)connectionDidFinishLoading:(NSURLConnection*)connection {
[self notifyDelegate];
}
// Store a formatted error message to return later
- (void)formatErrorMessage:(NSString*)error {
_errorMessage = kDefaultErrorMessage;
if (error != nil && error.length > 0) {
_errorMessage = [_errorMessage
stringByAppendingString:[@" " stringByAppendingString:error]];
}
}
// The connection has finished, call to delegate
- (void)notifyDelegate {
if (_jsonData.length == 0 && _errorMessage == nil) {
[self formatErrorMessage:nil];
}
[_delegate hostListRefresh:[Host parseListFromJSON:_jsonData]
errorMessage:_errorMessage];
}
@end
|