Browse Source

perf: cache whether or not ELECTRON_DEBUG_NOTIFICATIONS env var is set (#45161)

* perf: cache whether or not ELECTRON_DEBUG_NOTIFICATIONS env var is set

Co-authored-by: Charles Kerr <[email protected]>

* chore: remove unused #include

Co-authored-by: Charles Kerr <[email protected]>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <[email protected]>
trop[bot] 3 months ago
parent
commit
48aba26aac

+ 2 - 2
shell/browser/notifications/mac/cocoa_notification.mm

@@ -44,7 +44,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] << ")";
   }
 
@@ -170,7 +170,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]

+ 1 - 1
shell/browser/notifications/mac/notification_center_delegate.mm

@@ -38,7 +38,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]
               << ")";
   }

+ 1 - 1
shell/browser/notifications/mac/notification_presenter_mac.mm

@@ -30,7 +30,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];
   }

+ 4 - 0
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;
 

+ 2 - 0
shell/browser/notifications/notification.h

@@ -15,6 +15,8 @@
 
 namespace electron {
 
+extern const bool debug_notifications;
+
 class NotificationDelegate;
 class NotificationPresenter;
 

+ 1 - 6
shell/browser/notifications/win/notification_presenter_win.cc

@@ -10,7 +10,6 @@
 #include <string>
 #include <vector>
 
-#include "base/environment.h"
 #include "base/files/file_util.h"
 #include "base/hash/md5.h"
 #include "base/logging.h"
@@ -28,10 +27,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<unsigned char> png_data;
   if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
@@ -50,7 +45,7 @@ std::unique_ptr<NotificationPresenter> NotificationPresenter::Create() {
   if (!presenter->Init())
     return {};
 
-  if (IsDebuggingNotifications())
+  if (electron::debug_notifications)
     LOG(INFO) << "Successfully created Windows notifications presenter";
 
   return presenter;

+ 1 - 2
shell/browser/notifications/win/windows_toast_notification.cc

@@ -13,7 +13,6 @@
 #include <shlobj.h>
 #include <wrl\wrappers\corewrappers.h>
 
-#include "base/environment.h"
 #include "base/hash/hash.h"
 #include "base/logging.h"
 #include "base/strings/strcat.h"
@@ -67,7 +66,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;
 }