application_info_win.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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/stringprintf.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "shell/browser/win/scoped_hstring.h"
  16. namespace electron {
  17. namespace {
  18. base::string16 g_app_user_model_id;
  19. }
  20. const wchar_t kAppUserModelIDFormat[] = L"electron.app.$1";
  21. std::string GetApplicationName() {
  22. auto* module = GetModuleHandle(nullptr);
  23. std::unique_ptr<FileVersionInfo> info(
  24. FileVersionInfo::CreateFileVersionInfoForModule(module));
  25. return base::UTF16ToUTF8(info->product_name());
  26. }
  27. std::string GetApplicationVersion() {
  28. auto* module = GetModuleHandle(nullptr);
  29. std::unique_ptr<FileVersionInfo> info(
  30. FileVersionInfo::CreateFileVersionInfoForModule(module));
  31. return base::UTF16ToUTF8(info->product_version());
  32. }
  33. void SetAppUserModelID(const base::string16& name) {
  34. g_app_user_model_id = name;
  35. SetCurrentProcessExplicitAppUserModelID(g_app_user_model_id.c_str());
  36. }
  37. PCWSTR GetRawAppUserModelID() {
  38. if (g_app_user_model_id.empty()) {
  39. PWSTR current_app_id;
  40. if (SUCCEEDED(GetCurrentProcessExplicitAppUserModelID(&current_app_id))) {
  41. g_app_user_model_id = current_app_id;
  42. } else {
  43. std::string name = GetApplicationName();
  44. base::string16 generated_app_id = base::ReplaceStringPlaceholders(
  45. kAppUserModelIDFormat, base::UTF8ToUTF16(name), nullptr);
  46. SetAppUserModelID(generated_app_id);
  47. }
  48. CoTaskMemFree(current_app_id);
  49. }
  50. return g_app_user_model_id.c_str();
  51. }
  52. bool GetAppUserModelID(ScopedHString* app_id) {
  53. app_id->Reset(GetRawAppUserModelID());
  54. return app_id->success();
  55. }
  56. bool IsRunningInDesktopBridgeImpl() {
  57. if (IsWindows8OrGreater()) {
  58. // GetPackageFamilyName is not available on Windows 7
  59. using GetPackageFamilyNameFuncPtr = decltype(&GetPackageFamilyName);
  60. static bool initialize_get_package_family_name = true;
  61. static GetPackageFamilyNameFuncPtr get_package_family_namePtr = NULL;
  62. if (initialize_get_package_family_name) {
  63. initialize_get_package_family_name = false;
  64. HMODULE kernel32_base = GetModuleHandle(L"Kernel32.dll");
  65. if (!kernel32_base) {
  66. NOTREACHED() << " " << __FUNCTION__ << "(): Can't open Kernel32.dll";
  67. return false;
  68. }
  69. get_package_family_namePtr =
  70. reinterpret_cast<GetPackageFamilyNameFuncPtr>(
  71. GetProcAddress(kernel32_base, "GetPackageFamilyName"));
  72. if (!get_package_family_namePtr) {
  73. return false;
  74. }
  75. }
  76. UINT32 length = PACKAGE_FAMILY_NAME_MAX_LENGTH;
  77. wchar_t packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH];
  78. HANDLE proc = GetCurrentProcess();
  79. LONG result =
  80. (*get_package_family_namePtr)(proc, &length, packageFamilyName);
  81. return result == ERROR_SUCCESS;
  82. } else {
  83. return false;
  84. }
  85. }
  86. bool IsRunningInDesktopBridge() {
  87. static bool result = IsRunningInDesktopBridgeImpl();
  88. return result;
  89. }
  90. } // namespace electron