Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1129)

Side by Side Diff: Source/core/html/HTMLImageElement.cpp

Issue 369423002: Have srcset respond to viewport changes (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: review comments Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserv ed. 4 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2010 Apple Inc. All rights reserv ed.
5 * Copyright (C) 2010 Google Inc. All rights reserved. 5 * Copyright (C) 2010 Google Inc. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
(...skipping 29 matching lines...) Expand all
40 #include "core/html/parser/HTMLParserIdioms.h" 40 #include "core/html/parser/HTMLParserIdioms.h"
41 #include "core/html/parser/HTMLSrcsetParser.h" 41 #include "core/html/parser/HTMLSrcsetParser.h"
42 #include "core/rendering/RenderImage.h" 42 #include "core/rendering/RenderImage.h"
43 #include "platform/MIMETypeRegistry.h" 43 #include "platform/MIMETypeRegistry.h"
44 #include "platform/RuntimeEnabledFeatures.h" 44 #include "platform/RuntimeEnabledFeatures.h"
45 45
46 namespace WebCore { 46 namespace WebCore {
47 47
48 using namespace HTMLNames; 48 using namespace HTMLNames;
49 49
50 class HTMLImageElement::Listener FINAL : public MediaQueryListListener {
51 public:
52 static RefPtrWillBeRawPtr<Listener> create(HTMLImageElement* element)
53 {
54 return adoptRefWillBeNoop(new Listener(element));
55 }
56
57 virtual void call() OVERRIDE
58 {
59 if (m_element)
60 m_element->notifyMediaQueryChanged();
61 }
62
63 void clearElement() { m_element = nullptr; }
64 virtual void trace(Visitor* visitor) OVERRIDE
65 {
66 visitor->trace(m_element);
67 MediaQueryListListener::trace(visitor);
68 }
69 private:
70 Listener(HTMLImageElement* element) : m_element(element) { }
71 RawPtrWillBeMember<HTMLImageElement> m_element;
72 };
73
50 HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo ol createdByParser) 74 HTMLImageElement::HTMLImageElement(Document& document, HTMLFormElement* form, bo ol createdByParser)
51 : HTMLElement(imgTag, document) 75 : HTMLElement(imgTag, document)
52 , m_imageLoader(HTMLImageLoader::create(this)) 76 , m_imageLoader(HTMLImageLoader::create(this))
53 , m_compositeOperator(CompositeSourceOver) 77 , m_compositeOperator(CompositeSourceOver)
54 , m_imageDevicePixelRatio(1.0f) 78 , m_imageDevicePixelRatio(1.0f)
55 , m_formWasSetByParser(false) 79 , m_formWasSetByParser(false)
56 , m_elementCreatedByParser(createdByParser) 80 , m_elementCreatedByParser(createdByParser)
81 , m_intrinsicSizingViewportDependant(false)
82 , m_effectiveSizeViewportDependant(false)
57 { 83 {
58 ScriptWrappable::init(this); 84 ScriptWrappable::init(this);
59 if (form && form->inDocument()) { 85 if (form && form->inDocument()) {
60 #if ENABLE(OILPAN) 86 #if ENABLE(OILPAN)
61 m_form = form; 87 m_form = form;
62 #else 88 #else
63 m_form = form->createWeakPtr(); 89 m_form = form->createWeakPtr();
64 #endif 90 #endif
65 m_formWasSetByParser = true; 91 m_formWasSetByParser = true;
66 m_form->associate(*this); 92 m_form->associate(*this);
67 m_form->didAssociateByParser(); 93 m_form->didAssociateByParser();
68 } 94 }
69 } 95 }
70 96
71 PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::create(Document& docu ment) 97 PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::create(Document& docu ment)
72 { 98 {
73 return adoptRefWillBeNoop(new HTMLImageElement(document)); 99 return adoptRefWillBeNoop(new HTMLImageElement(document));
74 } 100 }
75 101
76 PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::create(Document& docu ment, HTMLFormElement* form, bool createdByParser) 102 PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::create(Document& docu ment, HTMLFormElement* form, bool createdByParser)
77 { 103 {
78 return adoptRefWillBeNoop(new HTMLImageElement(document, form, createdByPars er)); 104 return adoptRefWillBeNoop(new HTMLImageElement(document, form, createdByPars er));
79 } 105 }
80 106
81 HTMLImageElement::~HTMLImageElement() 107 HTMLImageElement::~HTMLImageElement()
82 { 108 {
83 #if !ENABLE(OILPAN) 109 #if !ENABLE(OILPAN)
110 if (m_listener)
111 m_listener->clearElement();
Yoav Weiss 2014/07/15 09:46:31 Note that clearElement is only called on images th
84 if (m_form) 112 if (m_form)
85 m_form->disassociate(*this); 113 m_form->disassociate(*this);
86 #endif 114 #endif
87 } 115 }
88 116
117 void HTMLImageElement::createMediaQueryListIfDoesNotExist()
118 {
119 if (m_mediaQueryList)
120 return;
121 RefPtrWillBeRawPtr<MediaQuerySet> set = MediaQuerySet::create(MediaTypeNames ::all);
122 m_listener = Listener::create(this);
123 m_mediaQueryList = MediaQueryList::create(&document().mediaQueryMatcher(), s et.release());
124 m_mediaQueryList->addListener(m_listener);
125 m_mediaQueryList->alwaysUpdate();
126 }
127
89 void HTMLImageElement::trace(Visitor* visitor) 128 void HTMLImageElement::trace(Visitor* visitor)
90 { 129 {
91 visitor->trace(m_imageLoader); 130 visitor->trace(m_imageLoader);
92 visitor->trace(m_form); 131 visitor->trace(m_form);
93 HTMLElement::trace(visitor); 132 HTMLElement::trace(visitor);
94 } 133 }
95 134
135 void HTMLImageElement::notifyMediaQueryChanged()
136 {
137 // Re-selecting the source URL in order to pick a more fitting resource
138 // And update the image's intrinsic dimensions when the viewport changes.
139 // Picking of a better fitting resource is UA dependant, not spec required.
140 selectSourceURL(ImageLoader::UpdateForce);
141 }
142
96 PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::createForJSConstructo r(Document& document, int width, int height) 143 PassRefPtrWillBeRawPtr<HTMLImageElement> HTMLImageElement::createForJSConstructo r(Document& document, int width, int height)
97 { 144 {
98 RefPtrWillBeRawPtr<HTMLImageElement> image = adoptRefWillBeNoop(new HTMLImag eElement(document)); 145 RefPtrWillBeRawPtr<HTMLImageElement> image = adoptRefWillBeNoop(new HTMLImag eElement(document));
99 if (width) 146 if (width)
100 image->setWidth(width); 147 image->setWidth(width);
101 if (height) 148 if (height)
102 image->setHeight(height); 149 image->setHeight(height);
103 image->m_elementCreatedByParser = false; 150 image->m_elementCreatedByParser = false;
104 return image.release(); 151 return image.release();
105 } 152 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 #endif 221 #endif
175 } 222 }
176 } 223 }
177 224
178 void HTMLImageElement::setBestFitURLAndDPRFromImageCandidate(const ImageCandidat e& candidate) 225 void HTMLImageElement::setBestFitURLAndDPRFromImageCandidate(const ImageCandidat e& candidate)
179 { 226 {
180 m_bestFitImageURL = candidate.url(); 227 m_bestFitImageURL = candidate.url();
181 float candidateDensity = candidate.density(); 228 float candidateDensity = candidate.density();
182 if (candidateDensity >= 0) 229 if (candidateDensity >= 0)
183 m_imageDevicePixelRatio = 1.0 / candidateDensity; 230 m_imageDevicePixelRatio = 1.0 / candidateDensity;
231 if (candidate.resourceWidth() > 0)
232 m_intrinsicSizingViewportDependant = true;
184 if (renderer() && renderer()->isImage()) 233 if (renderer() && renderer()->isImage())
185 toRenderImage(renderer())->setImageDevicePixelRatio(m_imageDevicePixelRa tio); 234 toRenderImage(renderer())->setImageDevicePixelRatio(m_imageDevicePixelRa tio);
186 } 235 }
187 236
188 void HTMLImageElement::parseAttribute(const QualifiedName& name, const AtomicStr ing& value) 237 void HTMLImageElement::parseAttribute(const QualifiedName& name, const AtomicStr ing& value)
189 { 238 {
190 if (name == altAttr) { 239 if (name == altAttr) {
191 if (renderer() && renderer()->isImage()) 240 if (renderer() && renderer()->isImage())
192 toRenderImage(renderer())->updateAltText(); 241 toRenderImage(renderer())->updateAltText();
193 } else if (name == srcAttr || name == srcsetAttr || name == sizesAttr) { 242 } else if (name == srcAttr || name == srcsetAttr || name == sizesAttr) {
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 String srcset = source->fastGetAttribute(srcsetAttr); 288 String srcset = source->fastGetAttribute(srcsetAttr);
240 if (srcset.isEmpty()) 289 if (srcset.isEmpty())
241 continue; 290 continue;
242 String type = source->fastGetAttribute(typeAttr); 291 String type = source->fastGetAttribute(typeAttr);
243 if (!type.isEmpty() && !supportedImageType(type)) 292 if (!type.isEmpty() && !supportedImageType(type))
244 continue; 293 continue;
245 294
246 if (!source->mediaQueryMatches()) 295 if (!source->mediaQueryMatches())
247 continue; 296 continue;
248 297
249 unsigned effectiveSize = SizesAttributeParser::findEffectiveSize(source- >fastGetAttribute(sizesAttr), MediaValuesDynamic::create(document())); 298 SizesAttributeParser parser = SizesAttributeParser(MediaValuesDynamic::c reate(document()), source->fastGetAttribute(sizesAttr));
299 unsigned effectiveSize = parser.length();
300 m_effectiveSizeViewportDependant = parser.viewportDependant();
250 ImageCandidate candidate = bestFitSourceForSrcsetAttribute(document().de vicePixelRatio(), effectiveSize, source->fastGetAttribute(srcsetAttr)); 301 ImageCandidate candidate = bestFitSourceForSrcsetAttribute(document().de vicePixelRatio(), effectiveSize, source->fastGetAttribute(srcsetAttr));
251 if (candidate.isEmpty()) 302 if (candidate.isEmpty())
252 continue; 303 continue;
253 return candidate; 304 return candidate;
254 } 305 }
255 return ImageCandidate(); 306 return ImageCandidate();
256 } 307 }
257 308
258 RenderObject* HTMLImageElement::createRenderer(RenderStyle* style) 309 RenderObject* HTMLImageElement::createRenderer(RenderStyle* style)
259 { 310 {
(...skipping 301 matching lines...) Expand 10 before | Expand all | Expand 10 after
561 if (RuntimeEnabledFeatures::pictureEnabled()) { 612 if (RuntimeEnabledFeatures::pictureEnabled()) {
562 ImageCandidate candidate = findBestFitImageFromPictureParent(); 613 ImageCandidate candidate = findBestFitImageFromPictureParent();
563 if (!candidate.isEmpty()) { 614 if (!candidate.isEmpty()) {
564 setBestFitURLAndDPRFromImageCandidate(candidate); 615 setBestFitURLAndDPRFromImageCandidate(candidate);
565 foundURL = true; 616 foundURL = true;
566 } 617 }
567 } 618 }
568 619
569 if (!foundURL) { 620 if (!foundURL) {
570 unsigned effectiveSize = 0; 621 unsigned effectiveSize = 0;
571 if (RuntimeEnabledFeatures::pictureSizesEnabled()) 622 if (RuntimeEnabledFeatures::pictureSizesEnabled()) {
572 effectiveSize = SizesAttributeParser::findEffectiveSize(fastGetAttri bute(sizesAttr), MediaValuesDynamic::create(document())); 623 SizesAttributeParser parser = SizesAttributeParser(MediaValuesDynami c::create(document()), fastGetAttribute(sizesAttr));
624 effectiveSize = parser.length();
625 m_effectiveSizeViewportDependant = parser.viewportDependant();
626 }
573 ImageCandidate candidate = bestFitSourceForImageAttributes(document().de vicePixelRatio(), effectiveSize, fastGetAttribute(srcAttr), fastGetAttribute(src setAttr)); 627 ImageCandidate candidate = bestFitSourceForImageAttributes(document().de vicePixelRatio(), effectiveSize, fastGetAttribute(srcAttr), fastGetAttribute(src setAttr));
574 setBestFitURLAndDPRFromImageCandidate(candidate); 628 setBestFitURLAndDPRFromImageCandidate(candidate);
575 } 629 }
630 if (m_intrinsicSizingViewportDependant && m_effectiveSizeViewportDependant) {
631 createMediaQueryListIfDoesNotExist();
632 document().mediaQueryMatcher().addMediaQueryList(m_mediaQueryList.get()) ;
633 }
576 imageLoader().updateFromElement(behavior); 634 imageLoader().updateFromElement(behavior);
577 } 635 }
578 636
579 const KURL& HTMLImageElement::sourceURL() const 637 const KURL& HTMLImageElement::sourceURL() const
580 { 638 {
581 return cachedImage()->response().url(); 639 return cachedImage()->response().url();
582 } 640 }
583 641
584 } 642 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698