OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2016 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(__x86_64__) | |
6 #error "What are you doing?" | |
7 #endif | |
8 | |
9 #import <Foundation/Foundation.h> | |
10 #include "request.h" | |
11 | |
12 const NSString *omahaEndpoint = @"https://tools.google.com/service/update2"; | |
Elly Fong-Jones
2016/06/24 17:26:02
this can be local to the function where it's used
| |
13 static NSData *payload; | |
Elly Fong-Jones
2016/06/24 17:26:02
having a global for this value (which is only set
| |
14 | |
15 NSString* getArchitecture(void) { | |
16 const NXArchInfo *arch = NXGetLocalArchInfo(); | |
17 NSString *archName = [NSString stringWithUTF8String:arch->name]; | |
18 return archName; | |
19 } | |
20 | |
21 NSString *getOSVersion(void) { | |
22 NSString *versionString = [[NSProcessInfo processInfo] | |
23 operatingSystemVersionString]; | |
24 NSString *versionNumber = [[versionString componentsSeparatedByString:@" "] | |
25 objectAtIndex:1]; | |
26 return versionNumber; | |
27 } | |
28 | |
29 NSXMLDocument *createXMLRequestBody(NSString *operatingSystem /* important */, | |
30 NSString *architecture) { | |
31 NSString *protocol = @"3.0"; | |
32 NSString *keystoneVersion = @"KeystoneAgent-1.99.4.0"; | |
33 | |
34 NSString *platform = @"mac"; //important | |
35 NSString *plat_arch = [NSString stringWithFormat:@"%@_%@", operatingSystem, | |
36 architecture]; | |
37 | |
38 NSString *appid = @"com.google.Chrome"; //important | |
Elly Fong-Jones
2016/06/24 17:26:02
important how? are the other variables unimportant
| |
39 NSString *version = @"0.0.0.0"; //important | |
40 NSString *language = @"en-us"; | |
41 | |
42 NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"request"]; | |
43 [root addAttribute:[NSXMLNode attributeWithName:@"protocol" | |
44 stringValue:protocol]]; | |
45 [root addAttribute:[NSXMLNode attributeWithName:@"version" | |
46 stringValue:keystoneVersion]]; | |
47 | |
48 NSXMLElement *osChild = [[NSXMLElement alloc] initWithName:@"os"]; | |
49 [osChild addAttribute:[NSXMLNode attributeWithName:@"platform" | |
50 stringValue:platform]]; | |
51 [osChild addAttribute:[NSXMLNode attributeWithName:@"version" | |
52 stringValue:operatingSystem]]; | |
53 [osChild addAttribute:[NSXMLNode attributeWithName:@"arch" | |
54 stringValue:architecture]]; | |
55 [osChild addAttribute:[NSXMLNode attributeWithName:@"sp" | |
56 stringValue:plat_arch]]; | |
57 [root addChild:osChild]; | |
58 | |
59 NSXMLElement *appChild = [[NSXMLElement alloc] initWithName:@"app"]; | |
60 [appChild addAttribute:[NSXMLNode attributeWithName:@"appid" | |
61 stringValue:appid]]; | |
62 [appChild addAttribute:[NSXMLNode attributeWithName:@"version" | |
63 stringValue:version]]; | |
64 [appChild addAttribute:[NSXMLNode attributeWithName:@"lang" | |
65 stringValue:language]]; | |
66 [root addChild:appChild]; | |
67 | |
68 NSXMLElement *updateChildChild = [[NSXMLElement alloc] | |
69 initWithName:@"updatecheck"]; | |
70 [appChild addChild:updateChildChild]; | |
71 | |
72 NSXMLDocument *requestXMLDocument = [[NSXMLDocument alloc] | |
73 initWithRootElement:root]; | |
74 return requestXMLDocument; | |
75 } | |
76 | |
77 NSURLRequest* createOmahaRequest(NSXMLDocument *requestXMLBody) { | |
78 // create request | |
79 NSURL *requestURL = [NSURL URLWithString:(NSString*)omahaEndpoint]; | |
80 NSMutableURLRequest *request = [NSMutableURLRequest | |
81 requestWithURL:requestURL]; | |
82 | |
83 // populate metadata | |
84 request.HTTPMethod = @"POST"; | |
85 [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; | |
86 | |
87 // populate xml body | |
88 NSData *requestBody = [[requestXMLBody XMLString] | |
89 dataUsingEncoding:NSUTF8StringEncoding]; | |
90 request.HTTPBody = requestBody; | |
91 | |
92 return request; | |
93 } | |
94 | |
95 NSData* sendRequestAndReceiveResponse(NSURLRequest *request) { | |
96 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration | |
97 defaultSessionConfiguration]; | |
98 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig | |
99 delegate:nil | |
100 delegateQueue:nil]; | |
101 | |
102 NSURLSessionDataTask *omahaDataTask = [session dataTaskWithRequest:request | |
103 completionHandler:^(NSData *_Nullable data, | |
104 NSURLResponse *_Nullable response, | |
105 NSError *_Nullable error) { | |
106 if(error) { | |
107 NSLog(@"%@", error); | |
108 return; | |
109 } | |
110 | |
111 NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; | |
112 if(HTTPResponse.statusCode == 200) { | |
113 payload = [[NSData alloc] initWithData:data]; | |
114 } else { | |
115 NSLog(@"HTTP response: %ld", (unsigned long)HTTPResponse.statusCode); | |
116 return; | |
117 } | |
118 | |
119 return; | |
120 }]; | |
121 | |
122 [omahaDataTask resume]; | |
123 [session finishTasksAndInvalidate]; | |
124 while([omahaDataTask state] != NSURLSessionTaskStateCompleted) { | |
125 sleep(1); | |
Elly Fong-Jones
2016/06/24 17:26:02
This will hang the UI for the entire app (inside t
| |
126 } | |
127 | |
128 return [payload autorelease]; | |
129 } | |
OLD | NEW |