Skip to content

Conversation

@cbrevik
Copy link
Contributor

@cbrevik cbrevik commented Oct 31, 2016

Motivation
See #10055. As the issue says, a non-handled custom URL-scheme will throw an error on Android.
The motivation behind this PR is to make blocking URL-schemes more customizable.

In my specific use-case I do not want the URL to load, but I still need to intercept it. I've added a rejectedUrlSchemes array on WebView, which will let you set which schemes to block from loading, and a onUrlSchemeRejected event to notify you when an URL has been blocked.

On iOS you can normally handle it by implementing onShouldStartLoadWithRequest. But, what I've found is that onShouldStartLoadWithRequest can fail because it depends on an async callback from your JS code. If the code or device is a bit slow for whatever reason, it might not return true or false fast enough for onShouldStartLoadWithRequest.

So, this PR is an attempt to instead provide a synchronous alternative for blocking certain URL-schemes on both Android and iOS.

Test plan

<WebView
  source={{ uri: "customscheme://hello/2" }}
  onUrlSchemeRejected={(event) => console.log(event.nativeEvent)}
  rejectedUrlSchemes={["customscheme"]}
  />

The above should blacklist customscheme and block the above URL from loading. It should also log the given event which has an url and a scheme property.

@facebook-github-bot
Copy link
Contributor

By analyzing the blame information on this pull request, we identified @jacobp100 and @spicyj to be potential reviewers.

@facebook-github-bot facebook-github-bot added GH Review: review-needed CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. labels Oct 31, 2016
Copy link
Contributor

@jacobp100 jacobp100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good way to handle this.

});

if (!isJSNavigation && _urlSchemeBlacklist) {
for (id scheme in _urlSchemeBlacklist) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use [_urlSchemeBlacklist containsObject:request.URL.scheme]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure will do!

}

@ReactProp(name = "urlSchemeBlacklist")
public void setUrlSchemeBlacklist(WebView view, @Nullable ReadableArray urlSchemeBlacklistArray) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work when changing the urlBlacklistScheme from one non-null value to another non-null value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's from a non-null to another non-null, then the previous value will just be overwritten, right?
But, if you mean from a non-null value to a null value; yeah that would fail here. Will fix.


/**
* An array defining blacklisted URL-schemes.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we call this something like rejectedUrlSchemes and onUrlSchemeRejected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I'm not a native English speaker I was quite unsure about naming here. If "rejected" sounds better I will change it! 👍

@cbrevik
Copy link
Contributor Author

cbrevik commented Oct 31, 2016

On further testing I've found that the provided testplan only seems to work on iOS. For Android the rejectedUrlSchemes check only kicks in on redirects. Setting an inital source/uri to that custom scheme will throw an error:

Not sure if there is any way around that, because it might depend on a "race" between when the source prop is set, and when the rejectedUrlSchemes prop is set? Also I'm unsure if shouldOverrideUrlLoading is called for the first URL?

@cbrevik
Copy link
Contributor Author

cbrevik commented Nov 1, 2016

Is the above comment a showstopper @jacobp100?

An alternative could be to refactor the Android rejectedUrlSchemes logic into its own method, and call that in setSource (as well as shouldOverrideUrlLoading) to know wether the URL should be loaded?

@jacobp100
Copy link
Contributor

It’s important that things work as a user would expect, and consistently between platforms, so this does sound like something that needs to be resolved. I’m not too familiar with Android, so I can’t really offer any help. Go with what you think is best!

@satya164
Copy link
Contributor

satya164 commented Nov 6, 2016

@cbrevik

On further testing I've found that the provided testplan only seems to work on iOS. For Android the rejectedUrlSchemes check only kicks in on redirects. Setting an inital source/uri to that custom scheme will throw an error

Probably a manual check can be added to handle this before setting the initial URL

cc @javache

@cbrevik
Copy link
Contributor Author

cbrevik commented Nov 6, 2016

@jacobp100 jacobp100 mentioned this pull request Nov 6, 2016
@jacobp100
Copy link
Contributor

@cbrevik Somebody opened up a PR at #10772. Could you comment on whether this PR would achieve what you were wanting to with your PR?

@rseemann
Copy link

rseemann commented Nov 6, 2016

@jacobp100 I would say it would. Checking for schema is one of the things that one can achieve with my implementation

@cbrevik
Copy link
Contributor Author

cbrevik commented Nov 7, 2016

@rseemann @jacobp100 I've commented in the other issue.

@hramos
Copy link
Contributor

hramos commented Nov 11, 2016

I'm going through open PRs to see what can be merged. Looks like @10772 is still open, so I'm going to leave this one open as well.

@mkonicek
Copy link
Contributor

You might have to rebase on #10903 which just landed.

*/
scalesPageToFit: PropTypes.bool,

/**
Copy link
Contributor

@mkonicek mkonicek Nov 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't add any info to the name onUrlSchemeRejected below.

Can you please explain what "rejected" means?

onUrlSchemeRejected: PropTypes.func,

/**
* An array defining rejected URL-schemes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment doesn't add any info to the name rejectedUrlSchemes below.

Can you please explain what "rejected" means?


private static String getUrlScheme(String url) {
if (url.contains("://"))
return url.split("://")[0];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure there's a method for this in the SDK.

@mkonicek
Copy link
Contributor

This seems similar to #10772.

@jacobp100
Copy link
Contributor

It is, the discussion has moved over to there to ensure that PR handles this use case.

@lacker
Copy link
Contributor

lacker commented Feb 8, 2017

OK, it seems like we should close this PR in favor of #10772 then.

@lacker lacker closed this Feb 8, 2017
@bitfabrikken
Copy link

@lacker

OK, it seems like we should close this PR in favor of #10772 then.

That PR was closed a while ago, appears it was abandoned. Any chance of reviving this one? Would be very nice to be able to handle mailto: links and other such things, such as tel:, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants