Skip to content

feat: Custom cache path - #19693

Closed
emmkimme wants to merge 130 commits into
electron:mainfrom
emmkimme:custom_cache_path
Closed

emmkimme wants to merge 130 commits into
electron:mainfrom
emmkimme:custom_cache_path

Conversation

@emmkimme

@emmkimme emmkimme commented Aug 9, 2019 •

Copy link
Copy Markdown
Contributor

Description of Change

This MR fixes the following issues:

Notes: Allow to customize Electron/Chromium cache location

Checklist

  • PR description included and stakeholders cc'd
  • npm test passes
  • tests are unchanged
  • relevant documentation is changed or added
  • PR title follows semantic
  • PR release notes describe the change in a way relevant to app developers, and are capitalized, punctuated, and past tense.

Technical Notes

The notion of cache (DIR_CACHE, DIR_USER_CACHE) is already present in the code but not used.

atom\browser\api\atom_api_app.cc

int GetPathConstant(const std::string& name) {
...
  else if (name == "cache")
    return DIR_CACHE;
  else if (name == "userCache")
    return DIR_USER_CACHE;
...

atom/browser/atom_paths.h

...
  DIR_USER_CACHE,              // Directory where user cache can be written.
#if defined(OS_POSIX)
  DIR_CACHE = base::DIR_CACHE,  // Directory where to put cache data.
#else
  DIR_CACHE = base::DIR_APP_DATA,
#endif
...

lib\browser\api\app.ts

app._setDefaultAppPaths = (packagePath) => {
...
  app.setPath('userData', path.join(app.getPath('appData'), app.name!))
  app.setPath('userCache', path.join(app.getPath('cache'), app.name!))

There is 2 levels of cache :

  • userCache (DIR_USER_CACHE) and cache (DIR_CACHE)
    equivalent to the 2 levels of user data
  • userData (DIR_USER_DATA) and appData (DIR_APP_DATA)

Purpose was to use the userCache value for customizing the cache

atom\browser\atom_browser_context.cc
Before

if (!base::PathService::Get(DIR_USER_DATA, &path_)) {
    base::PathService::Get(DIR_APP_DATA, &path_);
    path_ = path_.Append(base::FilePath::FromUTF8Unsafe(GetApplicationName()));
    base::PathService::Override(DIR_USER_DATA, path_);
  }

After

  base::PathService::Get(DIR_USER_CACHE, &path_);

I added a AppPathService which provides default paths (data, cache, logs) if there are not overridden.

Usage

import { app } from 'electron';

// Must be called before application is ready
app.setPath('userCache', myOwnUserCache);

Backward compatibility

As before, by default,

  • 'userData' is computed from 'appData' and app name
  • 'userCache' from 'cache' and app name

But not at the same moment. previously it was done very early, before app main script was executed. Now it is done when the app is ready, giving some opportunities to customize 'appData', app name, ..
Previous behavior can be restored easily.

Unit tests

Unit tests are now covering 'userData', 'userCache' and 'logs'.

Additional notes

For concurrent access violation, the cache folder must not be shared by different users.
For performance purpose, it must be set on a fast drive.

@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Aug 9, 2019
@emmkimme
emmkimme force-pushed the custom_cache_path branch from 7428712 to cb1d28f Compare August 9, 2019 09:14
@emmkimme emmkimme changed the title WIP: Feat: Custom cache path WIP: feat:Custom cache path Aug 9, 2019
@electron-cation electron-cation Bot removed the new-pr 🌱 PR opened recently label Aug 10, 2019
@emmkimme emmkimme changed the title WIP: feat:Custom cache path feat:Custom cache path Aug 19, 2019
@emmkimme emmkimme changed the title feat:Custom cache path feat: Custom cache path Aug 19, 2019
@emmkimme
emmkimme force-pushed the custom_cache_path branch 4 times, most recently from dd43fd4 to fa982fa Compare August 26, 2019 06:04
@codebytere

Copy link
Copy Markdown
Member

@emmkimme

emmkimme commented Aug 30, 2019 •

Copy link
Copy Markdown
Contributor Author

@emmkimme it looks like https://github.com/electron/electron/pull/19693/files#diff-c0db39733aba3af6dedd3c8a42082f92 accidentally got changed more than intended?

You right, I have been again trapped with CR/LF mess on Wndows. I do not get why it happens only on Markdown files, it works for all others. I will fix that. Thanks

@emmkimme

emmkimme commented Aug 30, 2019 •

Copy link
Copy Markdown
Contributor Author

Hi @codebytere

I fixed MD using my Mac.

I deactivated my tests related to setAppLogsPath (BTW original tests are wrong), as it does not work on Windows.

If we change the current implem C++ signature from

void App::SetAppLogsPath(util::ErrorThrower thrower,
                         base::Optional<base::FilePath> custom_path) {

to

void App::SetAppLogsPath(util::ErrorThrower thrower,
                         const base::FilePath& custom_path) {

Removing base::Optional works !

Another point, AppLogs on Mac takes for application name

NSString* bundle_name =
      [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];

All others code related to path takes
Browser::Get()->GetName()
is it expected ?
For my point of view, this is not consistent.

If you agree, I can fix both and reactivate my unit test for applogs. It will fix this issue as well
#19836

@emmkimme

Copy link
Copy Markdown
Contributor Author

Dear @codebytere, @poiru, @ckerr, reviewers
Do you know when it is possible to plan a review of this MR please
Thanks

@emmkimme

Copy link
Copy Markdown
Contributor Author

Hi @MarshallOfSound,
do you know when it can be possible to schedule a review of this please.
Thanks

@emmkimme

emmkimme commented Jan 15, 2021 •

Copy link
Copy Markdown
Contributor Author

@jkleinsc
Remaining check failures are triggered by tests not related to my MR.
Then could we review this MR please ?

@jkleinsc
jkleinsc requested a review from a team January 20, 2021 16:22

@jkleinsc jkleinsc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test failures appear to be flakes unrelated to this PR. However it appears that this PR is adding two git submodules that shouldn't be there:
Screen Shot 2021-01-20 at 11 27 50 AM

@emmkimme

emmkimme commented Jan 21, 2021 •

Copy link
Copy Markdown
Contributor Author

Sorry for that, should be good now (finally!)

@codebytere
codebytere requested a review from jkleinsc January 25, 2021 19:48
Comment thread spec-main/api-app-path-spec.ts Resolved
Comment thread spec-main/api-app-path-spec.ts Resolved
Comment thread spec-main/api-app-path-spec.ts Resolved
Comment thread shell/browser/browser.cc Resolved
Comment thread shell/browser/browser.cc Resolved
Comment thread docs/api/app.md Resolved
@ckerr

ckerr commented Jan 25, 2021

Copy link
Copy Markdown
Member

BTW @emmkimme you have really gone above & beyond with your persistence on this PR. 🎉 ❤️

@jkleinsc
jkleinsc requested a review from ckerr February 4, 2021 16:36
Comment on lines +7 to +8
Allow to customize Electron/Chromium cache location
See MR: https://github.com/electron/electron/pull/19693

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per https://www.electronjs.org/docs/development/patches#patch-justification, every patch we carry is a significant ongoing maintenance burden. We ask that the description of a patch includes a justification of why they're worth that maintenance burden, in addition to a description of the functionality that the patch adds.

In order of descending desirability:

  1. Can this patch be removed entirely by achieving the desired functionality without having to include a patch?
  2. Can this patch be merged upstream into Chromium itself? (see here for a guide on contributing to Chromium)
  3. In the patch description, describe the reasons why neither of the above two are possible, and include guidance for a future maintainer looking to clean up patches about how they can check whether this patch is still required (maybe as simple as "if it compiles without this patch, the patch is no longer needed").

Thanks for helping us keep maintaining Electron sustainable 🙇

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Session data (like cookies and indexDB) should be considered as user data instead of cache, apps usually store login tokens and un-uploaded works in it. So if we silently change the place where session data are stored in Electron, users will lose login sessions and unsaved works after an app update, which is not acceptable.

It is totally Chromium's fault that it put session data and browser cache in one directory, and is impossible to separate them without a massive patch to Chromium, but I think we can use a different way other than userCache to make things less confusing.

I propose:

  1. Do not list userCache in public API.
  2. Add browserData path which is used to store cookies and cache, i.e. the "cache path" in this PR.
  3. The default value of browserData should be the same with userData.

In this way we can achieve:

  1. a clear API that does not give a wrong assumption of what "cache" means;
  2. backward-compatibility without risk losing users' data;
  3. future-proof that if in future it became possible to separate locations of cookies and caches, we could still introduce userCache path.

@emmkimme

emmkimme commented Feb 5, 2021

Copy link
Copy Markdown
Contributor Author

Hi @zcbenz,
I'm afraid, there is some misunderstandings

  • while new userCache path is not modified, the location remains the same, it is backward compatible.
  • if as an Electron app developer I want to control where the whole cache is located (cookies, db, session, file cache..), this is under my responsibility. I do not get the point to split the cache into different pieces. From my point of view, it is less error-prone, simpler and more consistent, to consider all 'temporary' files as a whole.
    my2cents

@zcbenz

zcbenz commented Feb 5, 2021

Copy link
Copy Markdown
Contributor

while new userCache path is not modified, the location remains the same, it is backward compatible.

The userCache path points to DIR_CACHE/AppName:

case DIR_USER_CACHE:
base::PathService::Get(DIR_CACHE, path);
*path = path->Append(
base::FilePath::FromUTF8Unsafe(Browser::Get()->GetName()));
return true;

I'm not sure for Windows, but for macOS and Linux the DIR_CACHE points to a different location than DIR_APP_DATA. So if we change BrowserContext's path from DIR_USER_DATA to DIR_USER_CACHE it will not be backward compatible.

if as an Electron app developer I want to control where the whole cache is located (cookies, db, session, file cache..), this is under my responsibility. I do not get the point to split the cache into different pieces. From my point of view, it is less error-prone, simpler and more consistent, to consider all 'temporary' files as a whole.

For desktop apps, cache is usually something that can be safely deleted without affecting current app's state, for example, for a chat app the login session should be kept after deleting the app's cache. This is different from the cache under browser's context, where cookies and sessions are also part of cache.

With the name of userCache, it is not clear whether things like cookies and localStorage belong to "cache", and according to the discussion in #8124, I believe lots of people would be surprised if cookies are treated as cache in this API.

So I'm proposing using browserData as the path's name instead, which will not cause confusions like userCache, and it will be clear that if this path is changed all cookies will be lost.

As for separating cookies from other cache, I agree it is error-prone and we should not do it unless it is supported by upstream Chromium.

@emmkimme

emmkimme commented Apr 1, 2021 •

Copy link
Copy Markdown
Contributor Author

I'm still looking for a Mac machine for investigating. Should have one soon !

@digitalgopnik

Copy link
Copy Markdown

I'm still looking for a Mac machine for investigating. Should have one soon !

Maybe i can help you? I will look through the failing checks.

@emmkimme

Copy link
Copy Markdown
Contributor Author

I'm still looking for a Mac machine for investigating. Should have one soon !

Maybe i can help you? I will look through the failing checks.

That would be great. Lot of thanks. Let me know if you need any help

# Conflicts:
#	lib/browser/api/app.ts
#	shell/app/electron_main_delegate.cc
#	shell/browser/api/electron_api_app.cc
#	shell/browser/api/electron_api_app_mac.mm
#	shell/browser/browser.cc
#	shell/browser/electron_browser_context.cc
#	shell/browser/electron_browser_main_parts.cc
@emmkimme

Copy link
Copy Markdown
Contributor Author

I did not rebase for 5 months and meanwhile the code change a lot, especially new DIR_* constant appeared and a new management of them. I'm afraid I have to redo this feature from scratch.

@emmkimme

Copy link
Copy Markdown
Contributor Author

Has been closed and replaced by this one: Redirect Electron/Chromium cache location

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

Labels

api-review/requested 🗳 semver/minor backwards-compatible functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants