CefRequestHandler - How to disable clicking on links ?

Having problems with building or using the CefGlue .NET/Mono binding? Ask your questions here.

Moderator: fddima

CefRequestHandler - How to disable clicking on links ?

Postby amaranth » Fri Feb 08, 2019 7:13 am

My own WinForms application based on the example of Dmitry Arazayev showing URL's in CefWebBrowser in cycle. Well, I want to disable clicking and following any links except by using CefBrowser.GetMainFrame().LoadUrl() in my cycle body. I try to use OnBeforeBrowse:
Code: Select all
protected override bool OnBeforeBrowse(CefBrowser browser, CefFrame frame, CefRequest request, bool userGesture, bool isRedirect)
        {
            if (userGesture)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

But I'm getting error ERR_ABORTED in LoadError event. Is it possible to turn off this error? Many thanks.
amaranth
Techie
 
Posts: 13
Joined: Sat Nov 12, 2011 12:28 pm

Re: CefRequestHandler - How to disable clicking on links ?

Postby ndesktop » Fri Feb 08, 2019 12:17 pm

This is normal.
From include/cef_request_handler.h
Code: Select all
class CefRequestHandler : public virtual CefBaseRefCounted {
...
  ///
  // Called on the UI thread before browser navigation. Return true to cancel
  // the navigation or false to allow the navigation to proceed. The |request|
  // object cannot be modified in this callback.
  // CefLoadHandler::OnLoadingStateChange will be called twice in all cases.
  // If the navigation is allowed CefLoadHandler::OnLoadStart and
  // CefLoadHandler::OnLoadEnd will be called. If the navigation is canceled
  // CefLoadHandler::OnLoadError will be called with an |errorCode| value of
  // ERR_ABORTED. The |user_gesture| value will be true if the browser
  // navigated via explicit user gesture (e.g. clicking a link) or false if it
  // navigated automatically (e.g. via the DomContentLoaded event).
  ///
  /*--cef()--*/
  virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
                              CefRefPtr<CefFrame> frame,
                              CefRefPtr<CefRequest> request,
                              bool user_gesture,
                              bool is_redirect) {
    return false;
  }


So:
1. The |request| object cannot be modified in this callback.
2. If the navigation is canceledCefLoadHandler::OnLoadError will be called with an |errorCode| value of ERR_ABORTED.

I'd try using OnOpenURLFromTab instead. Although the implementation ends with a comment which is not encouraging.
libcef/browser/browser_host_impl.cc
Code: Select all
// |source| may be NULL if the navigation originates from a guest view via
// CefContentBrowserClient::CanCreateWindow.
content::WebContents* CefBrowserHostImpl::OpenURLFromTab(
    content::WebContents* source,
    const content::OpenURLParams& params) {
  bool cancel = false;

  if (client_.get()) {
    CefRefPtr<CefRequestHandler> handler = client_->GetRequestHandler();
    if (handler.get()) {
      cancel = handler->OnOpenURLFromTab(
          this, GetFrame(params.frame_tree_node_id), params.url.spec(),
          static_cast<cef_window_open_disposition_t>(params.disposition),
          params.user_gesture);
    }
  }

  if (!cancel) {
    // Start a navigation in the current browser that will result in the
    // creation of a new render process.
    LoadURL(CefFrameHostImpl::kMainFrameId, params.url.spec(), params.referrer,
            params.transition, params.extra_headers);
    return source;
  }

  // We don't know where the navigation, if any, will occur.
  return nullptr;
}
ndesktop
Master
 
Posts: 750
Joined: Thu Dec 03, 2015 10:10 am


Return to CefGlue Forum

Who is online

Users browsing this forum: No registered users and 13 guests