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
|
// Copyright 2020 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/ui/ash/holding_space/holding_space_util.h"
#include "ash/public/cpp/holding_space/holding_space_constants.h"
#include "ash/public/cpp/holding_space/holding_space_util.h"
#include "ash/public/cpp/image_util.h"
#include "base/barrier_closure.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "chrome/browser/ash/drive/drive_integration_service.h"
#include "chrome/browser/ash/file_manager/fileapi_util.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ui/ash/thumbnail_loader/thumbnail_loader.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user.h"
#include "storage/browser/file_system/file_system_context.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "url/gurl.h"
namespace ash {
namespace holding_space_util {
namespace {
// Helpers ---------------------------------------------------------------------
HoldingSpaceFile::FileSystemType ToHoldingSpaceFileSystemType(
storage::FileSystemType file_system_type) {
switch (file_system_type) {
case storage::FileSystemType::kFileSystemTypeArcContent:
return HoldingSpaceFile::FileSystemType::kArcContent;
case storage::FileSystemType::kFileSystemTypeArcDocumentsProvider:
return HoldingSpaceFile::FileSystemType::kArcDocumentsProvider;
case storage::FileSystemType::kFileSystemTypeDeviceMedia:
return HoldingSpaceFile::FileSystemType::kDeviceMedia;
case storage::FileSystemType::kFileSystemTypeDeviceMediaAsFileStorage:
return HoldingSpaceFile::FileSystemType::kDeviceMediaAsFileStorage;
case storage::FileSystemType::kFileSystemTypeDragged:
return HoldingSpaceFile::FileSystemType::kDragged;
case storage::FileSystemType::kFileSystemTypeDriveFs:
return HoldingSpaceFile::FileSystemType::kDriveFs;
case storage::FileSystemType::kFileSystemTypeExternal:
return HoldingSpaceFile::FileSystemType::kExternal;
case storage::FileSystemType::kFileSystemTypeForTransientFile:
return HoldingSpaceFile::FileSystemType::kForTransientFile;
case storage::FileSystemType::kFileSystemTypeFuseBox:
return HoldingSpaceFile::FileSystemType::kFuseBox;
case storage::FileSystemType::kFileSystemTypeIsolated:
return HoldingSpaceFile::FileSystemType::kIsolated;
case storage::FileSystemType::kFileSystemTypeLocal:
return HoldingSpaceFile::FileSystemType::kLocal;
case storage::FileSystemType::kFileSystemTypeLocalForPlatformApp:
return HoldingSpaceFile::FileSystemType::kLocalForPlatformApp;
case storage::FileSystemType::kFileSystemTypeLocalMedia:
return HoldingSpaceFile::FileSystemType::kLocalMedia;
case storage::FileSystemType::kFileSystemTypePersistent:
return HoldingSpaceFile::FileSystemType::kPersistent;
case storage::FileSystemType::kFileSystemTypeProvided:
return HoldingSpaceFile::FileSystemType::kProvided;
case storage::FileSystemType::kFileSystemTypeSmbFs:
return HoldingSpaceFile::FileSystemType::kSmbFs;
case storage::FileSystemType::kFileSystemTypeSyncable:
return HoldingSpaceFile::FileSystemType::kSyncable;
case storage::FileSystemType::kFileSystemTypeSyncableForInternalSync:
return HoldingSpaceFile::FileSystemType::kSyncableForInternalSync;
case storage::FileSystemType::kFileSystemTypeTemporary:
return HoldingSpaceFile::FileSystemType::kTemporary;
case storage::FileSystemType::kFileSystemTypeTest:
return HoldingSpaceFile::FileSystemType::kTest;
case storage::FileSystemType::kFileSystemTypeUnknown:
return HoldingSpaceFile::FileSystemType::kUnknown;
case storage::FileSystemType::kFileSystemInternalTypeEnumStart:
case storage::FileSystemType::kFileSystemInternalTypeEnumEnd:
NOTREACHED();
}
}
} // namespace
// ValidityRequirement ---------------------------------------------------------
ValidityRequirement::ValidityRequirement() = default;
ValidityRequirement::ValidityRequirement(const ValidityRequirement&) = default;
ValidityRequirement::ValidityRequirement(ValidityRequirement&& other) = default;
// Utilities -------------------------------------------------------------------
bool ShouldSkipPathCheck(Profile* profile, const base::FilePath& path) {
// Drive FS may be in the middle of restarting, so if it is enabled but not
// mounted, assume any files in drive are valid.
const auto* drive_integration_service =
drive::DriveIntegrationServiceFactory::FindForProfile(profile);
return drive_integration_service && drive_integration_service->is_enabled() &&
!drive_integration_service->IsMounted() &&
drive_integration_service->GetMountPointPath().IsParent(path);
}
void FilePathValid(Profile* profile,
FilePathWithValidityRequirement file_path_with_requirement,
FilePathValidCallback callback) {
auto* user = ProfileHelper::Get()->GetUserByProfile(profile);
file_manager::util::GetMetadataForPath(
file_manager::util::GetFileManagerFileSystemContext(profile),
file_path_with_requirement.first,
// NOTE: Provided file systems DCHECK if no metadata field is requested.
// TODO(http://b/274011452): Investigate if provided file systems should
// be supported by holding space.
// TODO(http://b/274011722): Investigate if we can remove time based
// validation of items in holding space.
{storage::FileSystemOperation::GetMetadataField::kLastModified},
base::BindOnce(
[](FilePathValidCallback callback,
FilePathWithValidityRequirement file_path_with_requirement,
AccountId account_id, base::File::Error result,
const base::File::Info& file_info) {
Profile* profile =
ProfileHelper::Get()->GetProfileByAccountId(account_id);
if (profile && ShouldSkipPathCheck(
profile, file_path_with_requirement.first)) {
std::move(callback).Run(true);
return;
}
bool valid = true;
const ValidityRequirement& requirement =
file_path_with_requirement.second;
if (requirement.must_exist) {
valid = result == base::File::Error::FILE_OK;
}
if (valid && requirement.must_be_newer_than) {
valid =
file_info.creation_time >
base::Time::Now() - requirement.must_be_newer_than.value();
}
std::move(callback).Run(valid);
},
std::move(callback), file_path_with_requirement,
user ? user->GetAccountId() : EmptyAccountId()));
}
void PartitionFilePathsByExistence(
Profile* profile,
FilePathList file_paths,
PartitionFilePathsByExistenceCallback callback) {
FilePathsWithValidityRequirements file_paths_with_requirements;
for (const auto& file_path : file_paths) {
file_paths_with_requirements.push_back({file_path, /*requirements=*/{}});
}
PartitionFilePathsByValidity(profile, file_paths_with_requirements,
std::move(callback));
}
void PartitionFilePathsByValidity(
Profile* profile,
FilePathsWithValidityRequirements file_paths_with_requirements,
PartitionFilePathsByValidityCallback callback) {
if (file_paths_with_requirements.empty()) {
std::move(callback).Run(/*valid_file_paths=*/{},
/*invalid_file_paths=*/{});
return;
}
auto valid_file_paths = std::make_unique<FilePathList>();
auto invalid_file_paths = std::make_unique<FilePathList>();
auto* valid_file_paths_ptr = valid_file_paths.get();
auto* invalid_file_paths_ptr = invalid_file_paths.get();
FilePathList file_paths;
for (const auto& file_path_with_requirement : file_paths_with_requirements) {
file_paths.push_back(file_path_with_requirement.first);
}
// This `barrier_closure` will be run after verifying the existence of all
// `file_paths`. It is expected that both `valid_file_paths` and
// `invalid_file_paths` will have been populated by the time of
// invocation.
base::RepeatingClosure barrier_closure = base::BarrierClosure(
file_paths_with_requirements.size(),
base::BindOnce(
[](FilePathList sorted_file_paths,
std::unique_ptr<FilePathList> valid_file_paths,
std::unique_ptr<FilePathList> invalid_file_paths,
PartitionFilePathsByValidityCallback callback) {
// We need to sort our partitioned vectors to match the original
// order that was provided at call time. This is necessary as the
// original order may have been lost due to race conditions when
// checking for file path existence.
auto sort = [&sorted_file_paths](FilePathList* file_paths) {
FilePathList temp_file_paths;
temp_file_paths.swap(*file_paths);
for (const auto& file_path : sorted_file_paths) {
if (base::Contains(temp_file_paths, file_path)) {
file_paths->push_back(file_path);
}
}
};
sort(valid_file_paths.get());
sort(invalid_file_paths.get());
// Ownership of the partitioned vectors is passed to `callback`.
std::move(callback).Run(std::move(*valid_file_paths),
std::move(*invalid_file_paths));
},
/*sorted_file_paths=*/file_paths, std::move(valid_file_paths),
std::move(invalid_file_paths), std::move(callback)));
// Verify existence of each `file_path`. Upon successful check of existence,
// each `file_path` should be pushed into either `valid_file_paths` or
// `invalid_file_paths` as appropriate.
for (const auto& file_path_with_requirement : file_paths_with_requirements) {
FilePathValid(
profile, file_path_with_requirement,
base::BindOnce(
[](base::FilePath file_path, FilePathList* valid_file_paths,
FilePathList* invalid_file_paths,
base::RepeatingClosure barrier_closure, bool exists) {
if (exists) {
valid_file_paths->push_back(file_path);
} else {
invalid_file_paths->push_back(file_path);
}
barrier_closure.Run();
},
file_path_with_requirement.first,
base::Unretained(valid_file_paths_ptr),
base::Unretained(invalid_file_paths_ptr), barrier_closure));
}
}
HoldingSpaceFile::FileSystemType ResolveFileSystemType(
Profile* profile,
const GURL& file_system_url) {
return ToHoldingSpaceFileSystemType(
file_manager::util::GetFileManagerFileSystemContext(profile)
->CrackURLInFirstPartyContext(file_system_url)
.type());
}
GURL ResolveFileSystemUrl(Profile* profile, const base::FilePath& file_path) {
GURL file_system_url;
if (!file_manager::util::ConvertAbsoluteFilePathToFileSystemUrl(
profile, file_path, file_manager::util::GetFileManagerURL(),
&file_system_url)) {
VLOG(2) << "Unable to convert file path to File System URL.";
}
return file_system_url;
}
std::unique_ptr<HoldingSpaceImage> ResolveImage(
ThumbnailLoader* thumbnail_loader,
HoldingSpaceItem::Type type,
const base::FilePath& file_path) {
return ResolveImageWithPlaceholderImageSkiaResolver(
thumbnail_loader,
/*placeholder_image_skia_resolver=*/base::NullCallback(), type,
file_path);
}
std::unique_ptr<HoldingSpaceImage> ResolveImageWithPlaceholderImageSkiaResolver(
ThumbnailLoader* thumbnail_loader,
HoldingSpaceImage::PlaceholderImageSkiaResolver
placeholder_image_skia_resolver,
HoldingSpaceItem::Type type,
const base::FilePath& file_path) {
return std::make_unique<HoldingSpaceImage>(
GetMaxImageSizeForType(type), file_path,
/*async_bitmap_resolver=*/
base::BindRepeating(
[](const base::WeakPtr<ThumbnailLoader>& thumbnail_loader,
const base::FilePath& file_path, const gfx::Size& size,
HoldingSpaceImage::BitmapCallback callback) {
if (thumbnail_loader) {
thumbnail_loader->Load({file_path, size}, std::move(callback));
}
},
thumbnail_loader->GetWeakPtr()),
/*placeholder_image_skia_resolver=*/
base::BindRepeating(
[](HoldingSpaceImage::PlaceholderImageSkiaResolver
placeholder_image_skia_resolver,
const base::FilePath& file_path, const gfx::Size& size,
const std::optional<bool>& dark_background,
const std::optional<bool>& is_folder) {
// When the initial placeholder is being created during
// construction, `dark_background` and `is_folder` will be absent.
// In that case, don't show a placeholder to minimize jank.
if (!dark_background.has_value() && !is_folder.has_value()) {
return image_util::CreateEmptyImage(size);
}
// If an explicit `placeholder_image_skia_resolver` has been
// specified, use it to create the appropriate placeholder image.
if (!placeholder_image_skia_resolver.is_null()) {
return placeholder_image_skia_resolver.Run(
file_path, size, dark_background, is_folder);
}
// Otherwise, fallback to default behavior which is to create an
// image corresponding to the file type of the associated backing
// file.
return HoldingSpaceImage::
CreateDefaultPlaceholderImageSkiaResolver()
.Run(file_path, size, dark_background, is_folder);
},
placeholder_image_skia_resolver));
}
} // namespace holding_space_util
} // namespace ash
|