refactor: move safe_storage functions into anonymous namespace (#43051)
Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
parent
94381733ff
commit
871136bf91
3 changed files with 14 additions and 41 deletions
|
@ -290,7 +290,6 @@ filenames = {
|
||||||
"shell/browser/api/electron_api_push_notifications.cc",
|
"shell/browser/api/electron_api_push_notifications.cc",
|
||||||
"shell/browser/api/electron_api_push_notifications.h",
|
"shell/browser/api/electron_api_push_notifications.h",
|
||||||
"shell/browser/api/electron_api_safe_storage.cc",
|
"shell/browser/api/electron_api_safe_storage.cc",
|
||||||
"shell/browser/api/electron_api_safe_storage.h",
|
|
||||||
"shell/browser/api/electron_api_screen.cc",
|
"shell/browser/api/electron_api_screen.cc",
|
||||||
"shell/browser/api/electron_api_screen.h",
|
"shell/browser/api/electron_api_screen.h",
|
||||||
"shell/browser/api/electron_api_service_worker_context.cc",
|
"shell/browser/api/electron_api_service_worker_context.cc",
|
||||||
|
|
|
@ -2,8 +2,6 @@
|
||||||
// Use of this source code is governed by the MIT license that can be
|
// Use of this source code is governed by the MIT license that can be
|
||||||
// found in the LICENSE file.
|
// found in the LICENSE file.
|
||||||
|
|
||||||
#include "shell/browser/api/electron_api_safe_storage.h"
|
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "components/os_crypt/sync/os_crypt.h"
|
#include "components/os_crypt/sync/os_crypt.h"
|
||||||
|
@ -15,18 +13,18 @@
|
||||||
#include "shell/common/node_includes.h"
|
#include "shell/common/node_includes.h"
|
||||||
#include "shell/common/platform_util.h"
|
#include "shell/common/platform_util.h"
|
||||||
|
|
||||||
namespace electron::safestorage {
|
namespace {
|
||||||
|
|
||||||
static const char* kEncryptionVersionPrefixV10 = "v10";
|
const char* kEncryptionVersionPrefixV10 = "v10";
|
||||||
static const char* kEncryptionVersionPrefixV11 = "v11";
|
const char* kEncryptionVersionPrefixV11 = "v11";
|
||||||
static bool use_password_v10 = false;
|
bool use_password_v10 = false;
|
||||||
|
|
||||||
bool IsEncryptionAvailable() {
|
bool IsEncryptionAvailable() {
|
||||||
#if BUILDFLAG(IS_LINUX)
|
#if BUILDFLAG(IS_LINUX)
|
||||||
// Calling IsEncryptionAvailable() before the app is ready results in a crash
|
// Calling IsEncryptionAvailable() before the app is ready results in a crash
|
||||||
// on Linux.
|
// on Linux.
|
||||||
// Refs: https://github.com/electron/electron/issues/32206.
|
// Refs: https://github.com/electron/electron/issues/32206.
|
||||||
if (!Browser::Get()->is_ready())
|
if (!electron::Browser::Get()->is_ready())
|
||||||
return false;
|
return false;
|
||||||
return OSCrypt::IsEncryptionAvailable() ||
|
return OSCrypt::IsEncryptionAvailable() ||
|
||||||
(use_password_v10 &&
|
(use_password_v10 &&
|
||||||
|
@ -43,7 +41,7 @@ void SetUsePasswordV10(bool use) {
|
||||||
|
|
||||||
#if BUILDFLAG(IS_LINUX)
|
#if BUILDFLAG(IS_LINUX)
|
||||||
std::string GetSelectedLinuxBackend() {
|
std::string GetSelectedLinuxBackend() {
|
||||||
if (!Browser::Get()->is_ready())
|
if (!electron::Browser::Get()->is_ready())
|
||||||
return "unknown";
|
return "unknown";
|
||||||
return static_cast<BrowserProcessImpl*>(g_browser_process)
|
return static_cast<BrowserProcessImpl*>(g_browser_process)
|
||||||
->linux_storage_backend();
|
->linux_storage_backend();
|
||||||
|
@ -53,7 +51,7 @@ std::string GetSelectedLinuxBackend() {
|
||||||
v8::Local<v8::Value> EncryptString(v8::Isolate* isolate,
|
v8::Local<v8::Value> EncryptString(v8::Isolate* isolate,
|
||||||
const std::string& plaintext) {
|
const std::string& plaintext) {
|
||||||
if (!IsEncryptionAvailable()) {
|
if (!IsEncryptionAvailable()) {
|
||||||
if (!Browser::Get()->is_ready()) {
|
if (!electron::Browser::Get()->is_ready()) {
|
||||||
gin_helper::ErrorThrower(isolate).ThrowError(
|
gin_helper::ErrorThrower(isolate).ThrowError(
|
||||||
"safeStorage cannot be used before app is ready");
|
"safeStorage cannot be used before app is ready");
|
||||||
return v8::Local<v8::Value>();
|
return v8::Local<v8::Value>();
|
||||||
|
@ -81,7 +79,7 @@ v8::Local<v8::Value> EncryptString(v8::Isolate* isolate,
|
||||||
|
|
||||||
std::string DecryptString(v8::Isolate* isolate, v8::Local<v8::Value> buffer) {
|
std::string DecryptString(v8::Isolate* isolate, v8::Local<v8::Value> buffer) {
|
||||||
if (!IsEncryptionAvailable()) {
|
if (!IsEncryptionAvailable()) {
|
||||||
if (!Browser::Get()->is_ready()) {
|
if (!electron::Browser::Get()->is_ready()) {
|
||||||
gin_helper::ErrorThrower(isolate).ThrowError(
|
gin_helper::ErrorThrower(isolate).ThrowError(
|
||||||
"safeStorage cannot be used before app is ready");
|
"safeStorage cannot be used before app is ready");
|
||||||
return "";
|
return "";
|
||||||
|
@ -128,7 +126,7 @@ std::string DecryptString(v8::Isolate* isolate, v8::Local<v8::Value> buffer) {
|
||||||
return plaintext;
|
return plaintext;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace electron::safestorage
|
} // namespace
|
||||||
|
|
||||||
void Initialize(v8::Local<v8::Object> exports,
|
void Initialize(v8::Local<v8::Object> exports,
|
||||||
v8::Local<v8::Value> unused,
|
v8::Local<v8::Value> unused,
|
||||||
|
@ -136,16 +134,13 @@ void Initialize(v8::Local<v8::Object> exports,
|
||||||
void* priv) {
|
void* priv) {
|
||||||
v8::Isolate* isolate = context->GetIsolate();
|
v8::Isolate* isolate = context->GetIsolate();
|
||||||
gin_helper::Dictionary dict(isolate, exports);
|
gin_helper::Dictionary dict(isolate, exports);
|
||||||
dict.SetMethod("isEncryptionAvailable",
|
dict.SetMethod("decryptString", &DecryptString);
|
||||||
&electron::safestorage::IsEncryptionAvailable);
|
dict.SetMethod("encryptString", &EncryptString);
|
||||||
dict.SetMethod("encryptString", &electron::safestorage::EncryptString);
|
|
||||||
dict.SetMethod("decryptString", &electron::safestorage::DecryptString);
|
|
||||||
dict.SetMethod("setUsePlainTextEncryption",
|
|
||||||
&electron::safestorage::SetUsePasswordV10);
|
|
||||||
#if BUILDFLAG(IS_LINUX)
|
#if BUILDFLAG(IS_LINUX)
|
||||||
dict.SetMethod("getSelectedStorageBackend",
|
dict.SetMethod("getSelectedStorageBackend", &GetSelectedLinuxBackend);
|
||||||
&electron::safestorage::GetSelectedLinuxBackend);
|
|
||||||
#endif
|
#endif
|
||||||
|
dict.SetMethod("isEncryptionAvailable", &IsEncryptionAvailable);
|
||||||
|
dict.SetMethod("setUsePlainTextEncryption", &SetUsePasswordV10);
|
||||||
}
|
}
|
||||||
|
|
||||||
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_safe_storage, Initialize)
|
NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_safe_storage, Initialize)
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
// Copyright (c) 2021 Slack Technologies, Inc.
|
|
||||||
// Use of this source code is governed by the MIT license that can be
|
|
||||||
// found in the LICENSE file.
|
|
||||||
|
|
||||||
#ifndef ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SAFE_STORAGE_H_
|
|
||||||
#define ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SAFE_STORAGE_H_
|
|
||||||
|
|
||||||
#include "base/dcheck_is_on.h"
|
|
||||||
|
|
||||||
namespace electron::safestorage {
|
|
||||||
|
|
||||||
// Used in a DCHECK to validate that our assumption that the network context
|
|
||||||
// manager has initialized before app ready holds true. Only used in the
|
|
||||||
// testing build
|
|
||||||
#if DCHECK_IS_ON()
|
|
||||||
void SetElectronCryptoReady(bool ready);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace electron::safestorage
|
|
||||||
|
|
||||||
#endif // ELECTRON_SHELL_BROWSER_API_ELECTRON_API_SAFE_STORAGE_H_
|
|
Loading…
Reference in a new issue