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 21 matching lines...) Expand all Loading... |
32 NullImageResourceInfo() {} | 32 NullImageResourceInfo() {} |
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 url_; } | 37 const KURL& Url() const override { return 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& GetResponse() const override { return response_; } | 41 const ResourceResponse& GetResponse() const override { return response_; } |
42 ResourceStatus GetStatus() const override { return ResourceStatus::kCached; } | |
43 bool ShouldShowPlaceholder() const override { return false; } | 42 bool ShouldShowPlaceholder() const override { return false; } |
44 bool IsCacheValidator() const override { return false; } | 43 bool IsCacheValidator() const override { return false; } |
45 bool SchedulingReloadOrShouldReloadBrokenPlaceholder() const override { | 44 bool SchedulingReloadOrShouldReloadBrokenPlaceholder() const override { |
46 return false; | 45 return false; |
47 } | 46 } |
48 bool IsAccessAllowed( | 47 bool IsAccessAllowed( |
49 SecurityOrigin*, | 48 SecurityOrigin*, |
50 DoesCurrentFrameHaveSingleSecurityOrigin) const override { | 49 DoesCurrentFrameHaveSingleSecurityOrigin) const override { |
51 return true; | 50 return true; |
52 } | 51 } |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 int64_t length = image_->Data() ? image_->Data()->size() : 0; | 317 int64_t length = image_->Data() ? image_->Data()->size() : 0; |
319 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); | 318 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-length); |
320 | 319 |
321 // If our Image has an observer, it's always us so we need to clear the back | 320 // If our Image has an observer, it's always us so we need to clear the back |
322 // pointer before dropping our reference. | 321 // pointer before dropping our reference. |
323 image_->ClearImageObserver(); | 322 image_->ClearImageObserver(); |
324 image_.Clear(); | 323 image_.Clear(); |
325 size_available_ = Image::kSizeUnavailable; | 324 size_available_ = Image::kSizeUnavailable; |
326 } | 325 } |
327 | 326 |
| 327 // |new_status| is the status of corresponding ImageResource. |
| 328 void ImageResourceContent::UpdateToLoadedContentStatus( |
| 329 ResourceStatus new_status) { |
| 330 // When |ShouldNotifyFinish|, we set content_status_ |
| 331 // to a loaded ResourceStatus. |
| 332 |
| 333 // Checks |new_status| (i.e. Resource's current status). |
| 334 switch (new_status) { |
| 335 case ResourceStatus::kCached: |
| 336 case ResourceStatus::kPending: |
| 337 // In case of successful load, Resource's status can be |
| 338 // kCached (e.g. for second part of multipart image) or |
| 339 // still Pending (e.g. for a non-multipart image). |
| 340 // Therefore we use kCached as the new state here. |
| 341 new_status = ResourceStatus::kCached; |
| 342 break; |
| 343 |
| 344 case ResourceStatus::kLoadError: |
| 345 case ResourceStatus::kDecodeError: |
| 346 // In case of error, Resource's status is set to an error status |
| 347 // before UpdateImage() and thus we use the error status as-is. |
| 348 break; |
| 349 |
| 350 case ResourceStatus::kNotStarted: |
| 351 CHECK(false); |
| 352 break; |
| 353 } |
| 354 |
| 355 // Checks ImageResourceContent's previous status. |
| 356 switch (GetContentStatus()) { |
| 357 case ResourceStatus::kPending: |
| 358 // A non-multipart image or the first part of a multipart image. |
| 359 break; |
| 360 |
| 361 case ResourceStatus::kCached: |
| 362 // Second (or later) part of a multipart image. |
| 363 break; |
| 364 |
| 365 case ResourceStatus::kNotStarted: |
| 366 // Should have updated to kPending via NotifyStartLoad(). |
| 367 CHECK(false); |
| 368 break; |
| 369 |
| 370 case ResourceStatus::kLoadError: |
| 371 case ResourceStatus::kDecodeError: |
| 372 CHECK(false); |
| 373 break; |
| 374 } |
| 375 |
| 376 // Updates the status. |
| 377 content_status_ = new_status; |
| 378 } |
| 379 |
| 380 void ImageResourceContent::NotifyStartLoad() { |
| 381 // Checks ImageResourceContent's previous status. |
| 382 switch (GetContentStatus()) { |
| 383 case ResourceStatus::kPending: |
| 384 CHECK(false); |
| 385 break; |
| 386 |
| 387 case ResourceStatus::kNotStarted: |
| 388 // Normal load start. |
| 389 break; |
| 390 |
| 391 case ResourceStatus::kCached: |
| 392 case ResourceStatus::kLoadError: |
| 393 case ResourceStatus::kDecodeError: |
| 394 // Load start due to revalidation/reload. |
| 395 break; |
| 396 } |
| 397 |
| 398 content_status_ = ResourceStatus::kPending; |
| 399 } |
| 400 |
328 ImageResourceContent::UpdateImageResult ImageResourceContent::UpdateImage( | 401 ImageResourceContent::UpdateImageResult ImageResourceContent::UpdateImage( |
329 PassRefPtr<SharedBuffer> data, | 402 PassRefPtr<SharedBuffer> data, |
| 403 ResourceStatus status, |
330 UpdateImageOption update_image_option, | 404 UpdateImageOption update_image_option, |
331 bool all_data_received) { | 405 bool all_data_received) { |
332 TRACE_EVENT0("blink", "ImageResourceContent::updateImage"); | 406 TRACE_EVENT0("blink", "ImageResourceContent::updateImage"); |
333 | 407 |
334 #if DCHECK_IS_ON() | 408 #if DCHECK_IS_ON() |
335 DCHECK(!is_update_image_being_called_); | 409 DCHECK(!is_update_image_being_called_); |
336 AutoReset<bool> scope(&is_update_image_being_called_, true); | 410 AutoReset<bool> scope(&is_update_image_being_called_, true); |
337 #endif | 411 #endif |
338 | 412 |
| 413 CHECK_NE(GetContentStatus(), ResourceStatus::kNotStarted); |
| 414 |
339 // Clears the existing image, if instructed by |updateImageOption|. | 415 // Clears the existing image, if instructed by |updateImageOption|. |
340 switch (update_image_option) { | 416 switch (update_image_option) { |
341 case kClearAndUpdateImage: | 417 case kClearAndUpdateImage: |
342 case kClearImageAndNotifyObservers: | 418 case kClearImageAndNotifyObservers: |
343 ClearImage(); | 419 ClearImage(); |
344 break; | 420 break; |
345 case kUpdateImage: | 421 case kUpdateImage: |
346 break; | 422 break; |
347 } | 423 } |
348 | 424 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 if (!image_ || image_->IsNull()) { | 457 if (!image_ || image_->IsNull()) { |
382 ClearImage(); | 458 ClearImage(); |
383 return UpdateImageResult::kShouldDecodeError; | 459 return UpdateImageResult::kShouldDecodeError; |
384 } | 460 } |
385 break; | 461 break; |
386 } | 462 } |
387 | 463 |
388 // Notifies the observers. | 464 // Notifies the observers. |
389 // It would be nice to only redraw the decoded band of the image, but with the | 465 // It would be nice to only redraw the decoded band of the image, but with the |
390 // current design (decoding delayed until painting) that seems hard. | 466 // current design (decoding delayed until painting) that seems hard. |
391 NotifyObservers(all_data_received ? kShouldNotifyFinish : kDoNotNotifyFinish); | 467 |
| 468 if (all_data_received) { |
| 469 UpdateToLoadedContentStatus(status); |
| 470 NotifyObservers(kShouldNotifyFinish); |
| 471 } else { |
| 472 NotifyObservers(kDoNotNotifyFinish); |
| 473 } |
| 474 |
392 return UpdateImageResult::kNoDecodeError; | 475 return UpdateImageResult::kNoDecodeError; |
393 } | 476 } |
394 | 477 |
395 void ImageResourceContent::DecodedSizeChangedTo(const blink::Image* image, | 478 void ImageResourceContent::DecodedSizeChangedTo(const blink::Image* image, |
396 size_t new_size) { | 479 size_t new_size) { |
397 if (!image || image != image_) | 480 if (!image || image != image_) |
398 return; | 481 return; |
399 | 482 |
400 info_->SetDecodedSize(new_size); | 483 info_->SetDecodedSize(new_size); |
401 } | 484 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 : ImageResourceInfo::kHasMultipleSecurityOrigin); | 546 : ImageResourceInfo::kHasMultipleSecurityOrigin); |
464 } | 547 } |
465 | 548 |
466 void ImageResourceContent::EmulateLoadStartedForInspector( | 549 void ImageResourceContent::EmulateLoadStartedForInspector( |
467 ResourceFetcher* fetcher, | 550 ResourceFetcher* fetcher, |
468 const KURL& url, | 551 const KURL& url, |
469 const AtomicString& initiator_name) { | 552 const AtomicString& initiator_name) { |
470 info_->EmulateLoadStartedForInspector(fetcher, url, initiator_name); | 553 info_->EmulateLoadStartedForInspector(fetcher, url, initiator_name); |
471 } | 554 } |
472 | 555 |
| 556 bool ImageResourceContent::IsLoaded() const { |
| 557 return GetContentStatus() > ResourceStatus::kPending; |
| 558 } |
| 559 |
| 560 bool ImageResourceContent::IsLoading() const { |
| 561 return GetContentStatus() == ResourceStatus::kPending; |
| 562 } |
| 563 |
| 564 bool ImageResourceContent::ErrorOccurred() const { |
| 565 return GetContentStatus() == ResourceStatus::kLoadError || |
| 566 GetContentStatus() == ResourceStatus::kDecodeError; |
| 567 } |
| 568 |
| 569 bool ImageResourceContent::LoadFailedOrCanceled() const { |
| 570 return GetContentStatus() == ResourceStatus::kLoadError; |
| 571 } |
| 572 |
| 573 ResourceStatus ImageResourceContent::GetContentStatus() const { |
| 574 return content_status_; |
| 575 } |
| 576 |
473 // TODO(hiroshige): Consider removing the following methods, or stoping | 577 // TODO(hiroshige): Consider removing the following methods, or stoping |
474 // redirecting to ImageResource. | 578 // redirecting to ImageResource. |
475 bool ImageResourceContent::IsLoaded() const { | |
476 return GetStatus() > ResourceStatus::kPending; | |
477 } | |
478 | |
479 bool ImageResourceContent::IsLoading() const { | |
480 return GetStatus() == ResourceStatus::kPending; | |
481 } | |
482 | |
483 bool ImageResourceContent::ErrorOccurred() const { | |
484 return GetStatus() == ResourceStatus::kLoadError || | |
485 GetStatus() == ResourceStatus::kDecodeError; | |
486 } | |
487 | |
488 bool ImageResourceContent::LoadFailedOrCanceled() const { | |
489 return GetStatus() == ResourceStatus::kLoadError; | |
490 } | |
491 | |
492 ResourceStatus ImageResourceContent::GetStatus() const { | |
493 return info_->GetStatus(); | |
494 } | |
495 | |
496 const KURL& ImageResourceContent::Url() const { | 579 const KURL& ImageResourceContent::Url() const { |
497 return info_->Url(); | 580 return info_->Url(); |
498 } | 581 } |
499 | 582 |
500 bool ImageResourceContent::HasCacheControlNoStoreHeader() const { | 583 bool ImageResourceContent::HasCacheControlNoStoreHeader() const { |
501 return info_->HasCacheControlNoStoreHeader(); | 584 return info_->HasCacheControlNoStoreHeader(); |
502 } | 585 } |
503 | 586 |
504 float ImageResourceContent::DevicePixelRatioHeaderValue() const { | 587 float ImageResourceContent::DevicePixelRatioHeaderValue() const { |
505 return info_->DevicePixelRatioHeaderValue(); | 588 return info_->DevicePixelRatioHeaderValue(); |
506 } | 589 } |
507 | 590 |
508 bool ImageResourceContent::HasDevicePixelRatioHeaderValue() const { | 591 bool ImageResourceContent::HasDevicePixelRatioHeaderValue() const { |
509 return info_->HasDevicePixelRatioHeaderValue(); | 592 return info_->HasDevicePixelRatioHeaderValue(); |
510 } | 593 } |
511 | 594 |
512 const ResourceResponse& ImageResourceContent::GetResponse() const { | 595 const ResourceResponse& ImageResourceContent::GetResponse() const { |
513 return info_->GetResponse(); | 596 return info_->GetResponse(); |
514 } | 597 } |
515 | 598 |
516 const ResourceError& ImageResourceContent::GetResourceError() const { | 599 const ResourceError& ImageResourceContent::GetResourceError() const { |
517 return info_->GetResourceError(); | 600 return info_->GetResourceError(); |
518 } | 601 } |
519 | 602 |
520 } // namespace blink | 603 } // namespace blink |
OLD | NEW |