Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ImageCache::find_image_or_metadata -> ImageCache::{get_image, track_image} #23661

Merged
merged 2 commits into from Apr 17, 2020

Conversation

@julientregoat
Copy link
Contributor

julientregoat commented Jun 30, 2019

Updated the ImageCache trait to replace find_image_or_metadata with two new functions track_image and get_image, as well as a new enum (ImageCacheResult).

As a result, I was able to refactor the functions that previously called find_image_or_metadata pretty cleanly. For a list of these functions, please see the commit information.


  • ./mach build -d does not report any errors
  • ./mach test-tidy does not report any errors
  • These changes fix #21289 (GitHub issue number if applicable)
  • There are tests for these changes OR
  • These changes do not require tests because tests already exist for these components. I ran cargo test in net, net_traits, layout, and script successfully.

This change is Reviewable

@highfive
Copy link

highfive commented Jun 30, 2019

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @asajeffrey (or someone else) soon.

@highfive
Copy link

highfive commented Jun 30, 2019

Heads up! This PR modifies the following files:

  • @asajeffrey: components/script/dom/htmlvideoelement.rs, components/script/dom/canvasrenderingcontext2d.rs, components/script/dom/htmlimageelement.rs, components/script/dom/htmlcanvaselement.rs
  • @KiChjang: components/script/dom/htmlvideoelement.rs, components/script/dom/canvasrenderingcontext2d.rs, components/net/image_cache.rs, components/net_traits/image_cache.rs, components/script/dom/htmlimageelement.rs and 1 more
  • @emilio: components/layout/context.rs
@highfive
Copy link

highfive commented Jun 30, 2019

warning Warning warning

  • These commits modify net, layout, and script code, but no tests are modified. Please consider adding a test!
@julientregoat julientregoat force-pushed the julientregoat:i-21289 branch from d79cffc to 4c700ef Jul 2, 2019
@julientregoat
Copy link
Contributor Author

julientregoat commented Jul 2, 2019

@jdm re:

It's possible we could split this further into APIs that are allowed to load a placeholder and those that aren't, and reduce the set of possible responses that be received in the case where a placeholder image is not allowed.

I explored this, but it seemed like every way I thought of refactoring it would add the same amount of code, or duplicate functionality. I was looking into areas that called these functions, disallowing placeholder images.From what I can tell, the UsePlaceholder enum ultimately comes down to ImageCacheStore::get_completed_image_if_available which handles it reasonably elegantly... but idk, I'm new to this codebase and I'm sure there's something I'm not understanding or missing. What did you have in mind for that part of the refactor?

components/layout/context.rs Outdated Show resolved Hide resolved
@julientregoat julientregoat force-pushed the julientregoat:i-21289 branch from 125e12c to 14b7460 Jul 2, 2019
@jdm jdm assigned jdm and unassigned asajeffrey Jul 4, 2019
Copy link
Member

jdm left a comment

Good start! I've left some suggestions that I think will reduce some of the duplicated code and make it easier to understand the intentions of the code using the image cache.

components/layout/context.rs Outdated Show resolved Hide resolved
components/net/image_cache.rs Outdated Show resolved Hide resolved
components/net/image_cache.rs Outdated Show resolved Hide resolved
components/script/dom/htmlimageelement.rs Outdated Show resolved Hide resolved
components/net_traits/image_cache.rs Show resolved Hide resolved
components/script/dom/htmlimageelement.rs Outdated Show resolved Hide resolved
@julientregoat
Copy link
Contributor Author

julientregoat commented Jul 11, 2019

thanks so much for the thorough review, will get on this ASAP!

@julientregoat julientregoat force-pushed the julientregoat:i-21289 branch 2 times, most recently from 214c84d to 3427dc1 Jul 12, 2019
Copy link
Member

jdm left a comment

This is much improved! I think we can still simplify the rules of the API a little bit more by ensuring that the callers cannot forget to add an image cache listener.

}

let (sender, _receiver) = ipc::channel().unwrap();

This comment has been minimized.

Copy link
@jdm

jdm Jul 19, 2019

Member

This looks suspicious to me - I believe callers of track_image should be passing a sender as an argument so they can make use of the receiver. Right now any notifications go into the void because the receiver is ignored completely.

This comment has been minimized.

Copy link
@julientregoat

julientregoat Jul 27, 2019

Author Contributor

not sure what I was thinking here!


Err(ImageState::NotRequested(id)) => {
ImageCacheResult::Pending(id) => add_cache_listener_for_element(image_cache, id, self),
ImageCacheResult::ReadyForRequest(id) => {
add_cache_listener_for_element(image_cache, id, self);

This comment has been minimized.

Copy link
@jdm

jdm Jul 19, 2019

Member

I think the idea I had is that the track_image API would automatically call ImageCache::add_listener so that users of the API would not have to do that.

// See if the image is already available
let result =
// Check for available image or start tracking.
let sender; // FIXME

This comment has been minimized.

Copy link
@julientregoat

julientregoat Jul 28, 2019

Author Contributor

@jdm I left this broken for the moment. I wasn't sure where & how I should generate the IpcSender for the call to track_image here, since the LayoutContext isn't an element like the other callers of track_image. Based on the comments in the code above I imagined what I would want to do would be to add a route to the ipc_channel::router::ROUTER with a callback that would trigger the reflow. However I also realized that it might make more sense to have the caller of get_or_request_image_or_meta pass a sender in. This would ultimately be Fragements and ImageFragmentInfos. any advice?

This comment has been minimized.

Copy link
@jdm

jdm Sep 4, 2019

Member

Ok, I spent a while looking at this, and I think we don't actually want to call track_image here, because it doesn't really fit the model of the other consumers of that API. This code cares about three states:

  1. is there an image/metadata already?
  2. is there an image cache entry pending?
  3. is there no image cache entry?

We only want to add a cache listener in states 2 and 3, and that listener is added in separate code in components/script/dom/window.rs (in Window::force_reflow). I think we should add a new image cache API called get_cached_image_status which returns ImageCacheResult and does not take a Sender argument. Hopefully we can find a good way to avoid duplicating the implementation of track_image.

@julientregoat julientregoat force-pushed the julientregoat:i-21289 branch from 09c74ef to 6994314 Jul 28, 2019
@jdm
Copy link
Member

jdm commented Sep 4, 2019

I'm really sorry for forgetting about this PR!

@jdm jdm force-pushed the julientregoat:i-21289 branch from 81a14de to d4e85f9 Apr 17, 2020
@jdm
Copy link
Member

jdm commented Apr 17, 2020

@bors-servo r+
Thanks!

@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

📌 Commit d4e85f9 has been approved by jdm

@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

Testing commit d4e85f9 with merge fccfe08...

bors-servo added a commit that referenced this pull request Apr 17, 2020
Refactor ImageCache::find_image_or_metadata -> ImageCache::{get_image, track_image}

<!-- Please describe your changes on the following line: -->
Updated the `ImageCache` trait to replace `find_image_or_metadata` with two new functions `track_image` and `get_image`, as well as a new enum (`ImageCacheResult`).

As a result, I was able to refactor the functions that previously called `find_image_or_metadata` pretty cleanly. For a list of these functions, please see the commit information.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #21289  (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because tests already exist for these components. I ran `cargo test` in `net`, `net_traits`, `layout`, and `script` successfully.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23661)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

💔 Test failed - status-taskcluster

@jdm
Copy link
Member

jdm commented Apr 17, 2020

@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

Testing commit d4e85f9 with merge 912ecb3...

bors-servo added a commit that referenced this pull request Apr 17, 2020
Refactor ImageCache::find_image_or_metadata -> ImageCache::{get_image, track_image}

<!-- Please describe your changes on the following line: -->
Updated the `ImageCache` trait to replace `find_image_or_metadata` with two new functions `track_image` and `get_image`, as well as a new enum (`ImageCacheResult`).

As a result, I was able to refactor the functions that previously called `find_image_or_metadata` pretty cleanly. For a list of these functions, please see the commit information.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #21289  (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because tests already exist for these components. I ran `cargo test` in `net`, `net_traits`, `layout`, and `script` successfully.

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23661)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

💔 Test failed - status-taskcluster

@jdm
Copy link
Member

jdm commented Apr 17, 2020

@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

Testing commit d4e85f9 with merge c9480c8...

@bors-servo
Copy link
Contributor

bors-servo commented Apr 17, 2020

☀️ Test successful - status-taskcluster
Approved by: jdm
Pushing c9480c8 to master...

@bors-servo bors-servo merged commit c9480c8 into servo:master Apr 17, 2020
2 checks passed
2 checks passed
Community-TC (pull_request) TaskGroup: success
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

7 participants
You can’t perform that action at this time.