Help with iOS network stack

194 views
Skip to first unread message

ramm...@gmail.com

unread,
Sep 13, 2019, 8:39:01 AM9/13/19
to net-dev
I am trying to understand network stack on iOS and unable to find any documentation for the same. I have gone through the documentation - https://chromium.googlesource.com/chromium/src/+/master/net/docs/life-of-a-url-request.md and https://www.chromium.org/developers/design-documents/network-stack which briefly explain the life of a URL request. However, I could find no document specific to how iOS handles network requests. 

I did try to debug through the code to further understand what I had figured from the documents. Specifically, I added a break point in the URLLoader class in the file services/network/url_loader.cc class at just before a URLRequest is created . However, on iOS I notice that the page is already partially loaded at this point. Based on what I understand, the page should  not load until the URL loader creates a request and schedules it. Can someone help me understand why this is happening ?

Any help here is appreciated


Matt Menke

unread,
Sep 13, 2019, 8:44:44 AM9/13/19
to ramm...@gmail.com, net-dev
iOS's Safari-based WebView doesn't support replacing the platform's network stack, so on iOS, Chrome's network stack is only used for internal Chrome requests, not web-initiated requests or navigation.

--
You received this message because you are subscribed to the Google Groups "net-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to net-dev+u...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/net-dev/1e5c34e7-a9a0-4e45-b74a-24989d6b63c2%40chromium.org.

ramm...@gmail.com

unread,
Sep 13, 2019, 8:58:36 AM9/13/19
to net-dev, ramm...@gmail.com
Thanks Matt for such a quick response. I was trying to understand the network stack because I wanted to solve the following problem -
 
When there is request for navigation to a particular website, I try to see if the URL can be allowed based on some patterns. If it is not allowed, I want to redirect the user to a different URL . Is there any way I can achieve this on iOS. I tried doing this with Android and was able to do this successfully. But as iOS is using its own network stack, I have my apprehensions on whether it is possible to do this. 

Thanks again for helping out!


On Friday, 13 September 2019 18:14:44 UTC+5:30, Matt Menke wrote:
iOS's Safari-based WebView doesn't support replacing the platform's network stack, so on iOS, Chrome's network stack is only used for internal Chrome requests, not web-initiated requests or navigation.

On Fri, Sep 13, 2019 at 8:39 AM <ramm...@gmail.com> wrote:
I am trying to understand network stack on iOS and unable to find any documentation for the same. I have gone through the documentation - https://chromium.googlesource.com/chromium/src/+/master/net/docs/life-of-a-url-request.md and https://www.chromium.org/developers/design-documents/network-stack which briefly explain the life of a URL request. However, I could find no document specific to how iOS handles network requests. 

I did try to debug through the code to further understand what I had figured from the documents. Specifically, I added a break point in the URLLoader class in the file services/network/url_loader.cc class at just before a URLRequest is created . However, on iOS I notice that the page is already partially loaded at this point. Based on what I understand, the page should  not load until the URL loader creates a request and schedules it. Can someone help me understand why this is happening ?

Any help here is appreciated


--
You received this message because you are subscribed to the Google Groups "net-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to net...@chromium.org.

David Roger

unread,
Sep 13, 2019, 9:18:12 AM9/13/19
to ramm...@gmail.com, net-dev, gam...@chromium.org, Eugene But
Adding a couple folks more familiar with navigation hooks on iOS.

I'm not an expert, but have you looked into device policies? What you want to achieve looks like the kind of things that device policies are designed for.

To unsubscribe from this group and stop receiving emails from it, send an email to net-dev+u...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/net-dev/d0338a67-9c59-4584-b95e-0d6c4f7b3e67%40chromium.org.


--
David Roger | Software Engineer | dro...@google.com | (+33) 6 10 86 21 51

Gauthier Ambard

unread,
Sep 13, 2019, 9:23:17 AM9/13/19
to David Roger, ramm...@gmail.com, net-dev, Eugene But
To allow/prevent load request, decidePolicyForNavigationResponse would be the correct place (calling handler(WKNavigationResponsePolicyCancel) would prevent the load from occurring). I guess you could start loading another URL here too. I don't think this code would make it to production, but if you want to check if it is possible or not, it might be a good start.

Gauthier

ramm...@gmail.com

unread,
Sep 19, 2019, 5:45:06 AM9/19/19
to net-dev, dro...@google.com, ramm...@gmail.com, euge...@chromium.org, gam...@chromium.org
Thanks Gauthier, I was able to make changes to the code at decidePolicyForNavigationResponse to make allow/block decisions. I was also able to load another URL here. However, I see that I can't catch things like URLs of Ajax requests, external scripts and CSS files etc.. Is there any way to achieve this? At the moment, I am  trying to implement the solution as suggested here.  This however doesn't look like a very clean solution. I am not sure if I can intercept all URLs this way and if there is a possibility of a request slipping by before the script is injected. Is there any other way to achieve what I want ? 

Gauthier Ambard

unread,
Sep 19, 2019, 10:47:12 AM9/19/19
to ramm...@gmail.com, net-dev, David Roger, Eugene But
If it doesn't work in decidePolicyForNavigationResponse or other WKNavigationDelegate callback, I don't think we are getting any callback from WebKit. In that case I would also inject JavaScript.

ramad...@gmail.com

unread,
Sep 24, 2019, 2:41:50 AM9/24/19
to net-dev, ramm...@gmail.com, dro...@google.com, euge...@chromium.org, gam...@chromium.org
Hi all,

I did try the above approach of injecting Javascript, but this doesn't seem to be foolproof. I can't catch form submits, resource requests (like requests for images/scripts) etc.. I tried exploring things like service workers but there seems to be some limitations in using service workers with WKWebview. Moreover, from - this guide , I figure that service workers have to be fetched from the same origin as the host that is requesting it. This is not useful as I'd want to intercept traffic from any website loaded in chromium.

The other solution (discussed here)provided was redirecting the main navigation to a local server and then rewriting all URLs in the page. But as discussed in the comments, this looks like a very dicey approach. 

The only solution I can think of as of now is making chromium managed and setup a VPN as suggested here - https://forums.developer.apple.com/thread/110312 . I really would not want to do this unless it is my last resort.

I know that this is more a question on circumventing an iOS limitation rather than a specific problem with chromium. However, I would really appreciate if anyone here can suggest other solutions I can try out.

Gauthier Ambard

unread,
Sep 24, 2019, 3:35:00 AM9/24/19
to ramad...@gmail.com, net-dev, ramm...@gmail.com, David Roger, Eugene But
Maybe WKContentRuleListStore https://developer.apple.com/documentation/webkit/wkcontentruleliststore?language=objc would be useful. I don't know if it is possible to have rules to redirect.
Also, I really don't think that redirecting navigation to local server is something secure or that should be done.
Reply all
Reply to author
Forward
0 new messages