Skip to content

Commit a60901a

Browse files
authored
Merge pull request cztomczak#692 from linesight/fix-685-root-cache-path
Expose root_cache_path setting and set it in unit tests
2 parents 4000070 + b9a828f commit a60901a

16 files changed

Lines changed: 118 additions & 21 deletions

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ at this moment.
184184
* [persist_session_cookies](api/ApplicationSettings.md#persist_session_cookies)
185185
* [remote_debugging_port](api/ApplicationSettings.md#remote_debugging_port)
186186
* [resources_dir_path](api/ApplicationSettings.md#resources_dir_path)
187+
* [root_cache_path](api/ApplicationSettings.md#root_cache_path)
187188
* [string_encoding](api/ApplicationSettings.md#string_encoding)
188189
* [uncaught_exception_stack_size](api/ApplicationSettings.md#uncaught_exception_stack_size)
189190
* [unique_request_context_per_browser](api/ApplicationSettings.md#unique_request_context_per_browser)

‎api/API-index.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [persist_session_cookies](ApplicationSettings.md#persist_session_cookies)
2828
* [remote_debugging_port](ApplicationSettings.md#remote_debugging_port)
2929
* [resources_dir_path](ApplicationSettings.md#resources_dir_path)
30+
* [root_cache_path](ApplicationSettings.md#root_cache_path)
3031
* [string_encoding](ApplicationSettings.md#string_encoding)
3132
* [uncaught_exception_stack_size](ApplicationSettings.md#uncaught_exception_stack_size)
3233
* [unique_request_context_per_browser](ApplicationSettings.md#unique_request_context_per_browser)

‎api/ApplicationSettings.md‎

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Table of contents:
2828
* [persist_session_cookies](#persist_session_cookies)
2929
* [remote_debugging_port](#remote_debugging_port)
3030
* [resources_dir_path](#resources_dir_path)
31+
* [root_cache_path](#root_cache_path)
3132
* [string_encoding](#string_encoding)
3233
* [uncaught_exception_stack_size](#uncaught_exception_stack_size)
3334
* [unique_request_context_per_browser](#unique_request_context_per_browser)
@@ -117,19 +118,20 @@ switch.
117118
### cache_path
118119

119120
(string)
120-
The location where cache data will be stored on disk. If empty then
121-
browsers will be created in "incognito mode" where in-memory caches are
122-
used for storage and no data is persisted to disk. HTML5 databases such as
123-
localStorage will only persist across sessions if a cache path is
124-
specified. Can be overridden for individual CefRequestContext instances via
125-
the CefRequestContextSettings.cache_path value.
121+
Browsers store profile data such as cookies with an expiry date and HTML5
122+
databases (localStorage etc.) on disk, in the "Default" directory under
123+
[root_cache_path](#root_cache_path). Since v147 this is also the case when
124+
this value is empty, so there is no "incognito mode" with in-memory storage.
125+
126+
If `root_cache_path` is empty and this value is non-empty then
127+
`root_cache_path` will default to this value. Any non-empty value must be
128+
an absolute path that is either equal to or a child directory of
129+
`root_cache_path`. A child directory value is ignored and the "Default"
130+
directory is used instead.
126131

127132
CEF flushes cookies or other cache data to disk every 30 seconds,
128133
or immediately when [cefpython](cefpython.md).Shutdown() is called.
129134

130-
When this option is not set (empty string), a unique cache directory
131-
will be created in the user's temp directory for each run of the application.
132-
133135

134136
### command_line_args_disabled
135137

@@ -350,6 +352,32 @@ the module directory on Windows/Linux or the app bundle Resources directory
350352
on Mac OS X. Also configurable using the --resources-dir-path switch.
351353

352354

355+
### root_cache_path
356+
357+
(string)
358+
The root directory for installation-specific data and the parent directory
359+
for profile-specific data. All `cache_path` values must have this parent
360+
directory in common. If this value is empty and `cache_path` is non-empty
361+
then it will default to the `cache_path` value. Any non-empty value must be
362+
an absolute path.
363+
364+
If both `root_cache_path` and `cache_path` are empty then a default
365+
platform-specific directory will be used ("~/.config/cef_user_data" on
366+
Linux, "~/Library/Application Support/CEF/User Data" on Mac, and
367+
"AppData\Local\CEF\User Data" on Windows). Use of the default directory is
368+
not recommended in production applications: recent CEF/Chromium versions use
369+
a process singleton lock based on the `root_cache_path` value to guard
370+
against multiple application instances writing to the same directory, and
371+
relying on the default value may lead to unintended process singleton
372+
behavior (CEF logs a warning in this case). You should customize
373+
`root_cache_path` for your application.
374+
375+
When an application instance starts with the same `root_cache_path` as a
376+
running instance, its cef.Initialize() logs an error and returns False, and
377+
a running CEF Python application logs an error without opening a window.
378+
Check the return value of cef.Initialize() and exit when it is False.
379+
380+
353381
### string_encoding
354382

355383
(string)

‎api/CookieManager.md‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ Table of contents:
3333
| --- | --- |
3434
| __Return__ | [CookieManager](CookieManager.md) |
3535

36-
Returns the global cookie manager. By default data will be stored at
37-
[ApplicationSettings](ApplicationSettings.md).cache_path if specified or in memory otherwise.
36+
Returns the global cookie manager. Cookies with an expiry date are stored
37+
on disk under
38+
[ApplicationSettings](ApplicationSettings.md).root_cache_path, see
39+
[cache_path](ApplicationSettings.md#cache_path).
3840

39-
Description from upstream CEF:
41+
Description from upstream CEF (the storage description is outdated):
4042
> Returns the global cookie manager. By default data will be stored at
4143
> CefSettings.cache_path if specified or in memory otherwise. If |callback|
4244
> is non-NULL it will be executed asnychronously on the IO thread after the

‎api/cefpython.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ All parameters are optional.
5959

6060
This function can only be called on the UI thread.
6161

62+
Returns None if cef.Initialize() was not called or returned False.
63+
6264
If the url is a local path it needs to start with the `file://` prefix.
6365
If the url contains special characters it may need proper handling.
6466
Starting with v66.1+ it is required for the app code to encode the url
@@ -198,7 +200,14 @@ Return CEF Python and CEF versions dictionary with keys:
198200
| switches (optional) | [CommandLineSwitches](CommandLineSwitches.md) |
199201
| __Return__ | bool |
200202

201-
This function should be called on the main application thread (UI thread) to initialize CEF when the application is started. A call to Initialize() must have a corresponding call to Shutdown() so that CEF exits cleanly. Otherwise when application closes data (eg. storage, cookies) might not be saved to disk or the process might freeze (experienced on Windows XP).
203+
This function should be called on the main application thread (UI thread) to initialize CEF when the application is started. A successful call to Initialize() must have a corresponding call to Shutdown() so that CEF exits cleanly. Otherwise when application closes data (eg. storage, cookies) might not be saved to disk or the process might freeze (experienced on Windows XP).
204+
205+
Returns False if CEF could not be initialized, for example when another
206+
instance of the application is already running with the same
207+
ApplicationSettings.[root_cache_path](ApplicationSettings.md#root_cache_path).
208+
An error is logged in that case. Check the return value and, when it is
209+
False, exit the application immediately without calling Shutdown() or
210+
other CEF Python functions.
202211

203212
CefPython messages emitted before a successful CefInitialize() are written
204213
directly to stderr. On Windows, early CEF/Chromium startup messages may go to

‎docs/Migration-guide.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ Passing a removed setting raises an "Invalid appSettings key" or
551551
| `persist_user_preferences` | Application | Removed; persistent Chrome profiles save preferences automatically. |
552552
| `product_version` | Application | Renamed upstream to `user_agent_product`, which CEF Python does not expose. Use `switches={"user-agent-product": "..."}` in `cef.Initialize()` instead; it is ignored when `user_agent` is set. |
553553
| `single_process` | Application | Removed upstream. |
554-
| `user_data_path` | Application | Removed. |
554+
| `user_data_path` | Application | Removed upstream; CEF now stores this data in `root_cache_path`. |
555555
| `application_cache_disabled` | Browser | Removed along with Chromium's AppCache support. |
556556
| `databases_disabled` | Browser | Still accepted, but has no effect: Chromium removed WebSQL and CEF no longer applies this setting. |
557557
| `file_access_from_file_urls_allowed` | Browser | Removed. |

‎docs/Tutorial.md‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,17 @@ implemented. See below for details on each of these settings.
180180
A dict of [application settings](../api/ApplicationSettings.md#application-settings)
181181
can be passed to cef.[Initialize](../api/cefpython.md#initialize).
182182
Here are some settings worth noting:
183-
- [cache_path](../api/ApplicationSettings.md#cache_path) - set
184-
a directory path so that web cache data is persisted, otherwise
185-
an in-memory cache is used. Cookies and HTML 5 databases such
186-
as local storage will only persist if this option is set.
187183
- [context_menu](../api/ApplicationSettings.md#context_menu) -
188184
customize context menu
189185
- [locale](../api/ApplicationSettings.md#locale) - set language
190186
for localized resources
187+
- [root_cache_path](../api/ApplicationSettings.md#root_cache_path) -
188+
set a directory for your application's data. Cookies with an
189+
expiry date and HTML 5 databases such as local storage are stored
190+
there. Only one instance of an application can run per directory.
191+
If neither this option nor cache_path is set, CEF uses a default
192+
directory that is shared with other applications that set neither
193+
option.
191194
- [user_agent](../api/ApplicationSettings.md#user_agent) - set
192195
value that will be returned as the User-Agent HTTP header
193196
and js navigator.userAgent

‎src/cefpython.pyx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,13 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
520520
# ------------------------------------------------------------------------
521521
# Paths
522522
# ------------------------------------------------------------------------
523+
if "root_cache_path" in application_settings and application_settings["root_cache_path"]:
524+
application_settings["root_cache_path"] = \
525+
os.path.abspath(application_settings["root_cache_path"])
526+
Debug("root_cache_path: " + application_settings["root_cache_path"])
527+
else:
528+
Debug("root_cache_path: <empty>")
529+
523530
cdef str module_dir = GetModuleDirectory()
524531
if platform.system() == "Darwin":
525532
if "framework_dir_path" not in application_settings:
@@ -685,7 +692,9 @@ def Initialize(applicationSettings=None, commandLineSwitches=None, **kwargs):
685692
g_cef_initialized_successfully = bool(ret)
686693

687694
if not ret:
688-
Debug("CefInitialize() failed")
695+
NonCriticalError("CEF initialization failed (another instance may"
696+
" be running with the same root_cache_path, or"
697+
" settings are invalid)")
689698

690699
IF UNAME_SYSNAME == "Windows":
691700
# Chromium logs the messages emitted before its logging is set up
@@ -726,6 +735,8 @@ def CreateBrowserSync(windowInfo=None,
726735
raise Exception("Invalid argument: "+kwarg)
727736

728737
Debug("CreateBrowserSync() called")
738+
if not g_cef_initialized_successfully:
739+
return None
729740
assert IsThread(TID_UI), (
730741
"cefpython.CreateBrowserSync() may only be called on the UI thread")
731742

‎src/extern/cef/cef_types.pxd‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ cdef extern from "include/internal/cef_types.h":
2323
cef_string_t main_bundle_path
2424
int command_line_args_disabled
2525
cef_string_t cache_path
26+
cef_string_t root_cache_path
2627
int persist_session_cookies
2728
cef_string_t user_agent
2829
cef_string_t locale

‎src/settings.pyx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ cdef void SetApplicationSettings(
5050
cefString = new CefString(&cefAppSettings.cache_path)
5151
PyToCefStringPointer(appSettings[key], cefString)
5252
del cefString
53+
elif key == "root_cache_path":
54+
cefString = new CefString(&cefAppSettings.root_cache_path)
55+
PyToCefStringPointer(appSettings[key], cefString)
56+
del cefString
5357
elif key == "persist_session_cookies":
5458
cefAppSettings.persist_session_cookies = int(appSettings[key])
5559
elif key == "user_agent":

0 commit comments

Comments
 (0)