Pass URL to Browser::new(), delegate url checking logic to 3rd party#16477
Pass URL to Browser::new(), delegate url checking logic to 3rd party#16477bors-servo merged 1 commit intoservo:masterfrom
Conversation
|
Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @cbrewster (or someone else) soon. |
8cb16b0 to
9504711
Compare
| Some(&opt_match.free[0][..]) | ||
| } else { | ||
| homepage_pref.as_string() | ||
| None |
There was a problem hiding this comment.
Flag: what should the url_opt be set to if it can't be parsed from the cmdline?
There was a problem hiding this comment.
Also, Servo exits early if it can't find a URL. See https://github.com/sadmansk/servo/blob/95047114eeed77c4c9b310e6d4f04d3a3295a9e2/components/config/opts.rs#L678
This is not necessary anymore. Because the initial URL is now handled in main.rs, main.rs can do 2 things: either use about:blank or exit with the same error message (now sure which option is best).
There was a problem hiding this comment.
How would you like me to handle the case if it is null? Should I print the usage or simply assign None?
There was a problem hiding this comment.
Forgive my newbiness, but I'm having some problem with assigning None on a matching statement. I couldn't search up exactly why it does not work. I was wondering if you could give me a pointer for the syntax.
error[E0308]: match arms have incompatible types
--> /home/sadman/repos/servo/components/config/opts.rs:673:15
|
673 | let url = match url_opt {
| _______________^ starting here...
674 | | Some(url_string) => {
675 | | parse_url_or_filename(&cwd, url_string)
676 | | .unwrap_or_else(|()| args_fail("URL parsing failed"))
677 | | },
678 | | None => None,
679 | | };
| |_____^ ...ending here: expected struct `servo_url::ServoUrl`, found enum `std::option::Option`
|
= note: expected type `servo_url::ServoUrl`
found type `std::option::Option<_>`
note: match arm with an incompatible type
--> /home/sadman/repos/servo/components/config/opts.rs:678:17
|
678 | None => None,
| ^^^^
There was a problem hiding this comment.
Forgive my newbiness
I've been through this too ^^
but I'm having some problem with assigning None on a matching statement. I couldn't search up exactly why it does not work. I was wondering if you could give me a pointer for the syntax.
Sure. The issue here is that the variable url is a ServoUrl. So the first branch of the match does return a ServoUrl. But the None branch returns a Option<ServoUrl> (which is None in this case).
Before your PR, we had a url_opt (optional url: Option<ServoUrl>) that was transformed to url (ServoUrl). Because we didn't want url to be optional. Now we want. So we want to keep url optional.
So in this function, we don't want to unwrap the url (which mean moving from Option<X> to X), we want to keep it optional.
So this block of code your working on, was doing 2 things: 1) calling parse_url_or_filename to make sure it's a valid url and that a file path is transformed into a url, and 2) unwrap the url to enforce it (not make it optional).
Now we only want 1) to happen.
Let me know if you need more info. You can also ping me on irc (paul).
There was a problem hiding this comment.
First of all, thanks so much for giving me the time!
I do understand what we now want, I'm just having a bit of trouble understanding how to do it. Like, for instance, do I need to completely replace the match block and try unwrapping url_opt and save the value in url_string, and then only call parse_url_or_file on url_string if it isn't equal to either None or empty string?
There was a problem hiding this comment.
Sounds about right.
The match block should be replaced with something that would achieve this:
if url_opt is None, keep it None.
If url_opt is Some(foo), url_opt should become Some(parse_url_or_filename(foo).unwrap()), but if the result of parse_url_or_filename(foo) is Err(), we should call args_fail (which will exit).
Achieving this should be easy with some combinaison of unwrap and map calls (methods of Option and Result types).
parse_url_or_filename return a Result that is more or less like an Option.
Make sure to understand:
- https://doc.rust-lang.org/std/option/
- https://doc.rust-lang.org/std/result/
This should be one or two lines of code. I let you try to understand what this should look like. Manipulating options is a core concept of Rust. It's worth the time understanding it.
Do not hesitate to ask more questions.
There was a problem hiding this comment.
The map function for Option should work very well here.
| if let Some(url) = url { | ||
| constellation_chan.send(ConstellationMsg::InitLoadUrl(url)).unwrap(); | ||
| }; | ||
| constellation_chan.send(ConstellationMsg::InitLoadUrl(url)).unwrap(); |
There was a problem hiding this comment.
Flag: should we do a check here to see if url is None?
There was a problem hiding this comment.
It can't be None at this point.
|
Thank you for taking care of this. I'll look at this PR early next week. |
| if let Some(url) = url { | ||
| constellation_chan.send(ConstellationMsg::InitLoadUrl(url)).unwrap(); | ||
| }; | ||
| constellation_chan.send(ConstellationMsg::InitLoadUrl(url)).unwrap(); |
There was a problem hiding this comment.
It can't be None at this point.
| Some(&opt_match.free[0][..]) | ||
| } else { | ||
| homepage_pref.as_string() | ||
| None |
ports/servo/main.rs
Outdated
| browser: Browser::new(window.clone()), | ||
| browser: Browser::new(window.clone(), opts::get().url.clone() | ||
| .unwrap_or(ServoUrl::parse(PREFS.get("shell.homepage").as_string().unwrap()) | ||
| .unwrap())) |
There was a problem hiding this comment.
opts::get().url default value should be None, not Some("about:blank") (see here).
If PREFS.get("shell.homepage") doesn't exist, we should use "about:blank" (or exist early).
So you always have a valid URL.
There was a problem hiding this comment.
about:blank sounds like a good idea to me, I'll go with that.
| Some(&opt_match.free[0][..]) | ||
| } else { | ||
| homepage_pref.as_string() | ||
| None |
There was a problem hiding this comment.
Also, Servo exits early if it can't find a URL. See https://github.com/sadmansk/servo/blob/95047114eeed77c4c9b310e6d4f04d3a3295a9e2/components/config/opts.rs#L678
This is not necessary anymore. Because the initial URL is now handled in main.rs, main.rs can do 2 things: either use about:blank or exit with the same error message (now sure which option is best).
|
r? @paulrouget |
|
@bors-servo delegate=paulrouget |
|
✌️ @paulrouget can now approve this pull request |
|
@sadmansk let me know if you have any question about my comments. |
|
@paulrouget just give me a few days, I have exams this week, I'll address your comments as soon as that's done 😅 |
9504711 to
a26079d
Compare
cb61240 to
fdaa887
Compare
|
@paulrouget Made that change you requested. Please take another look and let me know if there was anything else you wanted me to change. Thanks. |
ports/servo/main.rs
Outdated
| browser: Browser::new(window.clone(), | ||
| opts::get().url.clone() | ||
| .unwrap_or(ServoUrl::parse(PREFS.get("shell.homepage").as_string() | ||
| .unwrap_or("about:blank")).unwrap())) |
There was a problem hiding this comment.
This line should get at least one more level of indentation to show that the method isn't operating at the same level as the previous line.
There was a problem hiding this comment.
opts.rs' from_cmdline_args method needs some more work. url_opt can be None, and this is valid now as we fallback to the preference or about:blank in main.rs. See
servo/components/config/opts.rs
Line 681 in 7338205
Also, the default url should be None I think:
servo/components/config/opts.rs
Line 506 in 7338205
ports/servo/main.rs
Outdated
| browser: Browser::new(window.clone(), | ||
| opts::get().url.clone() | ||
| .unwrap_or(ServoUrl::parse(PREFS.get("shell.homepage").as_string() | ||
| .unwrap_or("about:blank")).unwrap())) |
There was a problem hiding this comment.
Can you split that in a way that it's easier to understand?
Maybe move the url resolution above, and add a comment to explain the logic.
6c66110 to
c867f1e
Compare
|
@paulrouget I made the requested changes as I understood. Please let me know if I misunderstood anything. Also, if the parsing of the URL fails, i.e. |
|
☔ The latest upstream changes (presumably #16757) made this pull request unmergeable. Please resolve the merge conflicts. |
b5ccecb to
656bf8f
Compare
paulrouget
left a comment
There was a problem hiding this comment.
Address my comments, squash and rebase, and I think that should do it.
Thank you!
components/config/opts.rs
Outdated
| match parse_url_or_filename(&cwd, url_string) { | ||
| Ok(v) => Some(v), | ||
| Err(_) => None, | ||
| } |
There was a problem hiding this comment.
You can transform an Result into an Option with parse_url_or_filename().ok().
See https://doc.rust-lang.org/std/result/enum.Result.html#method.ok
components/config/opts.rs
Outdated
| let opts = Opts { | ||
| is_running_problem_test: is_running_problem_test, | ||
| url: Some(url), | ||
| url: url, |
There was a problem hiding this comment.
Keep url_opt:
url: url_opt
components/config/opts.rs
Outdated
| args_fail("servo asks that you provide a URL") | ||
| } | ||
| None => None, | ||
| }; |
There was a problem hiding this comment.
I think you can combine all of this with and_then, something like this:
let url_opt = url_opt.and_then(|url_string| parse_url_or_filename(…).ok());Also, rename url to url_opt (still an option at this point)
Yes, maybe a warning. |
|
Ok. It looks good now. You also need to update |
|
updated |
| let servo_browser = Browser::new(glutin_window.clone()); | ||
| let home_url = ServoUrl::parse(PREFS.get("shell.homepage").as_string() | ||
| .unwrap_or("about:blank")).unwrap(); | ||
| let servo_browser = Browser::new(glutin_window.clone(), home_url); |
There was a problem hiding this comment.
If I'm not mistaken, ServoCefBrowser::new is called by browser_host_create, which comes with a url. So you should use it instead of use about:blank.
There was a problem hiding this comment.
Sorry a bit confused, should I change the function signature of ServoCefBrowser::new so that it gets the url from browser_host_create and use that instead?
There was a problem hiding this comment.
@paulrouget Hey, still waiting on this clarification. Please let me know at your earliest convenience
There was a problem hiding this comment.
should I change the function signature of ServoCefBrowser::new so that it gets the url from browser_host_create and use that instead?
Yes.
And then I don't think it's necessary to call frame.set_url:
Line 274 in d767e60
| use interfaces::{cef_browser_t, cef_browser_host_t, cef_client_t, cef_frame_t}; | ||
| use interfaces::{cef_request_context_t}; | ||
| use servo::Browser; | ||
| use servo::servo_config::prefs::PREFS; |
There was a problem hiding this comment.
I don't know much about CEF, but I think you should not use PREFS here. Just use about:blank directly.
| window.set_browser(self.clone()); | ||
| let servo_browser = Browser::new(window.clone()); | ||
| let home_url = ServoUrl::parse(PREFS.get("shell.homepage").as_string() | ||
| .unwrap_or("about:blank")).unwrap(); |
There was a problem hiding this comment.
assign about:blank directly.
|
@sadmansk anything I can do to help? |
|
@paulrouget Sorry about that, it's been a really busy week at work, so didn't get a chance to work on it. I will get to it as soon as I catch a breath |
|
No worries. Just wanted to make sure you were not stuck. |
|
@sadmansk if you want, we can land this as it is, and add the url to |
|
☔ The latest upstream changes (presumably #17068) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@paulrouget yeah ofcourse, that sounds like a good plan, i'll rebase tonight and ping you for the merge, and also start working on the cef browser stuff. |
3831b6a to
13b1815
Compare
Use Option instead of ServoUrl for url in opts Cleaner mapping from parse_url to url_opt Add comment about url parsing error Print reason for parsing error Remove comment about warning Add home url when openning new browser window in CEF Fix merge error
be1d68f to
708c561
Compare
|
@paulrouget merge is ready now, i'll work on the cef browser things on a different PR |
|
@bors-servo r+ |
|
📌 Commit 708c561 has been approved by |
Pass URL to Browser::new(), delegate url checking logic to 3rd party <!-- Please describe your changes on the following line: --> 1. Move the logic of computing the initial url from `opts.rs` to `/ports/servo/main.rs` 2. Add a `ServoUrl` argument to `Browser::new` Based on the requested changes by @paulrouget: >We can read the pref in main() instead. shell.homepage would be used if the url is not passed as an argument. I'm trying to decouple the "app" logic and the "web engine" logic. I think it's up to the app to set the initial URL, and I'm not sure the initial url should be part of opts. --- <!-- 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 #15636 <!-- Either: --> - [ ] There are tests for these changes <!-- 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/16477) <!-- Reviewable:end -->
|
CEF Follow up: #17273 |
|
☀️ Test successful - android, arm32, arm64, linux-dev, linux-rel-css, linux-rel-wpt, mac-dev-unit, mac-rel-css1, mac-rel-css2, mac-rel-wpt1, mac-rel-wpt2, mac-rel-wpt3, mac-rel-wpt4, windows-msvc-dev |
opts.rsto/ports/servo/main.rsServoUrlargument toBrowser::newBased on the requested changes by @paulrouget:
./mach build -ddoes not report any errors./mach test-tidydoes not report any errorsThis change is