1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/fileapi/recent_arc_media_source.h"
#include <algorithm>
#include <iterator>
#include <utility>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/fileapi/arc_documents_provider_root.h"
#include "chrome/browser/ash/arc/fileapi/arc_documents_provider_root_map.h"
#include "chrome/browser/ash/arc/fileapi/arc_documents_provider_util.h"
#include "chrome/browser/ash/arc/fileapi/arc_media_view_util.h"
#include "chrome/browser/ash/fileapi/recent_file.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/file_system/external_mount_points.h"
using content::BrowserThread;
namespace ash {
namespace {
namespace fmp = extensions::api::file_manager_private;
const char kAndroidDownloadDirPrefix[] = "/storage/emulated/0/Download/";
// The path of the MyFiles directory inside Android. The UUID "0000....2019" is
// defined in
// chromeos/ash/experiences/arc/volume_mounter/arc_volume_mounter_bridge.cc.
// TODO(crbug.com/929031): Move MyFiles constants to a common place.
const char kAndroidMyFilesDirPrefix[] =
"/storage/0000000000000000000000000000CAFEF00D2019/";
base::FilePath GetRelativeMountPath(const std::string& root_id) {
base::FilePath mount_path = arc::GetDocumentsProviderMountPath(
arc::kMediaDocumentsProviderAuthority, root_id);
base::FilePath relative_mount_path;
base::FilePath(arc::kDocumentsProviderMountPointPath)
.AppendRelativePath(mount_path, &relative_mount_path);
return relative_mount_path;
}
bool IsInsideDownloadsOrMyFiles(const std::string& path) {
if (base::StartsWith(path, kAndroidDownloadDirPrefix,
base::CompareCase::SENSITIVE)) {
return true;
}
if (base::StartsWith(path, kAndroidMyFilesDirPrefix,
base::CompareCase::SENSITIVE)) {
return true;
}
return false;
}
std::vector<RecentFile> ExtractFoundFiles(
const std::map<std::string, std::optional<RecentFile>>&
document_id_to_file) {
std::vector<RecentFile> files;
for (const auto& entry : document_id_to_file) {
const std::optional<RecentFile>& file = entry.second;
if (file.has_value()) {
files.emplace_back(file.value());
}
}
return files;
}
// Tidies up the vector of files by sorting them and limiting their number to
// the specified maximum.
std::vector<RecentFile> PrepareResponse(std::vector<RecentFile>&& files,
size_t max_files) {
std::sort(files.begin(), files.end(), RecentFileComparator());
if (files.size() > max_files) {
files.resize(max_files);
}
return files;
}
} // namespace
RecentArcMediaSource::CallContext::CallContext(const Params& params,
GetRecentFilesCallback callback)
: params(params),
callback(std::move(callback)),
build_start_time(base::TimeTicks::Now()) {}
RecentArcMediaSource::CallContext::CallContext(CallContext&& context)
: params(context.params),
callback(std::move(context.callback)),
build_start_time(std::move(context.build_start_time)) {}
RecentArcMediaSource::CallContext::~CallContext() = default;
const char RecentArcMediaSource::kLoadHistogramName[] =
"FileBrowser.Recent.LoadArcMedia";
RecentArcMediaSource::RecentArcMediaSource(Profile* profile,
const std::string& root_id)
: RecentSource(fmp::VolumeType::kMediaView),
profile_(profile),
root_id_(root_id),
relative_mount_path_(GetRelativeMountPath(root_id)) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
RecentArcMediaSource::~RecentArcMediaSource() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
bool RecentArcMediaSource::MatchesFileType(FileType file_type) const {
switch (file_type) {
case FileType::kAll:
return true;
case FileType::kImage:
return root_id_ == arc::kImagesRootId;
case FileType::kVideo:
return root_id_ == arc::kVideosRootId;
case FileType::kDocument:
return root_id_ == arc::kDocumentsRootId;
case FileType::kAudio:
return root_id_ == arc::kAudioRootId;
default:
LOG(FATAL) << "Unhandled file_type: " << static_cast<int>(file_type);
}
}
void RecentArcMediaSource::GetRecentFiles(const Params& params,
GetRecentFilesCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
DCHECK(context_map_.Lookup(params.call_id()) == nullptr);
// If ARC file system operations will be deferred, return immediately without
// recording UMA metrics.
//
// TODO(nya): Return files progressively rather than simply giving up.
// Also, it is wrong to assume all following operations will not be deferred
// just because this function returned true. However, in practice, it is rare
// ArcFileSystemOperationRunner's deferring state switches from disabled to
// enabled (one such case is when ARC container crashes).
if (!WillArcFileSystemOperationsRunImmediately()) {
std::move(callback).Run({});
return;
}
auto context = std::make_unique<CallContext>(params, std::move(callback));
context_map_.AddWithID(std::move(context), params.call_id());
if (!MatchesFileType(params.file_type())) {
// Return immediately without results when this root's id does not match the
// requested file type.
OnComplete(params.call_id());
return;
}
auto* runner =
arc::ArcFileSystemOperationRunner::GetForBrowserContext(profile_);
if (!runner) {
// This happens when ARC is not allowed in this profile.
OnComplete(params.call_id());
return;
}
runner->GetRecentDocuments(
arc::kMediaDocumentsProviderAuthority, root_id_,
base::BindOnce(&RecentArcMediaSource::OnRunnerDone,
weak_ptr_factory_.GetWeakPtr(), params.call_id()));
}
void RecentArcMediaSource::OnRunnerDone(
const int32_t call_id,
std::optional<std::vector<arc::mojom::DocumentPtr>> maybe_documents) {
if (!lag_.is_positive()) {
OnGotRecentDocuments(call_id, std::move(maybe_documents));
return;
}
if (!timer_) {
timer_ = std::make_unique<base::OneShotTimer>();
}
timer_->Start(FROM_HERE, lag_,
base::BindOnce(&RecentArcMediaSource::OnGotRecentDocuments,
weak_ptr_factory_.GetWeakPtr(), call_id,
std::move(maybe_documents)));
}
void RecentArcMediaSource::OnGotRecentDocuments(
const int32_t call_id,
std::optional<std::vector<arc::mojom::DocumentPtr>> maybe_documents) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
CallContext* context = context_map_.Lookup(call_id);
if (context == nullptr) {
return;
}
// Initialize |document_id_to_file_| with recent document IDs returned.
if (maybe_documents.has_value()) {
const std::u16string q16 = base::UTF8ToUTF16(context->params.query());
for (const auto& document : maybe_documents.value()) {
// Exclude media files under Downloads or MyFiles directory since they are
// covered by RecentDiskSource.
if (document->android_file_system_path.has_value() &&
IsInsideDownloadsOrMyFiles(
document->android_file_system_path.value())) {
continue;
}
if (!FileNameMatches(base::UTF8ToUTF16(document->display_name), q16)) {
continue;
}
context->document_id_to_file.emplace(document->document_id, std::nullopt);
}
}
if (context->document_id_to_file.empty()) {
OnComplete(call_id);
return;
}
// We have several recent documents, so start searching their real paths.
ScanDirectory(call_id, base::FilePath());
}
void RecentArcMediaSource::ScanDirectory(const int32_t call_id,
const base::FilePath& path) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// If context was cleared while we were scanning directories, just abandon
// this effort.
CallContext* context = context_map_.Lookup(call_id);
if (context == nullptr) {
return;
}
++context->num_inflight_readdirs;
auto* root_map =
arc::ArcDocumentsProviderRootMap::GetForBrowserContext(profile_);
if (!root_map) {
// We already checked ARC is allowed for this profile (indirectly), so
// this should never happen.
LOG(ERROR) << "ArcDocumentsProviderRootMap is not available";
OnDirectoryRead(call_id, path, base::File::FILE_ERROR_FAILED, {});
return;
}
auto* root =
root_map->Lookup(arc::kMediaDocumentsProviderAuthority, root_id_);
if (!root) {
// Media roots should always exist.
LOG(ERROR) << "ArcDocumentsProviderRoot is missing";
OnDirectoryRead(call_id, path, base::File::FILE_ERROR_NOT_FOUND, {});
return;
}
root->ReadDirectory(
path, base::BindOnce(&RecentArcMediaSource::OnDirectoryRead,
weak_ptr_factory_.GetWeakPtr(), call_id, path));
}
void RecentArcMediaSource::OnDirectoryRead(
const int32_t call_id,
const base::FilePath& path,
base::File::Error result,
std::vector<arc::ArcDocumentsProviderRoot::ThinFileInfo> files) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
// If callback was cleared while we were scanning directories just abandon
// this effort.
CallContext* context = context_map_.Lookup(call_id);
if (context == nullptr) {
return;
}
for (const auto& file : files) {
base::FilePath subpath = path.Append(file.name);
if (file.is_directory) {
if (!context->params.IsLate()) {
ScanDirectory(call_id, subpath);
}
continue;
}
auto doc_it = context->document_id_to_file.find(file.document_id);
if (doc_it == context->document_id_to_file.end()) {
continue;
}
// Update |document_id_to_file_|.
// We keep the lexicographically smallest URL to stabilize the results when
// there are multiple files with the same document ID.
auto url = BuildDocumentsProviderUrl(context->params, subpath);
std::optional<RecentFile>& entry = doc_it->second;
if (!entry.has_value() ||
storage::FileSystemURL::Comparator()(url, entry.value().url())) {
entry = RecentFile(url, file.last_modified);
}
}
--context->num_inflight_readdirs;
DCHECK_LE(0, context->num_inflight_readdirs);
if (context->num_inflight_readdirs == 0) {
OnComplete(call_id);
}
}
std::vector<RecentFile> RecentArcMediaSource::Stop(const int32_t call_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
CallContext* context = context_map_.Lookup(call_id);
if (context == nullptr) {
// Here we assume that the call to stop came just after we returned the
// results in the OnComplete method.
return {};
}
size_t max_files = context->params.max_files();
// We do not call the callback, so just clean it up.
context->callback.Reset();
// Copy the files we collected so far.
std::vector<RecentFile> files =
ExtractFoundFiles(context->document_id_to_file);
context_map_.Remove(call_id);
return PrepareResponse(std::move(files), max_files);
}
void RecentArcMediaSource::OnComplete(const int32_t call_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
CallContext* context = context_map_.Lookup(call_id);
if (context == nullptr) {
// If we cannot find the context that means the Stop method has been called.
// Just return immediately.
return;
}
UMA_HISTOGRAM_TIMES(kLoadHistogramName,
base::TimeTicks::Now() - context->build_start_time);
DCHECK_EQ(0, context->num_inflight_readdirs);
DCHECK(!context->callback.is_null());
std::vector<RecentFile> files =
ExtractFoundFiles(context->document_id_to_file);
std::move(context->callback)
.Run(PrepareResponse(std::move(files), context->params.max_files()));
context_map_.Remove(call_id);
}
bool RecentArcMediaSource::WillArcFileSystemOperationsRunImmediately() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
auto* runner =
arc::ArcFileSystemOperationRunner::GetForBrowserContext(profile_);
// If ARC is not allowed the user, |runner| is nullptr.
if (!runner) {
return false;
}
return !runner->WillDefer();
}
void RecentArcMediaSource::SetLagForTesting(const base::TimeDelta& lag) {
lag_ = lag;
}
storage::FileSystemURL RecentArcMediaSource::BuildDocumentsProviderUrl(
const Params& params,
const base::FilePath& path) const {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
storage::ExternalMountPoints* mount_points =
storage::ExternalMountPoints::GetSystemInstance();
DCHECK(mount_points);
return mount_points->CreateExternalFileSystemURL(
blink::StorageKey::CreateFirstParty(url::Origin::Create(params.origin())),
arc::kDocumentsProviderMountPointName, relative_mount_path_.Append(path));
}
} // namespace ash
|