application_info_linux.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 <gio/gdesktopappinfo.h>
  6. #include <gio/gio.h>
  7. #include <string>
  8. #include "base/environment.h"
  9. #include "base/logging.h"
  10. #include "electron/electron_version.h"
  11. #include "shell/common/platform_util.h"
  12. #include "ui/gtk/gtk_util.h" // nogncheck
  13. namespace {
  14. GDesktopAppInfo* get_desktop_app_info() {
  15. GDesktopAppInfo* ret = nullptr;
  16. std::string desktop_id;
  17. if (platform_util::GetDesktopName(&desktop_id))
  18. ret = g_desktop_app_info_new(desktop_id.c_str());
  19. return ret;
  20. }
  21. } // namespace
  22. namespace electron {
  23. std::string GetApplicationName() {
  24. // attempt #1: the string set in app.setName()
  25. std::string ret = OverriddenApplicationName();
  26. // attempt #2: the 'Name' entry from .desktop file's [Desktop] section
  27. if (ret.empty()) {
  28. GDesktopAppInfo* info = get_desktop_app_info();
  29. if (info != nullptr) {
  30. char* str = g_desktop_app_info_get_string(info, "Name");
  31. g_clear_object(&info);
  32. if (str != nullptr)
  33. ret = str;
  34. g_clear_pointer(&str, g_free);
  35. }
  36. }
  37. // attempt #3: Electron's name
  38. if (ret.empty()) {
  39. ret = ELECTRON_PRODUCT_NAME;
  40. }
  41. return ret;
  42. }
  43. std::string GetApplicationVersion() {
  44. std::string ret;
  45. // ensure ELECTRON_PRODUCT_NAME and GetApplicationVersion match up
  46. if (GetApplicationName() == ELECTRON_PRODUCT_NAME)
  47. ret = ELECTRON_VERSION_STRING;
  48. // try to use the string set in app.setVersion()
  49. if (ret.empty())
  50. ret = OverriddenApplicationVersion();
  51. // no known version number; return some safe fallback
  52. if (ret.empty()) {
  53. LOG(WARNING) << "No version found. Was app.setVersion() called?";
  54. ret = "0.0";
  55. }
  56. return ret;
  57. }
  58. } // namespace electron