Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Feature request

thefunkyjoint
Posts: 459
Joined: Thu Aug 10, 2017 12:40 pm

Feature request

Post by thefunkyjoint »

Hi,

Long-time CEF4Delphi user / fan here... I really love this project, thank you very much Salvador !

There are 2 features that would really be awesome if could happen, at least for my use case :

- Set specific user agent for each TChromium instance ; at the moment the user agent is global, ie, same for all TChromium instances.
- Delete or change cookies/cache dir without having to close the app before. If i want to change to cookies dir, i can't do it unless i close my app before.

Is these possible in the near future ?

Thanks ! :D
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Feature request

Post by salvadordf »

thefunkyjoint wrote: Thu Jan 23, 2020 11:34 pm Long-time CEF4Delphi user / fan here... I really love this project, thank you very much Salvador !
Thank you so much for all your donations and support in Patreon!!! :D
thefunkyjoint wrote: Thu Jan 23, 2020 11:34 pm There are 2 features that would really be awesome if could happen, at least for my use case :
- Set specific user agent for each TChromium instance ; at the moment the user agent is global, ie, same for all TChromium instances.
You can modify the HTTP headers in the normal resource requests to set a custom user agent whenever you want but JavaScript will keep using the user agent you set when you initialize CEF.

In order to fix that we would need to modify Chromium's code, CEF's code and CEF4Delphi's code.

I've never modified Chromium's code and even if I could do that you would need to build custom CEF binaries with all those modifications each time you upgrade your application.
thefunkyjoint wrote: Thu Jan 23, 2020 11:34 pm - Delete or change cookies/cache dir without having to close the app before. If i want to change to cookies dir, i can't do it unless i close my app before.
You can do that right now and the MDIBrowser demo has all the code you need :
https://github.com/salvadordf/CEF4Delph ... m.pas#L203

Whenever you need to use a different cache directory you just need to create a new TChromium with a TCEFWindowParent and set a custom ICefRequestContext parameter when you call TChromium.CreateBrowser.

The custom ICefRequestContext would use a different cache directory and the new browser would be completely independent from the previous browsers.

For example, if you use the "c:\old_cache" directory when you initialize CEF you can have several browsers using that cache directory if you don't set a ICefRequestContext parameter when you call TChromium.CreateBrowser.

Later you can create a new independent browser using "c:\new_cache". You would have to create an instance of ICefRequestContext with this code :

Code: Select all

TempNewContext1 := TCefRequestContextRef.New('c:\new_cache', '', False, False, False);
Then use that context like this :

Code: Select all

TempNewChromium1.CreateBrowser(TempNewCEFWindowParent1, '', TempNewContext1);
At that point you would have some browsers using "c:\old_cache" and a new browser using "c:\new_cache".

In case you need a new browser using "c:\old_cache" again then you can create it with a NIL ICefRequestContext parameter when you call TChromium.CreateBrowser.

However, if you need a second browser using "c:\new_cache" then you would need to call TChromium.ShareRequestContext to get the new ICefRequestContext instance in the "aContext" parameter.

Code: Select all

if TempNewChromium1.ShareRequestContext(TempNewContext2, nil) then
  TempNewChromium2.CreateBrowser(TempNewCEFWindowParent2, '', TempNewContext2);
Notice that TempNewChromium1, TempNewCEFWindowParent1 and TempNewContext1 where used to create the first browser using "c:\new_cache" and now you are creating a new TempNewChromium2, TempNewCEFWindowParent2 and TempNewContext2.

In this situation TempNewChromium1 and TempNewChromium2 would use "c:\new_cache" while the rest of the browsers would use "c:\old_cache".
micmorozov
Posts: 18
Joined: Mon Oct 07, 2019 7:53 am

Re: Feature request

Post by micmorozov »

Hello!

Thanks for the great product! :)

I am not the author of the topic, but I have a similar question

I can change the cookie folder without calling CreateBrowser:

Code: Select all

SurfWebBrowserChr.ShareRequestContext(Context);
TempCookieManager: = Context.GetDefaultCookieManager (nil);
TempCookieManager.SetStoragePath('c:\new_cache', false, nil);
But the cache folder does not change. How to change the cache folder without calling CreateBrowser?

And the second question

I create contexts in two browsers in the same way

Code: Select all

Context: = TCefRequestContextRef.New('c:\old_cache', '', False, False, True, False);
SurfWebBrowserChr.CreateBrowser (SurfWebBrowserChrParent, '', Context);
without ShareRequestContext
It is not right? Need to create through ShareRequestContext?
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Feature request

Post by salvadordf »

Hi,
micmorozov wrote: Fri Jan 24, 2020 9:15 am I can change the cookie folder without calling CreateBrowser:

Code: Select all

SurfWebBrowserChr.ShareRequestContext(Context);
TempCookieManager: = Context.GetDefaultCookieManager (nil);
TempCookieManager.SetStoragePath('c:\new_cache', false, nil);
But the cache folder does not change. How to change the cache folder without calling CreateBrowser?
Some of those functions were deprecated and they are not used in the latest CEF4Delphi version.
CEF had a lot of API changes in the last months and CefCookieManager.SetStoragePath doesn't exist now :
https://bitbucket.org/chromiumembedded/ ... orkservice
micmorozov wrote: Fri Jan 24, 2020 9:15 am I create contexts in two browsers in the same way

Code: Select all

Context: = TCefRequestContextRef.New('c:\old_cache', '', False, False, True, False);
SurfWebBrowserChr.CreateBrowser (SurfWebBrowserChrParent, '', Context);
without ShareRequestContext
It is not right? Need to create through ShareRequestContext?
These are the CEF code comments for ShareRequestContext :

Code: Select all

///
// Creates a new context object that shares storage with |other| and uses an
// optional |handler|.
///
It's not possible to run several instances of your application that use the same cache directory. However, you can create as many browsers as you want in the same application that use the same cache directory.

I haven't checked Chromium's code to see if it's possible to create several contexts with TCefRequestContextRef.New sharing the same cache in the same application but if the CEF API has a specific function to do that, I would definitively recommend using it.
dvbss11
Posts: 28
Joined: Sat Oct 26, 2019 6:30 pm

Re: Feature request

Post by dvbss11 »

Code: Select all

  TempManager := TCefCookieManagerRef.Global(nil);
Tell me how to get back the cookie and cache instance when several independent browsers work.
When i turn to
TempManager: = TCefCookieManagerRef.Global (nil);
This is the default
And the rest of the samples are not available to me.

Code: Select all

TempManager:= RequestContext.GetHandler.GetCookieManager;
I tried so where RequestContext is an instance when creating a browser but to no avail.

Code: Select all

(Chrom.CreateBrowser(ChromWindow, '',RequestContext)) 
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Feature request

Post by salvadordf »

Try this for each browser :

Code: Select all

TempManager := TChromium.Browser.Host.RequestContext.GetCookieManager(nil);
thefunkyjoint
Posts: 459
Joined: Thu Aug 10, 2017 12:40 pm

Re: Feature request

Post by thefunkyjoint »

You can modify the HTTP headers in the normal resource requests to set a custom user agent whenever you want but JavaScript will keep using the user agent you set when you initialize CEF.
Do you have any examples about how to do it, please ?

Thanks !
User avatar
salvadordf
Posts: 4042
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Feature request

Post by salvadordf »

There's an example here :
https://github.com/salvadordf/CEF4Delph ... r.pas#L428

That demo searches for the "Accept-Encoding" header to set it to "identity" but you would have to search "User-Agent" and set it to your custom user agent string.

There's a new TChromium.SetUserAgentOverride procedure that you can also try to use a custom user agent.
thefunkyjoint
Posts: 459
Joined: Thu Aug 10, 2017 12:40 pm

Re: Feature request

Post by thefunkyjoint »

You can do that right now and the MDIBrowser demo has all the code you need :
https://github.com/salvadordf/CEF4Delph ... m.pas#L203

Whenever you need to use a different cache directory you just need to create a new TChromium with a TCEFWindowParent and set a custom ICefRequestContext parameter when you call TChromium.CreateBrowser.
I'm still confused on how to do this in the correct way.

Let's say my app has 2 instances of TChromium. Now i want to use a different cookies dir, so i want to free these two instances and create another new 2 instances of the new cookies dir, but without having to close my app.

What should be the correct procedure ? What i'm thinking :

1 - Free / destroy current TChromium instances (just call .free method for each instance ?)
2 - Create two new instances with the new cookie dir

Is this correct ?
Student
Posts: 72
Joined: Tue Aug 07, 2018 9:20 am

Re: Feature request

Post by Student »

I do this way, GlobalCEFApp.Cache not specify,
in GlobalCEFApp.RootCache specify the parent folder where the caches will be stored.
Dynamically creating CEFWindowParent/TChromium and specify requestContext

Code: Select all

 
 RequestContext := TCefRequestContextRef.New(PathToCache, '', false, false, false);
 Chromium1.CreateBrowser(CEFWindowParent1, '', RequestContext);
 
Now if I want to change the cache path, then I destroy instances CEFWindowParent/TChromium

Code: Select all

Chromium1.CloseBrowser(true);
CEFWindowParent1.Free;
And again dynamically creating CEFWindowParent/TChromium.
Specify RequestContext with the other path to cache.

Code: Select all

RequestContext := TCefRequestContextRef.New(OtherPathToCache, '', false, false, false);
Chromium1.CreateBrowser(CEFWindowParent1, '', RequestContext);
Post Reply