Skip to content

Commit

Permalink
feat: allow setting code cache directory
Browse files Browse the repository at this point in the history
  • Loading branch information
miniak committed Sep 30, 2021
1 parent 6f684d5 commit a7e1b10
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/api/session.md
Expand Up @@ -831,6 +831,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<void>` - resolves when the code cache clear operation is complete.

#### `ses.setSpellCheckerEnabled(enable)`

* `enable` Boolean
Expand Down
37 changes: 37 additions & 0 deletions shell/browser/api/electron_api_session.cc
Expand Up @@ -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"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_item_utils.h"
Expand Down Expand Up @@ -976,6 +977,40 @@ v8::Local<v8::Value> 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<v8::Promise> Session::ClearCodeCaches(
const gin_helper::Dictionary& options) {
auto* isolate = JavascriptEnvironment::GetIsolate();
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

std::set<GURL> url_list;
base::RepeatingCallback<bool(const GURL&)> url_matcher = base::NullCallback();
if (options.Get("urls", &url_list) && !url_list.empty()) {
url_matcher = base::BindRepeating(
[](const std::set<GURL>& 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<void>::ResolvePromise,
std::move(promise)));

return handle;
}

#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
base::Value Session::GetSpellCheckerLanguages() {
return browser_context_->prefs()
Expand Down Expand Up @@ -1203,6 +1238,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)
Expand Down
2 changes: 2 additions & 0 deletions shell/browser/api/electron_api_session.h
Expand Up @@ -127,6 +127,8 @@ class Session : public gin::Wrappable<Session>,
void Preconnect(const gin_helper::Dictionary& options, gin::Arguments* args);
v8::Local<v8::Promise> CloseAllConnections();
v8::Local<v8::Value> GetPath(v8::Isolate* isolate);
void SetCodeCachePath(const base::FilePath& code_cache_path);
v8::Local<v8::Promise> ClearCodeCaches(const gin_helper::Dictionary& options);
#if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
base::Value GetSpellCheckerLanguages();
void SetSpellCheckerLanguages(gin_helper::ErrorThrower thrower,
Expand Down

0 comments on commit a7e1b10

Please sign in to comment.