Chromium Code Reviews| Index: Source/core/html/HTMLImageElement.cpp |
| diff --git a/Source/core/html/HTMLImageElement.cpp b/Source/core/html/HTMLImageElement.cpp |
| index de6c4cec59283cc9420a4805efc8c78b754c4771..a20714b206fd0d0e047b113e256fc00dd4954020 100644 |
| --- a/Source/core/html/HTMLImageElement.cpp |
| +++ b/Source/core/html/HTMLImageElement.cpp |
| @@ -47,6 +47,30 @@ namespace blink { |
| using namespace HTMLNames; |
| +class HTMLImageElement::Listener FINAL : public MediaQueryListListener { |
| +public: |
| + static RefPtrWillBeRawPtr<Listener> create(HTMLImageElement* element) |
| + { |
| + return adoptRefWillBeNoop(new Listener(element)); |
| + } |
| + |
| + virtual void call() OVERRIDE |
| + { |
| + if (m_element) |
| + m_element->notifyViewportChanged(); |
| + } |
| + |
| + void clearElement() { m_element = nullptr; } |
|
haraken
2014/07/25 01:00:59
You can add #if !ENABLE(OILPAN) to this method.
|
| + virtual void trace(Visitor* visitor) OVERRIDE |
| + { |
| + visitor->trace(m_element); |
| + MediaQueryListListener::trace(visitor); |
| + } |
| +private: |
| + Listener(HTMLImageElement* element) : m_element(element) { } |
|
haraken
2014/07/25 01:00:59
Add explicit.
|
| + RawPtrWillBeMember<HTMLImageElement> m_element; |
| +}; |
| + |
| HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bool createdByParser) |
| : HTMLElement(imgTag, document) |
| , m_imageLoader(HTMLImageLoader::create(this)) |
| @@ -54,6 +78,8 @@ HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo |
| , m_imageDevicePixelRatio(1.0f) |
| , m_formWasSetByParser(false) |
| , m_elementCreatedByParser(createdByParser) |
| + , m_intrinsicSizingViewportDependant(false) |
| + , m_effectiveSizeViewportDependant(false) |
| { |
| ScriptWrappable::init(this); |
| if (form && form->inDocument()) { |
| @@ -81,6 +107,10 @@ PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::create(Document& docu |
| HTMLImageElement::~HTMLImageElement() |
| { |
| #if !ENABLE(OILPAN) |
| + if (m_listener) { |
| + document().mediaQueryMatcher().removeViewportListener(m_listener.get()); |
| + m_listener->clearElement(); |
| + } |
| if (m_form) |
| m_form->disassociate(*this); |
| #endif |
| @@ -90,9 +120,18 @@ void HTMLImageElement::trace(Visitor* visitor) |
| { |
| visitor->trace(m_imageLoader); |
| visitor->trace(m_form); |
| + visitor->trace(m_listener); |
| HTMLElement::trace(visitor); |
| } |
| +void HTMLImageElement::notifyViewportChanged() |
| +{ |
| + // Re-selecting the source URL in order to pick a more fitting resource |
| + // And update the image's intrinsic dimensions when the viewport changes. |
| + // Picking of a better fitting resource is UA dependant, not spec required. |
| + selectSourceURL(ImageLoader::UpdateForce); |
| +} |
| + |
| PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::createForJSConstructor(Document& document, int width, int height) |
| { |
| RefPtrWillBeRawPtr<HTMLImageElement> image = adoptRefWillBeNoop(new HTMLImageElement(document)); |
| @@ -181,6 +220,8 @@ void HTMLImageElement::setBestFitURLAndDPRFromImageCandidate(const ImageCandidat |
| float candidateDensity = candidate.density(); |
| if (candidateDensity >= 0) |
| m_imageDevicePixelRatio = 1.0 / candidateDensity; |
| + if (candidate.resourceWidth() > 0) |
| + m_intrinsicSizingViewportDependant = true; |
| if (renderer() && renderer()->isImage()) |
| toRenderImage(renderer())->setImageDevicePixelRatio(m_imageDevicePixelRatio); |
| } |
| @@ -246,7 +287,9 @@ ImageCandidate HTMLImageElement::findBestFitImageFromPictureParent() |
| if (!source->mediaQueryMatches()) |
| continue; |
| - unsigned effectiveSize = SizesAttributeParser::findEffectiveSize(source->fastGetAttribute(sizesAttr), MediaValuesDynamic::create(document())); |
| + SizesAttributeParser parser = SizesAttributeParser(MediaValuesDynamic::create(document()), source->fastGetAttribute(sizesAttr)); |
| + unsigned effectiveSize = parser.length(); |
| + m_effectiveSizeViewportDependant = parser.viewportDependant(); |
| ImageCandidate candidate = bestFitSourceForSrcsetAttribute(document().devicePixelRatio(), effectiveSize, source->fastGetAttribute(srcsetAttr)); |
| if (candidate.isEmpty()) |
| continue; |
| @@ -570,11 +613,19 @@ void HTMLImageElement::selectSourceURL(ImageLoader::UpdateFromElementBehavior be |
| if (!foundURL) { |
| unsigned effectiveSize = 0; |
| - if (RuntimeEnabledFeatures::pictureSizesEnabled()) |
| - effectiveSize = SizesAttributeParser::findEffectiveSize(fastGetAttribute(sizesAttr), MediaValuesDynamic::create(document())); |
| + if (RuntimeEnabledFeatures::pictureSizesEnabled()) { |
| + SizesAttributeParser parser = SizesAttributeParser(MediaValuesDynamic::create(document()), fastGetAttribute(sizesAttr)); |
| + effectiveSize = parser.length(); |
| + m_effectiveSizeViewportDependant = parser.viewportDependant(); |
| + } |
| ImageCandidate candidate = bestFitSourceForImageAttributes(document().devicePixelRatio(), effectiveSize, fastGetAttribute(srcAttr), fastGetAttribute(srcsetAttr)); |
| setBestFitURLAndDPRFromImageCandidate(candidate); |
| } |
| + if (m_intrinsicSizingViewportDependant && m_effectiveSizeViewportDependant) { |
| + if (!m_listener.get()) |
| + m_listener = Listener::create(this); |
| + document().mediaQueryMatcher().addViewportListener(m_listener.get()); |
| + } |
| imageLoader().updateFromElement(behavior); |
| } |