application_info_win.cc 3.2 KB

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