Commit 14a30c26 authored by Naoki Fukino's avatar Naoki Fukino Committed by Commit Bot

Filter recent files from disk source by specified file_type.

Based on the given |file_type|, GetRecentFiles to disk sources should
filtered result.
File types of the files inside disk sources will be guessed by file
extensions (inside net::GetMimeTypeFromFile()).

Bug: 1040049
Test: Ran unit_tests
Change-Id: I7ae9c5b71bbc2fc25016149fdf8e8a98d6b1b5f8
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2011592
Commit-Queue: Noel Gordon <noel@chromium.org>
Reviewed-by: default avatarNoel Gordon <noel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#733559}
parent ef2cc102
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "content/public/browser/browser_task_traits.h" #include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "net/base/mime_util.h"
#include "storage/browser/file_system/external_mount_points.h" #include "storage/browser/file_system/external_mount_points.h"
#include "storage/browser/file_system/file_system_context.h" #include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_operation.h" #include "storage/browser/file_system/file_system_operation.h"
...@@ -26,6 +27,10 @@ namespace chromeos { ...@@ -26,6 +27,10 @@ namespace chromeos {
namespace { namespace {
constexpr char kAudioMimeType[] = "audio/*";
constexpr char kImageMimeType[] = "image/*";
constexpr char kVideoMimeType[] = "video/*";
void OnReadDirectoryOnIOThread( void OnReadDirectoryOnIOThread(
const storage::FileSystemOperation::ReadDirectoryCallback& callback, const storage::FileSystemOperation::ReadDirectoryCallback& callback,
base::File::Error result, base::File::Error result,
...@@ -70,6 +75,33 @@ void GetMetadataOnIOThread( ...@@ -70,6 +75,33 @@ void GetMetadataOnIOThread(
base::BindOnce(&OnGetMetadataOnIOThread, std::move(callback))); base::BindOnce(&OnGetMetadataOnIOThread, std::move(callback)));
} }
// Returns true if the files at |path| matches the given |file_type|.
bool MatchesFileType(const base::FilePath& path,
RecentSource::FileType file_type) {
if (file_type == RecentSource::FileType::kAll)
return true;
// File type for |path| is guessed using net::GetMimeTypeFromFile.
// It guesses mime types based on file extensions, but it has a limited set
// of file extensions.
// TODO(fukino): It is better to have better coverage of file extensions to be
// consistent with file-type detection on Android system. crbug.com/1034874.
std::string mime_type;
if (!net::GetMimeTypeFromFile(path, &mime_type))
return false;
switch (file_type) {
case RecentSource::FileType::kAudio:
return net::MatchesMimeType(kAudioMimeType, mime_type);
case RecentSource::FileType::kImage:
return net::MatchesMimeType(kImageMimeType, mime_type);
case RecentSource::FileType::kVideo:
return net::MatchesMimeType(kVideoMimeType, mime_type);
default:
return false;
}
}
} // namespace } // namespace
RecentDiskSource::RecentDiskSource(std::string mount_point_name, RecentDiskSource::RecentDiskSource(std::string mount_point_name,
...@@ -153,6 +185,9 @@ void RecentDiskSource::OnReadDirectory( ...@@ -153,6 +185,9 @@ void RecentDiskSource::OnReadDirectory(
} }
ScanDirectory(subpath, depth + 1); ScanDirectory(subpath, depth + 1);
} else { } else {
if (!MatchesFileType(entry.name, params_.value().file_type())) {
continue;
}
storage::FileSystemURL url = BuildDiskURL(subpath); storage::FileSystemURL url = BuildDiskURL(subpath);
++inflight_stats_; ++inflight_stats_;
base::PostTask( base::PostTask(
......
...@@ -69,15 +69,16 @@ class RecentDiskSourceTest : public testing::Test { ...@@ -69,15 +69,16 @@ class RecentDiskSourceTest : public testing::Test {
return file.SetTimes(time, time); return file.SetTimes(time, time);
} }
std::vector<RecentFile> GetRecentFiles(size_t max_files, std::vector<RecentFile> GetRecentFiles(
const base::Time& cutoff_time) { size_t max_files,
const base::Time& cutoff_time,
RecentSource::FileType file_type = RecentSource::FileType::kAll) {
std::vector<RecentFile> files; std::vector<RecentFile> files;
base::RunLoop run_loop; base::RunLoop run_loop;
source_->GetRecentFiles(RecentSource::Params( source_->GetRecentFiles(RecentSource::Params(
file_system_context_.get(), origin_, max_files, cutoff_time, file_system_context_.get(), origin_, max_files, cutoff_time, file_type,
RecentSource::FileType::kAll,
base::BindOnce( base::BindOnce(
[](base::RunLoop* run_loop, std::vector<RecentFile>* out_files, [](base::RunLoop* run_loop, std::vector<RecentFile>* out_files,
std::vector<RecentFile> files) { std::vector<RecentFile> files) {
...@@ -213,6 +214,72 @@ TEST_F(RecentDiskSourceTest, MaxDepth) { ...@@ -213,6 +214,72 @@ TEST_F(RecentDiskSourceTest, MaxDepth) {
EXPECT_EQ(base::Time::FromJavaTime(1000), files[1].last_modified()); EXPECT_EQ(base::Time::FromJavaTime(1000), files[1].last_modified());
} }
TEST_F(RecentDiskSourceTest, GetAudioFiles) {
// Oldest
ASSERT_TRUE(CreateEmptyFile("1.jpg", base::Time::FromJavaTime(1000)));
ASSERT_TRUE(CreateEmptyFile("2.mp4", base::Time::FromJavaTime(2000)));
ASSERT_TRUE(CreateEmptyFile("3.png", base::Time::FromJavaTime(3000)));
ASSERT_TRUE(CreateEmptyFile("4.mp3", base::Time::FromJavaTime(4000)));
ASSERT_TRUE(CreateEmptyFile("5.gif", base::Time::FromJavaTime(5000)));
ASSERT_TRUE(CreateEmptyFile("6.webm", base::Time::FromJavaTime(6000)));
// Newest
std::vector<RecentFile> files =
GetRecentFiles(6, base::Time(), RecentSource::FileType::kAudio);
std::sort(files.begin(), files.end(), RecentFileComparator());
ASSERT_EQ(1u, files.size());
EXPECT_EQ("4.mp3", files[0].url().path().BaseName().value());
EXPECT_EQ(base::Time::FromJavaTime(4000), files[0].last_modified());
}
TEST_F(RecentDiskSourceTest, GetImageFiles) {
// Oldest
ASSERT_TRUE(CreateEmptyFile("1.jpg", base::Time::FromJavaTime(1000)));
ASSERT_TRUE(CreateEmptyFile("2.mp4", base::Time::FromJavaTime(2000)));
ASSERT_TRUE(CreateEmptyFile("3.png", base::Time::FromJavaTime(3000)));
ASSERT_TRUE(CreateEmptyFile("4.mp3", base::Time::FromJavaTime(4000)));
ASSERT_TRUE(CreateEmptyFile("5.gif", base::Time::FromJavaTime(5000)));
ASSERT_TRUE(CreateEmptyFile("6.webm", base::Time::FromJavaTime(6000)));
// Newest
std::vector<RecentFile> files =
GetRecentFiles(6, base::Time(), RecentSource::FileType::kImage);
std::sort(files.begin(), files.end(), RecentFileComparator());
ASSERT_EQ(3u, files.size());
EXPECT_EQ("5.gif", files[0].url().path().BaseName().value());
EXPECT_EQ(base::Time::FromJavaTime(5000), files[0].last_modified());
EXPECT_EQ("3.png", files[1].url().path().BaseName().value());
EXPECT_EQ(base::Time::FromJavaTime(3000), files[1].last_modified());
EXPECT_EQ("1.jpg", files[2].url().path().BaseName().value());
EXPECT_EQ(base::Time::FromJavaTime(1000), files[2].last_modified());
}
TEST_F(RecentDiskSourceTest, GetVideoFiles) {
// Oldest
ASSERT_TRUE(CreateEmptyFile("1.jpg", base::Time::FromJavaTime(1000)));
ASSERT_TRUE(CreateEmptyFile("2.mp4", base::Time::FromJavaTime(2000)));
ASSERT_TRUE(CreateEmptyFile("3.png", base::Time::FromJavaTime(3000)));
ASSERT_TRUE(CreateEmptyFile("4.mp3", base::Time::FromJavaTime(4000)));
ASSERT_TRUE(CreateEmptyFile("5.gif", base::Time::FromJavaTime(5000)));
ASSERT_TRUE(CreateEmptyFile("6.webm", base::Time::FromJavaTime(6000)));
// Newest
std::vector<RecentFile> files =
GetRecentFiles(6, base::Time(), RecentSource::FileType::kVideo);
std::sort(files.begin(), files.end(), RecentFileComparator());
ASSERT_EQ(2u, files.size());
EXPECT_EQ("6.webm", files[0].url().path().BaseName().value());
EXPECT_EQ(base::Time::FromJavaTime(6000), files[0].last_modified());
EXPECT_EQ("2.mp4", files[1].url().path().BaseName().value());
EXPECT_EQ(base::Time::FromJavaTime(2000), files[1].last_modified());
}
TEST_F(RecentDiskSourceTest, GetRecentFiles_UmaStats) { TEST_F(RecentDiskSourceTest, GetRecentFiles_UmaStats) {
base::HistogramTester histogram_tester; base::HistogramTester histogram_tester;
......
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