summaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorKonstantin Käfer <mail@kkaefer.com>2014-09-15 17:26:44 +0200
committerKonstantin Käfer <mail@kkaefer.com>2014-09-24 16:14:09 +0200
commitd9fc7708a2dfb6e2506a5d10d896a813557c056d (patch)
tree50b2dba9e0a8766c88f7c276a8f71742a06a1d67 /common
parent062e911c6d570a794431023f9f0cb0b02cd85667 (diff)
downloadqtlocation-mapboxgl-d9fc7708a2dfb6e2506a5d10d896a813557c056d.tar.gz
do 304 requests and cache them in sqlite
Diffstat (limited to 'common')
-rw-r--r--common/foundation_request.h1
-rw-r--r--common/foundation_request.mm119
2 files changed, 0 insertions, 120 deletions
diff --git a/common/foundation_request.h b/common/foundation_request.h
deleted file mode 100644
index 0b8c4b8fb0..0000000000
--- a/common/foundation_request.h
+++ /dev/null
@@ -1 +0,0 @@
-#import <Foundation/Foundation.h>
diff --git a/common/foundation_request.mm b/common/foundation_request.mm
deleted file mode 100644
index 478dadf4d9..0000000000
--- a/common/foundation_request.mm
+++ /dev/null
@@ -1,119 +0,0 @@
-#import "foundation_request.h"
-
-#include "TargetConditionals.h"
-#if TARGET_OS_IPHONE
-#import <UIKit/UIKit.h>
-#include <atomic>
-#endif
-
-#include <memory>
-#include <string>
-#include <functional>
-#include <mbgl/platform/request.hpp>
-#include <mbgl/platform/platform.hpp>
-#include <mbgl/util/std.hpp>
-#include <mbgl/util/uv.hpp>
-
-dispatch_once_t request_initialize = 0;
-NSURLSession *session = nullptr;
-
-#if TARGET_OS_IPHONE
-std::atomic<int> active_tasks;
-#endif
-
-
-// We're using a child class to make sure ARC is working correctly, as well as to add activity
-// indicators on iOS.
-class FoundationRequest : public mbgl::platform::Request {
-public:
- FoundationRequest(const std::string &url,
- std::function<void(mbgl::platform::Response *)> callback,
- std::shared_ptr<uv::loop> loop)
- : Request(url, callback, loop) {
-#if TARGET_OS_IPHONE
- active_tasks++;
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- [[UIApplication sharedApplication]
- setNetworkActivityIndicatorVisible:(active_tasks > 0)];
- });
-#endif
- }
-
- ~FoundationRequest() {
-#if TARGET_OS_IPHONE
- active_tasks--;
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- [[UIApplication sharedApplication]
- setNetworkActivityIndicatorVisible:(active_tasks > 0)];
- });
-#endif
- }
-
- NSURLSessionDataTask *task = nullptr;
-};
-
-std::shared_ptr<mbgl::platform::Request>
-mbgl::platform::request_http(const std::string &url,
- std::function<void(Response *)> callback,
- std::shared_ptr<uv::loop> loop) {
- dispatch_once(&request_initialize, ^{
- NSURLSessionConfiguration *sessionConfig =
- [NSURLSessionConfiguration defaultSessionConfiguration];
- sessionConfig.timeoutIntervalForResource = 30;
- sessionConfig.HTTPMaximumConnectionsPerHost = 8;
- sessionConfig.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
-
- session = [NSURLSession sessionWithConfiguration:sessionConfig];
-
-#if TARGET_OS_IPHONE
- active_tasks = 0;
-#endif
- });
-
- std::shared_ptr<FoundationRequest> req =
- std::make_shared<FoundationRequest>(url, callback, loop);
-
- // Note that we are creating a new shared_ptr pointer(!) to make sure there is at least one
- // shared_ptr in existence while the NSURLSession is loading our data. We are making sure in the
- // callback that this pointer gets destroyed again.
- std::shared_ptr<Request> *req_ptr = new std::shared_ptr<Request>(req);
-
- NSURLSessionDataTask *task = [session
- dataTaskWithURL:[NSURL URLWithString:@(url.c_str())]
- completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- if ([error code] == NSURLErrorCancelled) {
- // We intentionally cancelled this request. Make sure we clear the shared_ptr to resolve
- // the circular reference. We're referencing this shared_ptr by value so that the object
- // stays around until this completion handler is invoked.
- delete req_ptr;
-
- return;
- }
-
- if (!error && [response isKindOfClass:[NSHTTPURLResponse class]]) {
- NSDictionary *headers = [(NSHTTPURLResponse *)response allHeaderFields];
- (*req_ptr)->res->code = [(NSHTTPURLResponse *)response statusCode];
- (*req_ptr)->res->body = {(const char *)[data bytes], [data length]};
- (*req_ptr)->res->setCacheControl([[headers objectForKey:@"Cache-Control"] UTF8String]);
- (*req_ptr)->res->setLastModified([[headers objectForKey:@"Last-Modified"] UTF8String]);
-
- } else {
- (*req_ptr)->res->error_message = [[error localizedDescription] UTF8String];
- }
-
- (*req_ptr)->complete();
-
- delete req_ptr;
- }];
-
- req->task = task;
-
- [task resume];
- return req;
-}
-
-void mbgl::platform::cancel_request_http(const std::shared_ptr<Request> &req) {
- if (req) {
- [((FoundationRequest *)(req.get()))->task cancel];
- }
-}