示例#1
0
void ImageLoader::setImageWithoutConsideringPendingLoadEvent(
    ImageResource* newImage) {
    DCHECK(m_failedLoadURL.isEmpty());
    ImageResource* oldImage = m_image.get();
    if (newImage != oldImage) {
        m_image = newImage;
        if (m_hasPendingLoadEvent) {
            loadEventSender().cancelEvent(this);
            m_hasPendingLoadEvent = false;
        }
        if (m_hasPendingErrorEvent) {
            errorEventSender().cancelEvent(this);
            m_hasPendingErrorEvent = false;
        }
        m_imageComplete = true;
        if (newImage) {
            newImage->addObserver(this);
        }
        if (oldImage) {
            oldImage->removeObserver(this);
        }
    }

    if (LayoutImageResource* imageResource = layoutImageResource())
        imageResource->resetAnimation();
}
示例#2
0
void ImageLoader::updateFromElement(UpdateFromElementBehavior updateBehavior,
                                    ReferrerPolicy referrerPolicy) {
    AtomicString imageSourceURL = m_element->imageSourceURL();
    m_suppressErrorEvents = (updateBehavior == UpdateSizeChanged);

    if (updateBehavior == UpdateIgnorePreviousError)
        clearFailedLoadURL();

    if (!m_failedLoadURL.isEmpty() && imageSourceURL == m_failedLoadURL)
        return;

    // Prevent the creation of a ResourceLoader (and therefore a network request)
    // for ImageDocument loads. In this case, the image contents have already been
    // requested as a main resource and ImageDocumentParser will take care of
    // funneling the main resource bytes into m_image, so just create an
    // ImageResource to be populated later.
    if (m_loadingImageDocument && updateBehavior != UpdateForcedReload) {
        setImage(
            ImageResource::create(imageSourceToKURL(m_element->imageSourceURL())));
        m_image->setStatus(Resource::Pending);
        return;
    }

    // If we have a pending task, we have to clear it -- either we're now loading
    // immediately, or we need to reset the task's state.
    if (m_pendingTask) {
        m_pendingTask->clearLoader();
        m_pendingTask.reset();
    }

    KURL url = imageSourceToKURL(imageSourceURL);
    if (shouldLoadImmediately(url)) {
        doUpdateFromElement(DoNotBypassMainWorldCSP, updateBehavior, url,
                            referrerPolicy);
        return;
    }
    // Allow the idiom "img.src=''; img.src='.." to clear down the image before an
    // asynchronous load completes.
    if (imageSourceURL.isEmpty()) {
        ImageResource* image = m_image.get();
        if (image) {
            image->removeObserver(this);
        }
        m_image = nullptr;
    }

    // Don't load images for inactive documents. We don't want to slow down the
    // raw HTML parsing case by loading images we don't intend to display.
    Document& document = m_element->document();
    if (document.isActive())
        enqueueImageLoadingMicroTask(updateBehavior, referrerPolicy);
}
示例#3
0
void ImageLoader::doUpdateFromElement(BypassMainWorldBehavior bypassBehavior,
                                      UpdateFromElementBehavior updateBehavior,
                                      const KURL& url,
                                      ReferrerPolicy referrerPolicy) {
    // FIXME: According to
    // http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-content.html#the-img-element:the-img-element-55
    // When "update image" is called due to environment changes and the load
    // fails, onerror should not be called. That is currently not the case.
    //
    // We don't need to call clearLoader here: Either we were called from the
    // task, or our caller updateFromElement cleared the task's loader (and set
    // m_pendingTask to null).
    m_pendingTask.reset();
    // Make sure to only decrement the count when we exit this function
    std::unique_ptr<IncrementLoadEventDelayCount> loadDelayCounter;
    loadDelayCounter.swap(m_loadDelayCounter);

    Document& document = m_element->document();
    if (!document.isActive())
        return;

    AtomicString imageSourceURL = m_element->imageSourceURL();
    ImageResource* newImage = nullptr;
    if (!url.isNull()) {
        // Unlike raw <img>, we block mixed content inside of <picture> or
        // <img srcset>.
        ResourceLoaderOptions resourceLoaderOptions =
            ResourceFetcher::defaultResourceOptions();
        ResourceRequest resourceRequest(url);
        if (updateBehavior == UpdateForcedReload) {
            resourceRequest.setCachePolicy(WebCachePolicy::BypassingCache);
            resourceRequest.setLoFiState(WebURLRequest::LoFiOff);
        }

        if (referrerPolicy != ReferrerPolicyDefault) {
            resourceRequest.setHTTPReferrer(SecurityPolicy::generateReferrer(
                                                referrerPolicy, url, document.outgoingReferrer()));
        }

        if (isHTMLPictureElement(element()->parentNode()) ||
                !element()->fastGetAttribute(HTMLNames::srcsetAttr).isNull())
            resourceRequest.setRequestContext(WebURLRequest::RequestContextImageSet);
        FetchRequest request(resourceRequest, element()->localName(),
                             resourceLoaderOptions);
        configureRequest(request, bypassBehavior, *m_element,
                         document.clientHintsPreferences());

        newImage = ImageResource::fetch(request, document.fetcher());

        if (!newImage && !pageIsBeingDismissed(&document)) {
            crossSiteOrCSPViolationOccurred(imageSourceURL);
            dispatchErrorEvent();
        } else {
            clearFailedLoadURL();
        }
    } else {
        if (!imageSourceURL.isNull()) {
            // Fire an error event if the url string is not empty, but the KURL is.
            dispatchErrorEvent();
        }
        noImageResourceToLoad();
    }

    ImageResource* oldImage = m_image.get();
    if (updateBehavior == UpdateSizeChanged && m_element->layoutObject() &&
            m_element->layoutObject()->isImage() && newImage == oldImage) {
        toLayoutImage(m_element->layoutObject())->intrinsicSizeChanged();
    } else {
        if (m_hasPendingLoadEvent) {
            loadEventSender().cancelEvent(this);
            m_hasPendingLoadEvent = false;
        }

        // Cancel error events that belong to the previous load, which is now
        // cancelled by changing the src attribute. If newImage is null and
        // m_hasPendingErrorEvent is true, we know the error event has been just
        // posted by this load and we should not cancel the event.
        // FIXME: If both previous load and this one got blocked with an error, we
        // can receive one error event instead of two.
        if (m_hasPendingErrorEvent && newImage) {
            errorEventSender().cancelEvent(this);
            m_hasPendingErrorEvent = false;
        }

        m_image = newImage;
        m_hasPendingLoadEvent = newImage;
        m_imageComplete = !newImage;

        updateLayoutObject();
        // If newImage exists and is cached, addObserver() will result in the load
        // event being queued to fire. Ensure this happens after beforeload is
        // dispatched.
        if (newImage) {
            newImage->addObserver(this);
        }
        if (oldImage) {
            oldImage->removeObserver(this);
        }
    }

    if (LayoutImageResource* imageResource = layoutImageResource())
        imageResource->resetAnimation();

    // Only consider updating the protection ref-count of the Element immediately
    // before returning from this function as doing so might result in the
    // destruction of this ImageLoader.
    updatedHasPendingEvent();
}