Browse Source

chore: use base::Environment in Linux MoveItemToTrash() (#19367)

* chore: use base::Environment in MoveItemToTrash() Linux impl

* chore: remove unnecessary local function XDGUtil()

* chore: tweak code comment

* fix: remove errant reference
Charles Kerr 5 years ago
parent
commit
38507974d6
1 changed files with 19 additions and 39 deletions
  1. 19 39
      shell/common/platform_util_linux.cc

+ 19 - 39
shell/common/platform_util_linux.cc

@@ -16,11 +16,10 @@
 #include "url/gurl.h"
 
 #define ELECTRON_TRASH "ELECTRON_TRASH"
-#define ELECTRON_DEFAULT_TRASH "gio"
 
 namespace {
 
-bool XDGUtilV(const std::vector<std::string>& argv, const bool wait_for_exit) {
+bool XDGUtil(const std::vector<std::string>& argv, const bool wait_for_exit) {
   base::LaunchOptions options;
   options.allow_new_privs = true;
   // xdg-open can fall back on mailcap which eventually might plumb through
@@ -44,22 +43,12 @@ bool XDGUtilV(const std::vector<std::string>& argv, const bool wait_for_exit) {
   return true;
 }
 
-bool XDGUtil(const std::string& util,
-             const std::string& arg,
-             const bool wait_for_exit) {
-  std::vector<std::string> argv;
-  argv.push_back(util);
-  argv.push_back(arg);
-
-  return XDGUtilV(argv, wait_for_exit);
-}
-
 bool XDGOpen(const std::string& path, const bool wait_for_exit) {
-  return XDGUtil("xdg-open", path, wait_for_exit);
+  return XDGUtil({"xdg-open", path}, wait_for_exit);
 }
 
 bool XDGEmail(const std::string& email, const bool wait_for_exit) {
-  return XDGUtil("xdg-email", email, wait_for_exit);
+  return XDGUtil({"xdg-email", email}, wait_for_exit);
 }
 
 }  // namespace
@@ -94,44 +83,35 @@ void OpenExternal(const GURL& url,
 }
 
 bool MoveItemToTrash(const base::FilePath& full_path) {
+  std::unique_ptr<base::Environment> env(base::Environment::Create());
+
+  // find the trash method
   std::string trash;
-  if (getenv(ELECTRON_TRASH) != NULL) {
-    trash = getenv(ELECTRON_TRASH);
-  } else {
+  if (!env->GetVar(ELECTRON_TRASH, &trash)) {
     // Determine desktop environment and set accordingly.
-    std::unique_ptr<base::Environment> env(base::Environment::Create());
-    base::nix::DesktopEnvironment desktop_env(
-        base::nix::GetDesktopEnvironment(env.get()));
+    const auto desktop_env(base::nix::GetDesktopEnvironment(env.get()));
     if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE4 ||
         desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE5) {
       trash = "kioclient5";
     } else if (desktop_env == base::nix::DESKTOP_ENVIRONMENT_KDE3) {
       trash = "kioclient";
-    } else {
-      trash = ELECTRON_DEFAULT_TRASH;
     }
   }
 
+  // build the invocation
   std::vector<std::string> argv;
-
-  if (trash.compare("kioclient5") == 0 || trash.compare("kioclient") == 0) {
-    argv.push_back(trash);
-    argv.push_back("move");
-    argv.push_back(full_path.value());
-    argv.push_back("trash:/");
-  } else if (trash.compare("trash-cli") == 0) {
-    argv.push_back("trash-put");
-    argv.push_back(full_path.value());
-  } else if (trash.compare("gvfs-trash") == 0) {
-    // retain support for deprecated gvfs-trash
-    argv.push_back("gvfs-trash");
-    argv.push_back(full_path.value());
+  const auto& filename = full_path.value();
+  if (trash == "kioclient5" || trash == "kioclient") {
+    argv = {trash, "move", filename, "trash:/"};
+  } else if (trash == "trash-cli") {
+    argv = {"trash-put", filename};
+  } else if (trash == "gvfs-trash") {
+    argv = {"gvfs-trash", filename};  // deprecated, but still exists
   } else {
-    argv.push_back(ELECTRON_DEFAULT_TRASH);
-    argv.push_back("trash");
-    argv.push_back(full_path.value());
+    argv = {"gio", "trash", filename};
   }
-  return XDGUtilV(argv, true);
+
+  return XDGUtil(argv, true);
 }
 
 void Beep() {