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

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

Issue 2864253003: Split ImageLoader::SetImage() and set flags consistently in tests (Closed)
Patch Set: Update comment Created 3 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
« no previous file with comments | « third_party/WebKit/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 4 * Copyright (C) 2004, 2005, 2006, 2007, 2009, 2010 Apple Inc. All rights
5 * reserved. 5 * 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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 image_ = nullptr; 179 image_ = nullptr;
180 } 180 }
181 } 181 }
182 182
183 DEFINE_TRACE(ImageLoader) { 183 DEFINE_TRACE(ImageLoader) {
184 visitor->Trace(image_); 184 visitor->Trace(image_);
185 visitor->Trace(image_resource_for_image_document_); 185 visitor->Trace(image_resource_for_image_document_);
186 visitor->Trace(element_); 186 visitor->Trace(element_);
187 } 187 }
188 188
189 void ImageLoader::SetImage(ImageResourceContent* new_image) { 189 void ImageLoader::SetImageForTest(ImageResourceContent* new_image) {
190 DCHECK(new_image);
190 SetImageWithoutConsideringPendingLoadEvent(new_image); 191 SetImageWithoutConsideringPendingLoadEvent(new_image);
191 192
192 // Only consider updating the protection ref-count of the Element immediately 193 // Only consider updating the protection ref-count of the Element immediately
193 // before returning from this function as doing so might result in the 194 // before returning from this function as doing so might result in the
194 // destruction of this ImageLoader. 195 // destruction of this ImageLoader.
195 UpdatedHasPendingEvent(); 196 UpdatedHasPendingEvent();
196 } 197 }
197 198
199 void ImageLoader::ClearImage() {
200 SetImageWithoutConsideringPendingLoadEvent(nullptr);
201
202 // Only consider updating the protection ref-count of the Element immediately
203 // before returning from this function as doing so might result in the
204 // destruction of this ImageLoader.
205 UpdatedHasPendingEvent();
206 }
207
208 void ImageLoader::SetImageForImageDocument(ImageResource* new_image_resource) {
209 DCHECK(loading_image_document_);
210 DCHECK(new_image_resource);
211 DCHECK(new_image_resource->GetContent());
212
213 image_resource_for_image_document_ = new_image_resource;
214 SetImageWithoutConsideringPendingLoadEvent(new_image_resource->GetContent());
215
216 // |has_pending_load_event_| is always false and |image_complete_| is
217 // always true for ImageDocument loading, while the loading is just started.
218 // TODO(hiroshige): clean up the behavior of flags. https://crbug.com/719759
219 has_pending_load_event_ = false;
220 image_complete_ = true;
221
222 // Only consider updating the protection ref-count of the Element immediately
223 // before returning from this function as doing so might result in the
224 // destruction of this ImageLoader.
225 UpdatedHasPendingEvent();
226 }
227
198 void ImageLoader::SetImageWithoutConsideringPendingLoadEvent( 228 void ImageLoader::SetImageWithoutConsideringPendingLoadEvent(
199 ImageResourceContent* new_image) { 229 ImageResourceContent* new_image) {
200 DCHECK(failed_load_url_.IsEmpty()); 230 DCHECK(failed_load_url_.IsEmpty());
201 ImageResourceContent* old_image = image_.Get(); 231 ImageResourceContent* old_image = image_.Get();
202 if (new_image != old_image) { 232 if (new_image != old_image) {
203 image_ = new_image;
204 if (has_pending_load_event_) { 233 if (has_pending_load_event_) {
205 LoadEventSender().CancelEvent(this); 234 LoadEventSender().CancelEvent(this);
206 has_pending_load_event_ = false; 235 has_pending_load_event_ = false;
207 } 236 }
208 if (has_pending_error_event_) { 237 if (has_pending_error_event_) {
209 ErrorEventSender().CancelEvent(this); 238 ErrorEventSender().CancelEvent(this);
210 has_pending_error_event_ = false; 239 has_pending_error_event_ = false;
211 } 240 }
212 image_complete_ = true; 241 UpdateImageState(new_image);
213 if (new_image) { 242 if (new_image) {
214 new_image->AddObserver(this); 243 new_image->AddObserver(this);
215 } 244 }
216 if (old_image) { 245 if (old_image) {
217 old_image->RemoveObserver(this); 246 old_image->RemoveObserver(this);
218 } 247 }
219 } 248 }
220 249
221 if (LayoutImageResource* image_resource = GetLayoutImageResource()) 250 if (LayoutImageResource* image_resource = GetLayoutImageResource())
222 image_resource->ResetAnimation(); 251 image_resource->ResetAnimation();
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 ReferrerPolicy referrer_policy) { 290 ReferrerPolicy referrer_policy) {
262 std::unique_ptr<Task> task = 291 std::unique_ptr<Task> task =
263 Task::Create(this, update_behavior, referrer_policy); 292 Task::Create(this, update_behavior, referrer_policy);
264 pending_task_ = task->CreateWeakPtr(); 293 pending_task_ = task->CreateWeakPtr();
265 Microtask::EnqueueMicrotask( 294 Microtask::EnqueueMicrotask(
266 WTF::Bind(&Task::Run, WTF::Passed(std::move(task)))); 295 WTF::Bind(&Task::Run, WTF::Passed(std::move(task))));
267 load_delay_counter_ = 296 load_delay_counter_ =
268 IncrementLoadEventDelayCount::Create(element_->GetDocument()); 297 IncrementLoadEventDelayCount::Create(element_->GetDocument());
269 } 298 }
270 299
300 void ImageLoader::UpdateImageState(ImageResourceContent* new_image) {
301 image_ = new_image;
302 has_pending_load_event_ = new_image;
303 image_complete_ = !new_image;
304 }
305
271 void ImageLoader::DoUpdateFromElement(BypassMainWorldBehavior bypass_behavior, 306 void ImageLoader::DoUpdateFromElement(BypassMainWorldBehavior bypass_behavior,
272 UpdateFromElementBehavior update_behavior, 307 UpdateFromElementBehavior update_behavior,
273 const KURL& url, 308 const KURL& url,
274 ReferrerPolicy referrer_policy) { 309 ReferrerPolicy referrer_policy) {
275 // FIXME: According to 310 // FIXME: According to
276 // http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-conten t.html#the-img-element:the-img-element-55 311 // http://www.whatwg.org/specs/web-apps/current-work/multipage/embedded-conten t.html#the-img-element:the-img-element-55
277 // When "update image" is called due to environment changes and the load 312 // When "update image" is called due to environment changes and the load
278 // fails, onerror should not be called. That is currently not the case. 313 // fails, onerror should not be called. That is currently not the case.
279 // 314 //
280 // We don't need to call clearLoader here: Either we were called from the 315 // We don't need to call clearLoader here: Either we were called from the
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 // cancelled by changing the src attribute. If newImage is null and 384 // cancelled by changing the src attribute. If newImage is null and
350 // has_pending_error_event_ is true, we know the error event has been just 385 // has_pending_error_event_ is true, we know the error event has been just
351 // posted by this load and we should not cancel the event. 386 // posted by this load and we should not cancel the event.
352 // FIXME: If both previous load and this one got blocked with an error, we 387 // FIXME: If both previous load and this one got blocked with an error, we
353 // can receive one error event instead of two. 388 // can receive one error event instead of two.
354 if (has_pending_error_event_ && new_image) { 389 if (has_pending_error_event_ && new_image) {
355 ErrorEventSender().CancelEvent(this); 390 ErrorEventSender().CancelEvent(this);
356 has_pending_error_event_ = false; 391 has_pending_error_event_ = false;
357 } 392 }
358 393
359 image_ = new_image; 394 UpdateImageState(new_image);
360 has_pending_load_event_ = new_image;
361 image_complete_ = !new_image;
362 395
363 UpdateLayoutObject(); 396 UpdateLayoutObject();
364 // If newImage exists and is cached, addObserver() will result in the load 397 // If newImage exists and is cached, addObserver() will result in the load
365 // event being queued to fire. Ensure this happens after beforeload is 398 // event being queued to fire. Ensure this happens after beforeload is
366 // dispatched. 399 // dispatched.
367 if (new_image) { 400 if (new_image) {
368 new_image->AddObserver(this); 401 new_image->AddObserver(this);
369 } 402 }
370 if (old_image) { 403 if (old_image) {
371 old_image->RemoveObserver(this); 404 old_image->RemoveObserver(this);
(...skipping 23 matching lines...) Expand all
395 // Prevent the creation of a ResourceLoader (and therefore a network request) 428 // Prevent the creation of a ResourceLoader (and therefore a network request)
396 // for ImageDocument loads. In this case, the image contents have already been 429 // for ImageDocument loads. In this case, the image contents have already been
397 // requested as a main resource and ImageDocumentParser will take care of 430 // requested as a main resource and ImageDocumentParser will take care of
398 // funneling the main resource bytes into image_, so just create an 431 // funneling the main resource bytes into image_, so just create an
399 // ImageResource to be populated later. 432 // ImageResource to be populated later.
400 if (loading_image_document_ && update_behavior != kUpdateForcedReload) { 433 if (loading_image_document_ && update_behavior != kUpdateForcedReload) {
401 ImageResource* image_resource = ImageResource::Create( 434 ImageResource* image_resource = ImageResource::Create(
402 ResourceRequest(ImageSourceToKURL(element_->ImageSourceURL()))); 435 ResourceRequest(ImageSourceToKURL(element_->ImageSourceURL())));
403 image_resource->SetStatus(ResourceStatus::kPending); 436 image_resource->SetStatus(ResourceStatus::kPending);
404 image_resource->NotifyStartLoad(); 437 image_resource->NotifyStartLoad();
405 image_resource_for_image_document_ = image_resource; 438 SetImageForImageDocument(image_resource);
406 SetImage(image_resource->GetContent());
407 return; 439 return;
408 } 440 }
409 441
410 // If we have a pending task, we have to clear it -- either we're now loading 442 // If we have a pending task, we have to clear it -- either we're now loading
411 // immediately, or we need to reset the task's state. 443 // immediately, or we need to reset the task's state.
412 if (pending_task_) { 444 if (pending_task_) {
413 pending_task_->ClearLoader(); 445 pending_task_->ClearLoader();
414 pending_task_.reset(); 446 pending_task_.reset();
415 } 447 }
416 448
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
632 } 664 }
633 665
634 void ImageLoader::DispatchPendingErrorEvents() { 666 void ImageLoader::DispatchPendingErrorEvents() {
635 ErrorEventSender().DispatchPendingEvents(); 667 ErrorEventSender().DispatchPendingEvents();
636 } 668 }
637 669
638 void ImageLoader::ElementDidMoveToNewDocument() { 670 void ImageLoader::ElementDidMoveToNewDocument() {
639 if (load_delay_counter_) 671 if (load_delay_counter_)
640 load_delay_counter_->DocumentChanged(element_->GetDocument()); 672 load_delay_counter_->DocumentChanged(element_->GetDocument());
641 ClearFailedLoadURL(); 673 ClearFailedLoadURL();
642 SetImage(0); 674 ClearImage();
643 } 675 }
644 676
645 } // namespace blink 677 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/loader/ImageLoader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698