CEF3: Prevent page navigation

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

CEF3: Prevent page navigation

Postby dreijer » Tue Jan 22, 2013 11:30 pm

I'm migrating my CEF1 handlers to CEF3. CEF1 had CefRequestHandler::OnBeforeBrowse(), where returning false would prevent the browser from navigating away.

In CEF3, there's CefRenderProcessHandler::OnBeforeNavigation(), but that's being called in the context of my CefApp class, which doesn't generally know anything about the specific browser instance in my app (i.e. it doesn't know if the navigation is allowed or not).

Then there's CefRequestHandler::OnBeforeResourceLoad(), but I'm unclear if that's a more general method which gets called for all kinds of things while loading a page.

What's the proper way to prevent a single browser instance from navigating away?

EDIT: It does indeed look like CefRequestHandler::OnBeforeResourceLoad() is called for all resources on a single page. Is there a way to just outright deny any navigation whatsoever?
dreijer
Expert
 
Posts: 201
Joined: Mon Apr 11, 2011 10:09 pm

Re: CEF3: Prevent page navigation

Postby magreenblatt » Wed Jan 23, 2013 9:51 am

As stated in the OnBeforeNavigation documentation: Return true to cancel the navigation or false to allow the navigation to proceed.

See http://code.google.com/p/chromium/issue ... ?id=159923 for discussion related to how OnBeforeNavigation can be used.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF3: Prevent page navigation

Postby dreijer » Wed Jan 23, 2013 10:12 am

magreenblatt wrote:As stated in the OnBeforeNavigation documentation: Return true to cancel the navigation or false to allow the navigation to proceed.

Right, that's what I mentioned above. The issue is that this callback is on the CefApp object rather than CefClient, which makes it difficult for me to make a policy decision based on the current browser instance. All the other callbacks I'm using are implemented on the CefClient object that was passed to CreateBrowser, i.e. it's specific to that instance.
dreijer
Expert
 
Posts: 201
Joined: Mon Apr 11, 2011 10:09 pm

Re: CEF3: Prevent page navigation

Postby magreenblatt » Wed Jan 23, 2013 10:17 am

Did you read the above link?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF3: Prevent page navigation

Postby dreijer » Wed Jan 23, 2013 10:32 am

Yes, and I apologize if I don't have same in-depth knowledge of Chromium as a core committer like you do, but I'm still failing to understand how I can deny navigation requests on per tab basis. That is, (and again, I might misunderstand the new CEF3 design here) I can implement CefRenderProcessHandler::OnBeforeNavigation in my CefApp, but that is at a level higher than where it used to live in CEF1, i.e. CefApp doesn't generally know (in my application at least) what each individual browser instance is doing and whether it should deny or allow the navigation.
dreijer
Expert
 
Posts: 201
Joined: Mon Apr 11, 2011 10:09 pm

Re: CEF3: Prevent page navigation

Postby magreenblatt » Wed Jan 23, 2013 10:36 am

How are you determining whether navigation should be allowed currently? For example, what state information is the decision based on? What kinds of navigation do you want to block (link clicks, history nav, form submissions, etc)?
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF3: Prevent page navigation

Postby dreijer » Wed Jan 23, 2013 10:40 am

So, one use-case for CEF in my application is to show a window with a feed (Facebook-style). The feed can span multiple pages and the user is allowed to navigate through them. However, if he clicks, say, on a link to a YouTube video or something else that would cause the page to navigate away from the feed, I deny the navigation request and instead open the target in a new browser window.
dreijer
Expert
 
Posts: 201
Joined: Mon Apr 11, 2011 10:09 pm

Re: CEF3: Prevent page navigation

Postby magreenblatt » Wed Jan 23, 2013 10:47 am

dreijer wrote:So, one use-case for CEF in my application is to show a window with a feed (Facebook-style). The feed can span multiple pages and the user is allowed to navigate through them. However, if he clicks, say, on a link to a YouTube video or something else that would cause the page to navigate away from the feed, I deny the navigation request and instead open the target in a new browser window.


The non-generic solution is to hard-code logic in OnBeforeNavigation based on the URLs in question.

The generic solution is to:

1. Cancel all requests on OnBeforeNavigation.
2. Send a message to the browser process with the relevant request information (URL, headers, etc).
3. Evaluate the information in the browser process and either:
A. Open the link in an external browser, or
B. Ignore the request, or
C. Call CefBrowser::LoadRequest to re-start the request.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Re: CEF3: Prevent page navigation

Postby fddima » Wed Jan 23, 2013 10:49 am

dreijer wrote:So, one use-case for CEF in my application is to show a window with a feed (Facebook-style). The feed can span multiple pages and the user is allowed to navigate through them. However, if he clicks, say, on a link to a YouTube video or something else that would cause the page to navigate away from the feed, I deny the navigation request and instead open the target in a new browser window.


As i'm understand - you can apply this policy based only by URL from browser.

Other case is using browser and frames identifiers (CefBrowser.Identifier, CefFrame.Identifier).
If you need apply custom policy to any browser, not based from URL, probably easier is create browser and send message to renderer which update own internal state (policy), based on identifiers.

Mar be Marshall can fix me, if i'm wrong.
fddima
Master
 
Posts: 788
Joined: Tue Dec 07, 2010 6:10 am

Re: CEF3: Prevent page navigation

Postby magreenblatt » Wed Jan 23, 2013 11:07 am

fddima wrote:
dreijer wrote:So, one use-case for CEF in my application is to show a window with a feed (Facebook-style). The feed can span multiple pages and the user is allowed to navigate through them. However, if he clicks, say, on a link to a YouTube video or something else that would cause the page to navigate away from the feed, I deny the navigation request and instead open the target in a new browser window.


As i'm understand - you can apply this policy based only by URL from browser.

Other case is using browser and frames identifiers (CefBrowser.Identifier, CefFrame.Identifier).
If you need apply custom policy to any browser, not based from URL, probably easier is create browser and send message to renderer which update own internal state (policy), based on identifiers.

Mar be Marshall can fix me, if i'm wrong.

This works too. Just be careful about where/when you're sending state information. The render process will be created and the resource loading will proceed asynchronously with load-related callbacks in the browser process.
magreenblatt
Site Admin
 
Posts: 12409
Joined: Fri May 29, 2009 6:57 pm

Next

Return to Support Forum

Who is online

Users browsing this forum: No registered users and 73 guests