Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(854)

Unified Diff: third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp

Issue 2257513002: Refactor ImageDecoder factories (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/platform/image-decoders/ImageDecoder.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp
diff --git a/third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp b/third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp
index 3ffcaf751996d1ade4a68038a46012031136281d..1e3bb065585d9268f58c58bb34233b98073cfdd1 100644
--- a/third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp
+++ b/third_party/WebKit/Source/platform/image-decoders/ImageDecoder.cpp
@@ -83,42 +83,68 @@ inline bool matchesBMPSignature(const char* contents)
return !memcmp(contents, "BM", 2);
}
-std::unique_ptr<ImageDecoder> ImageDecoder::create(SniffResult sniffResult, AlphaOption alphaOption, GammaAndColorProfileOption colorOptions)
+// This needs to be updated if we ever add a matches*Signature() which requires more characters.
+static constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
+
+std::unique_ptr<ImageDecoder> ImageDecoder::create(PassRefPtr<SegmentReader> passData, bool dataComplete,
+ AlphaOption alphaOption, GammaAndColorProfileOption colorOptions)
{
- size_t maxDecodedBytes = Platform::current() ? Platform::current()->maxDecodedImageBytes() : noDecodedImageByteLimit;
+ RefPtr<SegmentReader> data = passData;
+
+ // We need at least kLongestSignatureLength bytes to run the signature matcher.
+ if (data->size() < kLongestSignatureLength)
+ return nullptr;
+
+ const size_t maxDecodedBytes = Platform::current()
+ ? Platform::current()->maxDecodedImageBytes()
+ : noDecodedImageByteLimit;
+
+ // Access the first kLongestSignatureLength chars to sniff the signature.
+ // (note: FastSharedBufferReader only makes a copy if the bytes are segmented)
+ char buffer[kLongestSignatureLength];
+ const FastSharedBufferReader fastReader(data);
+ const ImageDecoder::SniffResult sniffResult = determineImageType(
+ fastReader.getConsecutiveData(0, kLongestSignatureLength, buffer), kLongestSignatureLength);
+ std::unique_ptr<ImageDecoder> decoder;
switch (sniffResult) {
case SniffResult::JPEG:
- return wrapUnique(new JPEGImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ decoder.reset(new JPEGImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ break;
case SniffResult::PNG:
- return wrapUnique(new PNGImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ decoder.reset(new PNGImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ break;
case SniffResult::GIF:
- return wrapUnique(new GIFImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ decoder.reset(new GIFImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ break;
case SniffResult::WEBP:
- return wrapUnique(new WEBPImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ decoder.reset(new WEBPImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ break;
case SniffResult::ICO:
- return wrapUnique(new ICOImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ decoder.reset(new ICOImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ break;
case SniffResult::BMP:
- return wrapUnique(new BMPImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
- case SniffResult::InsufficientData:
+ decoder.reset(new BMPImageDecoder(alphaOption, colorOptions, maxDecodedBytes));
+ break;
case SniffResult::Invalid:
- return nullptr;
+ break;
}
- NOTREACHED();
- return nullptr;
-}
-namespace {
+ if (decoder)
+ decoder->setData(data.release(), dataComplete);
-// This needs to be updated if we ever add a matches*Signature() which requires more characters.
-constexpr size_t kLongestSignatureLength = sizeof("RIFF????WEBPVP") - 1;
+ return decoder;
+}
-} // anonymous ns
+bool ImageDecoder::hasSufficientDataToSniffImageType(const SharedBuffer& data)
+{
+ return data.size() >= kLongestSignatureLength;
+}
ImageDecoder::SniffResult ImageDecoder::determineImageType(const char* contents, size_t length)
{
- if (length < kLongestSignatureLength)
- return SniffResult::InsufficientData;
+ DCHECK_GE(length, kLongestSignatureLength);
+
if (matchesJPEGSignature(contents))
return SniffResult::JPEG;
if (matchesPNGSignature(contents))
@@ -134,24 +160,6 @@ ImageDecoder::SniffResult ImageDecoder::determineImageType(const char* contents,
return SniffResult::Invalid;
}
-ImageDecoder::SniffResult ImageDecoder::determineImageType(const SharedBuffer& data)
-{
- // TODO(fmalita): refactor the method signature to avoid casting.
- RefPtr<SegmentReader> reader = SegmentReader::createFromSharedBuffer(const_cast<SharedBuffer*>(&data));
-
- return determineImageType(*reader);
-}
-
-ImageDecoder::SniffResult ImageDecoder::determineImageType(const SegmentReader& data)
-{
- // TODO(fmalita): refactor the method signature to avoid casting.
- const FastSharedBufferReader fastReader(const_cast<SegmentReader*>(&data));
- char buffer[kLongestSignatureLength];
- const size_t len = std::min(kLongestSignatureLength, data.size());
-
- return determineImageType(fastReader.getConsecutiveData(0, len, buffer), len);
-}
-
size_t ImageDecoder::frameCount()
{
const size_t oldSize = m_frameBufferCache.size();
« no previous file with comments | « third_party/WebKit/Source/platform/image-decoders/ImageDecoder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698