1 #pragma once
2 
3 #include <mbgl/actor/actor_ref.hpp>
4 #include <mbgl/storage/file_source.hpp>
5 #include <mbgl/storage/offline.hpp>
6 #include <mbgl/util/constants.hpp>
7 #include <mbgl/util/optional.hpp>
8 
9 #include <vector>
10 #include <mutex>
11 
12 namespace mbgl {
13 
14 namespace util {
15 template <typename T> class Thread;
16 } // namespace util
17 
18 class ResourceTransform;
19 
20 class DefaultFileSource : public FileSource {
21 public:
22     /*
23      * The maximumCacheSize parameter is a limit applied to non-offline resources only,
24      * i.e. resources added to the database for the "ambient use" caching functionality.
25      * There is no size limit for offline resources. If a user never creates any offline
26      * regions, we want the database to remain fairly small (order tens or low hundreds
27      * of megabytes).
28      */
29     DefaultFileSource(const std::string& cachePath,
30                       const std::string& assetRoot,
31                       uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE);
32     DefaultFileSource(const std::string& cachePath,
33                       std::unique_ptr<FileSource>&& assetFileSource,
34                       uint64_t maximumCacheSize = util::DEFAULT_MAX_CACHE_SIZE);
35     ~DefaultFileSource() override;
36 
supportsCacheOnlyRequests() const37     bool supportsCacheOnlyRequests() const override {
38         return true;
39     }
40 
41     void setAPIBaseURL(const std::string&);
42     std::string getAPIBaseURL();
43 
44     void setAccessToken(const std::string&);
45     std::string getAccessToken();
46 
47     void setResourceTransform(optional<ActorRef<ResourceTransform>>&&);
48 
49     std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override;
50 
51     /*
52      * Retrieve all regions in the offline database.
53      *
54      * The query will be executed asynchronously and the results passed to the given
55      * callback, which will be executed on the database thread; it is the responsibility
56      * of the SDK bindings to re-execute a user-provided callback on the main thread.
57      */
58     void listOfflineRegions(std::function<void (std::exception_ptr,
59                                                 optional<std::vector<OfflineRegion>>)>);
60 
61     /*
62      * Create an offline region in the database.
63      *
64      * When the initial database queries have completed, the provided callback will be
65      * executed on the database thread; it is the responsibility of the SDK bindings
66      * to re-execute a user-provided callback on the main thread.
67      *
68      * Note that the resulting region will be in an inactive download state; to begin
69      * downloading resources, call `setOfflineRegionDownloadState(OfflineRegionDownloadState::Active)`,
70      * optionally registering an `OfflineRegionObserver` beforehand.
71      */
72     void createOfflineRegion(const OfflineRegionDefinition& definition,
73                              const OfflineRegionMetadata& metadata,
74                              std::function<void (std::exception_ptr,
75                                                  optional<OfflineRegion>)>);
76 
77     /*
78      * Update an offline region metadata in the database.
79      */
80     void updateOfflineMetadata(const int64_t regionID,
81                                const OfflineRegionMetadata& metadata,
82                                std::function<void (std::exception_ptr,
83                                                    optional<OfflineRegionMetadata>)>);
84     /*
85      * Register an observer to be notified when the state of the region changes.
86      */
87     void setOfflineRegionObserver(OfflineRegion&, std::unique_ptr<OfflineRegionObserver>);
88 
89     /*
90      * Pause or resume downloading of regional resources.
91      */
92     void setOfflineRegionDownloadState(OfflineRegion&, OfflineRegionDownloadState);
93 
94     /*
95      * Retrieve the current status of the region. The query will be executed
96      * asynchronously and the results passed to the given callback, which will be
97      * executed on the database thread; it is the responsibility of the SDK bindings
98      * to re-execute a user-provided callback on the main thread.
99      */
100     void getOfflineRegionStatus(OfflineRegion&, std::function<void (std::exception_ptr,
101                                                                     optional<OfflineRegionStatus>)>) const;
102 
103     /*
104      * Remove an offline region from the database and perform any resources evictions
105      * necessary as a result.
106      *
107      * Eviction works by removing the least-recently requested resources not also required
108      * by other regions, until the database shrinks below a certain size.
109      *
110      * Note that this method takes ownership of the input, reflecting the fact that once
111      * region deletion is initiated, it is not legal to perform further actions with the
112      * region.
113      *
114      * When the operation is complete or encounters an error, the given callback will be
115      * executed on the database thread; it is the responsibility of the SDK bindings
116      * to re-execute a user-provided callback on the main thread.
117      */
118     void deleteOfflineRegion(OfflineRegion&&, std::function<void (std::exception_ptr)>);
119 
120     /*
121      * Changing or bypassing this limit without permission from Mapbox is prohibited
122      * by the Mapbox Terms of Service.
123      */
124     void setOfflineMapboxTileCountLimit(uint64_t) const;
125 
126     /*
127      * Pause file request activity.
128      *
129      * If pause is called then no revalidation or network request activity
130      * will occur.
131      */
132     void pause();
133 
134     /*
135      * Resume file request activity.
136      *
137      * Calling resume will unpause the file source and process any tasks that
138      * expired while the file source was paused.
139      */
140     void resume();
141 
142     // For testing only.
143     void setOnlineStatus(bool);
144     void put(const Resource&, const Response&);
145 
146     class Impl;
147 
148 private:
149     // Shared so destruction is done on this thread
150     const std::shared_ptr<FileSource> assetFileSource;
151     const std::unique_ptr<util::Thread<Impl>> impl;
152 
153     std::mutex cachedBaseURLMutex;
154     std::string cachedBaseURL = mbgl::util::API_BASE_URL;
155 
156     std::mutex cachedAccessTokenMutex;
157     std::string cachedAccessToken;
158 };
159 
160 } // namespace mbgl
161