application_info_linux.cc 1.7 KB

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