From 2721bc2b6036ff67437c0289c3ffd7fe738b813b Mon Sep 17 00:00:00 2001 From: Milan Burda Date: Mon, 27 Sep 2021 19:20:46 +0200 Subject: [PATCH 1/4] feat: allow setting code cache directory --- docs/api/session.md | 15 +++++++++ shell/browser/api/electron_api_session.cc | 37 +++++++++++++++++++++++ shell/browser/api/electron_api_session.h | 2 ++ 3 files changed, 54 insertions(+) diff --git a/docs/api/session.md b/docs/api/session.md index 2517fd178d2e0..e10d478f2baeb 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -868,6 +868,21 @@ this session just before normal `preload` scripts run. Returns `string[]` an array of paths to preload scripts that have been registered. +#### `ses.setCodeCachePath(path)` + +* `path` String - The Code cache location. + +Sets code cache directory. By default, the directory will be `Code Cache` under the +respective user data folder. + +#### `ses.clearCodeCaches()` + +* `options` Object + * `urls` String[] (optional) - An array of url corresponding to the resource that needs to + be removed. If the list is empty then all entries in the cache will be removed. + +Returns `Promise` - resolves when the code cache clear operation is complete. + #### `ses.setSpellCheckerEnabled(enable)` * `enable` boolean diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 1322378fb20d7..8ecbbd014b569 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -28,6 +28,7 @@ #include "components/proxy_config/proxy_config_dictionary.h" #include "components/proxy_config/proxy_config_pref_names.h" #include "components/proxy_config/proxy_prefs.h" +#include "content/browser/code_cache/generated_code_cache_context.h" // nogncheck #include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_item_utils.h" @@ -974,6 +975,40 @@ v8::Local Session::GetPath(v8::Isolate* isolate) { return gin::ConvertToV8(isolate, browser_context_->GetPath()); } +void Session::SetCodeCachePath(const base::FilePath& code_cache_path) { + auto* storage_partition = browser_context_->GetDefaultStoragePartition(); + auto* code_cache_context = storage_partition->GetGeneratedCodeCacheContext(); + if (code_cache_context && !code_cache_path.empty()) { + code_cache_context->Initialize( + code_cache_path, 0 /* allows disk_cache to choose the size */); + } +} + +v8::Local Session::ClearCodeCaches( + const gin_helper::Dictionary& options) { + auto* isolate = JavascriptEnvironment::GetIsolate(); + gin_helper::Promise promise(isolate); + v8::Local handle = promise.GetHandle(); + + std::set url_list; + base::RepeatingCallback url_matcher = base::NullCallback(); + if (options.Get("urls", &url_list) && !url_list.empty()) { + url_matcher = base::BindRepeating( + [](const std::set& url_list, const GURL& url) { + auto it = url_list.find(url); + return it != url_list.end(); + }, + url_list); + } + + browser_context_->GetDefaultStoragePartition()->ClearCodeCaches( + base::Time(), base::Time::Max(), url_matcher, + base::BindOnce(gin_helper::Promise::ResolvePromise, + std::move(promise))); + + return handle; +} + #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value Session::GetSpellCheckerLanguages() { return browser_context_->prefs() @@ -1201,6 +1236,8 @@ gin::ObjectTemplateBuilder Session::GetObjectTemplateBuilder( .SetMethod("preconnect", &Session::Preconnect) .SetMethod("closeAllConnections", &Session::CloseAllConnections) .SetMethod("getStoragePath", &Session::GetPath) + .SetMethod("setCodeCachePath", &Session::SetCodeCachePath) + .SetMethod("clearCodeCaches", &Session::ClearCodeCaches) .SetProperty("cookies", &Session::Cookies) .SetProperty("netLog", &Session::NetLog) .SetProperty("protocol", &Session::Protocol) diff --git a/shell/browser/api/electron_api_session.h b/shell/browser/api/electron_api_session.h index aedf234d38a58..baf9f99ae8ab7 100644 --- a/shell/browser/api/electron_api_session.h +++ b/shell/browser/api/electron_api_session.h @@ -127,6 +127,8 @@ class Session : public gin::Wrappable, void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args); v8::Local CloseAllConnections(); v8::Local GetPath(v8::Isolate* isolate); + void SetCodeCachePath(const base::FilePath& code_cache_path); + v8::Local ClearCodeCaches(const gin_helper::Dictionary& options); #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value GetSpellCheckerLanguages(); void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower, From 19b5b054ecf298db7108f0bf0b35f04f0ecbc889 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Thu, 10 Mar 2022 21:08:44 +0900 Subject: [PATCH 2/4] chore: address review feedback --- docs/api/session.md | 9 ++++----- shell/browser/api/electron_api_session.cc | 10 ++++++++-- shell/browser/api/electron_api_session.h | 2 +- spec-main/api-session-spec.ts | 14 ++++++++++++++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/docs/api/session.md b/docs/api/session.md index e10d478f2baeb..16080ab4a24df 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -870,16 +870,15 @@ registered. #### `ses.setCodeCachePath(path)` -* `path` String - The Code cache location. +* `path` String - Absolute path to store the v8 generated JS code cache from the renderer. -Sets code cache directory. By default, the directory will be `Code Cache` under the +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 exit 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 respective user data folder. -#### `ses.clearCodeCaches()` +#### `ses.clearCodeCaches(options)` * `options` Object - * `urls` String[] (optional) - An array of url corresponding to the resource that needs to - be removed. If the list is empty then all entries in the cache will be removed. + * `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. Returns `Promise` - resolves when the code cache clear operation is complete. diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index 8ecbbd014b569..ddf763a5b02fe 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -975,10 +975,16 @@ v8::Local Session::GetPath(v8::Isolate* isolate) { return gin::ConvertToV8(isolate, browser_context_->GetPath()); } -void Session::SetCodeCachePath(const base::FilePath& code_cache_path) { +void Session::SetCodeCachePath(gin::Arguments* args) { + base::FilePath code_cache_path; auto* storage_partition = browser_context_->GetDefaultStoragePartition(); auto* code_cache_context = storage_partition->GetGeneratedCodeCacheContext(); - if (code_cache_context && !code_cache_path.empty()) { + if (code_cache_context) { + if (!args->GetNext(&code_cache_path) || !code_cache_path.IsAbsolute()) { + args->ThrowTypeError( + "Absolute path must be provided to store code cache."); + return; + } code_cache_context->Initialize( code_cache_path, 0 /* allows disk_cache to choose the size */); } diff --git a/shell/browser/api/electron_api_session.h b/shell/browser/api/electron_api_session.h index baf9f99ae8ab7..e9b69380dfaf9 100644 --- a/shell/browser/api/electron_api_session.h +++ b/shell/browser/api/electron_api_session.h @@ -127,7 +127,7 @@ class Session : public gin::Wrappable, void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args); v8::Local CloseAllConnections(); v8::Local GetPath(v8::Isolate* isolate); - void SetCodeCachePath(const base::FilePath& code_cache_path); + void SetCodeCachePath(gin::Arguments* args); v8::Local ClearCodeCaches(const gin_helper::Dictionary& options); #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER) base::Value GetSpellCheckerLanguages(); diff --git a/spec-main/api-session-spec.ts b/spec-main/api-session-spec.ts index 223d826d7eb84..f504d0db6d464 100644 --- a/spec-main/api-session-spec.ts +++ b/spec-main/api-session-spec.ts @@ -1121,6 +1121,20 @@ describe('session module', () => { }); }); + describe('session.setCodeCachePath()', () => { + it('throws when relative or empty path is provided', () => { + expect(() => { + session.defaultSession.setCodeCachePath('../fixtures'); + }).to.throw('Absolute path must be provided to store code cache.'); + expect(() => { + session.defaultSession.setCodeCachePath(''); + }).to.throw('Absolute path must be provided to store code cache.'); + expect(() => { + session.defaultSession.setCodeCachePath(path.join(app.getPath('userData'), 'test-code-cache')); + }).to.not.throw(); + }); + }); + describe('ses.setSSLConfig()', () => { it('can disable cipher suites', async () => { const ses = session.fromPartition('' + Math.random()); From 884fbd3a590925256eb9ade98013246f82dfd469 Mon Sep 17 00:00:00 2001 From: Robo Date: Mon, 14 Mar 2022 20:12:30 -0700 Subject: [PATCH 3/4] chore: update docs Co-authored-by: Charles Kerr --- docs/api/session.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/session.md b/docs/api/session.md index 16080ab4a24df..8bb1ad5ab3c5c 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -872,7 +872,7 @@ registered. * `path` String - Absolute path to store the v8 generated JS code cache from the renderer. -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 exit 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 +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 respective user data folder. #### `ses.clearCodeCaches(options)` From 45edc28cbee3caf922457f365126c207515cbd69 Mon Sep 17 00:00:00 2001 From: Robo Date: Mon, 14 Mar 2022 20:14:13 -0700 Subject: [PATCH 4/4] chore: rewrite with base::Contains Co-authored-by: Charles Kerr --- shell/browser/api/electron_api_session.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shell/browser/api/electron_api_session.cc b/shell/browser/api/electron_api_session.cc index ddf763a5b02fe..843a32d2c29a6 100644 --- a/shell/browser/api/electron_api_session.cc +++ b/shell/browser/api/electron_api_session.cc @@ -1001,8 +1001,7 @@ v8::Local Session::ClearCodeCaches( if (options.Get("urls", &url_list) && !url_list.empty()) { url_matcher = base::BindRepeating( [](const std::set& url_list, const GURL& url) { - auto it = url_list.find(url); - return it != url_list.end(); + return base::Contains(url_list, url); }, url_list); }