[all] Replace HTTPContextBase/HTTPRequestBase with FileSource
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
#include <mbgl/storage/http_context_base.hpp>
|
||||
#include <mbgl/storage/http_request_base.hpp>
|
||||
#include <mbgl/storage/http_file_source.hpp>
|
||||
#include <mbgl/storage/resource.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
@@ -7,26 +6,24 @@
|
||||
#include <mbgl/util/async_task.hpp>
|
||||
#include <mbgl/util/util.hpp>
|
||||
#include <mbgl/util/string.hpp>
|
||||
#include <mbgl/util/http_header.hpp>
|
||||
|
||||
#include <jni/jni.hpp>
|
||||
#include "attach_env.hpp"
|
||||
|
||||
namespace mbgl {
|
||||
namespace android {
|
||||
|
||||
class HTTPContext : public HTTPContextBase {
|
||||
class HTTPFileSource::Impl {
|
||||
public:
|
||||
HTTPRequestBase* createRequest(const Resource&, HTTPRequestBase::Callback) final;
|
||||
UniqueEnv env { android::AttachEnv() };
|
||||
android::UniqueEnv env { android::AttachEnv() };
|
||||
};
|
||||
|
||||
class HTTPRequest : public HTTPRequestBase {
|
||||
class HTTPRequest : public AsyncRequest {
|
||||
public:
|
||||
static constexpr auto Name() { return "com/mapbox/mapboxsdk/http/HTTPRequest"; };
|
||||
|
||||
HTTPRequest(jni::JNIEnv&, const Resource&, Callback);
|
||||
|
||||
void cancel() final;
|
||||
HTTPRequest(jni::JNIEnv&, const Resource&, FileSource::Callback);
|
||||
~HTTPRequest();
|
||||
|
||||
void onFailure(jni::JNIEnv&, int type, jni::String message);
|
||||
void onResponse(jni::JNIEnv&, int code,
|
||||
@@ -38,12 +35,16 @@ public:
|
||||
jni::UniqueObject<HTTPRequest> javaRequest;
|
||||
|
||||
private:
|
||||
void finish();
|
||||
Resource resource;
|
||||
FileSource::Callback callback;
|
||||
Response response;
|
||||
|
||||
std::unique_ptr<Response> response;
|
||||
const std::shared_ptr<const Response> existingResponse;
|
||||
|
||||
util::AsyncTask async;
|
||||
util::AsyncTask async { [this] {
|
||||
// Calling `callback` may result in deleting `this`. Copy data to temporaries first.
|
||||
auto callback_ = callback;
|
||||
auto response_ = response;
|
||||
callback_(response_);
|
||||
} };
|
||||
|
||||
static const int connectionError = 0;
|
||||
static const int temporaryError = 1;
|
||||
@@ -52,6 +53,8 @@ private:
|
||||
|
||||
jni::Class<HTTPRequest> HTTPRequest::javaClass;
|
||||
|
||||
namespace android {
|
||||
|
||||
void RegisterNativeHTTPRequest(jni::JNIEnv& env) {
|
||||
HTTPRequest::javaClass = *jni::Class<HTTPRequest>::Find(env).NewGlobalRef(env).release();
|
||||
|
||||
@@ -62,15 +65,11 @@ void RegisterNativeHTTPRequest(jni::JNIEnv& env) {
|
||||
METHOD(&HTTPRequest::onResponse, "nativeOnResponse"));
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------------------
|
||||
} // namespace android
|
||||
|
||||
HTTPRequestBase* HTTPContext::createRequest(const Resource& resource, HTTPRequestBase::Callback callback) {
|
||||
return new HTTPRequest(*env, resource, callback);
|
||||
}
|
||||
|
||||
HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, Callback callback_)
|
||||
: HTTPRequestBase(resource_, callback_),
|
||||
async([this] { finish(); }) {
|
||||
HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, FileSource::Callback callback_)
|
||||
: resource(resource_),
|
||||
callback(callback_) {
|
||||
std::string etagStr;
|
||||
std::string modifiedStr;
|
||||
|
||||
@@ -93,63 +92,53 @@ HTTPRequest::HTTPRequest(jni::JNIEnv& env, const Resource& resource_, Callback c
|
||||
jni::Make<jni::String>(env, modifiedStr)).NewGlobalRef(env);
|
||||
}
|
||||
|
||||
void HTTPRequest::cancel() {
|
||||
UniqueEnv env = android::AttachEnv();
|
||||
HTTPRequest::~HTTPRequest() {
|
||||
android::UniqueEnv env = android::AttachEnv();
|
||||
|
||||
static auto cancel = javaClass.GetMethod<void ()>(*env, "cancel");
|
||||
|
||||
javaRequest->Call(*env, cancel);
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
void HTTPRequest::finish() {
|
||||
assert(response);
|
||||
notify(*response);
|
||||
|
||||
delete this;
|
||||
}
|
||||
|
||||
void HTTPRequest::onResponse(jni::JNIEnv& env, int code,
|
||||
jni::String etag, jni::String modified, jni::String cacheControl,
|
||||
jni::String expires, jni::Array<jni::jbyte> body) {
|
||||
response = std::make_unique<Response>();
|
||||
using Error = Response::Error;
|
||||
|
||||
if (etag) {
|
||||
response->etag = jni::Make<std::string>(env, etag);
|
||||
response.etag = jni::Make<std::string>(env, etag);
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
response->modified = util::parseTimePoint(jni::Make<std::string>(env, modified).c_str());
|
||||
response.modified = util::parseTimePoint(jni::Make<std::string>(env, modified).c_str());
|
||||
}
|
||||
|
||||
if (cacheControl) {
|
||||
response->expires = parseCacheControl(jni::Make<std::string>(env, cacheControl).c_str());
|
||||
response.expires = http::CacheControl::parse(jni::Make<std::string>(env, cacheControl).c_str()).toTimePoint();
|
||||
}
|
||||
|
||||
if (expires) {
|
||||
response->expires = util::parseTimePoint(jni::Make<std::string>(env, expires).c_str());
|
||||
response.expires = util::parseTimePoint(jni::Make<std::string>(env, expires).c_str());
|
||||
}
|
||||
|
||||
if (code == 200) {
|
||||
if (body) {
|
||||
auto data = std::make_shared<std::string>(body.Length(env), char());
|
||||
jni::GetArrayRegion(env, *body, 0, data->size(), reinterpret_cast<jbyte*>(&(*data)[0]));
|
||||
response->data = data;
|
||||
response.data = data;
|
||||
} else {
|
||||
response->data = std::make_shared<std::string>();
|
||||
response.data = std::make_shared<std::string>();
|
||||
}
|
||||
} else if (code == 204 || (code == 404 && resource.kind == Resource::Kind::Tile)) {
|
||||
response->noContent = true;
|
||||
response.noContent = true;
|
||||
} else if (code == 304) {
|
||||
response->notModified = true;
|
||||
response.notModified = true;
|
||||
} else if (code == 404) {
|
||||
response->error = std::make_unique<Error>(Error::Reason::NotFound, "HTTP status code 404");
|
||||
response.error = std::make_unique<Error>(Error::Reason::NotFound, "HTTP status code 404");
|
||||
} else if (code >= 500 && code < 600) {
|
||||
response->error = std::make_unique<Error>(Error::Reason::Server, std::string{ "HTTP status code " } + std::to_string(code));
|
||||
response.error = std::make_unique<Error>(Error::Reason::Server, std::string{ "HTTP status code " } + std::to_string(code));
|
||||
} else {
|
||||
response->error = std::make_unique<Error>(Error::Reason::Other, std::string{ "HTTP status code " } + std::to_string(code));
|
||||
response.error = std::make_unique<Error>(Error::Reason::Other, std::string{ "HTTP status code " } + std::to_string(code));
|
||||
}
|
||||
|
||||
async.send();
|
||||
@@ -158,30 +147,33 @@ void HTTPRequest::onResponse(jni::JNIEnv& env, int code,
|
||||
void HTTPRequest::onFailure(jni::JNIEnv& env, int type, jni::String message) {
|
||||
std::string messageStr = jni::Make<std::string>(env, message);
|
||||
|
||||
response = std::make_unique<Response>();
|
||||
using Error = Response::Error;
|
||||
|
||||
switch (type) {
|
||||
case connectionError:
|
||||
response->error = std::make_unique<Error>(Error::Reason::Connection, messageStr);
|
||||
response.error = std::make_unique<Error>(Error::Reason::Connection, messageStr);
|
||||
break;
|
||||
case temporaryError:
|
||||
response->error = std::make_unique<Error>(Error::Reason::Server, messageStr);
|
||||
response.error = std::make_unique<Error>(Error::Reason::Server, messageStr);
|
||||
break;
|
||||
default:
|
||||
response->error = std::make_unique<Error>(Error::Reason::Other, messageStr);
|
||||
response.error = std::make_unique<Error>(Error::Reason::Other, messageStr);
|
||||
}
|
||||
|
||||
async.send();
|
||||
}
|
||||
|
||||
} // namespace android
|
||||
|
||||
std::unique_ptr<HTTPContextBase> HTTPContextBase::createContext() {
|
||||
return std::make_unique<android::HTTPContext>();
|
||||
HTTPFileSource::HTTPFileSource()
|
||||
: impl(std::make_unique<Impl>()) {
|
||||
}
|
||||
|
||||
uint32_t HTTPContextBase::maximumConcurrentRequests() {
|
||||
HTTPFileSource::~HTTPFileSource() = default;
|
||||
|
||||
std::unique_ptr<AsyncRequest> HTTPFileSource::request(const Resource& resource, Callback callback) {
|
||||
return std::make_unique<HTTPRequest>(*impl->env, resource, callback);
|
||||
}
|
||||
|
||||
uint32_t HTTPFileSource::maximumConcurrentRequests() {
|
||||
return 20;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user