Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upPass URL to Browser::new(), delegate url checking logic to 3rd party #16477
Conversation
highfive
commented
Apr 15, 2017
|
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. |
| let url_opt = if !opt_match.free.is_empty() { | ||
| Some(&opt_match.free[0][..]) | ||
| } else { | ||
| homepage_pref.as_string() | ||
| None |
This comment has been minimized.
This comment has been minimized.
sadmansk
Apr 15, 2017
Author
Contributor
Flag: what should the url_opt be set to if it can't be parsed from the cmdline?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
paulrouget
Apr 17, 2017
•
Contributor
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).
This comment has been minimized.
This comment has been minimized.
sadmansk
Apr 25, 2017
Author
Contributor
How would you like me to handle the case if it is null? Should I print the usage or simply assign None?
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
sadmansk
Apr 25, 2017
Author
Contributor
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,
| ^^^^
This comment has been minimized.
This comment has been minimized.
paulrouget
Apr 25, 2017
•
Contributor
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).
This comment has been minimized.
This comment has been minimized.
sadmansk
Apr 25, 2017
Author
Contributor
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?
This comment has been minimized.
This comment has been minimized.
paulrouget
Apr 25, 2017
Contributor
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:
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.
This comment has been minimized.
This comment has been minimized.
| if let Some(url) = url { | ||
| constellation_chan.send(ConstellationMsg::InitLoadUrl(url)).unwrap(); | ||
| }; | ||
| constellation_chan.send(ConstellationMsg::InitLoadUrl(url)).unwrap(); |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
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(); |
This comment has been minimized.
This comment has been minimized.
| let url_opt = if !opt_match.free.is_empty() { | ||
| Some(&opt_match.free[0][..]) | ||
| } else { | ||
| homepage_pref.as_string() | ||
| None |
This comment has been minimized.
This comment has been minimized.
| 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())) |
This comment has been minimized.
This comment has been minimized.
paulrouget
Apr 17, 2017
Contributor
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.
This comment has been minimized.
This comment has been minimized.
sadmansk
Apr 23, 2017
Author
Contributor
about:blank sounds like a good idea to me, I'll go with that.
| let url_opt = if !opt_match.free.is_empty() { | ||
| Some(&opt_match.free[0][..]) | ||
| } else { | ||
| homepage_pref.as_string() | ||
| None |
This comment has been minimized.
This comment has been minimized.
paulrouget
Apr 17, 2017
•
Contributor
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 |
|
|
|
@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 |
|
@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. |
| browser: Browser::new(window.clone(), | ||
| opts::get().url.clone() | ||
| .unwrap_or(ServoUrl::parse(PREFS.get("shell.homepage").as_string() | ||
| .unwrap_or("about:blank")).unwrap())) |
This comment has been minimized.
This comment has been minimized.
KiChjang
Apr 24, 2017
Member
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.
|
opts.rs' servo/components/config/opts.rs Line 681 in 7338205 Also, the default url should be servo/components/config/opts.rs Line 506 in 7338205 |
| browser: Browser::new(window.clone(), | ||
| opts::get().url.clone() | ||
| .unwrap_or(ServoUrl::parse(PREFS.get("shell.homepage").as_string() | ||
| .unwrap_or("about:blank")).unwrap())) |
This comment has been minimized.
This comment has been minimized.
paulrouget
Apr 24, 2017
Contributor
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.
|
@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. |
|
|
|
Address my comments, squash and rebase, and I think that should do it. Thank you! |
| match parse_url_or_filename(&cwd, url_string) { | ||
| Ok(v) => Some(v), | ||
| Err(_) => None, | ||
| } |
This comment has been minimized.
This comment has been minimized.
paulrouget
May 8, 2017
Contributor
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
| @@ -795,7 +793,7 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult { | |||
|
|
|||
| let opts = Opts { | |||
| is_running_problem_test: is_running_problem_test, | |||
| url: Some(url), | |||
| url: url, | |||
This comment has been minimized.
This comment has been minimized.
| print_usage(app_name, &opts); | ||
| args_fail("servo asks that you provide a URL") | ||
| } | ||
| None => None, | ||
| }; |
This comment has been minimized.
This comment has been minimized.
paulrouget
May 8, 2017
Contributor
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); |
This comment has been minimized.
This comment has been minimized.
paulrouget
May 11, 2017
Contributor
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.
This comment has been minimized.
This comment has been minimized.
sadmansk
May 20, 2017
•
Author
Contributor
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?
This comment has been minimized.
This comment has been minimized.
sadmansk
Jun 2, 2017
Author
Contributor
@paulrouget Hey, still waiting on this clarification. Please let me know at your earliest convenience
This comment has been minimized.
This comment has been minimized.
paulrouget
Jun 5, 2017
Contributor
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
| @@ -9,6 +9,8 @@ use interfaces::{CefBrowser, CefBrowserHost, CefClient, CefFrame, CefRequestCont | |||
| 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; | |||
This comment has been minimized.
This comment has been minimized.
paulrouget
May 11, 2017
Contributor
I don't know much about CEF, but I think you should not use PREFS here. Just use about:blank directly.
| @@ -171,7 +175,9 @@ impl ServoCefBrowserExtensions for CefBrowser { | |||
| if window_info.windowless_rendering_enabled != 0 { | |||
| let window = window::Window::new(window_info.width, window_info.height); | |||
| 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(); | |||
This comment has been minimized.
This comment has been minimized.
|
@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 |
|
|
|
@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. |
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
|
@paulrouget merge is ready now, i'll work on the cef browser things on a different PR |
|
@bors-servo r+ |
|
|
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 |
|
|
sadmansk commentedApr 15, 2017
•
edited by larsbergstrom
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