Chromium Code Reviews| OLD | NEW |
|---|---|
| 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(); | |
| 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 Loading... | |
| 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 = WeakPtr<Task>(); |
| 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 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL) |
| 137 return; | 184 return; |
| 138 | 185 |
| 139 // Do not load any image if the 'src' attribute is missing or if it is | 186 KURL url = imageURL(); |
| 140 // an empty string. | |
| 141 ResourcePtr<ImageResource> newImage = 0; | 187 ResourcePtr<ImageResource> newImage = 0; |
| 142 if (!attr.isNull() && !stripLeadingAndTrailingHTMLSpaces(attr).isEmpty()) { | 188 if (!attr.isNull() && !url.isNull()) { |
| 143 FetchRequest request(ResourceRequest(document.completeURL(sourceURI(attr ))), element()->localName()); | 189 FetchRequest request(ResourceRequest(url), element()->localName()); |
| 190 if (bypassMainWorldCSP) | |
| 191 request.setContentSecurityCheck(DoNotCheckContentSecurityPolicy); | |
| 144 | 192 |
| 145 AtomicString crossOriginMode = m_element->fastGetAttribute(HTMLNames::cr ossoriginAttr); | 193 AtomicString crossOriginMode = m_element->fastGetAttribute(HTMLNames::cr ossoriginAttr); |
| 146 if (!crossOriginMode.isNull()) | 194 if (!crossOriginMode.isNull()) |
| 147 request.setCrossOriginAccessControl(document.securityOrigin(), cross OriginMode); | 195 request.setCrossOriginAccessControl(document.securityOrigin(), cross OriginMode); |
| 148 | 196 |
| 149 if (m_loadManually) { | 197 if (m_loadManually) { |
| 150 bool autoLoadOtherImages = document.fetcher()->autoLoadImages(); | 198 bool autoLoadOtherImages = document.fetcher()->autoLoadImages(); |
| 151 document.fetcher()->setAutoLoadImages(false); | 199 document.fetcher()->setAutoLoadImages(false); |
| 152 newImage = new ImageResource(request.resourceRequest()); | 200 newImage = new ImageResource(request.resourceRequest()); |
| 153 newImage->setLoading(true); | 201 newImage->setLoading(true); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 } | 259 } |
| 212 | 260 |
| 213 if (RenderImageResource* imageResource = renderImageResource()) | 261 if (RenderImageResource* imageResource = renderImageResource()) |
| 214 imageResource->resetAnimation(); | 262 imageResource->resetAnimation(); |
| 215 | 263 |
| 216 // Only consider updating the protection ref-count of the Element immediatel y before returning | 264 // 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. | 265 // from this function as doing so might result in the destruction of this Im ageLoader. |
| 218 updatedHasPendingEvent(); | 266 updatedHasPendingEvent(); |
| 219 } | 267 } |
| 220 | 268 |
| 269 void ImageLoader::updateFromElement() | |
| 270 { | |
| 271 AtomicString attr = m_element->imageSourceURL(); | |
| 272 | |
| 273 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL) | |
| 274 return; | |
|
abarth-chromium
2014/05/02 17:04:59
Do we need to do this check both here and in doUpd
cbiesinger
2014/05/02 23:24:04
Oh, no, can be removed from doUpdate. Done.
| |
| 275 | |
| 276 KURL url = imageURL(); | |
| 277 if (!attr.isNull() && !url.isNull()) { | |
|
abarth-chromium
2014/05/02 17:04:59
Won't imageURL() always return a null |url| if |at
cbiesinger
2014/05/02 23:24:04
Indeed, done.
| |
| 278 // If we have a pending task, we have to clear it -- either we're | |
| 279 // now loading immediately, or we need to reset the task's state. | |
| 280 if (m_pendingTask) { | |
| 281 m_pendingTask->clearLoader(); | |
| 282 m_pendingTask = WeakPtr<Task>(); | |
|
abarth-chromium
2014/05/02 17:04:59
We should add a clear() method to WeakPtr
cbiesinger
2014/05/02 23:24:04
Turns out that method exists and I just missed it
| |
| 283 } | |
| 284 bool loadImmediately = shouldLoadImmediately(url); | |
| 285 if (loadImmediately) { | |
| 286 doUpdateFromElement(false); | |
| 287 } else { | |
| 288 Task* task = new Task(this); | |
|
abarth-chromium
2014/05/02 17:04:59
OwnPtr<Task> task = adoptPtr(new Task(this));
All
cbiesinger
2014/05/02 23:24:04
Done.
| |
| 289 m_pendingTask = task->createWeakPtr(); | |
| 290 Microtask::enqueueMicrotask(adoptPtr(task)); | |
|
abarth-chromium
2014/05/02 17:04:59
s/adoptPtr(task)/task.release()/
cbiesinger
2014/05/02 23:24:04
Done.
| |
| 291 m_delayLoad = adoptPtr(new IncrementLoadEventDelayCount(m_element->d ocument())); | |
| 292 return; | |
| 293 } | |
| 294 } else { | |
| 295 doUpdateFromElement(false); | |
| 296 } | |
| 297 } | |
| 298 | |
| 221 void ImageLoader::updateFromElementIgnoringPreviousError() | 299 void ImageLoader::updateFromElementIgnoringPreviousError() |
| 222 { | 300 { |
| 223 clearFailedLoadURL(); | 301 clearFailedLoadURL(); |
| 224 updateFromElement(); | 302 updateFromElement(); |
| 225 } | 303 } |
| 226 | 304 |
| 305 KURL ImageLoader::imageURL() const | |
| 306 { | |
| 307 KURL url; | |
| 308 | |
| 309 // Don't load images for inactive documents. We don't want to slow down the | |
| 310 // raw HTML parsing case by loading images we don't intend to display. | |
| 311 Document& document = m_element->document(); | |
| 312 if (!document.isActive()) | |
| 313 return url; | |
| 314 | |
| 315 AtomicString attr = m_element->imageSourceURL(); | |
| 316 | |
| 317 if (!m_failedLoadURL.isEmpty() && attr == m_failedLoadURL) | |
| 318 return url; | |
|
abarth-chromium
2014/05/02 17:04:59
This is a third place this check appears. Shouldn
cbiesinger
2014/05/02 23:24:04
Yeah, we don't need it here, a leftover from an ea
| |
| 319 | |
| 320 // Do not load any image if the 'src' attribute is missing or if it is | |
| 321 // an empty string. | |
| 322 ResourcePtr<ImageResource> newImage = 0; | |
| 323 if (!attr.isNull() && !stripLeadingAndTrailingHTMLSpaces(attr).isEmpty()) { | |
| 324 url = document.completeURL(sourceURI(attr)); | |
| 325 } | |
| 326 return url; | |
| 327 } | |
| 328 | |
| 329 bool ImageLoader::shouldLoadImmediately(const KURL& url) const | |
| 330 { | |
| 331 if (m_loadManually) | |
| 332 return true; | |
| 333 | |
| 334 if (url.protocolIsData()) | |
| 335 return true; | |
| 336 if (memoryCache()->resourceForURL(url)) | |
| 337 return true; | |
| 338 return false; | |
| 339 } | |
| 340 | |
| 227 void ImageLoader::notifyFinished(Resource* resource) | 341 void ImageLoader::notifyFinished(Resource* resource) |
| 228 { | 342 { |
| 229 ASSERT(m_failedLoadURL.isEmpty()); | 343 ASSERT(m_failedLoadURL.isEmpty()); |
| 230 ASSERT(resource == m_image.get()); | 344 ASSERT(resource == m_image.get()); |
| 231 | 345 |
| 232 m_imageComplete = true; | 346 m_imageComplete = true; |
| 233 updateRenderer(); | 347 updateRenderer(); |
| 234 | 348 |
| 235 if (!m_hasPendingLoadEvent) | 349 if (!m_hasPendingLoadEvent) |
| 236 return; | 350 return; |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 382 loadEventSender().dispatchPendingEvents(); | 496 loadEventSender().dispatchPendingEvents(); |
| 383 } | 497 } |
| 384 | 498 |
| 385 void ImageLoader::dispatchPendingErrorEvents() | 499 void ImageLoader::dispatchPendingErrorEvents() |
| 386 { | 500 { |
| 387 errorEventSender().dispatchPendingEvents(); | 501 errorEventSender().dispatchPendingEvents(); |
| 388 } | 502 } |
| 389 | 503 |
| 390 void ImageLoader::elementDidMoveToNewDocument() | 504 void ImageLoader::elementDidMoveToNewDocument() |
| 391 { | 505 { |
| 506 if (m_delayLoad) { | |
| 507 m_delayLoad->documentChanged(m_element->document()); | |
| 508 } | |
| 392 clearFailedLoadURL(); | 509 clearFailedLoadURL(); |
| 393 setImage(0); | 510 setImage(0); |
| 394 } | 511 } |
| 395 | 512 |
| 396 void ImageLoader::sourceImageChanged() | 513 void ImageLoader::sourceImageChanged() |
| 397 { | 514 { |
| 398 ImageLoaderClientSet::iterator end = m_clients.end(); | 515 ImageLoaderClientSet::iterator end = m_clients.end(); |
| 399 for (ImageLoaderClientSet::iterator it = m_clients.begin(); it != end; ++it) { | 516 for (ImageLoaderClientSet::iterator it = m_clients.begin(); it != end; ++it) { |
| 400 ImageLoaderClient* handle = *it; | 517 ImageLoaderClient* handle = *it; |
| 401 handle->notifyImageSourceChanged(); | 518 handle->notifyImageSourceChanged(); |
| 402 } | 519 } |
| 403 } | 520 } |
| 404 | 521 |
| 405 inline void ImageLoader::clearFailedLoadURL() | 522 inline void ImageLoader::clearFailedLoadURL() |
| 406 { | 523 { |
| 407 m_failedLoadURL = AtomicString(); | 524 m_failedLoadURL = AtomicString(); |
| 408 } | 525 } |
| 409 | 526 |
| 410 } | 527 } |
| OLD | NEW |