application_info_win.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/application_info.h"
  5. #include <windows.h> // windows.h must be included first
  6. #include <VersionHelpers.h>
  7. #include <appmodel.h>
  8. #include <shlobj.h>
  9. #include <memory>
  10. #include "base/file_version_info.h"
  11. #include "base/notreached.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/strings/string_util_win.h"
  14. #include "base/strings/stringprintf.h"
  15. #include "base/strings/utf_string_conversions.h"
  16. #include "shell/browser/win/scoped_hstring.h"
  17. namespace electron {
  18. namespace {
  19. std::wstring g_app_user_model_id;
  20. }
  21. const wchar_t kAppUserModelIDFormat[] = L"electron.app.$1";
  22. std::string GetApplicationName() {
  23. auto* module = GetModuleHandle(nullptr);
  24. std::unique_ptr<FileVersionInfo> info(
  25. FileVersionInfo::CreateFileVersionInfoForModule(module));
  26. return base::UTF16ToUTF8(info->product_name());
  27. }
  28. std::string GetApplicationVersion() {
  29. auto* module = GetModuleHandle(nullptr);
  30. std::unique_ptr<FileVersionInfo> info(
  31. FileVersionInfo::CreateFileVersionInfoForModule(module));
  32. return base::UTF16ToUTF8(info->product_version());
  33. }
  34. void SetAppUserModelID(const std::wstring& name) {
  35. g_app_user_model_id = name;
  36. SetCurrentProcessExplicitAppUserModelID(g_app_user_model_id.c_str());
  37. }
  38. PCWSTR GetRawAppUserModelID() {
  39. if (g_app_user_model_id.empty()) {
  40. PWSTR current_app_id;
  41. if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&current_app_id))) {
  42. g_app_user_model_id = current_app_id;
  43. } else {
  44. std::string name = GetApplicationName();
  45. std::wstring generated_app_id = base::ReplaceStringPlaceholders(
  46. kAppUserModelIDFormat, {base::UTF8ToWide(name)}, nullptr);
  47. SetAppUserModelID(generated_app_id);
  48. }
  49. CoTaskMemFree(current_app_id);
  50. }
  51. return g_app_user_model_id.c_str();
  52. }
  53. bool GetAppUserModelID(ScopedHString* app_id) {
  54. app_id->Reset(GetRawAppUserModelID());
  55. return app_id->success();
  56. }
  57. bool IsRunningInDesktopBridgeImpl() {
  58. if (IsWindows8OrGreater()) {
  59. // GetPackageFamilyName is not available on Windows 7
  60. using GetPackageFamilyNameFuncPtr = decltype(&GetPackageFamilyName);
  61. static bool initialize_get_package_family_name = true;
  62. static GetPackageFamilyNameFuncPtr get_package_family_namePtr = NULL;
  63. if (initialize_get_package_family_name) {
  64. initialize_get_package_family_name = false;
  65. HMODULE kernel32_base = GetModuleHandle(L"Kernel32.dll");
  66. if (!kernel32_base) {
  67. NOTREACHED() << " " << __FUNCTION__ << "(): Can't open Kernel32.dll";
  68. return false;
  69. }
  70. get_package_family_namePtr =
  71. reinterpret_cast<GetPackageFamilyNameFuncPtr>(
  72. GetProcAddress(kernel32_base, "GetPackageFamilyName"));
  73. if (!get_package_family_namePtr) {
  74. return false;
  75. }
  76. }
  77. UINT32 length = PACKAGE_FAMILY_NAME_MAX_LENGTH;
  78. wchar_t packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH];
  79. HANDLE proc = GetCurrentProcess();
  80. LONG result =
  81. (*get_package_family_namePtr)(proc, &length, packageFamilyName);
  82. return result == ERROR_SUCCESS;
  83. } else {
  84. return false;
  85. }
  86. }
  87. bool IsRunningInDesktopBridge() {
  88. static bool result = IsRunningInDesktopBridgeImpl();
  89. return result;
  90. }
  91. } // namespace electron