CefLifeSpanHandler: OnBeforePopup not being called.

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.

CefLifeSpanHandler: OnBeforePopup not being called.

Postby Astharoth » Wed Nov 09, 2016 5:59 am

Hi.

When navigating nasty web pages with a lot of tricks / bad behaviours to open advertisements i noticed sometimes OnBeforePopup was not called but the new view gets open so you do not have any chance to block it. Even, if you're working as OSR, the new view opens in a new native window without control over it.

Messing a bit on the source i found.

One:
The function bool CefBrowserInfoManager::CanCreateWindow on browser_info_manager.cc returns true if the new view is allowed false otherwise.
It sets the boolean variable "allow" to true, but next, if client.get() fails or handler.get() fails, allow is not set to false, making the code to allow the creation run even if the handler has not been called / parameters modified.

Two:
Also, negates the value returned by handler->OnBeforePopup, that could be right, but we gonna look in the called functions:

"CEF_CALLBACK life_span_handler_on_before_popup" on "life_span_handler_cpptoc.cc"
"CefLifeSpanHandlerCToCpp::OnBeforePopup " on "life_span_handler_ctocpp.cc"

The two functions make some extra checks... the point is in case of check fauilure, they return 0 / false, making the new view get opened also without call the handler / set parameters.

Solution:

I added sentences to set "allow=false" in case of error and reversed the returned values in case of fail for the two routines, compiled and checked again.
Now the result is adequate, if asked for a new view but the checks before call the user handler fails (frame gets invalid for example because was deleted while the view was opening), the view is canceled so, no new view will be created if no call to OnBeforePopup is made.

I checked it on 2623 but looking at sources i saw 2840 stills the same, so, the fix could apply i guess, until now.

browser_info_manager.cc
Code: Select all
 if (client.get()) {
    CefRefPtr<CefLifeSpanHandler> handler = client->GetLifeSpanHandler();
    if (handler.get()) {
      CefRefPtr<CefFrame> frame =
          browser->GetFrame(pending_popup->opener_frame_id);

      CefPopupFeatures cef_features;
      TranslatePopupFeatures(features, cef_features);

#if (defined(OS_WIN) || defined(OS_MACOSX))
      // Default to the size from the popup features.
      if (cef_features.xSet)
        pending_popup->window_info.x = cef_features.x;
      if (cef_features.ySet)
        pending_popup->window_info.y = cef_features.y;
      if (cef_features.widthSet)
        pending_popup->window_info.width = cef_features.width;
      if (cef_features.heightSet)
        pending_popup->window_info.height = cef_features.height;
#endif

      allow = !handler->OnBeforePopup(browser.get(),
          frame,
          pending_popup->target_url.spec(),
          pending_popup->target_frame_name,
          static_cast<cef_window_open_disposition_t>(disposition),
          user_gesture,
          cef_features,
          pending_popup->window_info,
          pending_popup->client,
          pending_popup->settings,
          no_javascript_access);
    }
   else{
      allow = false;
   }
  }
  else{
     allow = false;
  }


life_span_handler_cpptoc.cc
Code: Select all
  DCHECK(self);
  if (!self)
    return 1;
  // Verify param: browser; type: refptr_diff
  DCHECK(browser);
  if (!browser)
    return 1;
  // Verify param: frame; type: refptr_diff
  DCHECK(frame);
  if (!frame)
    return 1;
  // Verify param: popupFeatures; type: struct_byref_const
  DCHECK(popupFeatures);
  if (!popupFeatures)
    return 1;
  // Verify param: windowInfo; type: struct_byref
  DCHECK(windowInfo);
  if (!windowInfo)
    return 1;
  // Verify param: client; type: refptr_same_byref
  DCHECK(client);
  if (!client)
    return 1;
  // Verify param: settings; type: struct_byref
  DCHECK(settings);
  if (!settings)
    return 1;
  // Verify param: no_javascript_access; type: bool_byaddr
  DCHECK(no_javascript_access);
  if (!no_javascript_access)
    return 1;


life_span_handler_ctocpp.cc
Code: Select all
  // Verify param: browser; type: refptr_diff
  DCHECK(browser.get());
  if (!browser.get())
    return true;
  // Verify param: frame; type: refptr_diff
  DCHECK(frame.get());
  if (!frame.get())
    return true;
  // Verify param: no_javascript_access; type: bool_byaddr
  DCHECK(no_javascript_access);
  if (!no_javascript_access)
    return true;
  // Unverified params: target_url, target_frame_name


Marshall, im just newbie to the forums, i dont know if there is a better place to report bugs / give fixes, feel free to instruct me because im working "a bit" with the source, just in case.

Cheers.
Astharoth
Newbie
 
Posts: 1
Joined: Wed Nov 09, 2016 5:17 am

Re: CefLifeSpanHandler: OnBeforePopup not being called.

Postby klas » Tue Aug 22, 2017 1:43 pm

Hi,

Did you ever hear back on this? I'm running into the issue of OnBeforePopup never getting called too. If possible I'd love to avoid patching files manually.

Thanks,
Klas
klas
Newbie
 
Posts: 2
Joined: Tue Aug 22, 2017 1:38 pm

Re: CefLifeSpanHandler: OnBeforePopup not being called.

Postby cretz » Wed Aug 23, 2017 12:58 pm

Just as an alternative, OnBeforePopup does work for me, but I also combine it with CefRequestHandler::OnOpenURLFromTab checking the window disposition. This is because some popups are based on user gestures or may not otherwise be seen as popups. Maybe use both OnBeforePopup and OnOpenURLFromTab together to determine popup?
cretz
Techie
 
Posts: 34
Joined: Mon Jun 26, 2017 11:41 am

Re: CefLifeSpanHandler: OnBeforePopup not being called.

Postby klas » Thu Aug 24, 2017 9:57 am

cretz wrote:Just as an alternative, OnBeforePopup does work for me, but I also combine it with CefRequestHandler::OnOpenURLFromTab checking the window disposition. This is because some popups are based on user gestures or may not otherwise be seen as popups. Maybe use both OnBeforePopup and OnOpenURLFromTab together to determine popup?


That actually worked! I manage to capture the request and open the requested site in the default system browser. But a blank popup is still opened (I'm using OSR, btw). I beleive CefRenderProcessHandler::OnBeforeNavigation might do the trick, but it isn't optional for me to use right now.
klas
Newbie
 
Posts: 2
Joined: Tue Aug 22, 2017 1:38 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 80 guests