From 871136bf91d46a2440223ad69a7630b649e611ea Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Thu, 25 Jul 2024 22:32:17 +0200 Subject: [PATCH] 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 --- 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 02e1e24b1a3d..65f0495e2755 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 2bbf774c2bde..3109fc9d7853 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" @@ -15,18 +13,18 @@ #include "shell/common/node_includes.h" #include "shell/common/platform_util.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 && @@ -43,7 +41,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(); @@ -53,7 +51,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(); @@ -81,7 +79,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 ""; @@ -128,7 +126,7 @@ std::string DecryptString(v8::Isolate* isolate, v8::Local buffer) { return plaintext; } -} // namespace electron::safestorage +} // namespace void Initialize(v8::Local exports, v8::Local unused, @@ -136,16 +134,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_