summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
Diffstat (limited to 'include')
-rw-r--r--include/mbgl/storage/resource.hpp16
-rw-r--r--include/mbgl/storage/response.hpp3
-rw-r--r--include/mbgl/style/style.hpp5
-rw-r--r--include/mbgl/util/blob.hpp34
-rw-r--r--include/mbgl/util/compression.hpp7
-rw-r--r--include/mbgl/util/image.hpp6
6 files changed, 63 insertions, 8 deletions
diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp
index 318fa389f4..d9da494e4d 100644
--- a/include/mbgl/storage/resource.hpp
+++ b/include/mbgl/storage/resource.hpp
@@ -42,12 +42,23 @@ public:
All = Cache | Network,
};
+ enum class Compression : bool {
+ // The data will be an uncompressed blob, even if it was obtained in compressed form.
+ Uncompressed = false,
+
+ // The data will be returned compressed if it was already obtained in compressed form,
+ // and uncompressed otherwise.
+ PreferCompressed = true,
+ };
+
Resource(Kind kind_,
std::string url_,
optional<TileData> tileData_ = {},
- LoadingMethod loadingMethod_ = LoadingMethod::All)
+ LoadingMethod loadingMethod_ = LoadingMethod::All,
+ Compression compression_ = Compression::PreferCompressed)
: kind(kind_),
loadingMethod(loadingMethod_),
+ compression(compression_),
url(std::move(url_)),
tileData(std::move(tileData_)) {
}
@@ -72,6 +83,7 @@ public:
Kind kind;
LoadingMethod loadingMethod;
+ Compression compression;
std::string url;
// Includes auxiliary data if this is a tile request.
@@ -80,7 +92,7 @@ public:
optional<Timestamp> priorModified = {};
optional<Timestamp> priorExpires = {};
optional<std::string> priorEtag = {};
- std::shared_ptr<const std::string> priorData;
+ Blob priorData;
};
diff --git a/include/mbgl/storage/response.hpp b/include/mbgl/storage/response.hpp
index 508400141b..81a84effb7 100644
--- a/include/mbgl/storage/response.hpp
+++ b/include/mbgl/storage/response.hpp
@@ -2,6 +2,7 @@
#include <mbgl/util/chrono.hpp>
#include <mbgl/util/optional.hpp>
+#include <mbgl/util/blob.hpp>
#include <string>
#include <memory>
@@ -31,7 +32,7 @@ public:
bool mustRevalidate = false;
// The actual data of the response. Present only for non-error, non-notModified responses.
- std::shared_ptr<const std::string> data;
+ Blob data;
optional<Timestamp> modified;
optional<Timestamp> expires;
diff --git a/include/mbgl/style/style.hpp b/include/mbgl/style/style.hpp
index d6fdbd8f2c..dffdbf99ec 100644
--- a/include/mbgl/style/style.hpp
+++ b/include/mbgl/style/style.hpp
@@ -3,6 +3,7 @@
#include <mbgl/style/transition_options.hpp>
#include <mbgl/map/camera.hpp>
#include <mbgl/util/geo.hpp>
+#include <mbgl/util/blob.hpp>
#include <string>
#include <vector>
@@ -25,10 +26,10 @@ public:
Style(Scheduler&, FileSource&, float pixelRatio);
~Style();
- void loadJSON(const std::string&);
+ void loadJSON(Blob);
void loadURL(const std::string&);
- std::string getJSON() const;
+ Blob getJSON() const;
std::string getURL() const;
// Defaults
diff --git a/include/mbgl/util/blob.hpp b/include/mbgl/util/blob.hpp
new file mode 100644
index 0000000000..df8fddc773
--- /dev/null
+++ b/include/mbgl/util/blob.hpp
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <memory>
+#include <string>
+
+namespace mbgl {
+
+class Blob {
+public:
+ Blob();
+ Blob(std::shared_ptr<const std::string> bytes, bool gzip);
+ Blob(std::string&& bytes, bool compressed);
+
+ // Return uncompressed/compressed data.
+ std::shared_ptr<const std::string> uncompressedData() const;
+ std::shared_ptr<const std::string> compressedData() const;
+
+ // Transform the blob to being uncompressed.
+ void uncompress();
+
+ bool isCompressed() const {
+ return compressed;
+ }
+
+ explicit operator bool() const {
+ return (bool)bytes;
+ }
+
+private:
+ std::shared_ptr<const std::string> bytes;
+ bool compressed;
+};
+
+} // namespace mbgl
diff --git a/include/mbgl/util/compression.hpp b/include/mbgl/util/compression.hpp
index 93a3ddb8bc..cbce764683 100644
--- a/include/mbgl/util/compression.hpp
+++ b/include/mbgl/util/compression.hpp
@@ -5,8 +5,15 @@
namespace mbgl {
namespace util {
+// Compresses data with the deflate algorithm.
std::string compress(const std::string& raw);
+
+// Decompresses data that is in deflate format, optionally wrapped in a gzip container.
std::string decompress(const std::string& raw);
+// Returns true when there's a good chance that the string can be compressed.
+// In particular, it returns false when the data is an already compressed image format.
+bool isCompressible(const std::string& raw);
+
} // namespace util
} // namespace mbgl
diff --git a/include/mbgl/util/image.hpp b/include/mbgl/util/image.hpp
index 4887058f79..1cb3cca8c9 100644
--- a/include/mbgl/util/image.hpp
+++ b/include/mbgl/util/image.hpp
@@ -2,6 +2,7 @@
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/geometry.hpp>
+#include <mbgl/util/blob.hpp>
#include <mbgl/util/size.hpp>
#include <string>
@@ -170,8 +171,7 @@ using UnassociatedImage = Image<ImageAlphaMode::Unassociated>;
using PremultipliedImage = Image<ImageAlphaMode::Premultiplied>;
using AlphaImage = Image<ImageAlphaMode::Exclusive>;
-// TODO: don't use std::string for binary data.
-PremultipliedImage decodeImage(const std::string&);
-std::string encodePNG(const PremultipliedImage&);
+PremultipliedImage decodeImage(Blob);
+Blob encodePNG(const PremultipliedImage&);
} // namespace mbgl