Browse Source

fix: Windows Store Notifications (#13258)

* :wrench: Basic 'are we in the desktop bridge' check

* :wrench: Store the result of the call

* :wrench: Create ToastNotifier correctly in UWP environment

* :wrench: Actually, improve this all around

* :heart: Implement feedback

* :wrench: Fix compiler issues

* :wrench: Mutex is banned, go to option 2

* :wrench: Use getProcAddress

* :memo: Make comment clearer

* :heart: Implement feedback
Felix Rieseberg 6 years ago
parent
commit
163e2d3527

+ 11 - 4
brightray/browser/win/windows_toast_notification.cc

@@ -58,12 +58,19 @@ bool WindowsToastNotification::Initialize() {
                                                        &toast_manager_)))
     return false;
 
-  ScopedHString app_id;
-  if (!GetAppUserModelID(&app_id))
-    return false;
+  if (IsRunningInDesktopBridge()) {
+    // Ironically, the Desktop Bridge / UWP environment
+    // requires us to not give Windows an appUserModelId.
+    return SUCCEEDED(
+      toast_manager_->CreateToastNotifier(&toast_notifier_));
+  } else {
+    ScopedHString app_id;
+    if (!GetAppUserModelID(&app_id))
+      return false;
 
-  return SUCCEEDED(
+    return SUCCEEDED(
       toast_manager_->CreateToastNotifierWithId(app_id, &toast_notifier_));
+  }
 }
 
 WindowsToastNotification::WindowsToastNotification(

+ 1 - 0
brightray/common/application_info.h

@@ -23,6 +23,7 @@ std::string GetApplicationVersion();
 PCWSTR GetRawAppUserModelID();
 bool GetAppUserModelID(ScopedHString* app_id);
 void SetAppUserModelID(const base::string16& name);
+bool IsRunningInDesktopBridge();
 #endif
 
 }  // namespace brightray

+ 44 - 0
brightray/common/application_info_win.cc

@@ -2,7 +2,9 @@
 
 #include <windows.h>  // windows.h must be included first
 
+#include <appmodel.h>
 #include <shlobj.h>
+#include <VersionHelpers.h>
 
 #include <memory>
 
@@ -62,4 +64,46 @@ bool GetAppUserModelID(ScopedHString* app_id) {
   return app_id->success();
 }
 
+bool IsRunningInDesktopBridgeImpl() {
+  if (IsWindows8OrGreater()) {
+    // GetPackageFamilyName is not available on Windows 7
+    using GetPackageFamilyNameFuncPtr = decltype(&GetPackageFamilyName);
+
+    static bool initialize_get_package_family_name = true;
+    static GetPackageFamilyNameFuncPtr get_package_family_namePtr = NULL;
+
+    if (initialize_get_package_family_name) {
+      initialize_get_package_family_name = false;
+      HMODULE kernel32_base = GetModuleHandle(L"Kernel32.dll");
+      if (!kernel32_base) {
+        NOTREACHED() << " " << __FUNCTION__ << "(): Can't open Kernel32.dll";
+        return false;
+      }
+
+      get_package_family_namePtr =
+          reinterpret_cast<GetPackageFamilyNameFuncPtr>(
+              GetProcAddress(kernel32_base, "GetPackageFamilyName"));
+
+      if (!get_package_family_namePtr) {
+        return false;
+      }
+    }
+
+    UINT32 length;
+    wchar_t packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH + 1];
+    HANDLE proc = GetCurrentProcess();
+    LONG result =
+      (*get_package_family_namePtr)(proc, &length, packageFamilyName);
+
+    return result == ERROR_SUCCESS;
+  } else {
+    return false;
+  }
+}
+
+bool IsRunningInDesktopBridge() {
+  static bool result = IsRunningInDesktopBridgeImpl();
+  return result;
+}
+
 }  // namespace brightray