Add FilePath::FinalExtension() to avoid double extensions (.tar.gz) for file selector

Windows and OS X file selectors break badly on double-extensions. GTK and
Chrome OS handle it slightly better, but CrOS has a slight bug and GTK's
handling of file extensions is minimal, so not a whole lot changed. See
https://codereview.chromium.org/4883003/#msg14 for full analysis of
platform behavior.

There is some logic that does benefit from long extensions (renaming "foo.tar.gz"
to "foo (1).tar.gz" instead of "foo.tar (1).gz"), and some other callers store
state based on extension, so rather than changing FilePath::Extension, add a
new FilePath::FinalExtension and change SelectFileDialog callers to use it.

Also work around a problem in NSSavePanel when saving "foo.tar.gz" with
extensions hidden.

TEST=FilePath.Extension,
     FilePath.Extension2
     FilePath.RemoveExtension
     Enabling "Ask where to save each file before downloading"; saving a tar.gz doesn't result in weird confirmation prompt on OS X, with or without "Show all filename extensions" enabled in Finder.
BUG=83084

Review URL: https://codereview.chromium.org/4883003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@239505 0039d316-1c4b-4281-b951-d872f2087c98


CrOS-Libchrome-Original-Commit: 18cbba0aedd9d569b27be8adbd2836c839ae9897
diff --git a/base/files/file_path.cc b/base/files/file_path.cc
index 4cfa2e6..3ea5856 100644
--- a/base/files/file_path.cc
+++ b/base/files/file_path.cc
@@ -107,18 +107,21 @@
 
 // Find the position of the '.' that separates the extension from the rest
 // of the file name. The position is relative to BaseName(), not value().
-// This allows a second extension component of up to 4 characters when the
-// rightmost extension component is a common double extension (gz, bz2, Z).
-// For example, foo.tar.gz or foo.tar.Z would have extension components of
-// '.tar.gz' and '.tar.Z' respectively. Returns npos if it can't find an
-// extension.
-StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
+// Returns npos if it can't find an extension.
+StringType::size_type FinalExtensionSeparatorPosition(const StringType& path) {
   // Special case "." and ".."
   if (path == FilePath::kCurrentDirectory || path == FilePath::kParentDirectory)
     return StringType::npos;
 
-  const StringType::size_type last_dot =
-      path.rfind(FilePath::kExtensionSeparator);
+  return path.rfind(FilePath::kExtensionSeparator);
+}
+
+// Same as above, but allow a second extension component of up to 4
+// characters when the rightmost extension component is a common double
+// extension (gz, bz2, Z).  For example, foo.tar.gz or foo.tar.Z would have
+// extension components of '.tar.gz' and '.tar.Z' respectively.
+StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
+  const StringType::size_type last_dot = FinalExtensionSeparatorPosition(path);
 
   // No extension, or the extension is the whole filename.
   if (last_dot == StringType::npos || last_dot == 0U)
@@ -370,6 +373,15 @@
   return base.path_.substr(dot, StringType::npos);
 }
 
+StringType FilePath::FinalExtension() const {
+  FilePath base(BaseName());
+  const StringType::size_type dot = FinalExtensionSeparatorPosition(base.path_);
+  if (dot == StringType::npos)
+    return StringType();
+
+  return base.path_.substr(dot, StringType::npos);
+}
+
 FilePath FilePath::RemoveExtension() const {
   if (Extension().empty())
     return *this;
@@ -381,6 +393,17 @@
   return FilePath(path_.substr(0, dot));
 }
 
+FilePath FilePath::RemoveFinalExtension() const {
+  if (FinalExtension().empty())
+    return *this;
+
+  const StringType::size_type dot = FinalExtensionSeparatorPosition(path_);
+  if (dot == StringType::npos)
+    return *this;
+
+  return FilePath(path_.substr(0, dot));
+}
+
 FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
   if (suffix.empty())
     return FilePath(path_);