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
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/file_system_access/file_system_access_watcher_manager.h"
#include <algorithm>
#include <memory>
#include "base/check.h"
#include "base/containers/cxx20_erase_list.h"
#include "base/functional/bind.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/ranges/algorithm.h"
#include "base/sequence_checker.h"
#include "base/types/pass_key.h"
#include "build/buildflag.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "content/browser/file_system_access/file_system_access_bucket_path_watcher.h"
#include "content/browser/file_system_access/file_system_access_change_source.h"
#include "content/browser/file_system_access/file_system_access_error.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "content/browser/file_system_access/file_system_access_observer_host.h"
#include "content/browser/file_system_access/file_system_access_observer_observation.h"
#include "content/browser/file_system_access/file_system_access_watch_scope.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "storage/browser/file_system/file_system_url.h"
#include "storage/common/file_system/file_system_types.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_error.mojom-shared.h"
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) && !BUILDFLAG(IS_FUCHSIA)
#include "content/browser/file_system_access/file_system_access_local_path_watcher.h"
#endif // !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS) &&
// !BUILDFLAG(IS_FUCHSIA)
namespace content {
FileSystemAccessWatcherManager::Observation::Observation(
FileSystemAccessWatcherManager* watcher_manager,
FileSystemAccessWatchScope scope,
base::PassKey<FileSystemAccessWatcherManager> /*pass_key*/)
: scope_(std::move(scope)) {
CHECK(watcher_manager);
obs_.Observe(watcher_manager);
}
FileSystemAccessWatcherManager::Observation::~Observation() = default;
void FileSystemAccessWatcherManager::Observation::SetCallback(
OnChangesCallback on_change_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!on_change_callback_);
on_change_callback_ = std::move(on_change_callback);
}
void FileSystemAccessWatcherManager::Observation::NotifyOfChanges(
const std::list<Change>& changes,
base::PassKey<FileSystemAccessWatcherManager> pass_key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (on_change_callback_) {
on_change_callback_.Run(std::move(changes));
}
}
FileSystemAccessWatcherManager::FileSystemAccessWatcherManager(
FileSystemAccessManagerImpl* manager,
base::PassKey<FileSystemAccessManagerImpl> /*pass_key*/)
: manager_(manager),
bucket_path_watcher_(std::make_unique<FileSystemAccessBucketPathWatcher>(
base::WrapRefCounted(manager_->context()),
base::PassKey<FileSystemAccessWatcherManager>())) {
RegisterSource(bucket_path_watcher_.get());
}
FileSystemAccessWatcherManager::~FileSystemAccessWatcherManager() = default;
void FileSystemAccessWatcherManager::BindObserverHost(
const BindingContext& binding_context,
mojo::PendingReceiver<blink::mojom::FileSystemAccessObserverHost>
host_receiver) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observer_hosts_.insert(std::make_unique<FileSystemAccessObserverHost>(
manager_, this, binding_context, std::move(host_receiver)));
}
void FileSystemAccessWatcherManager::RemoveObserverHost(
FileSystemAccessObserverHost* host) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
size_t count_removed = observer_hosts_.erase(host);
CHECK_EQ(count_removed, 1u);
}
void FileSystemAccessWatcherManager::GetFileObservation(
const storage::FileSystemURL& file_url,
GetObservationCallback get_observation_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto scope = FileSystemAccessWatchScope::GetScopeForFileWatch(file_url);
EnsureSourceIsInitializedForScope(
scope, base::BindOnce(
&FileSystemAccessWatcherManager::PrepareObservationForScope,
weak_factory_.GetWeakPtr(), scope,
std::move(get_observation_callback)));
}
void FileSystemAccessWatcherManager::GetDirectoryObservation(
const storage::FileSystemURL& directory_url,
bool is_recursive,
GetObservationCallback get_observation_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto scope = FileSystemAccessWatchScope::GetScopeForDirectoryWatch(
directory_url, is_recursive);
EnsureSourceIsInitializedForScope(
scope, base::BindOnce(
&FileSystemAccessWatcherManager::PrepareObservationForScope,
weak_factory_.GetWeakPtr(), scope,
std::move(get_observation_callback)));
}
void FileSystemAccessWatcherManager::OnRawChange(
const storage::FileSystemURL& changed_url,
bool error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(https://crbug.com/1019297):
// - Batch changes.
// - Ignore changes caused by API implementation details, such as writes to
// swap files.
// - Discard changes corresponding to non-fully-active pages.
const std::list<Observation::Change> changes = {
{.url = changed_url, .error = error}};
for (auto& observation : observations_) {
if (observation.scope().Contains(changed_url)) {
observation.NotifyOfChanges(
changes, base::PassKey<FileSystemAccessWatcherManager>());
}
}
}
void FileSystemAccessWatcherManager::OnSourceBeingDestroyed(
FileSystemAccessChangeSource* source) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
source_observations_.RemoveObservation(source);
size_t count_removed = base::Erase(all_sources_, source);
CHECK_EQ(count_removed, 1u);
}
void FileSystemAccessWatcherManager::RegisterSource(
FileSystemAccessChangeSource* source) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
source_observations_.AddObservation(source);
all_sources_.push_back(source);
}
void FileSystemAccessWatcherManager::AddObserver(Observation* observation) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
observations_.AddObserver(observation);
}
void FileSystemAccessWatcherManager::RemoveObserver(Observation* observation) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const auto newly_unobserved_scope = observation->scope();
observations_.RemoveObserver(observation);
// Remove the respective source if we own it and it was the only observer
// for this scope.
//
// TODO(https://crbug.com/1019297): Handle initializing sources.
base::EraseIf(owned_sources_, [&](const auto& source) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return source->scope().Contains(newly_unobserved_scope) &&
base::ranges::none_of(
observations_, [&source](const auto& observation) {
return source->scope().Contains(observation.scope());
});
});
}
bool FileSystemAccessWatcherManager::HasSourceContainingScopeForTesting(
const FileSystemAccessWatchScope& scope) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return base::ranges::any_of(
all_sources_, [&scope](const FileSystemAccessChangeSource* source) {
return source->scope().Contains(scope);
});
}
void FileSystemAccessWatcherManager::EnsureSourceIsInitializedForScope(
FileSystemAccessWatchScope scope,
base::OnceCallback<void(blink::mojom::FileSystemAccessErrorPtr)>
on_source_initialized) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// TODO(https://crbug.com/1489057): Handle overlapping scopes and initializing
// sources.
FileSystemAccessChangeSource* raw_change_source = nullptr;
auto it = base::ranges::find_if(
all_sources_, [&scope](const FileSystemAccessChangeSource* source) {
return source->scope().Contains(scope);
});
if (it != all_sources_.end()) {
raw_change_source = *it;
} else {
auto owned_change_source = CreateOwnedSourceForScope(scope);
if (!owned_change_source) {
// TODO(https://crbug.com/1019297): Watching `scope` is not supported.
std::move(on_source_initialized)
.Run(file_system_access_error::FromStatus(
blink::mojom::FileSystemAccessStatus::kNotSupportedError));
return;
}
raw_change_source = owned_change_source.get();
owned_sources_.insert(std::move(owned_change_source));
}
CHECK(raw_change_source);
raw_change_source->EnsureInitialized(
base::BindOnce(&FileSystemAccessWatcherManager::DidInitializeSource,
weak_factory_.GetWeakPtr(), raw_change_source->AsWeakPtr(),
std::move(on_source_initialized)));
}
void FileSystemAccessWatcherManager::DidInitializeSource(
base::WeakPtr<FileSystemAccessChangeSource> source,
base::OnceCallback<void(blink::mojom::FileSystemAccessErrorPtr)>
on_source_initialized,
blink::mojom::FileSystemAccessErrorPtr result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (!source) {
// `source` was destroyed as we tried to initialize it. Abort.
std::move(on_source_initialized)
.Run(file_system_access_error::FromStatus(
blink::mojom::FileSystemAccessStatus::kOperationFailed));
return;
}
if (result->status != blink::mojom::FileSystemAccessStatus::kOk) {
// If we owned this source, remove it. A source which is not initialized
// will not notify of changes, so there's no use keeping it around.
//
// TODO(https://crbug.com/1019297): Decide how to handle unowned sources
// which fail to initialize.
base::EraseIf(
owned_sources_,
[&source](
const std::unique_ptr<FileSystemAccessChangeSource>& owned_source) {
return owned_source.get() == source.get();
});
}
std::move(on_source_initialized).Run(std::move(result));
}
void FileSystemAccessWatcherManager::PrepareObservationForScope(
FileSystemAccessWatchScope scope,
GetObservationCallback get_observation_callback,
blink::mojom::FileSystemAccessErrorPtr source_initialization_result) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
if (source_initialization_result->status !=
blink::mojom::FileSystemAccessStatus::kOk) {
std::move(get_observation_callback)
.Run(base::unexpected(std::move(source_initialization_result)));
return;
}
std::move(get_observation_callback)
.Run(std::make_unique<Observation>(
this, std::move(scope),
base::PassKey<FileSystemAccessWatcherManager>()));
}
std::unique_ptr<FileSystemAccessChangeSource>
FileSystemAccessWatcherManager::CreateOwnedSourceForScope(
FileSystemAccessWatchScope scope) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(scope.root_url().is_valid());
if (scope.root_url().mount_type() !=
storage::FileSystemType::kFileSystemTypeLocal) {
// We should never have to create an owned source for a bucket file system,
// since `bucket_path_watcher_` covers all possible bucket scopes.
CHECK(scope.root_url().type() !=
storage::FileSystemType::kFileSystemTypeTemporary);
// TODO(https://crbug.com/1489061): Support non-local file systems.
return nullptr;
}
// Access to the local file system is not supported on Android or iOS. See
// https://crbug.com/1011535.
// Meanwhile, `base::FilePatchWatcher` is not implemented on Fuchsia. See
// https://crbug.com/851641.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
return nullptr;
#else
auto new_source = std::make_unique<FileSystemAccessLocalPathWatcher>(
std::move(scope), base::WrapRefCounted(manager()->context()),
base::PassKey<FileSystemAccessWatcherManager>());
RegisterSource(new_source.get());
return new_source;
#endif // BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
}
} // namespace content
|