summaryrefslogtreecommitdiff
path: root/src/mbgl/storage/caching_http_file_source.cpp
blob: 634a56f9c4f462e72ecb628004c7b90a982dae63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <mbgl/storage/caching_http_file_source.hpp>
#include <mbgl/storage/asset_request.hpp>
#include <mbgl/storage/http_request.hpp>
#include <mbgl/storage/sqlite_store.hpp>
#include <mbgl/storage/asset_request.hpp>
#include <mbgl/util/uv-messenger.h>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/util/std.hpp>

#include <uv.h>

namespace mbgl {

CachingHTTPFileSource::CachingHTTPFileSource(const std::string &path_)
    : path(path_) {}

CachingHTTPFileSource::~CachingHTTPFileSource() {
}

void CachingHTTPFileSource::setLoop(uv_loop_t* loop_) {
    assert(!loop);

    threadId = std::this_thread::get_id();
    store = !path.empty() ? util::ptr<SQLiteStore>(new SQLiteStore(loop_, path)) : nullptr;
    loop = loop_;
    queue = new uv_messenger_t;

    uv_messenger_init(loop, queue, [](void *ptr) {
        std::unique_ptr<std::function<void()>> fn { reinterpret_cast<std::function<void()> *>(ptr) };
        (*fn)();
    });
    uv_unref((uv_handle_t *)&queue->async);
}

bool CachingHTTPFileSource::hasLoop() {
    return loop;
}

void CachingHTTPFileSource::clearLoop() {
    assert(std::this_thread::get_id() == threadId);
    assert(loop);

    uv_messenger_stop(queue, [](uv_messenger_t *msgr) {
        delete msgr;
    });

    util::ptr<BaseRequest> req;

    // Send a cancel() message to all requests that we are still holding.
    for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) {
        if ((req = pair.second.lock())) {
            req->cancel();
        }
    }

    store.reset();

    loop = nullptr;
}

void CachingHTTPFileSource::setBase(std::string value) {
    // TODO: Make threadsafe.
    base.swap(value);
}

void CachingHTTPFileSource::setAccessToken(std::string value) {
    // TODO: Make threadsafe.
    accessToken.swap(value);
}

std::string CachingHTTPFileSource::getAccessToken() const {
    return accessToken;
}

std::unique_ptr<Request> CachingHTTPFileSource::request(ResourceType type, const std::string& url_) {
    assert(std::this_thread::get_id() == threadId);

    std::string url = url_;

    // Make URL absolute.
    const size_t separator = url.find("://");
    if (separator == std::string::npos) {
        url = base + url;
    }

    // Normalize mapbox:// URLs.
    switch (type) {
    case ResourceType::Glyphs:
        url = util::mapbox::normalizeGlyphsURL(url, accessToken);
    default:
        url = util::mapbox::normalizeSourceURL(url, accessToken);
    }

    util::ptr<BaseRequest> req;

    // First, try to find an existing Request object.
    auto it = pending.find(url);
    if (it != pending.end()) {
        req = it->second.lock();
    }

    if (!req) {
        if (url.substr(0, 8) == "asset://") {
            req = std::make_shared<AssetRequest>(url.substr(8), loop);
        } else {
            req = std::make_shared<HTTPRequest>(type, url, loop, store);
        }

        pending.emplace(url, req);
    }

    return util::make_unique<Request>(req);
}

void CachingHTTPFileSource::prepare(std::function<void()> fn) {
    if (std::this_thread::get_id() == threadId) {
        fn();
    } else {
        uv_messenger_send(queue, new std::function<void()>(std::move(fn)));
    }
}

void CachingHTTPFileSource::setReachability(bool reachable) {
    if (reachable && loop) {
        prepare([this]() {
            util::ptr<BaseRequest> req;
            for (const std::pair<std::string, std::weak_ptr<BaseRequest>> &pair : pending) {
                if ((req = pair.second.lock())) {
                    req->retryImmediately();
                }
            }
        });
    }
}

}