summaryrefslogtreecommitdiff
path: root/src/mbgl/storage/default_file_source.cpp
blob: 9f70ac99432321954ca9fc81f601f0ddcb3fe485 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include <mbgl/storage/default_file_source.hpp>
#include <mbgl/storage/default/request.hpp>
#include <mbgl/storage/default/asset_request.hpp>
#include <mbgl/storage/default/http_request.hpp>

#include <mbgl/storage/response.hpp>
#include <mbgl/platform/platform.hpp>

#include <mbgl/util/async_queue.hpp>
#include <mbgl/util/util.hpp>

#include <mbgl/util/variant.hpp>
#include <mbgl/platform/log.hpp>

#pragma GCC diagnostic push
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#include <boost/algorithm/string.hpp>
#pragma GCC diagnostic pop

#include <thread>
#include <algorithm>
#include <cassert>


namespace algo = boost::algorithm;

namespace mbgl {

struct DefaultFileSource::ActionDispatcher {
    DefaultFileSource &fileSource;
    template <typename T> void operator()(T &t) { fileSource.process(t); }
};

struct DefaultFileSource::AddRequestAction {
    Request *const request;
};

struct DefaultFileSource::RemoveRequestAction {
    Request *const request;
};

struct DefaultFileSource::ResultAction {
    const Resource resource;
    std::unique_ptr<Response> response;
};

struct DefaultFileSource::StopAction {
};

struct DefaultFileSource::AbortAction {
    const Environment &env;
};


DefaultFileSource::DefaultFileSource(FileCache *cache_, const std::string &root)
    : assetRoot(root.empty() ? platform::assetRoot() : root),
      loop(uv_loop_new()),
      cache(cache_),
      queue(new Queue(loop, [this](Action &action) {
          mapbox::util::apply_visitor(ActionDispatcher{*this}, action);
      })),
      thread([this]() {
#ifdef __APPLE__
          pthread_setname_np("FileSource");
#endif
          uv_run(loop, UV_RUN_DEFAULT);
      }) {
}

DefaultFileSource::DefaultFileSource(FileCache *cache_, uv_loop_t *loop_, const std::string &root)
    : assetRoot(root.empty() ? platform::assetRoot() : root),
      loop(loop_),
      cache(cache_),
      queue(new Queue(loop, [this](Action &action) {
          mapbox::util::apply_visitor(ActionDispatcher{*this}, action);
      })) {
    // Make sure that the queue doesn't block the loop from exiting.
    queue->unref();
}

DefaultFileSource::~DefaultFileSource() {
    MBGL_VERIFY_THREAD(tid);

    if (thread.joinable()) {
        if (queue) {
            queue->send(StopAction{ });
        }
        thread.join();
        uv_loop_delete(loop);
    } else {
        // Assume that the loop we received is running in the current thread.
        StopAction action {};
        process(action);
    }
}

SharedRequestBase *DefaultFileSource::find(const Resource &resource) {
    // We're using a set of pointers here instead of a map between url and SharedRequestBase because
    // we need to find the requests both by pointer and by URL. Given that the number of requests
    // is generally very small (typically < 10 at a time), hashing by URL incurs too much overhead
    // anyway.
    const auto it = pending.find(resource);
    if (it != pending.end()) {
        return it->second;
    }
    return nullptr;
}

Request *DefaultFileSource::request(const Resource &resource, uv_loop_t *l, const Environment &env,
                                    Callback callback) {
    auto req = new Request(resource, l, env, std::move(callback));

    // This function can be called from any thread. Make sure we're executing the actual call in the
    // file source loop by sending it over the queue. It will be processed in processAction().
    queue->send(AddRequestAction{ req });
    return req;
}

void DefaultFileSource::request(const Resource &resource, const Environment &env,
                                Callback callback) {
    auto req = new Request(resource, nullptr, env, std::move(callback));

    // This function can be called from any thread. Make sure we're executing the actual call in the
    // file source loop by sending it over the queue. It will be processed in processAction().
    queue->send(AddRequestAction{ req });
}

void DefaultFileSource::cancel(Request *req) {
    req->cancel();

    // This function can be called from any thread. Make sure we're executing the actual call in the
    // file source loop by sending it over the queue. It will be processed in processAction().
    queue->send(RemoveRequestAction{ req });
}

void DefaultFileSource::abort(const Environment &env) {
    queue->send(AbortAction{ env });
}


void DefaultFileSource::process(AddRequestAction &action) {
    const Resource &resource = action.request->resource;

    // We're adding a new Request.
    SharedRequestBase *sharedRequest = find(resource);
    if (!sharedRequest) {
        // There is no request for this URL yet. Create a new one and start it.
        if (algo::starts_with(resource.url, "asset://")) {
            sharedRequest = new AssetRequest(this, resource);
        } else {
            sharedRequest = new HTTPRequest(this, resource);
        }

        // Make sure the loop stays alive when we're not running the file source in it's own thread.
        if (!thread.joinable() && pending.empty()) {
            queue->ref();
        }

        const bool inserted = pending.emplace(resource, sharedRequest).second;
        assert(inserted);
        (void (inserted)); // silence unused variable warning on Release builds.

        // But first, we're going to start querying the database if it exists.
        if (!cache) {
            sharedRequest->start(loop);
        } else {
            // Otherwise, first check the cache for existing data so that we can potentially
            // revalidate the information without having to redownload everything.
            cache->get(resource, [this, resource](std::unique_ptr<Response> response) {
                queue->send(ResultAction { resource, std::move(response) });
            });
        }
    }
    sharedRequest->subscribe(action.request);
}

void DefaultFileSource::process(RemoveRequestAction &action) {
    SharedRequestBase *sharedRequest = find(action.request->resource);
    if (sharedRequest) {
        // If the number of dependent requests of the SharedRequestBase drops to zero, the
        // unsubscribe callback triggers the removal of the SharedRequestBase pointer from the list
        // of pending requests and initiates cancelation.
        sharedRequest->unsubscribe(action.request);
    } else {
        // There is no request for this URL anymore. Likely, the request already completed
        // before we got around to process the cancelation request.
    }

    // Send a message back to the requesting thread and notify it that this request has been
    // canceled and is now safe to be deleted.
    action.request->destruct();
}

void DefaultFileSource::process(ResultAction &action) {
    SharedRequestBase *sharedRequest = find(action.resource);
    if (sharedRequest) {
        if (action.response) {
            // This entry was stored in the cache. Now determine if we need to revalidate.
            const int64_t now = std::chrono::duration_cast<std::chrono::seconds>(
                                    std::chrono::system_clock::now().time_since_epoch()).count();
            if (action.response->expires > now) {
                // The response is fresh. We're good to notify the caller.
                sharedRequest->notify(std::move(action.response), FileCache::Hint::No);
                sharedRequest->cancel();
                return;
            } else {
                // The cached response is stale. Now run the real request.
                sharedRequest->start(loop, std::move(action.response));
            }
        } else {
            // There is no response. Now run the real request.
            sharedRequest->start(loop);
        }
    } else {
        // There is no request for this URL anymore. Likely, the request was canceled
        // before we got around to process the cache result.
    }
}

// A stop action means the file source is about to be destructed. We need to cancel all requests
// for all environments.
void DefaultFileSource::process(StopAction &) {
    // There may not be any pending requests in this file source anymore. You must terminate all
    // Map objects before deleting the FileSource.
    assert(pending.empty());
    assert(queue);
    queue->stop();
    queue = nullptr;
}

// Aborts all requests that are part of the current environment.
void DefaultFileSource::process(AbortAction &action) {
    // Construct a cancellation response.
    auto res = util::make_unique<Response>();
    res->status = Response::Error;
    res->message = "Environment is terminating";
    std::shared_ptr<const Response> response = std::move(res);

    // Iterate through all pending requests and remove them in case they're abandoned.
    util::erase_if(pending, [&](const std::pair<Resource, SharedRequestBase *> &it) -> bool {
        // Obtain all pending requests that are in the current environment.
        const auto aborted = it.second->removeAllInEnvironment(action.env);

        // Notify all observers.
        for (auto req : aborted) {
            req->notify(response);
        }

        // Finally, remove all requests that are now abandoned.
        if (it.second->abandoned()) {
            it.second->cancel();
            return true;
        } else {
            return false;
        }
    });
}

void DefaultFileSource::notify(SharedRequestBase *sharedRequest,
                               const std::set<Request *> &observers,
                               std::shared_ptr<const Response> response, FileCache::Hint hint) {
    // First, remove the request, since it might be destructed at any point now.
    assert(find(sharedRequest->resource) == sharedRequest);
    pending.erase(sharedRequest->resource);

    if (response) {
        if (cache) {
            // Store response in database
            cache->put(sharedRequest->resource, response, hint);
        }

        // Notify all observers.
        for (auto req : observers) {
            req->notify(response);
        }
    }

    if (!thread.joinable() && pending.empty()) {
        // When there are no pending requests, we're going to allow the queue to stop.
        queue->unref();
    }
}

}