application_info_linux.cc 1.8 KB

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