application_info_win.cc 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. UINT32 length = PACKAGE_FAMILY_NAME_MAX_LENGTH;
  59. wchar_t packageFamilyName[PACKAGE_FAMILY_NAME_MAX_LENGTH];
  60. HANDLE proc = GetCurrentProcess();
  61. LONG result = GetPackageFamilyName(proc, &length, packageFamilyName);
  62. return result == ERROR_SUCCESS;
  63. }
  64. bool IsRunningInDesktopBridge() {
  65. static bool result = IsRunningInDesktopBridgeImpl();
  66. return result;
  67. }
  68. } // namespace electron