Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

request: add support for creating browsers in different contexts #47

Open
jepp opened this issue Aug 26, 2016 · 0 comments
Open

request: add support for creating browsers in different contexts #47

jepp opened this issue Aug 26, 2016 · 0 comments

Comments

@jepp
Copy link

jepp commented Aug 26, 2016

I've been working with testing some of the new SetPreferences features (as noted in my other thread about dynamically changing the proxy). As part of testing some network issues, I wanted to put several TChromium instances in one app, each with a different proxy. But I eventually noticed that when I used SetPreferences, the change was global. This is because crm.Browser.Host.RequestContext is shared among the different instances. I dug down and here's how I changed it so that each browser got their own context:

procedure TCustomChromium.CreateBrowser;
var
  info: TCefWindowInfo;
  rect: TRect;
  settings: TCefBrowserSettings;
  contextSettings: TCefRequestContextSettings;
  context: ICefRequestContext;
begin
  if not (csDesigning in ComponentState) then
  begin
    FillChar(info, SizeOf(info), 0);
    rect := GetClientRect;
    info.Style := WS_CHILD or WS_VISIBLE or WS_CLIPCHILDREN or WS_CLIPSIBLINGS or WS_TABSTOP;
    info.parent_window := Handle;
    info.x := rect.left;
    info.y := rect.top;
    info.Width := rect.right - rect.left;
    info.Height := rect.bottom - rect.top;
    FillChar(settings, SizeOf(TCefBrowserSettings), 0);
    settings.size := SizeOf(TCefBrowserSettings);
    GetSettings(settings);
    //contextHandler := TCefRequestContextHandlerRef.
    FillChar(contextSettings, SizeOf(contextSettings), 0);
    contextSettings.size := SizeOf(contextSettings);
    context := TCefRequestContextRef.New(@contextSettings, nil);
{$IFDEF CEF_MULTI_THREADED_MESSAGE_LOOP}
//    CefBrowserHostCreate(@info, FHandler, FDefaultUrl, @settings, nil);
    CefBrowserHostCreate(@info, FHandler, FDefaultUrl, @settings, context);
{$ELSE}
//    FBrowser := CefBrowserHostCreateSync(@info, FHandler, '', @settings, nil);
    FBrowser := CefBrowserHostCreateSync(@info, FHandler, '', @settings, context);
    FBrowserId := FBrowser.Identifier;
{$ENDIF}
  end;
end;

First, does this look correct or have I messed anything up? Do you know of anything wrong with passing in nil to the TCefRequestContextRef.New call?

This is working for me, at least with CEF_MULTI_THREADED_MESSAGE_LOOP turned off.

I was thinking was that TChromium could have a property like UseNewContext and if it was set, then CreateBrowser could use a different context for that one. Otherwise, it would use the global context.

What do you think?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant