Commit 3c675fda authored by Fredrik Söderquist's avatar Fredrik Söderquist Committed by Commit Bot

Avoid resolving the URL twice when initiating image loads

Rather than resolving the URL again during Task::Create, pass the KURL
resolved by ImageLoader::UpdateFromElement through.
Also move the resolution before the |loading_image_document_| special-
case.

Bug: 889183
Change-Id: I52ea65d1af96a5919e128f0623fa6bd9026850f7
Reviewed-on: https://chromium-review.googlesource.com/c/1254266Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Reviewed-by: default avatarTakashi Toyoshima <toyoshim@chromium.org>
Commit-Queue: Fredrik Söderquist <fs@opera.com>
Cr-Commit-Position: refs/heads/master@{#597071}
parent 42cb8ff2
...@@ -120,18 +120,22 @@ static ImageLoader::BypassMainWorldBehavior ShouldBypassMainWorldCSP( ...@@ -120,18 +120,22 @@ static ImageLoader::BypassMainWorldBehavior ShouldBypassMainWorldCSP(
class ImageLoader::Task { class ImageLoader::Task {
public: public:
static std::unique_ptr<Task> Create(ImageLoader* loader, static std::unique_ptr<Task> Create(ImageLoader* loader,
const KURL& request_url,
UpdateFromElementBehavior update_behavior, UpdateFromElementBehavior update_behavior,
ReferrerPolicy referrer_policy) { ReferrerPolicy referrer_policy) {
return std::make_unique<Task>(loader, update_behavior, referrer_policy); return std::make_unique<Task>(loader, request_url, update_behavior,
referrer_policy);
} }
Task(ImageLoader* loader, Task(ImageLoader* loader,
const KURL& request_url,
UpdateFromElementBehavior update_behavior, UpdateFromElementBehavior update_behavior,
ReferrerPolicy referrer_policy) ReferrerPolicy referrer_policy)
: loader_(loader), : loader_(loader),
should_bypass_main_world_csp_(ShouldBypassMainWorldCSP(loader)), should_bypass_main_world_csp_(ShouldBypassMainWorldCSP(loader)),
update_behavior_(update_behavior), update_behavior_(update_behavior),
referrer_policy_(referrer_policy), referrer_policy_(referrer_policy),
request_url_(request_url),
weak_factory_(this) { weak_factory_(this) {
ExecutionContext& context = loader_->GetElement()->GetDocument(); ExecutionContext& context = loader_->GetElement()->GetDocument();
probe::AsyncTaskScheduled(&context, "Image", this); probe::AsyncTaskScheduled(&context, "Image", this);
...@@ -146,8 +150,6 @@ class ImageLoader::Task { ...@@ -146,8 +150,6 @@ class ImageLoader::Task {
loader->GetElement()->GetDocument().GetFrame()); loader->GetElement()->GetDocument().GetFrame());
DCHECK(script_state_); DCHECK(script_state_);
} }
request_url_ =
loader->ImageSourceToKURL(loader->GetElement()->ImageSourceURL());
} }
void Run() { void Run() {
...@@ -388,10 +390,11 @@ inline void ImageLoader::ClearFailedLoadURL() { ...@@ -388,10 +390,11 @@ inline void ImageLoader::ClearFailedLoadURL() {
} }
inline void ImageLoader::EnqueueImageLoadingMicroTask( inline void ImageLoader::EnqueueImageLoadingMicroTask(
const KURL& request_url,
UpdateFromElementBehavior update_behavior, UpdateFromElementBehavior update_behavior,
ReferrerPolicy referrer_policy) { ReferrerPolicy referrer_policy) {
std::unique_ptr<Task> task = std::unique_ptr<Task> task =
Task::Create(this, update_behavior, referrer_policy); Task::Create(this, request_url, update_behavior, referrer_policy);
pending_task_ = task->GetWeakPtr(); pending_task_ = task->GetWeakPtr();
Microtask::EnqueueMicrotask( Microtask::EnqueueMicrotask(
WTF::Bind(&Task::Run, WTF::Passed(std::move(task)))); WTF::Bind(&Task::Run, WTF::Passed(std::move(task))));
...@@ -576,13 +579,15 @@ void ImageLoader::UpdateFromElement(UpdateFromElementBehavior update_behavior, ...@@ -576,13 +579,15 @@ void ImageLoader::UpdateFromElement(UpdateFromElementBehavior update_behavior,
ClearImage(); ClearImage();
} }
KURL url = ImageSourceToKURL(image_source_url);
// Prevent the creation of a ResourceLoader (and therefore a network request) // Prevent the creation of a ResourceLoader (and therefore a network request)
// for ImageDocument loads. In this case, the image contents have already been // for ImageDocument loads. In this case, the image contents have already been
// requested as a main resource and ImageDocumentParser will take care of // requested as a main resource and ImageDocumentParser will take care of
// funneling the main resource bytes into |image_content_|, so just create an // funneling the main resource bytes into |image_content_|, so just create an
// ImageResource to be populated later. // ImageResource to be populated later.
if (loading_image_document_) { if (loading_image_document_) {
ResourceRequest request(ImageSourceToKURL(element_->ImageSourceURL())); ResourceRequest request(url);
request.SetFetchCredentialsMode( request.SetFetchCredentialsMode(
network::mojom::FetchCredentialsMode::kOmit); network::mojom::FetchCredentialsMode::kOmit);
ImageResource* image_resource = ImageResource::Create(request); ImageResource* image_resource = ImageResource::Create(request);
...@@ -601,7 +606,6 @@ void ImageLoader::UpdateFromElement(UpdateFromElementBehavior update_behavior, ...@@ -601,7 +606,6 @@ void ImageLoader::UpdateFromElement(UpdateFromElementBehavior update_behavior,
delay_until_do_update_from_element_ = nullptr; delay_until_do_update_from_element_ = nullptr;
} }
KURL url = ImageSourceToKURL(image_source_url);
if (ShouldLoadImmediately(url)) { if (ShouldLoadImmediately(url)) {
DoUpdateFromElement(kDoNotBypassMainWorldCSP, update_behavior, url, DoUpdateFromElement(kDoNotBypassMainWorldCSP, update_behavior, url,
referrer_policy, UpdateType::kSync); referrer_policy, UpdateType::kSync);
...@@ -627,7 +631,7 @@ void ImageLoader::UpdateFromElement(UpdateFromElementBehavior update_behavior, ...@@ -627,7 +631,7 @@ void ImageLoader::UpdateFromElement(UpdateFromElementBehavior update_behavior,
// raw HTML parsing case by loading images we don't intend to display. // raw HTML parsing case by loading images we don't intend to display.
Document& document = element_->GetDocument(); Document& document = element_->GetDocument();
if (document.IsActive()) if (document.IsActive())
EnqueueImageLoadingMicroTask(update_behavior, referrer_policy); EnqueueImageLoadingMicroTask(url, update_behavior, referrer_policy);
} }
KURL ImageLoader::ImageSourceToKURL(AtomicString image_source_url) const { KURL ImageLoader::ImageSourceToKURL(AtomicString image_source_url) const {
......
...@@ -177,7 +177,9 @@ class CORE_EXPORT ImageLoader : public GarbageCollectedFinalized<ImageLoader>, ...@@ -177,7 +177,9 @@ class CORE_EXPORT ImageLoader : public GarbageCollectedFinalized<ImageLoader>,
void ClearFailedLoadURL(); void ClearFailedLoadURL();
void DispatchErrorEvent(); void DispatchErrorEvent();
void CrossSiteOrCSPViolationOccurred(AtomicString); void CrossSiteOrCSPViolationOccurred(AtomicString);
void EnqueueImageLoadingMicroTask(UpdateFromElementBehavior, ReferrerPolicy); void EnqueueImageLoadingMicroTask(const KURL&,
UpdateFromElementBehavior,
ReferrerPolicy);
KURL ImageSourceToKURL(AtomicString) const; KURL ImageSourceToKURL(AtomicString) const;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment