Skip to content

[Bug]: capturePage doesn't work for a hidden browser view since version 13.0.0-beta.21 #30666

Closed

Description

@zeeker999

Preflight Checklist

Electron Version

13.0.0-beta.21 and newer
12.0.0-beta.21 and newer

What operating system are you using?

Windows

Operating System Version

Windows 10

What arch are you using?

x64

Last Known Working Electron version

11.x

Expected Behavior

A screenshot should be captured successfully

Actual Behavior

A zero-sized image is returned instead.

Testcase Gist URL

https://gist.github.com/d4051b57412bf289d3a812d5387179fe

Additional Information

  • createBrowserView3 works fine with electron 12.x.y on Mac OS X.
  • createBrowserView2 doesn't work with electron 12.0.0-beta.21 and newer
  • createBrowserView3 doesn't work with electron 13.0.0-beta.21 and newer
  • The only difference between createBrowserView2 and createBrowserView3 is the createBrowserView3 loads about:blank before loading the target page.
  • Periodically reloading the page and capturing the page will break page loading completely, further loading requests will never be finished, and setting app.allowRendererProcessReuse to false doesn't help.

Activity

  1. changed the title [-][Bug]: capturePage doesn't work for a hidden browser view since version 12.x[/-] [+][Bug]: capturePage doesn't work for a hidden browser view since version 13.0.0-beta.21[/+] on Aug 24, 2021
  2. self-assigned this
    on Aug 25, 2021
  3. zeeker999 commented on Aug 26, 2021

    @zeeker999
    ContributorAuthor

    The gist is updated with a new one that could reproduce this issue with electron 12.

  4. zeeker999 commented on Aug 26, 2021

    @zeeker999
    ContributorAuthor

    it seems setting app.allowRendererProcessReuse to false fixes the issue for electron 12, but it doesn't fix it for electron 13.

  5. zeeker999 commented on Aug 26, 2021

    @zeeker999
    ContributorAuthor

    I have updated the gist again to add another test createBrowserView4 to reproduce another issue we're facing, that is, periodic reloading the page and capturing the page will break page loading completely.

  6. codebytere commented on Aug 26, 2021

    @codebytere
    Member

    @LuoJinghua are you sure it works on macOS? i'm seeing an empty image on macOS too, starting with 13.0.0-beta.21

  7. zeeker999 commented on Aug 26, 2021

    @zeeker999
    ContributorAuthor

    No. My memory is bad. It doesn't work on macOS starting with 13.0.0-beta.21, but it does work with 12.0.0-beta.21.

  8. zeeker999 commented on Oct 3, 2021

    @zeeker999
    ContributorAuthor

    find a workaround for this issue:

    • v12 - browserView.webContents.incrementCapturerCount() before capturing the page, browserView.webContents.decrementCapturerCount() after it(this is optional)
    • v13+ - browserView.webContents.incrementCapturerCount() before capturing the page, browserView.webContents.decrementCapturerCount() must be removed.

    @codebytere

  9. h5nc commented on Oct 19, 2021

    @h5nc

    Same here. The workaround above doesn't work for my project. I am using electron/remote.
    remote.getCurrentWebContents().capturePage().then(r=>console.log(r))
    The above promise will just hang forever without being solved.

    However if I call
    webView.capturePage().then(r=>console.log(r))
    this works fine when the webview is visible. Empty image is returned if the app is minimized / even covered by other app.
    At least the promise returns something.

  10. miniak commented on Nov 25, 2021

    @miniak
    Contributor

    Could it be caused by #27883?

  11. zeeker999 commented on Jan 18, 2022

    @zeeker999
    ContributorAuthor

    Could it be caused by #27883?

    I don't think so. I did attach to the process and manually step into the method, the visibility state is correct.

  12. sanperrier commented on Feb 17, 2022

    @sanperrier

    I think related to this bug in chromium: https://bugs.chromium.org/p/chromium/issues/detail?id=999305&q=incrementCapturerCount&can=2

    Current workaround we are using in our project (painful and hacky):

    • on Windows:
      • not really needed, just in case
        app.commandLine.appendSwitch('disable-renderer-backgrounding');
        app.commandLine.appendSwitch('disable-background-timer-throttling');
        app.commandLine.appendSwitch('disable-backgrounding-occluded-windows');
        
      • backgroundThrottling: false
      • incrementCapturerCount() just once
      • instead of minimizing or hiding just move window offscreen (this is painful and tedious, especially for windows with parent-child relations)
    • on MacOS:
      • really needed, especially last line:
        app.commandLine.appendSwitch('disable-renderer-backgrounding');
        app.commandLine.appendSwitch('disable-background-timer-throttling');
        app.commandLine.appendSwitch('disable-backgrounding-occluded-windows');
        
      • backgroundThrottling: false
      • incrementCapturerCount() just once

    Out testing shows that with that workaround:

    • requestAnimationFrame() works just fine
    • CSS animations and transitions work
    • IntersectionObserver works
    • document.visibilityState stays visible (but on MacOS it switches visible -> hidden -> visible if window becomes occluded)
  13. sanperrier commented on Feb 17, 2022

    @sanperrier

    find a workaround for this issue:

    • v12 - browserView.webContents.incrementCapturerCount() before capturing the page, browserView.webContents.decrementCapturerCount() after it(this is optional)
    • v13+ - browserView.webContents.incrementCapturerCount() before capturing the page, browserView.webContents.decrementCapturerCount() must be removed.

    Can you test, that incrementCapturerCount() really increments visible_capturer_count_?
    isBeingCaptured() always returns false for hidden windows in 14.2.6

    @zeeker999

  14. zeeker999 commented on Feb 17, 2022

    @zeeker999
    ContributorAuthor

    find a workaround for this issue:

    • v12 - browserView.webContents.incrementCapturerCount() before capturing the page, browserView.webContents.decrementCapturerCount() after it(this is optional)
    • v13+ - browserView.webContents.incrementCapturerCount() before capturing the page, browserView.webContents.decrementCapturerCount() must be removed.

    Can you test, that incrementCapturerCount() really increments visible_capturer_count_? isBeingCaptured() always returns false for hidden windows in 14.2.6

    @zeeker999

    The incrementCapturerCount() is no longer working due to upstream changes and needs some changes to make it work again. The visible capturer count is reverted to the previous value just before returning to the javascript world, so the isBeingCaptured() always returns false.

    base::ScopedClosureRunner WebContentsImpl::IncrementCapturerCount(
        const gfx::Size& capture_size,
        bool stay_hidden,
        bool stay_awake) {`
      ....
      return base::ScopedClosureRunner(
          base::BindOnce(&WebContentsImpl::DecrementCapturerCount,
                         weak_factory_.GetWeakPtr(), stay_hidden, stay_awake));
    }
  15. zeeker999 commented on Feb 17, 2022

    @zeeker999
    ContributorAuthor

    With the following changes, the incrementCapturerCount() works as intended and the isBeingCaptured() return true now. But I'm not sure whether this is the correct fix, need some help from the dev team.

    diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc
    index c1d28e24c2..ed5e744623 100644
    --- a/shell/browser/api/electron_api_web_contents.cc
    +++ b/shell/browser/api/electron_api_web_contents.cc
    @@ -3006,8 +3006,9 @@ void WebContents::IncrementCapturerCount(gin::Arguments* args) {
       // get stayAwake arguments if they exist
       args->GetNext(&stay_awake);
    
    -  ignore_result(
    -      web_contents()->IncrementCapturerCount(size, stay_hidden, stay_awake));
    +  auto handler =
    +      web_contents()->IncrementCapturerCount(size, stay_hidden, stay_awake);
    +  ignore_result(handler.Release());
     }
  16. zeeker999 commented on Feb 17, 2022

    @zeeker999
    ContributorAuthor

    The change was introduced by commit 22a70eb.

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions