application_info_win.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/strings/string_util.h"
  12. #include "base/strings/string_util_win.h"
  13. #include "base/strings/utf_string_conversions.h"
  14. #include "shell/browser/win/scoped_hstring.h"
  15. namespace electron {
  16. namespace {
  17. std::wstring 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 std::wstring& 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. std::wstring generated_app_id = base::ReplaceStringPlaceholders(
  44. kAppUserModelIDFormat, {base::UTF8ToWide(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. UINT32 length = PACKAGE_FAMILY_NAME_MAX_LENGTH;
  57. wchar_t packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH];
  58. HANDLE proc = GetCurrentProcess();
  59. LONG result = GetPackageFamilyName(proc, &length, packageFamilyName);
  60. return result == ERROR_SUCCESS;
  61. }
  62. bool IsRunningInDesktopBridge() {
  63. static bool result = IsRunningInDesktopBridgeImpl();
  64. return result;
  65. }
  66. } // namespace electron