Index: chrome/installer/mac/app/request.m |
diff --git a/chrome/installer/mac/app/request.m b/chrome/installer/mac/app/request.m |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d51ab55bcd08ce08dfba7e992059afb23a3e1d8c |
--- /dev/null |
+++ b/chrome/installer/mac/app/request.m |
@@ -0,0 +1,129 @@ |
+// Copyright (c) 2016 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(__x86_64__) |
+#error "What are you doing?" |
+#endif |
+ |
+#import <Foundation/Foundation.h> |
+#include "request.h" |
+ |
+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
|
+static NSData *payload; |
Elly Fong-Jones
2016/06/24 17:26:02
having a global for this value (which is only set
|
+ |
+NSString* getArchitecture(void) { |
+ const NXArchInfo *arch = NXGetLocalArchInfo(); |
+ NSString *archName = [NSString stringWithUTF8String:arch->name]; |
+ return archName; |
+} |
+ |
+NSString *getOSVersion(void) { |
+ NSString *versionString = [[NSProcessInfo processInfo] |
+ operatingSystemVersionString]; |
+ NSString *versionNumber = [[versionString componentsSeparatedByString:@" "] |
+ objectAtIndex:1]; |
+ return versionNumber; |
+} |
+ |
+NSXMLDocument *createXMLRequestBody(NSString *operatingSystem /* important */, |
+ NSString *architecture) { |
+ NSString *protocol = @"3.0"; |
+ NSString *keystoneVersion = @"KeystoneAgent-1.99.4.0"; |
+ |
+ NSString *platform = @"mac"; //important |
+ NSString *plat_arch = [NSString stringWithFormat:@"%@_%@", operatingSystem, |
+ architecture]; |
+ |
+ NSString *appid = @"com.google.Chrome"; //important |
Elly Fong-Jones
2016/06/24 17:26:02
important how? are the other variables unimportant
|
+ NSString *version = @"0.0.0.0"; //important |
+ NSString *language = @"en-us"; |
+ |
+ NSXMLElement *root = [[NSXMLElement alloc] initWithName:@"request"]; |
+ [root addAttribute:[NSXMLNode attributeWithName:@"protocol" |
+ stringValue:protocol]]; |
+ [root addAttribute:[NSXMLNode attributeWithName:@"version" |
+ stringValue:keystoneVersion]]; |
+ |
+ NSXMLElement *osChild = [[NSXMLElement alloc] initWithName:@"os"]; |
+ [osChild addAttribute:[NSXMLNode attributeWithName:@"platform" |
+ stringValue:platform]]; |
+ [osChild addAttribute:[NSXMLNode attributeWithName:@"version" |
+ stringValue:operatingSystem]]; |
+ [osChild addAttribute:[NSXMLNode attributeWithName:@"arch" |
+ stringValue:architecture]]; |
+ [osChild addAttribute:[NSXMLNode attributeWithName:@"sp" |
+ stringValue:plat_arch]]; |
+ [root addChild:osChild]; |
+ |
+ NSXMLElement *appChild = [[NSXMLElement alloc] initWithName:@"app"]; |
+ [appChild addAttribute:[NSXMLNode attributeWithName:@"appid" |
+ stringValue:appid]]; |
+ [appChild addAttribute:[NSXMLNode attributeWithName:@"version" |
+ stringValue:version]]; |
+ [appChild addAttribute:[NSXMLNode attributeWithName:@"lang" |
+ stringValue:language]]; |
+ [root addChild:appChild]; |
+ |
+ NSXMLElement *updateChildChild = [[NSXMLElement alloc] |
+ initWithName:@"updatecheck"]; |
+ [appChild addChild:updateChildChild]; |
+ |
+ NSXMLDocument *requestXMLDocument = [[NSXMLDocument alloc] |
+ initWithRootElement:root]; |
+ return requestXMLDocument; |
+} |
+ |
+NSURLRequest* createOmahaRequest(NSXMLDocument *requestXMLBody) { |
+ // create request |
+ NSURL *requestURL = [NSURL URLWithString:(NSString*)omahaEndpoint]; |
+ NSMutableURLRequest *request = [NSMutableURLRequest |
+ requestWithURL:requestURL]; |
+ |
+ // populate metadata |
+ request.HTTPMethod = @"POST"; |
+ [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; |
+ |
+ // populate xml body |
+ NSData *requestBody = [[requestXMLBody XMLString] |
+ dataUsingEncoding:NSUTF8StringEncoding]; |
+ request.HTTPBody = requestBody; |
+ |
+ return request; |
+} |
+ |
+NSData* sendRequestAndReceiveResponse(NSURLRequest *request) { |
+ NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration |
+ defaultSessionConfiguration]; |
+ NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig |
+ delegate:nil |
+ delegateQueue:nil]; |
+ |
+ NSURLSessionDataTask *omahaDataTask = [session dataTaskWithRequest:request |
+ completionHandler:^(NSData *_Nullable data, |
+ NSURLResponse *_Nullable response, |
+ NSError *_Nullable error) { |
+ if(error) { |
+ NSLog(@"%@", error); |
+ return; |
+ } |
+ |
+ NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)response; |
+ if(HTTPResponse.statusCode == 200) { |
+ payload = [[NSData alloc] initWithData:data]; |
+ } else { |
+ NSLog(@"HTTP response: %ld", (unsigned long)HTTPResponse.statusCode); |
+ return; |
+ } |
+ |
+ return; |
+ }]; |
+ |
+ [omahaDataTask resume]; |
+ [session finishTasksAndInvalidate]; |
+ while([omahaDataTask state] != NSURLSessionTaskStateCompleted) { |
+ sleep(1); |
Elly Fong-Jones
2016/06/24 17:26:02
This will hang the UI for the entire app (inside t
|
+ } |
+ |
+ return [payload autorelease]; |
+} |