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

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: fix last test failure 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
« no previous file with comments | « Source/core/loader/ImageLoader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 void run() OVERRIDE
75 {
76 if (m_loader) {
77 m_loader->doUpdateFromElement(m_shouldBypassMainWorldContentSecurity Policy);
78 }
79 }
80
81 void clearLoader()
82 {
83 m_loader = 0;
84 }
85
86 WeakPtr<Task> createWeakPtr()
87 {
88 return m_weakFactory.createWeakPtr();
89 }
90
91 private:
92 ImageLoader* m_loader;
93 bool m_shouldBypassMainWorldContentSecurityPolicy;
94 WeakPtrFactory<Task> m_weakFactory;
95 };
96
59 ImageLoader::ImageLoader(Element* element) 97 ImageLoader::ImageLoader(Element* element)
60 : m_element(element) 98 : m_element(element)
61 , m_image(0) 99 , m_image(0)
62 , m_derefElementTimer(this, &ImageLoader::timerFired) 100 , m_derefElementTimer(this, &ImageLoader::timerFired)
63 , m_hasPendingLoadEvent(false) 101 , m_hasPendingLoadEvent(false)
64 , m_hasPendingErrorEvent(false) 102 , m_hasPendingErrorEvent(false)
65 , m_imageComplete(true) 103 , m_imageComplete(true)
66 , m_loadManually(false) 104 , m_loadManually(false)
67 , m_elementIsProtected(false) 105 , m_elementIsProtected(false)
68 , m_highPriorityClientCount(0) 106 , m_highPriorityClientCount(0)
69 { 107 {
70 } 108 }
71 109
72 ImageLoader::~ImageLoader() 110 ImageLoader::~ImageLoader()
73 { 111 {
112 if (m_pendingTask)
113 m_pendingTask->clearLoader();
114
74 if (m_image) 115 if (m_image)
75 m_image->removeClient(this); 116 m_image->removeClient(this);
76 117
77 ASSERT(m_hasPendingLoadEvent || !loadEventSender().hasPendingEvents(this)); 118 ASSERT(m_hasPendingLoadEvent || !loadEventSender().hasPendingEvents(this));
78 if (m_hasPendingLoadEvent) 119 if (m_hasPendingLoadEvent)
79 loadEventSender().cancelEvent(this); 120 loadEventSender().cancelEvent(this);
80 121
81 ASSERT(m_hasPendingErrorEvent || !errorEventSender().hasPendingEvents(this)) ; 122 ASSERT(m_hasPendingErrorEvent || !errorEventSender().hasPendingEvents(this)) ;
82 if (m_hasPendingErrorEvent) 123 if (m_hasPendingErrorEvent)
83 errorEventSender().cancelEvent(this); 124 errorEventSender().cancelEvent(this);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 if (newImage) 157 if (newImage)
117 newImage->addClient(this); 158 newImage->addClient(this);
118 if (oldImage) 159 if (oldImage)
119 oldImage->removeClient(this); 160 oldImage->removeClient(this);
120 } 161 }
121 162
122 if (RenderImageResource* imageResource = renderImageResource()) 163 if (RenderImageResource* imageResource = renderImageResource())
123 imageResource->resetAnimation(); 164 imageResource->resetAnimation();
124 } 165 }
125 166
126 void ImageLoader::updateFromElement() 167 void ImageLoader::doUpdateFromElement(bool bypassMainWorldCSP)
127 { 168 {
128 // Don't load images for inactive documents. We don't want to slow down the 169 // We don't need to call clearLoader here: Either we were called from the
129 // raw HTML parsing case by loading images we don't intend to display. 170 // task, or our caller updateFromElement cleared the task's loader (and set
171 // m_pendingTask to null).
172 m_pendingTask.clear();
173 // Make sure to only decrement the count when we exit this function
174 OwnPtr<IncrementLoadEventDelayCount> delayLoad;
175 delayLoad.swap(m_delayLoad);
176
130 Document& document = m_element->document(); 177 Document& document = m_element->document();
131 if (!document.isActive())
132 return;
133 178
134 AtomicString attr = m_element->imageSourceURL(); 179 AtomicString attr = m_element->imageSourceURL();
135 180
136 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL) 181 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; 182 ResourcePtr<ImageResource> newImage = 0;
142 if (!attr.isNull() && !stripLeadingAndTrailingHTMLSpaces(attr).isEmpty()) { 183 if (!url.isNull()) {
143 FetchRequest request(ResourceRequest(document.completeURL(sourceURI(attr ))), element()->localName()); 184 FetchRequest request(ResourceRequest(url), element()->localName());
185 if (bypassMainWorldCSP)
186 request.setContentSecurityCheck(DoNotCheckContentSecurityPolicy);
144 187
145 AtomicString crossOriginMode = m_element->fastGetAttribute(HTMLNames::cr ossoriginAttr); 188 AtomicString crossOriginMode = m_element->fastGetAttribute(HTMLNames::cr ossoriginAttr);
146 if (!crossOriginMode.isNull()) 189 if (!crossOriginMode.isNull())
147 request.setCrossOriginAccessControl(document.securityOrigin(), cross OriginMode); 190 request.setCrossOriginAccessControl(document.securityOrigin(), cross OriginMode);
148 191
149 if (m_loadManually) { 192 if (m_loadManually) {
150 bool autoLoadOtherImages = document.fetcher()->autoLoadImages(); 193 bool autoLoadOtherImages = document.fetcher()->autoLoadImages();
151 document.fetcher()->setAutoLoadImages(false); 194 document.fetcher()->setAutoLoadImages(false);
152 newImage = new ImageResource(request.resourceRequest()); 195 newImage = new ImageResource(request.resourceRequest());
153 newImage->setLoading(true); 196 newImage->setLoading(true);
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 } 254 }
212 255
213 if (RenderImageResource* imageResource = renderImageResource()) 256 if (RenderImageResource* imageResource = renderImageResource())
214 imageResource->resetAnimation(); 257 imageResource->resetAnimation();
215 258
216 // Only consider updating the protection ref-count of the Element immediatel y before returning 259 // 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. 260 // from this function as doing so might result in the destruction of this Im ageLoader.
218 updatedHasPendingEvent(); 261 updatedHasPendingEvent();
219 } 262 }
220 263
264 void ImageLoader::updateFromElement()
265 {
266 AtomicString attr = m_element->imageSourceURL();
267
268 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL)
269 return;
270
271 KURL url = imageURL();
272 if (!attr.isNull() && !url.isNull()) {
273 // If we have a pending task, we have to clear it -- either we're
274 // now loading immediately, or we need to reset the task's state.
275 if (m_pendingTask) {
276 m_pendingTask->clearLoader();
277 m_pendingTask.clear();
278 }
279 bool loadImmediately = shouldLoadImmediately(url);
280 if (loadImmediately) {
281 doUpdateFromElement(false);
282 } else {
283 OwnPtr<Task> task = adoptPtr(new Task(this));
284 m_pendingTask = task->createWeakPtr();
285 Microtask::enqueueMicrotask(task.release());
286 m_delayLoad = adoptPtr(new IncrementLoadEventDelayCount(m_element->d ocument()));
287 return;
288 }
289 } else {
290 doUpdateFromElement(false);
291 }
292 }
293
221 void ImageLoader::updateFromElementIgnoringPreviousError() 294 void ImageLoader::updateFromElementIgnoringPreviousError()
222 { 295 {
223 clearFailedLoadURL(); 296 clearFailedLoadURL();
224 updateFromElement(); 297 updateFromElement();
225 } 298 }
226 299
300 KURL ImageLoader::imageURL() const
301 {
302 KURL url;
303
304 // Don't load images for inactive documents. We don't want to slow down the
305 // raw HTML parsing case by loading images we don't intend to display.
306 Document& document = m_element->document();
307 if (!document.isActive())
308 return url;
309
310 AtomicString attr = m_element->imageSourceURL();
311
312 // Do not load any image if the 'src' attribute is missing or if it is
313 // an empty string.
314 if (!attr.isNull() && !stripLeadingAndTrailingHTMLSpaces(attr).isEmpty()) {
315 url = document.completeURL(sourceURI(attr));
316 }
317 return url;
318 }
319
320 bool ImageLoader::shouldLoadImmediately(const KURL& url) const
321 {
322 if (m_loadManually)
323 return true;
324 if (isHTMLObjectElement(m_element) || isHTMLEmbedElement(m_element))
325 return true;
326
327 if (url.protocolIsData())
328 return true;
329 if (memoryCache()->resourceForURL(url))
330 return true;
331 return false;
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
« no previous file with comments | « Source/core/loader/ImageLoader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698