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

Side by Side Diff: third_party/WebKit/Source/core/fetch/ImageResource.cpp

Issue 2543073002: Don't show a placeholder if the response contains the full image. (Closed)
Patch Set: Initial patchset Created 4 years 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) 5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved. 6 Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
7 7
8 This library is free software; you can redistribute it and/or 8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public 9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either 10 License as published by the Free Software Foundation; either
(...skipping 18 matching lines...) Expand all
29 #include "core/fetch/ResourceFetcher.h" 29 #include "core/fetch/ResourceFetcher.h"
30 #include "core/fetch/ResourceLoader.h" 30 #include "core/fetch/ResourceLoader.h"
31 #include "core/fetch/ResourceLoadingLog.h" 31 #include "core/fetch/ResourceLoadingLog.h"
32 #include "core/svg/graphics/SVGImage.h" 32 #include "core/svg/graphics/SVGImage.h"
33 #include "platform/Histogram.h" 33 #include "platform/Histogram.h"
34 #include "platform/RuntimeEnabledFeatures.h" 34 #include "platform/RuntimeEnabledFeatures.h"
35 #include "platform/SharedBuffer.h" 35 #include "platform/SharedBuffer.h"
36 #include "platform/geometry/IntSize.h" 36 #include "platform/geometry/IntSize.h"
37 #include "platform/graphics/BitmapImage.h" 37 #include "platform/graphics/BitmapImage.h"
38 #include "platform/graphics/PlaceholderImage.h" 38 #include "platform/graphics/PlaceholderImage.h"
39 #include "platform/network/HTTPParsers.h"
39 #include "platform/tracing/TraceEvent.h" 40 #include "platform/tracing/TraceEvent.h"
40 #include "public/platform/Platform.h" 41 #include "public/platform/Platform.h"
41 #include "public/platform/WebCachePolicy.h" 42 #include "public/platform/WebCachePolicy.h"
42 #include "wtf/CurrentTime.h" 43 #include "wtf/CurrentTime.h"
43 #include "wtf/HashCountedSet.h" 44 #include "wtf/HashCountedSet.h"
44 #include "wtf/StdLibExtras.h" 45 #include "wtf/StdLibExtras.h"
45 #include "wtf/Vector.h" 46 #include "wtf/Vector.h"
46 #include <memory> 47 #include <memory>
48 #include <stdint.h>
47 #include <v8.h> 49 #include <v8.h>
48 50
49 namespace blink { 51 namespace blink {
50 namespace { 52 namespace {
51 // The amount of time to wait before informing the clients that the image has 53 // The amount of time to wait before informing the clients that the image has
52 // been updated (in seconds). This effectively throttles invalidations that 54 // been updated (in seconds). This effectively throttles invalidations that
53 // result from new data arriving for this image. 55 // result from new data arriving for this image.
54 constexpr double kFlushDelaySeconds = 1.; 56 constexpr double kFlushDelaySeconds = 1.;
55 } // namespace 57 } // namespace
56 58
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 int64_t length = m_image->data() ? m_image->data()->size() : 0; 456 int64_t length = m_image->data() ? m_image->data()->size() : 0;
455 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); 457 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length);
456 458
457 // If our Image has an observer, it's always us so we need to clear the back 459 // If our Image has an observer, it's always us so we need to clear the back
458 // pointer before dropping our reference. 460 // pointer before dropping our reference.
459 m_image->clearImageObserver(); 461 m_image->clearImageObserver();
460 m_image.clear(); 462 m_image.clear();
461 m_sizeAvailable = Image::SizeUnavailable; 463 m_sizeAvailable = Image::SizeUnavailable;
462 } 464 }
463 465
466 // Determines if |response| likely contains the entire resource for the purposes
467 // of determining whether or not to show a placeholder, e.g. if the server
468 // responded with a full 200 response or if the full image is smaller than the
469 // requested range.
470 static bool isEntireResource(const ResourceResponse& response) {
471 if (response.httpStatusCode() != 206)
472 return true;
473
474 int64_t firstBytePosition = -1, lastBytePosition = -1, instanceLength = -1;
475 return parseContentRangeHeader(response.httpHeaderField("Content-Range"),
476 &firstBytePosition, &lastBytePosition,
477 &instanceLength) &&
478 firstBytePosition == 0 && lastBytePosition + 1 == instanceLength;
479 }
480
481 static bool shouldShowFullImageInsteadOfPlaceholder(
482 const ResourceResponse& response,
483 const Image* image) {
484 if (!isEntireResource(response))
485 return false;
486 if (image && !image->isNull())
487 return true;
488
489 // Don't treat a complete and broken image as a placeholder if the response
490 // code is something other than a 4xx or 5xx error. This is done to prevent
491 // reissuing the request in cases like "204 No Content" responses to tracking
492 // requests triggered by <img> tags, and <img> tags used to preload non-image
493 // resources.
494 return response.httpStatusCode() < 400 || response.httpStatusCode() >= 600;
495 }
496
464 void ImageResource::updateImage(bool allDataReceived) { 497 void ImageResource::updateImage(bool allDataReceived) {
465 TRACE_EVENT0("blink", "ImageResource::updateImage"); 498 TRACE_EVENT0("blink", "ImageResource::updateImage");
466 499
467 if (data()) 500 if (data())
468 createImage(); 501 createImage();
469 502
470 // Have the image update its data from its internal buffer. It will not do 503 // Have the image update its data from its internal buffer. It will not do
471 // anything now, but will delay decoding until queried for info (like size or 504 // anything now, but will delay decoding until queried for info (like size or
472 // specific image frames). 505 // specific image frames).
473 if (data()) { 506 if (data()) {
474 DCHECK(m_image); 507 DCHECK(m_image);
475 m_sizeAvailable = m_image->setData(data(), allDataReceived); 508 m_sizeAvailable = m_image->setData(data(), allDataReceived);
476 } 509 }
477 510
478 // Go ahead and tell our observers to try to draw if we have either received 511 // Go ahead and tell our observers to try to draw if we have either received
479 // all the data or the size is known. Each chunk from the network causes 512 // all the data or the size is known. Each chunk from the network causes
480 // observers to repaint, which will force that chunk to decode. 513 // observers to repaint, which will force that chunk to decode.
481 if (m_sizeAvailable == Image::SizeUnavailable && !allDataReceived) 514 if (m_sizeAvailable == Image::SizeUnavailable && !allDataReceived)
482 return; 515 return;
483 516
484 if (m_isPlaceholder && allDataReceived && m_image && !m_image->isNull()) { 517 if (m_isPlaceholder && allDataReceived) {
485 if (m_sizeAvailable == Image::SizeAvailable) { 518 if (shouldShowFullImageInsteadOfPlaceholder(response(), m_image.get())) {
486 // TODO(sclittle): Show the original image if the response consists of the 519 m_isPlaceholder = false;
487 // entire image, such as if the entire image response body is smaller than 520 } else if (m_image && !m_image->isNull()) {
488 // the requested range.
489 IntSize dimensions = m_image->size(); 521 IntSize dimensions = m_image->size();
490 clearImage(); 522 clearImage();
491 m_image = PlaceholderImage::create(this, dimensions); 523 m_image = PlaceholderImage::create(this, dimensions);
492 } else {
493 // Clear the image so that it gets treated like a decoding error, since
494 // the attempt to build a placeholder image failed.
495 clearImage();
496 } 524 }
497 } 525 }
498 526
499 if (!m_image || m_image->isNull()) { 527 if (!m_image || m_image->isNull()) {
500 size_t size = encodedSize(); 528 size_t size = encodedSize();
501 clear(); 529 clear();
502 if (!errorOccurred()) 530 if (!errorOccurred())
503 setStatus(DecodeError); 531 setStatus(DecodeError);
504 if (!allDataReceived && loader()) { 532 if (!allDataReceived && loader()) {
505 loader()->didFinishLoading(monotonicallyIncreasingTime(), size, size); 533 loader()->didFinishLoading(monotonicallyIncreasingTime(), size, size);
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 WebServiceWorkerResponseTypeOpaque; 741 WebServiceWorkerResponseTypeOpaque;
714 } 742 }
715 if (!getImage()->currentFrameHasSingleSecurityOrigin()) 743 if (!getImage()->currentFrameHasSingleSecurityOrigin())
716 return false; 744 return false;
717 if (passesAccessControlCheck(securityOrigin)) 745 if (passesAccessControlCheck(securityOrigin))
718 return true; 746 return true;
719 return !securityOrigin->taintsCanvas(response().url()); 747 return !securityOrigin->taintsCanvas(response().url());
720 } 748 }
721 749
722 } // namespace blink 750 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698