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 "core/loader/resource/ImageResource.h" | 7 #include "core/loader/resource/ImageResource.h" |
8 #include "core/loader/resource/ImageResourceInfo.h" | 8 #include "core/loader/resource/ImageResourceInfo.h" |
9 #include "core/loader/resource/ImageResourceObserver.h" | 9 #include "core/loader/resource/ImageResourceObserver.h" |
10 #include "core/svg/graphics/SVGImage.h" | 10 #include "core/svg/graphics/SVGImage.h" |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 int64_t length = m_image->data() ? m_image->data()->size() : 0; | 290 int64_t length = m_image->data() ? m_image->data()->size() : 0; |
291 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); | 291 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); |
292 | 292 |
293 // 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 |
294 // pointer before dropping our reference. | 294 // pointer before dropping our reference. |
295 m_image->clearImageObserver(); | 295 m_image->clearImageObserver(); |
296 m_image.clear(); | 296 m_image.clear(); |
297 m_sizeAvailable = Image::SizeUnavailable; | 297 m_sizeAvailable = Image::SizeUnavailable; |
298 } | 298 } |
299 | 299 |
300 void ImageResourceContent::clearImageAndNotifyObservers( | |
301 NotifyFinishOption notifyingFinishOption) { | |
302 clearImage(); | |
303 notifyObservers(notifyingFinishOption); | |
304 } | |
305 | |
306 void ImageResourceContent::updateImage(PassRefPtr<SharedBuffer> data, | 300 void ImageResourceContent::updateImage(PassRefPtr<SharedBuffer> data, |
307 ClearImageOption clearImageOption, | 301 UpdateImageOption updateImageOption, |
308 bool allDataReceived) { | 302 bool allDataReceived) { |
309 TRACE_EVENT0("blink", "ImageResourceContent::updateImage"); | 303 TRACE_EVENT0("blink", "ImageResourceContent::updateImage"); |
310 | 304 |
311 if (clearImageOption == ImageResourceContent::ClearExistingImage) { | 305 // Clears the existing image, if instructed by |updateImageOption|. |
312 clearImage(); | 306 switch (updateImageOption) { |
| 307 case ClearAndUpdateImage: |
| 308 case ClearImageAndNotifyObservers: |
| 309 clearImage(); |
| 310 break; |
| 311 case UpdateImage: |
| 312 break; |
313 } | 313 } |
314 | 314 |
315 // Have the image update its data from its internal buffer. It will not do | 315 // Updates the image, if instructed by |updateImageOption|. |
316 // anything now, but will delay decoding until queried for info (like size or | 316 switch (updateImageOption) { |
317 // specific image frames). | 317 case ClearImageAndNotifyObservers: |
318 if (data) { | 318 DCHECK(!data); |
319 if (!m_image) | 319 break; |
320 m_image = createImage(); | 320 |
321 DCHECK(m_image); | 321 case UpdateImage: |
322 m_sizeAvailable = m_image->setData(std::move(data), allDataReceived); | 322 case ClearAndUpdateImage: |
| 323 // Have the image update its data from its internal buffer. It will not do |
| 324 // anything now, but will delay decoding until queried for info (like size |
| 325 // or specific image frames). |
| 326 if (data) { |
| 327 if (!m_image) |
| 328 m_image = createImage(); |
| 329 DCHECK(m_image); |
| 330 m_sizeAvailable = m_image->setData(std::move(data), allDataReceived); |
| 331 } |
| 332 |
| 333 // Go ahead and tell our observers to try to draw if we have either |
| 334 // received all the data or the size is known. Each chunk from the network |
| 335 // causes observers to repaint, which will force that chunk to decode. |
| 336 if (m_sizeAvailable == Image::SizeUnavailable && !allDataReceived) |
| 337 return; |
| 338 |
| 339 if (m_info->isPlaceholder() && allDataReceived && m_image && |
| 340 !m_image->isNull()) { |
| 341 if (m_sizeAvailable == Image::SizeAvailable) { |
| 342 // TODO(sclittle): Show the original image if the response consists of |
| 343 // the entire image, such as if the entire image response body is |
| 344 // smaller than the requested range. |
| 345 IntSize dimensions = m_image->size(); |
| 346 |
| 347 clearImage(); |
| 348 m_image = PlaceholderImage::create(this, dimensions); |
| 349 } else { |
| 350 // Clear the image so that it gets treated like a decoding error, |
| 351 // since the attempt to build a placeholder image failed. |
| 352 clearImage(); |
| 353 } |
| 354 } |
| 355 if (!m_image || m_image->isNull()) { |
| 356 clearImage(); |
| 357 m_info->decodeError(allDataReceived); |
| 358 } |
| 359 break; |
323 } | 360 } |
324 | 361 |
325 // Go ahead and tell our observers to try to draw if we have either received | 362 // Notifies the observers. |
326 // all the data or the size is known. Each chunk from the network causes | |
327 // observers to repaint, which will force that chunk to decode. | |
328 if (m_sizeAvailable == Image::SizeUnavailable && !allDataReceived) | |
329 return; | |
330 | |
331 if (m_info->isPlaceholder() && allDataReceived && m_image && | |
332 !m_image->isNull()) { | |
333 if (m_sizeAvailable == Image::SizeAvailable) { | |
334 // TODO(sclittle): Show the original image if the response consists of the | |
335 // entire image, such as if the entire image response body is smaller than | |
336 // the requested range. | |
337 IntSize dimensions = m_image->size(); | |
338 | |
339 clearImage(); | |
340 m_image = PlaceholderImage::create(this, dimensions); | |
341 } else { | |
342 // Clear the image so that it gets treated like a decoding error, since | |
343 // the attempt to build a placeholder image failed. | |
344 clearImage(); | |
345 } | |
346 } | |
347 | |
348 if (!m_image || m_image->isNull()) { | |
349 clearImage(); | |
350 m_info->decodeError(allDataReceived); | |
351 } | |
352 | |
353 // It would be nice to only redraw the decoded band of the image, but with the | 363 // It would be nice to only redraw the decoded band of the image, but with the |
354 // current design (decoding delayed until painting) that seems hard. | 364 // current design (decoding delayed until painting) that seems hard. |
355 notifyObservers(allDataReceived ? ShouldNotifyFinish : DoNotNotifyFinish); | 365 notifyObservers(allDataReceived ? ShouldNotifyFinish : DoNotNotifyFinish); |
356 } | 366 } |
357 | 367 |
358 void ImageResourceContent::decodedSizeChangedTo(const blink::Image* image, | 368 void ImageResourceContent::decodedSizeChangedTo(const blink::Image* image, |
359 size_t newSize) { | 369 size_t newSize) { |
360 if (!image || image != m_image) | 370 if (!image || image != m_image) |
361 return; | 371 return; |
362 | 372 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 | 478 |
469 const ResourceResponse& ImageResourceContent::response() const { | 479 const ResourceResponse& ImageResourceContent::response() const { |
470 return m_info->response(); | 480 return m_info->response(); |
471 } | 481 } |
472 | 482 |
473 const ResourceError& ImageResourceContent::resourceError() const { | 483 const ResourceError& ImageResourceContent::resourceError() const { |
474 return m_info->resourceError(); | 484 return m_info->resourceError(); |
475 } | 485 } |
476 | 486 |
477 } // namespace blink | 487 } // namespace blink |
OLD | NEW |