Commit fb2ad95f authored by Naoki Fukino's avatar Naoki Fukino Committed by Commit Bot

Filter recent files from ARC media source by specified file_type.

Based on the given |file_type|, GetRecentFiles to ARC media source
should return filtered result.
For instance, if the specified file_type is "Images", we should query
recent files only on "images_root" in the MediaDocumentsProvider.

Bug: 1040049
Test: Ran unit_tests
Change-Id: I1e24d9c8338355b7036fe2a7ad1d129c54d7a2c9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2012200Reviewed-by: default avatarSatoshi Niwa <niwa@chromium.org>
Commit-Queue: Naoki Fukino <fukino@chromium.org>
Cr-Commit-Position: refs/heads/master@{#733906}
parent a0d97bcc
......@@ -36,8 +36,11 @@ const char kAndroidDownloadDirPrefix[] = "/storage/emulated/0/Download/";
const char kMediaDocumentsProviderAuthority[] =
"com.android.providers.media.documents";
constexpr char kMediaDocumentsProviderImagesRoot[] = "images_root";
constexpr char kMediaDocumentsProviderVideosRoot[] = "videos_root";
const char* kMediaDocumentsProviderRootIds[] = {
"images_root", "videos_root",
kMediaDocumentsProviderImagesRoot,
kMediaDocumentsProviderVideosRoot,
};
base::FilePath GetRelativeMountPath(const std::string& root_id) {
......@@ -89,6 +92,7 @@ class RecentArcMediaSource::MediaRoot {
storage::FileSystemURL BuildDocumentsProviderUrl(
const base::FilePath& path) const;
bool MatchesFileType(FileType file_type) const;
// Set in the constructor.
const std::string root_id_;
......@@ -143,6 +147,13 @@ void RecentArcMediaSource::MediaRoot::GetRecentFiles(Params params) {
return;
}
if (!MatchesFileType(params_.value().file_type())) {
// Return immediately without results when this root's id does not match the
// requested file type.
OnComplete();
return;
}
runner->GetRecentDocuments(kMediaDocumentsProviderAuthority, root_id_,
base::Bind(&MediaRoot::OnGetRecentDocuments,
weak_ptr_factory_.GetWeakPtr()));
......@@ -277,6 +288,20 @@ RecentArcMediaSource::MediaRoot::BuildDocumentsProviderUrl(
relative_mount_path_.Append(path));
}
bool RecentArcMediaSource::MediaRoot::MatchesFileType(
FileType file_type) const {
switch (file_type) {
case FileType::kAll:
return true;
case FileType::kImage:
return root_id_ == kMediaDocumentsProviderImagesRoot;
case FileType::kVideo:
return root_id_ == kMediaDocumentsProviderVideosRoot;
default:
return false;
}
}
RecentArcMediaSource::RecentArcMediaSource(Profile* profile)
: profile_(profile) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
......
......@@ -30,7 +30,9 @@ namespace {
const char kMediaDocumentsProviderAuthority[] =
"com.android.providers.media.documents";
const char kAudioRootId[] = "audio_root";
const char kImagesRootId[] = "images_root";
const char kVideosRootId[] = "videos_root";
std::unique_ptr<KeyedService> CreateFileSystemOperationRunnerForTesting(
content::BrowserContext* context) {
......@@ -88,7 +90,7 @@ class RecentArcMediaSourceTest : public testing::Test {
protected:
void AddDocumentsToFakeFileSystemInstance() {
auto root_doc =
auto images_root_doc =
MakeDocument(kImagesRootId, "", "", arc::kAndroidDirectoryMimeType,
base::Time::FromJavaTime(1));
auto cat_doc = MakeDocument("cat", kImagesRootId, "cat.png", "image/png",
......@@ -99,14 +101,38 @@ class RecentArcMediaSourceTest : public testing::Test {
base::Time::FromJavaTime(4));
auto elk_doc = MakeDocument("elk", kImagesRootId, "elk.tiff", "image/tiff",
base::Time::FromJavaTime(5));
fake_file_system_.AddDocument(root_doc);
auto audio_root_doc =
MakeDocument(kAudioRootId, "", "", arc::kAndroidDirectoryMimeType,
base::Time::FromJavaTime(1));
auto god_doc = MakeDocument("god", kAudioRootId, "god.mp3", "audio/mp3",
base::Time::FromJavaTime(6));
auto videos_root_doc =
MakeDocument(kVideosRootId, "", "", arc::kAndroidDirectoryMimeType,
base::Time::FromJavaTime(1));
auto hot_doc = MakeDocument("hot", kVideosRootId, "hot.mp4", "video/mp4",
base::Time::FromJavaTime(7));
auto ink_doc = MakeDocument("ink", kVideosRootId, "ink.webm", "video/webm",
base::Time::FromJavaTime(8));
fake_file_system_.AddDocument(images_root_doc);
fake_file_system_.AddDocument(cat_doc);
fake_file_system_.AddDocument(dog_doc);
fake_file_system_.AddDocument(fox_doc);
fake_file_system_.AddRecentDocument(kImagesRootId, root_doc);
fake_file_system_.AddDocument(audio_root_doc);
fake_file_system_.AddDocument(god_doc);
fake_file_system_.AddDocument(videos_root_doc);
fake_file_system_.AddDocument(hot_doc);
fake_file_system_.AddDocument(ink_doc);
fake_file_system_.AddRecentDocument(kImagesRootId, images_root_doc);
fake_file_system_.AddRecentDocument(kImagesRootId, cat_doc);
fake_file_system_.AddRecentDocument(kImagesRootId, dog_doc);
fake_file_system_.AddRecentDocument(kImagesRootId, elk_doc);
fake_file_system_.AddRecentDocument(kAudioRootId, audio_root_doc);
fake_file_system_.AddRecentDocument(kAudioRootId, god_doc);
fake_file_system_.AddRecentDocument(kVideosRootId, videos_root_doc);
fake_file_system_.AddRecentDocument(kVideosRootId, hot_doc);
fake_file_system_.AddRecentDocument(kVideosRootId, ink_doc);
}
void EnableFakeFileSystemInstance() {
......@@ -116,7 +142,8 @@ class RecentArcMediaSourceTest : public testing::Test {
arc_service_manager_->arc_bridge_service()->file_system());
}
std::vector<RecentFile> GetRecentFiles() {
std::vector<RecentFile> GetRecentFiles(
RecentSource::FileType file_type = RecentSource::FileType::kAll) {
std::vector<RecentFile> files;
base::RunLoop run_loop;
......@@ -124,7 +151,7 @@ class RecentArcMediaSourceTest : public testing::Test {
source_->GetRecentFiles(RecentSource::Params(
nullptr /* file_system_context */, GURL() /* origin */,
1 /* max_files: ignored */, base::Time() /* cutoff_time: ignored */,
RecentSource::FileType::kAll /* file_type: ignored */,
file_type /* file_type */,
base::BindOnce(
[](base::RunLoop* run_loop, std::vector<RecentFile>* out_files,
std::vector<RecentFile> files) {
......@@ -158,7 +185,7 @@ TEST_F(RecentArcMediaSourceTest, Normal) {
std::vector<RecentFile> files = GetRecentFiles();
ASSERT_EQ(2u, files.size());
ASSERT_EQ(4u, files.size());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kImagesRootId)
.Append("cat.png"),
......@@ -169,6 +196,16 @@ TEST_F(RecentArcMediaSourceTest, Normal) {
.Append("dog.jpg"),
files[1].url().path());
EXPECT_EQ(base::Time::FromJavaTime(3), files[1].last_modified());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kVideosRootId)
.Append("hot.mp4"),
files[2].url().path());
EXPECT_EQ(base::Time::FromJavaTime(7), files[2].last_modified());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kVideosRootId)
.Append("ink.webm"),
files[3].url().path());
EXPECT_EQ(base::Time::FromJavaTime(8), files[3].last_modified());
}
TEST_F(RecentArcMediaSourceTest, ArcNotAvailable) {
......@@ -186,6 +223,54 @@ TEST_F(RecentArcMediaSourceTest, Deferred) {
EXPECT_EQ(0u, files.size());
}
TEST_F(RecentArcMediaSourceTest, GetAudioFiles) {
EnableFakeFileSystemInstance();
std::vector<RecentFile> files =
GetRecentFiles(RecentSource::FileType::kAudio);
// Query for recently-modified audio files should be ignored, since
// MediaDocumentsProvider doesn't support queryRecentDocuments for audio.
ASSERT_EQ(0u, files.size());
}
TEST_F(RecentArcMediaSourceTest, GetImageFiles) {
EnableFakeFileSystemInstance();
std::vector<RecentFile> files =
GetRecentFiles(RecentSource::FileType::kImage);
ASSERT_EQ(2u, files.size());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kImagesRootId)
.Append("cat.png"),
files[0].url().path());
EXPECT_EQ(base::Time::FromJavaTime(2), files[0].last_modified());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kImagesRootId)
.Append("dog.jpg"),
files[1].url().path());
EXPECT_EQ(base::Time::FromJavaTime(3), files[1].last_modified());
}
TEST_F(RecentArcMediaSourceTest, GetVideoFiles) {
EnableFakeFileSystemInstance();
std::vector<RecentFile> files =
GetRecentFiles(RecentSource::FileType::kVideo);
ASSERT_EQ(2u, files.size());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kVideosRootId)
.Append("hot.mp4"),
files[0].url().path());
EXPECT_EQ(base::Time::FromJavaTime(7), files[0].last_modified());
EXPECT_EQ(arc::GetDocumentsProviderMountPath(kMediaDocumentsProviderAuthority,
kVideosRootId)
.Append("ink.webm"),
files[1].url().path());
EXPECT_EQ(base::Time::FromJavaTime(8), files[1].last_modified());
}
TEST_F(RecentArcMediaSourceTest, UmaStats) {
EnableFakeFileSystemInstance();
......
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