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

Side by Side Diff: Source/core/loader/ImageLoader.cpp

Issue 200923002: Post a microtask to load <img> elements. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: review comments (retry) Created 6 years, 7 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 | Annotate | Revision Log
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, 2009, 2010 Apple Inc. All rights reserv ed. 4 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010 Apple Inc. All rights reserv ed.
5 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
11 * This library is distributed in the hope that it will be useful, 11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details. 14 * Library General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU Library General Public License 16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to 17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA. 19 * Boston, MA 02110-1301, USA.
20 */ 20 */
21 21
22 #include "config.h" 22 #include "config.h"
23 #include "core/loader/ImageLoader.h" 23 #include "core/loader/ImageLoader.h"
24 24
25 #include "bindings/v8/ScriptController.h"
25 #include "core/dom/Document.h" 26 #include "core/dom/Document.h"
26 #include "core/dom/Element.h" 27 #include "core/dom/Element.h"
28 #include "core/dom/IncrementLoadEventDelayCount.h"
29 #include "core/dom/Microtask.h"
27 #include "core/events/Event.h" 30 #include "core/events/Event.h"
28 #include "core/events/EventSender.h" 31 #include "core/events/EventSender.h"
29 #include "core/fetch/CrossOriginAccessControl.h" 32 #include "core/fetch/CrossOriginAccessControl.h"
30 #include "core/fetch/FetchRequest.h" 33 #include "core/fetch/FetchRequest.h"
31 #include "core/fetch/MemoryCache.h" 34 #include "core/fetch/MemoryCache.h"
32 #include "core/fetch/ResourceFetcher.h" 35 #include "core/fetch/ResourceFetcher.h"
33 #include "core/html/HTMLObjectElement.h" 36 #include "core/frame/LocalFrame.h"
37 #include "core/html/HTMLImageElement.h"
34 #include "core/html/parser/HTMLParserIdioms.h" 38 #include "core/html/parser/HTMLParserIdioms.h"
35 #include "core/rendering/RenderImage.h" 39 #include "core/rendering/RenderImage.h"
36 #include "core/rendering/RenderVideo.h" 40 #include "core/rendering/RenderVideo.h"
37 #include "core/rendering/svg/RenderSVGImage.h" 41 #include "core/rendering/svg/RenderSVGImage.h"
38 #include "platform/weborigin/SecurityOrigin.h" 42 #include "platform/weborigin/SecurityOrigin.h"
39 43
40 namespace WebCore { 44 namespace WebCore {
41 45
42 static ImageEventSender& loadEventSender() 46 static ImageEventSender& loadEventSender()
43 { 47 {
44 DEFINE_STATIC_LOCAL(ImageEventSender, sender, (EventTypeNames::load)); 48 DEFINE_STATIC_LOCAL(ImageEventSender, sender, (EventTypeNames::load));
45 return sender; 49 return sender;
46 } 50 }
47 51
48 static ImageEventSender& errorEventSender() 52 static ImageEventSender& errorEventSender()
49 { 53 {
50 DEFINE_STATIC_LOCAL(ImageEventSender, sender, (EventTypeNames::error)); 54 DEFINE_STATIC_LOCAL(ImageEventSender, sender, (EventTypeNames::error));
51 return sender; 55 return sender;
52 } 56 }
53 57
54 static inline bool pageIsBeingDismissed(Document* document) 58 static inline bool pageIsBeingDismissed(Document* document)
55 { 59 {
56 return document->pageDismissalEventBeingDispatched() != Document::NoDismissa l; 60 return document->pageDismissalEventBeingDispatched() != Document::NoDismissa l;
57 } 61 }
58 62
63 class ImageLoader::Task : public blink::WebThread::Task {
64 public:
65 Task(ImageLoader* loader)
66 : m_loader(loader)
67 , m_shouldBypassMainWorldContentSecurityPolicy(false)
68 , m_weakFactory(this)
69 {
70 LocalFrame* frame = loader->m_element->document().frame();
71 m_shouldBypassMainWorldContentSecurityPolicy = frame->script().shouldByp assMainWorldContentSecurityPolicy();
72 }
73
74 virtual ~Task()
75 {
76 clearLoader();
abarth-chromium 2014/05/03 01:22:31 Why do we need to clear the loader from the destru
cbiesinger 2014/05/05 22:39:30 Oh, yeah, a leftover. Let me remove that.
77 }
78
79 virtual void run() OVERRIDE
80 {
81 if (m_loader) {
82 m_loader->doUpdateFromElement(m_shouldBypassMainWorldContentSecurity Policy);
83 }
84 }
85
86 void clearLoader()
87 {
88 m_loader = 0;
89 }
90
91 WeakPtr<Task> createWeakPtr()
92 {
93 return m_weakFactory.createWeakPtr();
94 }
95
96 private:
97 ImageLoader* m_loader;
98 bool m_shouldBypassMainWorldContentSecurityPolicy;
99 WeakPtrFactory<Task> m_weakFactory;
100 };
101
59 ImageLoader::ImageLoader(Element* element) 102 ImageLoader::ImageLoader(Element* element)
60 : m_element(element) 103 : m_element(element)
61 , m_image(0) 104 , m_image(0)
62 , m_derefElementTimer(this, &ImageLoader::timerFired) 105 , m_derefElementTimer(this, &ImageLoader::timerFired)
63 , m_hasPendingLoadEvent(false) 106 , m_hasPendingLoadEvent(false)
64 , m_hasPendingErrorEvent(false) 107 , m_hasPendingErrorEvent(false)
65 , m_imageComplete(true) 108 , m_imageComplete(true)
66 , m_loadManually(false) 109 , m_loadManually(false)
67 , m_elementIsProtected(false) 110 , m_elementIsProtected(false)
68 , m_highPriorityClientCount(0) 111 , m_highPriorityClientCount(0)
69 { 112 {
70 } 113 }
71 114
72 ImageLoader::~ImageLoader() 115 ImageLoader::~ImageLoader()
73 { 116 {
117 if (m_pendingTask)
118 m_pendingTask->clearLoader();
119
74 if (m_image) 120 if (m_image)
75 m_image->removeClient(this); 121 m_image->removeClient(this);
76 122
77 ASSERT(m_hasPendingLoadEvent || !loadEventSender().hasPendingEvents(this)); 123 ASSERT(m_hasPendingLoadEvent || !loadEventSender().hasPendingEvents(this));
78 if (m_hasPendingLoadEvent) 124 if (m_hasPendingLoadEvent)
79 loadEventSender().cancelEvent(this); 125 loadEventSender().cancelEvent(this);
80 126
81 ASSERT(m_hasPendingErrorEvent || !errorEventSender().hasPendingEvents(this)) ; 127 ASSERT(m_hasPendingErrorEvent || !errorEventSender().hasPendingEvents(this)) ;
82 if (m_hasPendingErrorEvent) 128 if (m_hasPendingErrorEvent)
83 errorEventSender().cancelEvent(this); 129 errorEventSender().cancelEvent(this);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 if (newImage) 162 if (newImage)
117 newImage->addClient(this); 163 newImage->addClient(this);
118 if (oldImage) 164 if (oldImage)
119 oldImage->removeClient(this); 165 oldImage->removeClient(this);
120 } 166 }
121 167
122 if (RenderImageResource* imageResource = renderImageResource()) 168 if (RenderImageResource* imageResource = renderImageResource())
123 imageResource->resetAnimation(); 169 imageResource->resetAnimation();
124 } 170 }
125 171
126 void ImageLoader::updateFromElement() 172 void ImageLoader::doUpdateFromElement(bool bypassMainWorldCSP)
127 { 173 {
128 // Don't load images for inactive documents. We don't want to slow down the 174 m_pendingTask.clear();
abarth-chromium 2014/05/03 01:22:31 Do we need to call clearLoader() on the m_pendingT
cbiesinger 2014/05/05 22:39:30 No - let me add a comment to this effect: // W
129 // raw HTML parsing case by loading images we don't intend to display. 175 // Make sure to only decrement the count when we exit this function
176 OwnPtr<IncrementLoadEventDelayCount> delayLoad;
177 delayLoad.swap(m_delayLoad);
178
130 Document& document = m_element->document(); 179 Document& document = m_element->document();
131 if (!document.isActive())
132 return;
133 180
134 AtomicString attr = m_element->imageSourceURL(); 181 AtomicString attr = m_element->imageSourceURL();
135 182
136 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL) 183 KURL url = imageURL();
137 return;
138
139 // Do not load any image if the 'src' attribute is missing or if it is
140 // an empty string.
141 ResourcePtr<ImageResource> newImage = 0; 184 ResourcePtr<ImageResource> newImage = 0;
142 if (!attr.isNull() && !stripLeadingAndTrailingHTMLSpaces(attr).isEmpty()) { 185 if (!url.isNull()) {
143 FetchRequest request(ResourceRequest(document.completeURL(sourceURI(attr ))), element()->localName()); 186 FetchRequest request(ResourceRequest(url), element()->localName());
187 if (bypassMainWorldCSP)
188 request.setContentSecurityCheck(DoNotCheckContentSecurityPolicy);
144 189
145 AtomicString crossOriginMode = m_element->fastGetAttribute(HTMLNames::cr ossoriginAttr); 190 AtomicString crossOriginMode = m_element->fastGetAttribute(HTMLNames::cr ossoriginAttr);
146 if (!crossOriginMode.isNull()) 191 if (!crossOriginMode.isNull())
147 request.setCrossOriginAccessControl(document.securityOrigin(), cross OriginMode); 192 request.setCrossOriginAccessControl(document.securityOrigin(), cross OriginMode);
148 193
149 if (m_loadManually) { 194 if (m_loadManually) {
150 bool autoLoadOtherImages = document.fetcher()->autoLoadImages(); 195 bool autoLoadOtherImages = document.fetcher()->autoLoadImages();
151 document.fetcher()->setAutoLoadImages(false); 196 document.fetcher()->setAutoLoadImages(false);
152 newImage = new ImageResource(request.resourceRequest()); 197 newImage = new ImageResource(request.resourceRequest());
153 newImage->setLoading(true); 198 newImage->setLoading(true);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 } 256 }
212 257
213 if (RenderImageResource* imageResource = renderImageResource()) 258 if (RenderImageResource* imageResource = renderImageResource())
214 imageResource->resetAnimation(); 259 imageResource->resetAnimation();
215 260
216 // Only consider updating the protection ref-count of the Element immediatel y before returning 261 // Only consider updating the protection ref-count of the Element immediatel y before returning
217 // from this function as doing so might result in the destruction of this Im ageLoader. 262 // from this function as doing so might result in the destruction of this Im ageLoader.
218 updatedHasPendingEvent(); 263 updatedHasPendingEvent();
219 } 264 }
220 265
266 void ImageLoader::updateFromElement()
267 {
268 AtomicString attr = m_element->imageSourceURL();
269
270 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL)
271 return;
272
273 KURL url = imageURL();
274 if (!attr.isNull() && !url.isNull()) {
275 // If we have a pending task, we have to clear it -- either we're
276 // now loading immediately, or we need to reset the task's state.
277 if (m_pendingTask) {
278 m_pendingTask->clearLoader();
279 m_pendingTask.clear();
280 }
281 bool loadImmediately = shouldLoadImmediately(url);
282 if (loadImmediately) {
283 doUpdateFromElement(false);
284 } else {
285 OwnPtr<Task> task = adoptPtr(new Task(this));
286 m_pendingTask = task->createWeakPtr();
287 Microtask::enqueueMicrotask(task.release());
288 m_delayLoad = adoptPtr(new IncrementLoadEventDelayCount(m_element->d ocument()));
289 return;
290 }
291 } else {
292 doUpdateFromElement(false);
293 }
294 }
295
221 void ImageLoader::updateFromElementIgnoringPreviousError() 296 void ImageLoader::updateFromElementIgnoringPreviousError()
222 { 297 {
223 clearFailedLoadURL(); 298 clearFailedLoadURL();
224 updateFromElement(); 299 updateFromElement();
225 } 300 }
226 301
302 KURL ImageLoader::imageURL() const
303 {
304 KURL url;
305
306 // Don't load images for inactive documents. We don't want to slow down the
307 // raw HTML parsing case by loading images we don't intend to display.
308 Document& document = m_element->document();
309 if (!document.isActive())
310 return url;
311
312 AtomicString attr = m_element->imageSourceURL();
313
314 // Do not load any image if the 'src' attribute is missing or if it is
315 // an empty string.
316 if (!attr.isNull() && !stripLeadingAndTrailingHTMLSpaces(attr).isEmpty()) {
Erik Dahlström (inactive) 2014/05/05 08:41:01 technically this may not always be a 'src' attribu
cbiesinger 2014/05/05 22:39:30 I'm not changing this code (and comment) - it's id
317 url = document.completeURL(sourceURI(attr));
318 }
319 return url;
320 }
321
322 bool ImageLoader::shouldLoadImmediately(const KURL& url) const
323 {
324 if (m_loadManually)
325 return true;
326
327 if (url.protocolIsData())
328 return true;
329 if (memoryCache()->resourceForURL(url))
330 return true;
331 return false;
Erik Dahlström (inactive) 2014/05/05 08:41:01 should we delay loads for <svg:image> elements?
cbiesinger 2014/05/05 22:39:30 I think so. Do you disagree?
332 }
333
227 void ImageLoader::notifyFinished(Resource* resource) 334 void ImageLoader::notifyFinished(Resource* resource)
228 { 335 {
229 ASSERT(m_failedLoadURL.isEmpty()); 336 ASSERT(m_failedLoadURL.isEmpty());
230 ASSERT(resource == m_image.get()); 337 ASSERT(resource == m_image.get());
231 338
232 m_imageComplete = true; 339 m_imageComplete = true;
233 updateRenderer(); 340 updateRenderer();
234 341
235 if (!m_hasPendingLoadEvent) 342 if (!m_hasPendingLoadEvent)
236 return; 343 return;
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 loadEventSender().dispatchPendingEvents(); 489 loadEventSender().dispatchPendingEvents();
383 } 490 }
384 491
385 void ImageLoader::dispatchPendingErrorEvents() 492 void ImageLoader::dispatchPendingErrorEvents()
386 { 493 {
387 errorEventSender().dispatchPendingEvents(); 494 errorEventSender().dispatchPendingEvents();
388 } 495 }
389 496
390 void ImageLoader::elementDidMoveToNewDocument() 497 void ImageLoader::elementDidMoveToNewDocument()
391 { 498 {
499 if (m_delayLoad) {
500 m_delayLoad->documentChanged(m_element->document());
501 }
392 clearFailedLoadURL(); 502 clearFailedLoadURL();
393 setImage(0); 503 setImage(0);
394 } 504 }
395 505
396 void ImageLoader::sourceImageChanged() 506 void ImageLoader::sourceImageChanged()
397 { 507 {
398 ImageLoaderClientSet::iterator end = m_clients.end(); 508 ImageLoaderClientSet::iterator end = m_clients.end();
399 for (ImageLoaderClientSet::iterator it = m_clients.begin(); it != end; ++it) { 509 for (ImageLoaderClientSet::iterator it = m_clients.begin(); it != end; ++it) {
400 ImageLoaderClient* handle = *it; 510 ImageLoaderClient* handle = *it;
401 handle->notifyImageSourceChanged(); 511 handle->notifyImageSourceChanged();
402 } 512 }
403 } 513 }
404 514
405 inline void ImageLoader::clearFailedLoadURL() 515 inline void ImageLoader::clearFailedLoadURL()
406 { 516 {
407 m_failedLoadURL = AtomicString(); 517 m_failedLoadURL = AtomicString();
408 } 518 }
409 519
410 } 520 }
OLDNEW
« Source/core/dom/IncrementLoadEventDelayCount.h ('K') | « Source/core/loader/ImageLoader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698