From 660872c048b6c040cb740560125eebc71114e31a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 25 Jul 2024 13:18:13 -0500 Subject: [PATCH] refactor: move safe_storage functions into anonymous namespace (#43032) --- filenames.gni | 1 - .../browser/api/electron_api_safe_storage.cc | 33 ++++++++----------- shell/browser/api/electron_api_safe_storage.h | 21 ------------ 3 files changed, 14 insertions(+), 41 deletions(-) delete mode 100644 shell/browser/api/electron_api_safe_storage.h diff --git a/filenames.gni b/filenames.gni index 5c7ca21fa07a..97b348cd7b1c 100644 --- a/filenames.gni +++ b/filenames.gni @@ -290,7 +290,6 @@ filenames = { "shell/browser/api/electron_api_push_notifications.cc", "shell/browser/api/electron_api_push_notifications.h", "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.h", "shell/browser/api/electron_api_service_worker_context.cc", diff --git a/shell/browser/api/electron_api_safe_storage.cc b/shell/browser/api/electron_api_safe_storage.cc index b1704754d169..a8c63ec718f7 100644 --- a/shell/browser/api/electron_api_safe_storage.cc +++ b/shell/browser/api/electron_api_safe_storage.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include "shell/browser/api/electron_api_safe_storage.h" - #include #include "components/os_crypt/sync/os_crypt.h" @@ -14,18 +12,18 @@ #include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_includes.h" -namespace electron::safestorage { +namespace { -static const char* kEncryptionVersionPrefixV10 = "v10"; -static const char* kEncryptionVersionPrefixV11 = "v11"; -static bool use_password_v10 = false; +const char* kEncryptionVersionPrefixV10 = "v10"; +const char* kEncryptionVersionPrefixV11 = "v11"; +bool use_password_v10 = false; bool IsEncryptionAvailable() { #if BUILDFLAG(IS_LINUX) // Calling IsEncryptionAvailable() before the app is ready results in a crash // on Linux. // Refs: https://github.com/electron/electron/issues/32206. - if (!Browser::Get()->is_ready()) + if (!electron::Browser::Get()->is_ready()) return false; return OSCrypt::IsEncryptionAvailable() || (use_password_v10 && @@ -42,7 +40,7 @@ void SetUsePasswordV10(bool use) { #if BUILDFLAG(IS_LINUX) std::string GetSelectedLinuxBackend() { - if (!Browser::Get()->is_ready()) + if (!electron::Browser::Get()->is_ready()) return "unknown"; return static_cast(g_browser_process) ->linux_storage_backend(); @@ -52,7 +50,7 @@ std::string GetSelectedLinuxBackend() { v8::Local EncryptString(v8::Isolate* isolate, const std::string& plaintext) { if (!IsEncryptionAvailable()) { - if (!Browser::Get()->is_ready()) { + if (!electron::Browser::Get()->is_ready()) { gin_helper::ErrorThrower(isolate).ThrowError( "safeStorage cannot be used before app is ready"); return v8::Local(); @@ -80,7 +78,7 @@ v8::Local EncryptString(v8::Isolate* isolate, std::string DecryptString(v8::Isolate* isolate, v8::Local buffer) { if (!IsEncryptionAvailable()) { - if (!Browser::Get()->is_ready()) { + if (!electron::Browser::Get()->is_ready()) { gin_helper::ErrorThrower(isolate).ThrowError( "safeStorage cannot be used before app is ready"); return ""; @@ -127,7 +125,7 @@ std::string DecryptString(v8::Isolate* isolate, v8::Local buffer) { return plaintext; } -} // namespace electron::safestorage +} // namespace void Initialize(v8::Local exports, v8::Local unused, @@ -135,16 +133,13 @@ void Initialize(v8::Local exports, void* priv) { v8::Isolate* isolate = context->GetIsolate(); gin_helper::Dictionary dict(isolate, exports); - dict.SetMethod("isEncryptionAvailable", - &electron::safestorage::IsEncryptionAvailable); - dict.SetMethod("encryptString", &electron::safestorage::EncryptString); - dict.SetMethod("decryptString", &electron::safestorage::DecryptString); - dict.SetMethod("setUsePlainTextEncryption", - &electron::safestorage::SetUsePasswordV10); + dict.SetMethod("decryptString", &DecryptString); + dict.SetMethod("encryptString", &EncryptString); #if BUILDFLAG(IS_LINUX) - dict.SetMethod("getSelectedStorageBackend", - &electron::safestorage::GetSelectedLinuxBackend); + dict.SetMethod("getSelectedStorageBackend", &GetSelectedLinuxBackend); #endif + dict.SetMethod("isEncryptionAvailable", &IsEncryptionAvailable); + dict.SetMethod("setUsePlainTextEncryption", &SetUsePasswordV10); } NODE_LINKED_BINDING_CONTEXT_AWARE(electron_browser_safe_storage, Initialize) diff --git a/shell/browser/api/electron_api_safe_storage.h b/shell/browser/api/electron_api_safe_storage.h deleted file mode 100644 index 1ecde80f8cee..000000000000 --- a/shell/browser/api/electron_api_safe_storage.h +++ /dev/null @@ -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_