application_info_linux.cc 1.8 KB

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