X Tutup
Skip to content

Commit 46d7439

Browse files
trop[bot]miniakdeepak1556ckerr
authored
feat: allow setting code cache directory (electron#33286)
* feat: allow setting code cache directory * chore: address review feedback * chore: update docs Co-authored-by: Charles Kerr <charles@charleskerr.com> * chore: rewrite with base::Contains Co-authored-by: Charles Kerr <charles@charleskerr.com> Co-authored-by: Milan Burda <milan.burda@gmail.com> Co-authored-by: deepak1556 <hop2deep@gmail.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
1 parent d305082 commit 46d7439

File tree

4 files changed

+72
-0
lines changed

4 files changed

+72
-0
lines changed

docs/api/session.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,20 @@ this session just before normal `preload` scripts run.
868868
Returns `string[]` an array of paths to preload scripts that have been
869869
registered.
870870

871+
#### `ses.setCodeCachePath(path)`
872+
873+
* `path` String - Absolute path to store the v8 generated JS code cache from the renderer.
874+
875+
Sets the directory to store the generated JS [code cache](https://v8.dev/blog/code-caching-for-devs) for this session. The directory is not required to be created by the user before this call, the runtime will create if it does not exist otherwise will use the existing directory. If directory cannot be created, then code cache will not be used and all operations related to code cache will fail silently inside the runtime. By default, the directory will be `Code Cache` under the
876+
respective user data folder.
877+
878+
#### `ses.clearCodeCaches(options)`
879+
880+
* `options` Object
881+
* `urls` String[] (optional) - An array of url corresponding to the resource whose generated code cache needs to be removed. If the list is empty then all entries in the cache directory will be removed.
882+
883+
Returns `Promise<void>` - resolves when the code cache clear operation is complete.
884+
871885
#### `ses.setSpellCheckerEnabled(enable)`
872886

873887
* `enable` boolean

shell/browser/api/electron_api_session.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "components/proxy_config/proxy_config_dictionary.h"
2929
#include "components/proxy_config/proxy_config_pref_names.h"
3030
#include "components/proxy_config/proxy_prefs.h"
31+
#include "content/browser/code_cache/generated_code_cache_context.h" // nogncheck
3132
#include "content/public/browser/browser_task_traits.h"
3233
#include "content/public/browser/browser_thread.h"
3334
#include "content/public/browser/download_item_utils.h"
@@ -976,6 +977,45 @@ v8::Local<v8::Value> Session::GetPath(v8::Isolate* isolate) {
976977
return gin::ConvertToV8(isolate, browser_context_->GetPath());
977978
}
978979

980+
void Session::SetCodeCachePath(gin::Arguments* args) {
981+
base::FilePath code_cache_path;
982+
auto* storage_partition = browser_context_->GetDefaultStoragePartition();
983+
auto* code_cache_context = storage_partition->GetGeneratedCodeCacheContext();
984+
if (code_cache_context) {
985+
if (!args->GetNext(&code_cache_path) || !code_cache_path.IsAbsolute()) {
986+
args->ThrowTypeError(
987+
"Absolute path must be provided to store code cache.");
988+
return;
989+
}
990+
code_cache_context->Initialize(
991+
code_cache_path, 0 /* allows disk_cache to choose the size */);
992+
}
993+
}
994+
995+
v8::Local<v8::Promise> Session::ClearCodeCaches(
996+
const gin_helper::Dictionary& options) {
997+
auto* isolate = JavascriptEnvironment::GetIsolate();
998+
gin_helper::Promise<void> promise(isolate);
999+
v8::Local<v8::Promise> handle = promise.GetHandle();
1000+
1001+
std::set<GURL> url_list;
1002+
base::RepeatingCallback<bool(const GURL&)> url_matcher = base::NullCallback();
1003+
if (options.Get("urls", &url_list) && !url_list.empty()) {
1004+
url_matcher = base::BindRepeating(
1005+
[](const std::set<GURL>& url_list, const GURL& url) {
1006+
return base::Contains(url_list, url);
1007+
},
1008+
url_list);
1009+
}
1010+
1011+
browser_context_->GetDefaultStoragePartition()->ClearCodeCaches(
1012+
base::Time(), base::Time::Max(), url_matcher,
1013+
base::BindOnce(gin_helper::Promise<void>::ResolvePromise,
1014+
std::move(promise)));
1015+
1016+
return handle;
1017+
}
1018+
9791019
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
9801020
base::Value Session::GetSpellCheckerLanguages() {
9811021
return browser_context_->prefs()
@@ -1203,6 +1243,8 @@ gin::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
12031243
.SetMethod("preconnect", &Session::Preconnect)
12041244
.SetMethod("closeAllConnections", &Session::CloseAllConnections)
12051245
.SetMethod("getStoragePath", &Session::GetPath)
1246+
.SetMethod("setCodeCachePath", &Session::SetCodeCachePath)
1247+
.SetMethod("clearCodeCaches", &Session::ClearCodeCaches)
12061248
.SetProperty("cookies", &Session::Cookies)
12071249
.SetProperty("netLog", &Session::NetLog)
12081250
.SetProperty("protocol", &Session::Protocol)

shell/browser/api/electron_api_session.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ class Session : public gin::Wrappable<Session>,
127127
void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args);
128128
v8::Local<v8::Promise> CloseAllConnections();
129129
v8::Local<v8::Value> GetPath(v8::Isolate* isolate);
130+
void SetCodeCachePath(gin::Arguments* args);
131+
v8::Local<v8::Promise> ClearCodeCaches(const gin_helper::Dictionary& options);
130132
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
131133
base::Value GetSpellCheckerLanguages();
132134
void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,

spec-main/api-session-spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,20 @@ describe('session module', () => {
11211121
});
11221122
});
11231123

1124+
describe('session.setCodeCachePath()', () => {
1125+
it('throws when relative or empty path is provided', () => {
1126+
expect(() => {
1127+
session.defaultSession.setCodeCachePath('../fixtures');
1128+
}).to.throw('Absolute path must be provided to store code cache.');
1129+
expect(() => {
1130+
session.defaultSession.setCodeCachePath('');
1131+
}).to.throw('Absolute path must be provided to store code cache.');
1132+
expect(() => {
1133+
session.defaultSession.setCodeCachePath(path.join(app.getPath('userData'), 'test-code-cache'));
1134+
}).to.not.throw();
1135+
});
1136+
});
1137+
11241138
describe('ses.setSSLConfig()', () => {
11251139
it('can disable cipher suites', async () => {
11261140
const ses = session.fromPartition('' + Math.random());

0 commit comments

Comments
 (0)
X Tutup