summaryrefslogtreecommitdiff
path: root/include/mbgl/storage
diff options
context:
space:
mode:
Diffstat (limited to 'include/mbgl/storage')
-rw-r--r--include/mbgl/storage/base_request.hpp62
-rw-r--r--include/mbgl/storage/caching_http_file_source.hpp52
-rw-r--r--include/mbgl/storage/file_request.hpp27
-rw-r--r--include/mbgl/storage/file_request_baton.hpp36
-rw-r--r--include/mbgl/storage/file_source.hpp5
-rw-r--r--include/mbgl/storage/http_request.hpp58
-rw-r--r--include/mbgl/storage/http_request_baton.hpp2
-rw-r--r--include/mbgl/storage/request.hpp5
-rw-r--r--include/mbgl/storage/sqlite_store.hpp48
9 files changed, 59 insertions, 236 deletions
diff --git a/include/mbgl/storage/base_request.hpp b/include/mbgl/storage/base_request.hpp
deleted file mode 100644
index 2913c5eae5..0000000000
--- a/include/mbgl/storage/base_request.hpp
+++ /dev/null
@@ -1,62 +0,0 @@
-#ifndef MBGL_STORAGE_BASE_REQUEST
-#define MBGL_STORAGE_BASE_REQUEST
-
-#include <mbgl/storage/request_callback.hpp>
-#include <mbgl/util/ptr.hpp>
-
-#include <string>
-#include <forward_list>
-#include <functional>
-#include <thread>
-
-typedef struct uv_loop_s uv_loop_t;
-typedef struct uv_async_s uv_async_t;
-
-namespace mbgl {
-
-class Response;
-class Request;
-
-class BaseRequest {
-private:
- // Make noncopyable and immovable
- BaseRequest(const BaseRequest &) = delete;
- BaseRequest(BaseRequest &&) = delete;
- BaseRequest& operator=(const BaseRequest &) = delete;
- BaseRequest& operator=(BaseRequest &&) = delete;
-
-public:
- BaseRequest(const std::string &path);
- virtual ~BaseRequest();
-
- Callback *add(Callback &&callback, const util::ptr<BaseRequest> &request);
- void remove(Callback *callback);
-
- // Must be called by subclasses when a valid Response object is available. It will notify
- // all listeners.
- void notify();
-
- // This function is called when the request ought to be stopped. Any subclass must make sure this
- // is also called in its destructor. Calling this function repeatedly must be safe.
- // This function must call notify().
- virtual void cancel() = 0;
-
- // This function is called when the request should be reattempted immediately. This is typically
- // reaction to a network status change.
- virtual void retryImmediately();
-
-public:
- const std::thread::id thread_id;
- const std::string path;
- std::unique_ptr<Response> response;
-
-protected:
- // This object may hold a shared_ptr to itself. It does this to prevent destruction of this object
- // while a request is in progress.
- util::ptr<BaseRequest> self;
- std::forward_list<std::unique_ptr<Callback>> callbacks;
-};
-
-}
-
-#endif
diff --git a/include/mbgl/storage/caching_http_file_source.hpp b/include/mbgl/storage/caching_http_file_source.hpp
new file mode 100644
index 0000000000..a093ef2441
--- /dev/null
+++ b/include/mbgl/storage/caching_http_file_source.hpp
@@ -0,0 +1,52 @@
+#ifndef MBGL_STORAGE_CACHING_HTTP_FILE_SOURCE
+#define MBGL_STORAGE_CACHING_HTTP_FILE_SOURCE
+
+#include "file_source.hpp"
+
+#include <thread>
+#include <unordered_map>
+
+typedef struct uv_messenger_s uv_messenger_t;
+
+namespace mbgl {
+
+class BaseRequest;
+class SQLiteStore;
+
+class CachingHTTPFileSource : public FileSource {
+public:
+ CachingHTTPFileSource(const std::string &path_);
+ virtual ~CachingHTTPFileSource();
+
+ // Stores and checks the libuv loop for requests
+ void setLoop(uv_loop_t*);
+ bool hasLoop();
+
+ // Stores and retrieves the base path/URL for relative requests
+ void setBase(const std::string &value);
+ const std::string &getBase() const;
+
+ std::unique_ptr<Request> request(ResourceType type, const std::string &url);
+
+ void prepare(std::function<void()> fn);
+
+ void retryAllPending();
+
+private:
+ std::thread::id thread_id;
+
+ // Path to the cache database.
+ std::string path;
+
+ // Stores a URL that is used as a base for loading resources with relative path.
+ std::string base;
+
+ std::unordered_map<std::string, std::weak_ptr<BaseRequest>> pending;
+ util::ptr<SQLiteStore> store;
+ uv_loop_t *loop = nullptr;
+ uv_messenger_t *queue = nullptr;
+};
+
+}
+
+#endif
diff --git a/include/mbgl/storage/file_request.hpp b/include/mbgl/storage/file_request.hpp
deleted file mode 100644
index 2f883728ff..0000000000
--- a/include/mbgl/storage/file_request.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-#ifndef MBGL_STORAGE_FILE_REQUEST
-#define MBGL_STORAGE_FILE_REQUEST
-
-#include <mbgl/storage/base_request.hpp>
-
-namespace mbgl {
-
-typedef struct uv_loop_s uv_loop_t;
-
-struct FileRequestBaton;
-
-class FileRequest : public BaseRequest {
-public:
- FileRequest(const std::string &path, uv_loop_t *loop);
- ~FileRequest();
-
- void cancel();
-
-private:
- FileRequestBaton *ptr = nullptr;
-
- friend struct FileRequestBaton;
-};
-
-}
-
-#endif \ No newline at end of file
diff --git a/include/mbgl/storage/file_request_baton.hpp b/include/mbgl/storage/file_request_baton.hpp
deleted file mode 100644
index 897c88061d..0000000000
--- a/include/mbgl/storage/file_request_baton.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#ifndef MBGL_STORAGE_FILE_REQUEST_BATON
-#define MBGL_STORAGE_FILE_REQUEST_BATON
-
-#include <mbgl/storage/file_request.hpp>
-#include <thread>
-
-#include <uv.h>
-
-namespace mbgl {
-
-struct FileRequestBaton {
- FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop);
- ~FileRequestBaton();
-
- void cancel();
- static void file_opened(uv_fs_t *req);
- static void file_stated(uv_fs_t *req);
- static void file_read(uv_fs_t *req);
- static void file_closed(uv_fs_t *req);
- static void notify_error(uv_fs_t *req);
- static void cleanup(uv_fs_t *req);
-
- const std::thread::id thread_id;
- FileRequest *request = nullptr;
- uv_fs_t req;
- uv_file fd = -1;
- bool canceled = false;
- std::string body;
- uv_buf_t buffer;
-};
-
-
-}
-
-
-#endif
diff --git a/include/mbgl/storage/file_source.hpp b/include/mbgl/storage/file_source.hpp
index e825a9d61c..eeda529bf1 100644
--- a/include/mbgl/storage/file_source.hpp
+++ b/include/mbgl/storage/file_source.hpp
@@ -2,14 +2,15 @@
#define MBGL_STORAGE_FILE_SOURCE
#include <mbgl/util/noncopyable.hpp>
-#include <mbgl/storage/resource_type.hpp>
-#include <mbgl/storage/request.hpp>
+#include "resource_type.hpp"
+#include "request.hpp"
#include <string>
#include <functional>
typedef struct uv_loop_s uv_loop_t;
+
namespace mbgl {
class FileSource : public util::noncopyable {
diff --git a/include/mbgl/storage/http_request.hpp b/include/mbgl/storage/http_request.hpp
deleted file mode 100644
index 71d6e8814c..0000000000
--- a/include/mbgl/storage/http_request.hpp
+++ /dev/null
@@ -1,58 +0,0 @@
-#ifndef MBGL_STORAGE_HTTP_REQUEST
-#define MBGL_STORAGE_HTTP_REQUEST
-
-#include <mbgl/storage/resource_type.hpp>
-#include <mbgl/storage/base_request.hpp>
-#include <mbgl/storage/http_request_baton.hpp>
-
-#include <string>
-#include <memory>
-#include <cassert>
-#include <thread>
-
-typedef struct uv_loop_s uv_loop_t;
-typedef struct uv_timer_s uv_timer_t;
-
-namespace mbgl {
-
-struct CacheRequestBaton;
-struct HTTPRequestBaton;
-struct CacheEntry;
-class SQLiteStore;
-
-class HTTPRequest : public BaseRequest {
-public:
- HTTPRequest(ResourceType type, const std::string &path, uv_loop_t *loop, util::ptr<SQLiteStore> store);
- ~HTTPRequest();
-
- void cancel();
- void retryImmediately();
-
-private:
- void startCacheRequest();
- void handleCacheResponse(std::unique_ptr<Response> &&response);
- void startHTTPRequest(std::unique_ptr<Response> &&res);
- void handleHTTPResponse(HTTPResponseType responseType, std::unique_ptr<Response> &&response);
-
- void retryHTTPRequest(std::unique_ptr<Response> &&res, uint64_t timeout);
-
- void removeCacheBaton();
- void removeHTTPBaton();
- void removeBackoffTimer();
-
-private:
- const std::thread::id thread_id;
- uv_loop_t *const loop;
- CacheRequestBaton *cache_baton = nullptr;
- util::ptr<HTTPRequestBaton> http_baton;
- uv_timer_t *backoff_timer = nullptr;
- util::ptr<SQLiteStore> store;
- const ResourceType type;
- uint8_t attempts = 0;
-
- friend struct HTTPRequestBaton;
-};
-
-}
-
-#endif \ No newline at end of file
diff --git a/include/mbgl/storage/http_request_baton.hpp b/include/mbgl/storage/http_request_baton.hpp
index 545f9e236c..74c0ff498e 100644
--- a/include/mbgl/storage/http_request_baton.hpp
+++ b/include/mbgl/storage/http_request_baton.hpp
@@ -1,7 +1,7 @@
#ifndef MBGL_STORAGE_HTTP_REQUEST_BATON
#define MBGL_STORAGE_HTTP_REQUEST_BATON
-#include <mbgl/storage/response.hpp>
+#include "response.hpp"
#include <mbgl/util/ptr.hpp>
#include <string>
diff --git a/include/mbgl/storage/request.hpp b/include/mbgl/storage/request.hpp
index 11b5541d18..f63283aa2f 100644
--- a/include/mbgl/storage/request.hpp
+++ b/include/mbgl/storage/request.hpp
@@ -1,8 +1,9 @@
#ifndef MBGL_STORAGE_REQUEST
#define MBGL_STORAGE_REQUEST
-#include <mbgl/storage/request_callback.hpp>
-#include <mbgl/storage/response.hpp>
+#include "request_callback.hpp"
+#include "response.hpp"
+
#include <mbgl/util/ptr.hpp>
#include <thread>
diff --git a/include/mbgl/storage/sqlite_store.hpp b/include/mbgl/storage/sqlite_store.hpp
deleted file mode 100644
index a691b474e0..0000000000
--- a/include/mbgl/storage/sqlite_store.hpp
+++ /dev/null
@@ -1,48 +0,0 @@
-#ifndef MBGL_STORAGE_SQLITE_STORE
-#define MBGL_STORAGE_SQLITE_STORE
-
-#include <mbgl/storage/file_source.hpp>
-#include <mbgl/storage/response.hpp>
-
-#include <uv.h>
-
-#include <string>
-#include <thread>
-
-typedef struct uv_worker_s uv_worker_t;
-
-namespace mapbox {
-namespace sqlite {
-class Database;
-}
-}
-
-namespace mbgl {
-
-class SQLiteStore {
-public:
- SQLiteStore(uv_loop_t *loop, const std::string &path);
- ~SQLiteStore();
-
- typedef void (*GetCallback)(std::unique_ptr<Response> &&entry, void *ptr);
-
- void get(const std::string &path, GetCallback cb, void *ptr);
- void put(const std::string &path, ResourceType type, const Response &entry);
- void updateExpiration(const std::string &path, int64_t expires);
-
-private:
- void createSchema();
- void closeDatabase();
- static void runGet(uv_work_t *req);
- static void runPut(uv_work_t *req);
- static void deliverResult(uv_work_t *req, int status);
-
-private:
- const std::thread::id thread_id;
- util::ptr<mapbox::sqlite::Database> db;
- uv_worker_t *worker = nullptr;
-};
-
-}
-
-#endif