summaryrefslogtreecommitdiff
path: root/platform/default/asset_file_source.cpp
blob: 9ca002039b203dfb68a3a78ccfe7bbc99cc317a3 (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
#include <mbgl/storage/asset_file_source.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/io.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/string.hpp>
#include <mbgl/util/url.hpp>
#include <mbgl/util/util.hpp>

#include <mbgl/actor/actor.hpp>
#include <mbgl/actor/actor_ref.hpp>

#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

namespace mbgl {

class AssetFileRequest : public AsyncRequest {
public:
    AssetFileRequest(std::shared_ptr<FileSource::Callback> callback_)
        : callback(std::move(callback_)) {
    }

private:
    std::shared_ptr<FileSource::Callback> callback;
};

class AssetFileSource::Worker {
public:
    Worker(ActorRef<Worker>, ActorRef<AssetFileSource> parent_, std::string root_)
        : parent(std::move(parent_)), root(std::move(root_)) {
    }
    void readFile(const std::string& url, std::weak_ptr<Callback> callback) {
        std::string path;

        if (url.size() <= 8 || url[8] == '/') {
            // This is an empty or absolute path.
            path = mbgl::util::percentDecode(url.substr(8));
        } else {
            // This is a relative path. Prefix with the application root.
            path = root + "/" + mbgl::util::percentDecode(url.substr(8));
        }

        Response response;

        struct stat buf;
        int result = stat(path.c_str(), &buf);

        if (result == 0 && S_ISDIR(buf.st_mode)) {
            response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound);
        } else if (result == -1 && errno == ENOENT) {
            response.error = std::make_unique<Response::Error>(Response::Error::Reason::NotFound);
        } else {
            try {
                response.data = std::make_shared<std::string>(util::read_file(path));
            } catch (...) {
                response.error = std::make_unique<Response::Error>(
                    Response::Error::Reason::Other, util::toString(std::current_exception()));
            }
        }

        parent.invoke(&AssetFileSource::respond, callback, response);
    }

private:
    ActorRef<AssetFileSource> parent;
    const std::string root;
};

AssetFileSource::AssetFileSource(Scheduler& scheduler, const std::string& root)
    : mailbox(std::make_shared<Mailbox>(*util::RunLoop::Get())),
      worker(std::make_unique<Actor<Worker>>(
          scheduler, ActorRef<AssetFileSource>(*this, mailbox), root)) {
}

AssetFileSource::~AssetFileSource() = default;

std::unique_ptr<AsyncRequest> AssetFileSource::request(const Resource& resource,
                                                       Callback callback) {
    auto cb = std::make_shared<Callback>(std::move(callback));
    worker->invoke(&Worker::readFile, resource.url, cb);
    return std::make_unique<AssetFileRequest>(std::move(cb));
}

void AssetFileSource::respond(std::weak_ptr<Callback> callback, Response response) {
    if (auto locked = callback.lock()) {
        (*locked)(response);
    }
}

} // namespace mbgl