chromiumembedded / cef

Chromium Embedded Framework (CEF). A simple framework for embedding Chromium-based browsers in other applications.
https://bitbucket.org/chromiumembedded/cef/
Other
3.09k stars 450 forks source link

Implement CefDevToolsManagerDelegate::GetDefaultBrowserContext for CDP cookie management #3684

Closed mxschmitt closed 2 months ago

mxschmitt commented 2 months ago

Describe the bug

Currently CefDevToolsManagerDelegate::GetDefaultBrowserContext is not implemented, this causes the following CDP calls to fail:

When implementing the method like done in Electron, things will start working: https://github.com/electron/electron/pull/41738. This also makes it then compatible with Playwright.

There was a quite popular feature request on our side about connecting to CEF instances from Playwright: https://github.com/microsoft/playwright/issues/10927

To Reproduce Steps to reproduce the behavior:

  1. mkdir repro && cd repro && npm init -y && npm install playwright
  2. Create the test.mjs file
  3. node test.mjs
import { chromium } from 'playwright';

(async () => {
  const browser = await chromium.connectOverCDP('http://localhost:1234');

  console.log(browser.contexts().length);
  console.log(browser.contexts()[0].pages().length);

  const page = browser.contexts()[0].pages()[0];
  await page.goto('https://facebook.com');
  console.log(await page.context().cookies());

  await browser.close();
})();

It throws with Browser context management not supported from here.

Expected behavior

CDP calls work.

Screenshots

N/A

Versions (please complete the following information):

Additional context

Does the problem reproduce with the cefclient or cefsimple sample application at the same version?

Yes

Does the problem reproduce with Google Chrome at the same version?

No

Add any other context about the problem here.

I'm currently working on a fix.

magreenblatt commented 2 months ago

Related PR: https://bitbucket.org/chromiumembedded/cef/pull-requests/748

magreenblatt commented 2 months ago

Given that this API should already work with the CEF Chrome bootstrap (same code as Google Chrome), and that the CEF Alloy bootstrap will be deleted in a few months (see #3685), I think we can just leave this unimplemented for Alloy and recommend that users start migrating to Chrome.

mxschmitt commented 2 months ago

Sounds good! Is there an easy way of trying out the new CEF Chrome bootstrap mode? cefclient / sefsimple is only using the alloy mode?

magreenblatt commented 2 months ago

Is there an easy way of trying out the new CEF Chrome bootstrap mode?

Yes, just add the --enable-chrome-runtime command-line flag to cefclient or cefsimple. If using cefsimple, also add --use-views.

mxschmitt commented 2 months ago

Awesome, just tried it out and it works out of the box. For reference:

// @ts-check
// Requires: './cefclient --remote-debugging-port=1235 --url=about:blank --enable-chrome-runtime
import { chromium } from 'playwright';

process.env.PW_CHROMIUM_ATTACH_TO_OTHER = '1';

(async () => {
  const browser = await chromium.connectOverCDP('http://localhost:1235');
  const defaultContext = browser.contexts()[0];
  const defaultPage = defaultContext.pages()[0];
  await defaultPage.goto('http://example.com');

  console.log(await defaultPage.title());
  console.log(defaultPage.url());

  await defaultContext.close();
  await browser.close();
})();