OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/loader/resource/ImageResourceContent.h" | 5 #include "core/loader/resource/ImageResourceContent.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 | 8 |
9 #include "core/loader/resource/ImageResource.h" | 9 #include "core/loader/resource/ImageResource.h" |
10 #include "core/loader/resource/ImageResourceInfo.h" | 10 #include "core/loader/resource/ImageResourceInfo.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 | 33 |
34 DEFINE_INLINE_VIRTUAL_TRACE() { ImageResourceInfo::trace(visitor); } | 34 DEFINE_INLINE_VIRTUAL_TRACE() { ImageResourceInfo::trace(visitor); } |
35 | 35 |
36 private: | 36 private: |
37 const KURL& url() const override { return m_url; } | 37 const KURL& url() const override { return m_url; } |
38 bool isSchedulingReload() const override { return false; } | 38 bool isSchedulingReload() const override { return false; } |
39 bool hasDevicePixelRatioHeaderValue() const override { return false; } | 39 bool hasDevicePixelRatioHeaderValue() const override { return false; } |
40 float devicePixelRatioHeaderValue() const override { return 1.0; } | 40 float devicePixelRatioHeaderValue() const override { return 1.0; } |
41 const ResourceResponse& response() const override { return m_response; } | 41 const ResourceResponse& response() const override { return m_response; } |
42 ResourceStatus getStatus() const override { return ResourceStatus::Cached; } | 42 ResourceStatus getStatus() const override { return ResourceStatus::Cached; } |
43 bool isPlaceholder() const override { return false; } | 43 bool shouldShowPlaceholder() const override { return false; } |
44 bool isCacheValidator() const override { return false; } | 44 bool isCacheValidator() const override { return false; } |
45 bool schedulingReloadOrShouldReloadBrokenPlaceholder() const override { | 45 bool schedulingReloadOrShouldReloadBrokenPlaceholder() const override { |
46 return false; | 46 return false; |
47 } | 47 } |
48 bool isAccessAllowed( | 48 bool isAccessAllowed( |
49 SecurityOrigin*, | 49 SecurityOrigin*, |
50 DoesCurrentFrameHaveSingleSecurityOrigin) const override { | 50 DoesCurrentFrameHaveSingleSecurityOrigin) const override { |
51 return true; | 51 return true; |
52 } | 52 } |
53 bool hasCacheControlNoStoreHeader() const override { return false; } | 53 bool hasCacheControlNoStoreHeader() const override { return false; } |
54 const ResourceError& resourceError() const override { return m_error; } | 54 const ResourceError& resourceError() const override { return m_error; } |
55 | 55 |
56 void setDecodedSize(size_t) override {} | 56 void setDecodedSize(size_t) override {} |
57 void setIsPlaceholder(bool) override {} | |
58 void willAddClientOrObserver() override {} | 57 void willAddClientOrObserver() override {} |
59 void didRemoveClientOrObserver() override {} | 58 void didRemoveClientOrObserver() override {} |
60 void emulateLoadStartedForInspector( | 59 void emulateLoadStartedForInspector( |
61 ResourceFetcher*, | 60 ResourceFetcher*, |
62 const KURL&, | 61 const KURL&, |
63 const AtomicString& initiatorName) override {} | 62 const AtomicString& initiatorName) override {} |
64 | 63 |
65 const KURL m_url; | 64 const KURL m_url; |
66 const ResourceResponse m_response; | 65 const ResourceResponse m_response; |
67 const ResourceError m_error; | 66 const ResourceError m_error; |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 int64_t length = m_image->data() ? m_image->data()->size() : 0; | 290 int64_t length = m_image->data() ? m_image->data()->size() : 0; |
292 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); | 291 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); |
293 | 292 |
294 // If our Image has an observer, it's always us so we need to clear the back | 293 // If our Image has an observer, it's always us so we need to clear the back |
295 // pointer before dropping our reference. | 294 // pointer before dropping our reference. |
296 m_image->clearImageObserver(); | 295 m_image->clearImageObserver(); |
297 m_image.clear(); | 296 m_image.clear(); |
298 m_sizeAvailable = Image::SizeUnavailable; | 297 m_sizeAvailable = Image::SizeUnavailable; |
299 } | 298 } |
300 | 299 |
301 // Determines if |response| likely contains the entire resource for the purposes | |
302 // of determining whether or not to show a placeholder, e.g. if the server | |
303 // responded with a full 200 response or if the full image is smaller than the | |
304 // requested range. | |
305 static bool isEntireResource(const ResourceResponse& response) { | |
306 if (response.httpStatusCode() != 206) | |
307 return true; | |
308 | |
309 int64_t firstBytePosition = -1, lastBytePosition = -1, instanceLength = -1; | |
310 return parseContentRangeHeaderFor206( | |
311 response.httpHeaderField("Content-Range"), &firstBytePosition, | |
312 &lastBytePosition, &instanceLength) && | |
313 firstBytePosition == 0 && lastBytePosition + 1 == instanceLength; | |
314 } | |
315 | |
316 static bool shouldShowFullImageInsteadOfPlaceholder( | |
317 const ResourceResponse& response, | |
318 const Image* image) { | |
319 if (!isEntireResource(response)) | |
320 return false; | |
321 if (image && !image->isNull()) | |
322 return true; | |
323 | |
324 // Don't treat a complete and broken image as a placeholder if the response | |
325 // code is something other than a 4xx or 5xx error. This is done to prevent | |
326 // reissuing the request in cases like "204 No Content" responses to tracking | |
327 // requests triggered by <img> tags, and <img> tags used to preload non-image | |
328 // resources. | |
329 return response.httpStatusCode() < 400 || response.httpStatusCode() >= 600; | |
330 } | |
331 | |
332 ImageResourceContent::UpdateImageResult ImageResourceContent::updateImage( | 300 ImageResourceContent::UpdateImageResult ImageResourceContent::updateImage( |
333 PassRefPtr<SharedBuffer> data, | 301 PassRefPtr<SharedBuffer> data, |
334 UpdateImageOption updateImageOption, | 302 UpdateImageOption updateImageOption, |
335 bool allDataReceived) { | 303 bool allDataReceived) { |
336 TRACE_EVENT0("blink", "ImageResourceContent::updateImage"); | 304 TRACE_EVENT0("blink", "ImageResourceContent::updateImage"); |
337 | 305 |
338 #if DCHECK_IS_ON() | 306 #if DCHECK_IS_ON() |
339 DCHECK(!m_isUpdateImageBeingCalled); | 307 DCHECK(!m_isUpdateImageBeingCalled); |
340 AutoReset<bool> scope(&m_isUpdateImageBeingCalled, true); | 308 AutoReset<bool> scope(&m_isUpdateImageBeingCalled, true); |
341 #endif | 309 #endif |
(...skipping 25 matching lines...) Expand all Loading... |
367 DCHECK(m_image); | 335 DCHECK(m_image); |
368 m_sizeAvailable = m_image->setData(std::move(data), allDataReceived); | 336 m_sizeAvailable = m_image->setData(std::move(data), allDataReceived); |
369 } | 337 } |
370 | 338 |
371 // Go ahead and tell our observers to try to draw if we have either | 339 // Go ahead and tell our observers to try to draw if we have either |
372 // received all the data or the size is known. Each chunk from the network | 340 // received all the data or the size is known. Each chunk from the network |
373 // causes observers to repaint, which will force that chunk to decode. | 341 // causes observers to repaint, which will force that chunk to decode. |
374 if (m_sizeAvailable == Image::SizeUnavailable && !allDataReceived) | 342 if (m_sizeAvailable == Image::SizeUnavailable && !allDataReceived) |
375 return UpdateImageResult::NoDecodeError; | 343 return UpdateImageResult::NoDecodeError; |
376 | 344 |
377 if (m_info->isPlaceholder() && allDataReceived) { | 345 if (m_info->shouldShowPlaceholder() && allDataReceived) { |
378 if (shouldShowFullImageInsteadOfPlaceholder(response(), | 346 if (m_image && !m_image->isNull()) { |
379 m_image.get())) { | |
380 m_info->setIsPlaceholder(false); | |
381 } else if (m_image && !m_image->isNull()) { | |
382 IntSize dimensions = m_image->size(); | 347 IntSize dimensions = m_image->size(); |
383 clearImage(); | 348 clearImage(); |
384 m_image = PlaceholderImage::create(this, dimensions); | 349 m_image = PlaceholderImage::create(this, dimensions); |
385 } | 350 } |
386 } | 351 } |
387 | 352 |
388 if (!m_image || m_image->isNull()) { | 353 if (!m_image || m_image->isNull()) { |
389 clearImage(); | 354 clearImage(); |
390 return UpdateImageResult::ShouldDecodeError; | 355 return UpdateImageResult::ShouldDecodeError; |
391 } | 356 } |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 | 477 |
513 const ResourceResponse& ImageResourceContent::response() const { | 478 const ResourceResponse& ImageResourceContent::response() const { |
514 return m_info->response(); | 479 return m_info->response(); |
515 } | 480 } |
516 | 481 |
517 const ResourceError& ImageResourceContent::resourceError() const { | 482 const ResourceError& ImageResourceContent::resourceError() const { |
518 return m_info->resourceError(); | 483 return m_info->resourceError(); |
519 } | 484 } |
520 | 485 |
521 } // namespace blink | 486 } // namespace blink |
OLD | NEW |