Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2013 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 */ | 30 */ |
| 31 | 31 |
| 32 #include "config.h" | 32 #include "config.h" |
| 33 #include "core/html/parser/HTMLSrcsetParser.h" | 33 #include "core/html/parser/HTMLSrcsetParser.h" |
| 34 | 34 |
| 35 #include "core/dom/Document.h" | 35 #include "core/dom/Document.h" |
| 36 #include "core/fetch/MemoryCache.h" | |
| 36 #include "core/frame/FrameConsole.h" | 37 #include "core/frame/FrameConsole.h" |
| 37 #include "core/frame/LocalFrame.h" | 38 #include "core/frame/LocalFrame.h" |
| 38 #include "core/frame/UseCounter.h" | 39 #include "core/frame/UseCounter.h" |
| 39 #include "core/html/parser/HTMLParserIdioms.h" | 40 #include "core/html/parser/HTMLParserIdioms.h" |
| 40 #include "core/inspector/ConsoleMessage.h" | 41 #include "core/inspector/ConsoleMessage.h" |
| 41 #include "platform/ParsingUtilities.h" | 42 #include "platform/ParsingUtilities.h" |
| 42 #include "platform/RuntimeEnabledFeatures.h" | 43 #include "platform/RuntimeEnabledFeatures.h" |
| 43 | 44 |
| 44 namespace blink { | 45 namespace blink { |
| 45 | 46 |
| (...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 347 | 348 |
| 348 currentDensity = imageCandidates[i].density(); | 349 currentDensity = imageCandidates[i].density(); |
| 349 geometricMean = sqrt(currentDensity * nextDensity); | 350 geometricMean = sqrt(currentDensity * nextDensity); |
| 350 if (deviceScaleFactor >= geometricMean) | 351 if (deviceScaleFactor >= geometricMean) |
| 351 return next; | 352 return next; |
| 352 break; | 353 break; |
| 353 } | 354 } |
| 354 return i; | 355 return i; |
| 355 } | 356 } |
| 356 | 357 |
| 357 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour ceSize, Vector<ImageCandidate>& imageCandidates) | 358 static unsigned avoidDownloadIfHigherDensityResourceIsInCache(Vector<ImageCandid ate>& imageCandidates, unsigned winner, Document* document) |
| 359 { | |
| 360 if (!document) | |
| 361 return winner; | |
| 362 for (unsigned i = imageCandidates.size() - 1; i > winner; --i) { | |
| 363 KURL url = document->completeURL(stripLeadingAndTrailingHTMLSpaces(image Candidates[i].url())); | |
| 364 if (memoryCache()->resourceForURL(url)) | |
| 365 return i; | |
| 366 } | |
| 367 return winner; | |
| 368 } | |
| 369 | |
| 370 static ImageCandidate pickBestImageCandidate(float deviceScaleFactor, float sour ceSize, Vector<ImageCandidate>& imageCandidates, Document* document = nullptr) | |
| 358 { | 371 { |
| 359 const float defaultDensityValue = 1.0; | 372 const float defaultDensityValue = 1.0; |
| 360 bool ignoreSrc = false; | 373 bool ignoreSrc = false; |
| 361 if (imageCandidates.isEmpty()) | 374 if (imageCandidates.isEmpty()) |
| 362 return ImageCandidate(); | 375 return ImageCandidate(); |
| 363 | 376 |
| 364 // http://picture.responsiveimages.org/#normalize-source-densities | 377 // http://picture.responsiveimages.org/#normalize-source-densities |
| 365 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im ageCandidates.end(); ++it) { | 378 for (Vector<ImageCandidate>::iterator it = imageCandidates.begin(); it != im ageCandidates.end(); ++it) { |
| 366 if (it->resourceWidth() > 0) { | 379 if (it->resourceWidth() > 0) { |
| 367 it->setDensity((float)it->resourceWidth() / sourceSize); | 380 it->setDensity((float)it->resourceWidth() / sourceSize); |
| 368 ignoreSrc = true; | 381 ignoreSrc = true; |
| 369 } else if (it->density() < 0) { | 382 } else if (it->density() < 0) { |
| 370 it->setDensity(defaultDensityValue); | 383 it->setDensity(defaultDensityValue); |
| 371 } | 384 } |
| 372 } | 385 } |
| 373 | 386 |
| 374 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe nsity); | 387 std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByDe nsity); |
| 375 | 388 |
| 376 unsigned i = selectionLogic(imageCandidates, deviceScaleFactor, ignoreSrc); | 389 unsigned winner = selectionLogic(imageCandidates, deviceScaleFactor, ignoreS rc); |
| 377 ASSERT(i < imageCandidates.size()); | 390 ASSERT(winner < imageCandidates.size()); |
|
Mike West
2014/10/24 08:03:13
&& >= 0?
| |
| 391 winner = avoidDownloadIfHigherDensityResourceIsInCache(imageCandidates, winn er, document); | |
| 378 | 392 |
| 379 float winningDensity = imageCandidates[i].density(); | 393 float winningDensity = imageCandidates[winner].density(); |
| 380 | |
| 381 unsigned winner = i; | |
| 382 // 16. If an entry b in candidates has the same associated ... pixel density as an earlier entry a in candidates, | 394 // 16. If an entry b in candidates has the same associated ... pixel density as an earlier entry a in candidates, |
| 383 // then remove entry b | 395 // then remove entry b |
| 384 while ((i > 0) && (imageCandidates[--i].density() == winningDensity)) | 396 while ((winner > 0) && (imageCandidates[winner - 1].density() == winningDens ity)) |
| 385 winner = i; | 397 --winner; |
| 386 | 398 |
| 387 return imageCandidates[winner]; | 399 return imageCandidates[winner]; |
| 388 } | 400 } |
| 389 | 401 |
| 390 ImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, float so urceSize, const String& srcsetAttribute, Document* document) | 402 ImageCandidate bestFitSourceForSrcsetAttribute(float deviceScaleFactor, float so urceSize, const String& srcsetAttribute, Document* document) |
| 391 { | 403 { |
| 392 Vector<ImageCandidate> imageCandidates; | 404 Vector<ImageCandidate> imageCandidates; |
| 393 | 405 |
| 394 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates, do cument); | 406 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates, do cument); |
| 395 | 407 |
| 396 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ); | 408 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates , document); |
| 397 } | 409 } |
| 398 | 410 |
| 399 ImageCandidate bestFitSourceForImageAttributes(float deviceScaleFactor, float so urceSize, const String& srcAttribute, const String& srcsetAttribute, Document* d ocument) | 411 ImageCandidate bestFitSourceForImageAttributes(float deviceScaleFactor, float so urceSize, const String& srcAttribute, const String& srcsetAttribute, Document* d ocument) |
| 400 { | 412 { |
| 401 if (srcsetAttribute.isNull()) { | 413 if (srcsetAttribute.isNull()) { |
| 402 if (srcAttribute.isNull()) | 414 if (srcAttribute.isNull()) |
| 403 return ImageCandidate(); | 415 return ImageCandidate(); |
| 404 return ImageCandidate(srcAttribute, 0, srcAttribute.length(), Descriptor ParsingResult(), ImageCandidate::SrcOrigin); | 416 return ImageCandidate(srcAttribute, 0, srcAttribute.length(), Descriptor ParsingResult(), ImageCandidate::SrcOrigin); |
| 405 } | 417 } |
| 406 | 418 |
| 407 Vector<ImageCandidate> imageCandidates; | 419 Vector<ImageCandidate> imageCandidates; |
| 408 | 420 |
| 409 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates, do cument); | 421 parseImageCandidatesFromSrcsetAttribute(srcsetAttribute, imageCandidates, do cument); |
| 410 | 422 |
| 411 if (!srcAttribute.isEmpty()) | 423 if (!srcAttribute.isEmpty()) |
| 412 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); | 424 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); |
| 413 | 425 |
| 414 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ); | 426 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates , document); |
| 415 } | 427 } |
| 416 | 428 |
| 417 String bestFitSourceForImageAttributes(float deviceScaleFactor, float sourceSize , const String& srcAttribute, ImageCandidate& srcsetImageCandidate) | 429 String bestFitSourceForImageAttributes(float deviceScaleFactor, float sourceSize , const String& srcAttribute, ImageCandidate& srcsetImageCandidate) |
| 418 { | 430 { |
| 419 if (srcsetImageCandidate.isEmpty()) | 431 if (srcsetImageCandidate.isEmpty()) |
| 420 return srcAttribute; | 432 return srcAttribute; |
| 421 | 433 |
| 422 Vector<ImageCandidate> imageCandidates; | 434 Vector<ImageCandidate> imageCandidates; |
| 423 imageCandidates.append(srcsetImageCandidate); | 435 imageCandidates.append(srcsetImageCandidate); |
| 424 | 436 |
| 425 if (!srcAttribute.isEmpty()) | 437 if (!srcAttribute.isEmpty()) |
| 426 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); | 438 imageCandidates.append(ImageCandidate(srcAttribute, 0, srcAttribute.leng th(), DescriptorParsingResult(), ImageCandidate::SrcOrigin)); |
| 427 | 439 |
| 428 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ).toString(); | 440 return pickBestImageCandidate(deviceScaleFactor, sourceSize, imageCandidates ).toString(); |
| 429 } | 441 } |
| 430 | 442 |
| 431 } | 443 } |
| OLD | NEW |