From 08383a69cea60db3d87fbe9842d21ffe0c4ef753 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Fri, 24 Jul 2015 03:10:03 -0700 Subject: [PATCH 1/4] Propagate User App Model ID to Tray Icon This PR prevents dozens of items showing up in the notification area preferences when using Squirrel for Windows, by ensuring that notification tray items are tied to the User App Model ID. --- atom/browser/ui/win/notify_icon.cc | 16 ++++++++++++++++ atom/browser/ui/win/notify_icon.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/atom/browser/ui/win/notify_icon.cc b/atom/browser/ui/win/notify_icon.cc index bc9ff46f939..6f75bb311da 100644 --- a/atom/browser/ui/win/notify_icon.cc +++ b/atom/browser/ui/win/notify_icon.cc @@ -4,7 +4,10 @@ #include "atom/browser/ui/win/notify_icon.h" +#include + #include "atom/browser/ui/win/notify_icon_host.h" +#include "base/md5.h" #include "base/strings/string_number_conversions.h" #include "base/strings/utf_string_conversions.h" #include "base/win/windows_version.h" @@ -26,6 +29,18 @@ NotifyIcon::NotifyIcon(NotifyIconHost* host, window_(window), message_id_(message), menu_model_(NULL) { + + // NB: If we have an App Model ID, we should propagate that to the tray. + // Doing this prevents duplicate items from showing up in the notification + // preferences (i.e. "Always Show / Show notifications only / etc") + PWSTR explicit_app_id; + + if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&explicit_app_id))) { + // GUIDs and MD5 hashes are the same length. So convenient! + MD5Sum(explicit_app_id, sizeof(wchar_t) * wcslen(explicit_app_id), (MD5Digest*)&tray_app_id_hash_); + CoTaskMemFree(explicit_app_id); + } + NOTIFYICONDATA icon_data; InitIconData(&icon_data); icon_data.uFlags = NIF_MESSAGE; @@ -161,6 +176,7 @@ void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) { icon_data->cbSize = sizeof(NOTIFYICONDATA); icon_data->hWnd = window_; icon_data->uID = icon_id_; + memcpy(icon_data->guidItem, tray_app_id_hash_, sizeof(GUID)); } } // namespace atom diff --git a/atom/browser/ui/win/notify_icon.h b/atom/browser/ui/win/notify_icon.h index 8e00f126792..56b4b4f09be 100644 --- a/atom/browser/ui/win/notify_icon.h +++ b/atom/browser/ui/win/notify_icon.h @@ -75,6 +75,9 @@ class NotifyIcon : public TrayIcon { // The context menu. ui::SimpleMenuModel* menu_model_; + + // A hash of the app model ID + GUID tray_app_id_hash_; DISALLOW_COPY_AND_ASSIGN(NotifyIcon); }; From 75b08f510ee021b3a984095dce873769ae127fb8 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Fri, 24 Jul 2015 03:27:15 -0700 Subject: [PATCH 2/4] Fix up namespaces --- atom/browser/ui/win/notify_icon.cc | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/atom/browser/ui/win/notify_icon.cc b/atom/browser/ui/win/notify_icon.cc index 6f75bb311da..e9b153b49ab 100644 --- a/atom/browser/ui/win/notify_icon.cc +++ b/atom/browser/ui/win/notify_icon.cc @@ -29,18 +29,21 @@ NotifyIcon::NotifyIcon(NotifyIconHost* host, window_(window), message_id_(message), menu_model_(NULL) { - - // NB: If we have an App Model ID, we should propagate that to the tray. + + // NB: If we have an App Model ID, we should propagate that to the tray. // Doing this prevents duplicate items from showing up in the notification // preferences (i.e. "Always Show / Show notifications only / etc") PWSTR explicit_app_id; - + if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&explicit_app_id))) { // GUIDs and MD5 hashes are the same length. So convenient! - MD5Sum(explicit_app_id, sizeof(wchar_t) * wcslen(explicit_app_id), (MD5Digest*)&tray_app_id_hash_); + base::MD5Sum(explicit_app_id, + sizeof(wchar_t) * wcslen(explicit_app_id), + (base::MD5Digest*)&tray_app_id_hash_); + CoTaskMemFree(explicit_app_id); } - + NOTIFYICONDATA icon_data; InitIconData(&icon_data); icon_data.uFlags = NIF_MESSAGE; @@ -176,7 +179,7 @@ void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) { icon_data->cbSize = sizeof(NOTIFYICONDATA); icon_data->hWnd = window_; icon_data->uID = icon_id_; - memcpy(icon_data->guidItem, tray_app_id_hash_, sizeof(GUID)); + memcpy((void*)&icon_data->guidItem, &tray_app_id_hash_, sizeof(GUID)); } } // namespace atom From 70feb08f8495eabbb44c3fae3064aaa5c836638e Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Fri, 24 Jul 2015 03:30:23 -0700 Subject: [PATCH 3/4] Fix linting issues --- atom/browser/ui/win/notify_icon.cc | 7 +++++-- atom/browser/ui/win/notify_icon.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/atom/browser/ui/win/notify_icon.cc b/atom/browser/ui/win/notify_icon.cc index e9b153b49ab..787982d04db 100644 --- a/atom/browser/ui/win/notify_icon.cc +++ b/atom/browser/ui/win/notify_icon.cc @@ -40,7 +40,7 @@ NotifyIcon::NotifyIcon(NotifyIconHost* host, base::MD5Sum(explicit_app_id, sizeof(wchar_t) * wcslen(explicit_app_id), (base::MD5Digest*)&tray_app_id_hash_); - + CoTaskMemFree(explicit_app_id); } @@ -179,7 +179,10 @@ void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) { icon_data->cbSize = sizeof(NOTIFYICONDATA); icon_data->hWnd = window_; icon_data->uID = icon_id_; - memcpy((void*)&icon_data->guidItem, &tray_app_id_hash_, sizeof(GUID)); + + memcpy(reinterpret_cast(&icon_data->guidItem), + &tray_app_id_hash_, + sizeof(GUID)); } } // namespace atom diff --git a/atom/browser/ui/win/notify_icon.h b/atom/browser/ui/win/notify_icon.h index 56b4b4f09be..a52388d5629 100644 --- a/atom/browser/ui/win/notify_icon.h +++ b/atom/browser/ui/win/notify_icon.h @@ -75,7 +75,7 @@ class NotifyIcon : public TrayIcon { // The context menu. ui::SimpleMenuModel* menu_model_; - + // A hash of the app model ID GUID tray_app_id_hash_; From 5e61974c24c90ebc81562a2f1259ae018db0aab0 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Fri, 24 Jul 2015 08:02:14 -0700 Subject: [PATCH 4/4] Set NIF_GUID if we have a GUID --- atom/browser/ui/win/notify_icon.cc | 20 ++++++++++++-------- atom/browser/ui/win/notify_icon.h | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/atom/browser/ui/win/notify_icon.cc b/atom/browser/ui/win/notify_icon.cc index 787982d04db..6cf207d7b8e 100644 --- a/atom/browser/ui/win/notify_icon.cc +++ b/atom/browser/ui/win/notify_icon.cc @@ -40,13 +40,14 @@ NotifyIcon::NotifyIcon(NotifyIconHost* host, base::MD5Sum(explicit_app_id, sizeof(wchar_t) * wcslen(explicit_app_id), (base::MD5Digest*)&tray_app_id_hash_); + has_tray_app_id_hash_ = true; CoTaskMemFree(explicit_app_id); } NOTIFYICONDATA icon_data; InitIconData(&icon_data); - icon_data.uFlags = NIF_MESSAGE; + icon_data.uFlags |= NIF_MESSAGE; icon_data.uCallbackMessage = message_id_; BOOL result = Shell_NotifyIcon(NIM_ADD, &icon_data); // This can happen if the explorer process isn't running when we try to @@ -89,7 +90,7 @@ void NotifyIcon::ResetIcon() { // Delete any previously existing icon. Shell_NotifyIcon(NIM_DELETE, &icon_data); InitIconData(&icon_data); - icon_data.uFlags = NIF_MESSAGE; + icon_data.uFlags |= NIF_MESSAGE; icon_data.uCallbackMessage = message_id_; icon_data.hIcon = icon_.Get(); // If we have an image, then set the NIF_ICON flag, which tells @@ -106,7 +107,7 @@ void NotifyIcon::SetImage(const gfx::Image& image) { // Create the icon. NOTIFYICONDATA icon_data; InitIconData(&icon_data); - icon_data.uFlags = NIF_ICON; + icon_data.uFlags |= NIF_ICON; icon_.Set(IconUtil::CreateHICONFromSkBitmap(image.AsBitmap())); icon_data.hIcon = icon_.Get(); BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data); @@ -123,7 +124,7 @@ void NotifyIcon::SetToolTip(const std::string& tool_tip) { // Create the icon. NOTIFYICONDATA icon_data; InitIconData(&icon_data); - icon_data.uFlags = NIF_TIP; + icon_data.uFlags |= NIF_TIP; wcscpy_s(icon_data.szTip, base::UTF8ToUTF16(tool_tip).c_str()); BOOL result = Shell_NotifyIcon(NIM_MODIFY, &icon_data); if (!result) @@ -135,7 +136,7 @@ void NotifyIcon::DisplayBalloon(const gfx::Image& icon, const base::string16& contents) { NOTIFYICONDATA icon_data; InitIconData(&icon_data); - icon_data.uFlags = NIF_INFO; + icon_data.uFlags |= NIF_INFO; icon_data.dwInfoFlags = NIIF_INFO; wcscpy_s(icon_data.szInfoTitle, title.c_str()); wcscpy_s(icon_data.szInfo, contents.c_str()); @@ -180,9 +181,12 @@ void NotifyIcon::InitIconData(NOTIFYICONDATA* icon_data) { icon_data->hWnd = window_; icon_data->uID = icon_id_; - memcpy(reinterpret_cast(&icon_data->guidItem), - &tray_app_id_hash_, - sizeof(GUID)); + if (has_tray_app_id_hash_) { + icon_data->uFlags |= NIF_GUID; + memcpy(reinterpret_cast(&icon_data->guidItem), + &tray_app_id_hash_, + sizeof(GUID)); + } } } // namespace atom diff --git a/atom/browser/ui/win/notify_icon.h b/atom/browser/ui/win/notify_icon.h index a52388d5629..25ba8c8dc43 100644 --- a/atom/browser/ui/win/notify_icon.h +++ b/atom/browser/ui/win/notify_icon.h @@ -78,6 +78,7 @@ class NotifyIcon : public TrayIcon { // A hash of the app model ID GUID tray_app_id_hash_; + bool has_tray_app_id_hash_; DISALLOW_COPY_AND_ASSIGN(NotifyIcon); };