From 3a4edd7a2bc635dc167e620bc04b9d0fbbfa7679 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Thu, 9 Jan 2025 09:22:58 -0600 Subject: [PATCH] perf: cache whether or not ELECTRON_DEBUG_NOTIFICATIONS env var is set (#45162) * perf: cache whether or not ELECTRON_DEBUG_NOTIFICATIONS env var is set Co-authored-by: Charles Kerr * chore: remove unused #include Co-authored-by: Charles Kerr --------- Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/browser/notifications/mac/cocoa_notification.mm | 4 ++-- .../notifications/mac/notification_center_delegate.mm | 2 +- .../notifications/mac/notification_presenter_mac.mm | 2 +- shell/browser/notifications/notification.cc | 4 ++++ shell/browser/notifications/notification.h | 2 ++ .../notifications/win/notification_presenter_win.cc | 7 +------ .../notifications/win/windows_toast_notification.cc | 3 +-- 7 files changed, 12 insertions(+), 12 deletions(-) diff --git a/shell/browser/notifications/mac/cocoa_notification.mm b/shell/browser/notifications/mac/cocoa_notification.mm index c073dd2e531..67ad3dbe39f 100644 --- a/shell/browser/notifications/mac/cocoa_notification.mm +++ b/shell/browser/notifications/mac/cocoa_notification.mm @@ -40,7 +40,7 @@ void CocoaNotification::Show(const NotificationOptions& options) { [notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)]; [notification_ setIdentifier:identifier]; - if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { + if (electron::debug_notifications) { LOG(INFO) << "Notification created (" << [identifier UTF8String] << ")"; } @@ -166,7 +166,7 @@ void CocoaNotification::NotificationDismissed() { } void CocoaNotification::LogAction(const char* action) { - if (getenv("ELECTRON_DEBUG_NOTIFICATIONS") && notification_) { + if (electron::debug_notifications && notification_) { NSString* identifier = [notification_ valueForKey:@"identifier"]; DCHECK(identifier); LOG(INFO) << "Notification " << action << " (" << [identifier UTF8String] diff --git a/shell/browser/notifications/mac/notification_center_delegate.mm b/shell/browser/notifications/mac/notification_center_delegate.mm index d9603bcd94c..29a87bd7a45 100644 --- a/shell/browser/notifications/mac/notification_center_delegate.mm +++ b/shell/browser/notifications/mac/notification_center_delegate.mm @@ -33,7 +33,7 @@ didActivateNotification:(NSUserNotification*)notif { auto* notification = presenter_->GetNotification(notif); - if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { + if (electron::debug_notifications) { LOG(INFO) << "Notification activated (" << [notif.identifier UTF8String] << ")"; } diff --git a/shell/browser/notifications/mac/notification_presenter_mac.mm b/shell/browser/notifications/mac/notification_presenter_mac.mm index 88877ae3fa8..adc4394fe22 100644 --- a/shell/browser/notifications/mac/notification_presenter_mac.mm +++ b/shell/browser/notifications/mac/notification_presenter_mac.mm @@ -25,7 +25,7 @@ CocoaNotification* NotificationPresenterMac::GetNotification( return native_notification; } - if (getenv("ELECTRON_DEBUG_NOTIFICATIONS")) { + if (electron::debug_notifications) { LOG(INFO) << "Could not find notification for " << [ns_notification.identifier UTF8String]; } diff --git a/shell/browser/notifications/notification.cc b/shell/browser/notifications/notification.cc index 9dae5fc56b5..64d04926ebf 100644 --- a/shell/browser/notifications/notification.cc +++ b/shell/browser/notifications/notification.cc @@ -4,11 +4,15 @@ #include "shell/browser/notifications/notification.h" +#include "base/environment.h" #include "shell/browser/notifications/notification_delegate.h" #include "shell/browser/notifications/notification_presenter.h" namespace electron { +const bool debug_notifications = + base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS"); + NotificationOptions::NotificationOptions() = default; NotificationOptions::~NotificationOptions() = default; diff --git a/shell/browser/notifications/notification.h b/shell/browser/notifications/notification.h index 391d0e26fd6..dd2881bd2b9 100644 --- a/shell/browser/notifications/notification.h +++ b/shell/browser/notifications/notification.h @@ -15,6 +15,8 @@ namespace electron { +extern const bool debug_notifications; + class NotificationDelegate; class NotificationPresenter; diff --git a/shell/browser/notifications/win/notification_presenter_win.cc b/shell/browser/notifications/win/notification_presenter_win.cc index 965ef45c9b2..843927b34e5 100644 --- a/shell/browser/notifications/win/notification_presenter_win.cc +++ b/shell/browser/notifications/win/notification_presenter_win.cc @@ -10,7 +10,6 @@ #include #include -#include "base/environment.h" #include "base/files/file_util.h" #include "base/hash/md5.h" #include "base/logging.h" @@ -29,10 +28,6 @@ namespace electron { namespace { -bool IsDebuggingNotifications() { - return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS"); -} - bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) { std::vector png_data; if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data)) @@ -53,7 +48,7 @@ std::unique_ptr NotificationPresenter::Create() { if (!presenter->Init()) return {}; - if (IsDebuggingNotifications()) + if (electron::debug_notifications) LOG(INFO) << "Successfully created Windows notifications presenter"; return presenter; diff --git a/shell/browser/notifications/win/windows_toast_notification.cc b/shell/browser/notifications/win/windows_toast_notification.cc index 1c4fa0bd47d..e35e83b12c9 100644 --- a/shell/browser/notifications/win/windows_toast_notification.cc +++ b/shell/browser/notifications/win/windows_toast_notification.cc @@ -13,7 +13,6 @@ #include #include -#include "base/environment.h" #include "base/hash/hash.h" #include "base/logging.h" #include "base/strings/strcat.h" @@ -68,7 +67,7 @@ namespace { constexpr wchar_t kGroup[] = L"Notifications"; void DebugLog(std::string_view log_msg) { - if (base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS")) + if (electron::debug_notifications) LOG(INFO) << log_msg; }