Commit 22a0e78c authored by Shuhei Takahashi's avatar Shuhei Takahashi Committed by Commit Bot

arc: Move GetFileNameForDocument to public utilities.

This function will be used in Recent. As a bonus, unit tests are added.

Bug: 742722
Test: unit_tests
Change-Id: I0c5d6271e7845fb0d7f86047414f652e049348f3
Reviewed-on: https://chromium-review.googlesource.com/588811
Commit-Queue: Shuhei Takahashi <nya@chromium.org>
Reviewed-by: default avatarHidehiko Abe <hidehiko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#491972}
parent 92b36db1
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h" #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_root.h"
#include <algorithm>
#include <utility> #include <utility>
#include "base/bind.h" #include "base/bind.h"
...@@ -11,8 +12,6 @@ ...@@ -11,8 +12,6 @@
#include "base/files/file.h" #include "base/files/file.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h" #include "chrome/browser/chromeos/arc/fileapi/arc_documents_provider_util.h"
...@@ -24,46 +23,6 @@ using EntryList = storage::AsyncFileUtil::EntryList; ...@@ -24,46 +23,6 @@ using EntryList = storage::AsyncFileUtil::EntryList;
namespace arc { namespace arc {
namespace {
// Computes a file name for a document.
// TODO(crbug.com/675868): Consolidate with the similar logic for Drive.
base::FilePath::StringType GetFileNameForDocument(
const mojom::DocumentPtr& document) {
base::FilePath::StringType filename = document->display_name;
// Replace path separators appearing in the file name.
// Chrome OS is POSIX and kSeparators is "/".
base::ReplaceChars(filename, base::FilePath::kSeparators, "_", &filename);
// Do not allow an empty file name and all-dots file names.
if (filename.empty() ||
filename.find_first_not_of('.', 0) == std::string::npos) {
filename = "_";
}
// Since Chrome detects MIME type from file name extensions, we need to change
// the file name extension of the document if it does not match with its MIME
// type.
// For example, Audio Media Provider presents a music file with its title as
// the file name.
base::FilePath::StringType extension =
base::ToLowerASCII(base::FilePath(filename).Extension());
if (!extension.empty())
extension = extension.substr(1); // Strip the leading dot.
std::vector<base::FilePath::StringType> possible_extensions =
GetExtensionsForArcMimeType(document->mime_type);
if (!possible_extensions.empty() &&
!base::ContainsValue(possible_extensions, extension)) {
filename =
base::FilePath(filename).AddExtension(possible_extensions[0]).value();
}
return filename;
}
} // namespace
// static // static
const int64_t ArcDocumentsProviderRoot::kInvalidWatcherId = -1; const int64_t ArcDocumentsProviderRoot::kInvalidWatcherId = -1;
// static // static
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include "base/stl_util.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
...@@ -216,4 +217,39 @@ std::vector<base::FilePath::StringType> GetExtensionsForArcMimeType( ...@@ -216,4 +217,39 @@ std::vector<base::FilePath::StringType> GetExtensionsForArcMimeType(
return std::vector<base::FilePath::StringType>(); return std::vector<base::FilePath::StringType>();
} }
// TODO(crbug.com/675868): Consolidate with the similar logic for Drive.
base::FilePath::StringType GetFileNameForDocument(
const mojom::DocumentPtr& document) {
base::FilePath::StringType filename = document->display_name;
// Replace path separators appearing in the file name.
// Chrome OS is POSIX and kSeparators is "/".
base::ReplaceChars(filename, base::FilePath::kSeparators, "_", &filename);
// Do not allow an empty file name and all-dots file names.
if (filename.empty() ||
filename.find_first_not_of('.', 0) == std::string::npos) {
filename = "_";
}
// Since Chrome detects MIME type from file name extensions, we need to change
// the file name extension of the document if it does not match with its MIME
// type.
// For example, Audio Media Provider presents a music file with its title as
// the file name.
base::FilePath::StringType extension =
base::ToLowerASCII(base::FilePath(filename).Extension());
if (!extension.empty())
extension = extension.substr(1); // Strip the leading dot.
std::vector<base::FilePath::StringType> possible_extensions =
GetExtensionsForArcMimeType(document->mime_type);
if (!possible_extensions.empty() &&
!base::ContainsValue(possible_extensions, extension)) {
filename =
base::FilePath(filename).AddExtension(possible_extensions[0]).value();
}
return filename;
}
} // namespace arc } // namespace arc
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include <vector> #include <vector>
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "components/arc/common/file_system.mojom.h"
class GURL; class GURL;
...@@ -70,6 +71,10 @@ GURL BuildDocumentUrl(const std::string& authority, ...@@ -70,6 +71,10 @@ GURL BuildDocumentUrl(const std::string& authority,
std::vector<base::FilePath::StringType> GetExtensionsForArcMimeType( std::vector<base::FilePath::StringType> GetExtensionsForArcMimeType(
const std::string& mime_type); const std::string& mime_type);
// Computes a file name for a document.
base::FilePath::StringType GetFileNameForDocument(
const mojom::DocumentPtr& document);
} // namespace arc } // namespace arc
#endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_DOCUMENTS_PROVIDER_UTIL_H_ #endif // CHROME_BROWSER_CHROMEOS_ARC_FILEAPI_ARC_DOCUMENTS_PROVIDER_UTIL_H_
...@@ -12,6 +12,14 @@ namespace arc { ...@@ -12,6 +12,14 @@ namespace arc {
namespace { namespace {
mojom::DocumentPtr MakeDocument(const std::string& display_name,
const std::string& mime_type) {
mojom::DocumentPtr document = mojom::Document::New();
document->display_name = display_name;
document->mime_type = mime_type;
return document;
}
TEST(ArcDocumentsProviderUtilTest, EscapePathComponent) { TEST(ArcDocumentsProviderUtilTest, EscapePathComponent) {
EXPECT_EQ("", EscapePathComponent("")); EXPECT_EQ("", EscapePathComponent(""));
EXPECT_EQ("%2E", EscapePathComponent(".")); EXPECT_EQ("%2E", EscapePathComponent("."));
...@@ -221,6 +229,22 @@ TEST(ArcDocumentsProviderUtilTest, GetExtensionsForArcMimeType) { ...@@ -221,6 +229,22 @@ TEST(ArcDocumentsProviderUtilTest, GetExtensionsForArcMimeType) {
EXPECT_EQ(0u, GetExtensionsForArcMimeType("application/octet-stream").size()); EXPECT_EQ(0u, GetExtensionsForArcMimeType("application/octet-stream").size());
} }
TEST(ArcDocumentsProviderUtilTest, GetFileNameForDocument) {
EXPECT_EQ("kitten.png",
GetFileNameForDocument(MakeDocument("kitten.png", "image/png")));
EXPECT_EQ("a__b.png",
GetFileNameForDocument(MakeDocument("a//b.png", "image/png")));
EXPECT_EQ("_.png", GetFileNameForDocument(MakeDocument("", "image/png")));
EXPECT_EQ("_.png", GetFileNameForDocument(MakeDocument(".", "image/png")));
EXPECT_EQ("_.png", GetFileNameForDocument(MakeDocument("..", "image/png")));
EXPECT_EQ("_.png",
GetFileNameForDocument(MakeDocument("......", "image/png")));
EXPECT_EQ("kitten.png",
GetFileNameForDocument(MakeDocument("kitten", "image/png")));
EXPECT_EQ("kitten",
GetFileNameForDocument(MakeDocument("kitten", "abc/xyz")));
}
} // namespace } // namespace
} // namespace arc } // namespace arc
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment