application_info_win.cc 2.4 KB

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