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
  391
  392
  393
  394
  395
  396
  397
  398
  399
  400
  401
  402
  403
  404
  405
  406
  407
  408
  409
  410
  411
  412
  413
  414
  415
  416
  417
  418
  419
  420
  421
  422
  423
  424
  425
  426
  427
  428
  429
  430
  431
  432
  433
  434
  435
  436
  437
  438
  439
  440
  441
  442
  443
  444
  445
  446
  447
  448
  449

content / browser / file_system_access / file_system_access_watcher_manager.cc [blame]

// 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 <list>
#include <memory>

#include "base/check.h"
#include "base/files/file_path.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"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_observer.mojom.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 {

using WatchType = FileSystemAccessWatchScope::WatchType;

namespace {

storage::FileSystemURL ToFileSystemURL(storage::FileSystemContext& context,
                                       const storage::FileSystemURL& base_url,
                                       const base::FilePath& absolute_path) {
  storage::FileSystemURL result = context.CreateCrackedFileSystemURL(
      base_url.storage_key(), base_url.mount_type(), absolute_path);
  if (base_url.bucket()) {
    result.SetBucket(base_url.bucket().value());
  }
  return result;
}

}  // namespace

FileSystemAccessWatcherManager::Observation::Change::Change(
    storage::FileSystemURL url,
    FileSystemAccessChangeSource::ChangeInfo change_info)
    : url(std::move(url)), change_info(std::move(change_info)) {}
FileSystemAccessWatcherManager::Observation::Change::~Change() = default;

FileSystemAccessWatcherManager::Observation::Change::Change(
    const FileSystemAccessWatcherManager::Observation::Change& other)
    : url(other.url), change_info(std::move(other.change_info)) {}
FileSystemAccessWatcherManager::Observation::Change::Change(
    FileSystemAccessWatcherManager::Observation::Change&&) noexcept = default;

FileSystemAccessWatcherManager::Observation::Change&
FileSystemAccessWatcherManager::Observation::Change::operator=(
    const FileSystemAccessWatcherManager::Observation::Change&) = default;
FileSystemAccessWatcherManager::Observation::Change&
FileSystemAccessWatcherManager::Observation::Change::operator=(
    FileSystemAccessWatcherManager::Observation::Change&&) noexcept = default;

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::optional<std::list<Change>>& changes_or_error,
    base::PassKey<FileSystemAccessWatcherManager> pass_key) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (on_change_callback_) {
    on_change_callback_.Run(std::move(changes_or_error));
  }
}

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,
    const FileSystemAccessChangeSource::ChangeInfo& raw_change_info,
    const FileSystemAccessWatchScope& scope) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // TODO(crbug.com/40268906): Batch changes.

  // Writes via FileSystemWritableFileStream are done on a temporary swap file,
  // so ignore the swap file changes. When a writable is closed, the swap file
  // overrides the original file, producing a moved event. We will convert this
  // "moved" event to "modified" below.
  if (changed_url.virtual_path().FinalExtension() ==
      FILE_PATH_LITERAL(".crswap")) {
    return;
  }

  if (raw_change_info != FileSystemAccessChangeSource::ChangeInfo()) {
    // If non-empty ChangeInfo exists, this change should not be an error.
    CHECK(!error);
  }
  // For ChangeType::kMoved, ChangeInfo.moved_from_path is expected.
  bool is_move_event = raw_change_info.change_type ==
                       FileSystemAccessChangeSource::ChangeType::kMoved;
  CHECK(!is_move_event || raw_change_info.moved_from_path.has_value());

  // Convert the "moved" event to "modified" if the event is from the swap file
  // overriding the original file due to `FileSystemWritableFileStream.close()`.
  FileSystemAccessChangeSource::ChangeInfo change_info = raw_change_info;
  if (is_move_event &&
      raw_change_info.moved_from_path.value().FinalExtension() ==
          FILE_PATH_LITERAL(".crswap")) {
    is_move_event = false;
    change_info = FileSystemAccessChangeSource::ChangeInfo(
        raw_change_info.file_path_type,
        FileSystemAccessChangeSource::ChangeType::kModified,
        raw_change_info.modified_path);
  }

  std::optional<storage::FileSystemURL> moved_from_url =
      is_move_event ? std::make_optional(
                          ToFileSystemURL(*manager()->context(), changed_url,
                                          change_info.moved_from_path.value()))
                    : std::nullopt;

  const std::optional<std::list<Observation::Change>> changes_or_error =
      error ? std::nullopt
            : std::make_optional(
                  std::list<Observation::Change>({{changed_url, change_info}}));
  for (auto& observation : observations_) {
    // TODO(crbug.com/321980367): Currently, sharing partially overlapping
    // observations are not supported, hence checking for the exact scope
    // matching on Local FS.
    if (scope.GetWatchType() != WatchType::kAllBucketFileSystems &&
        observation.scope() != scope) {
      continue;
    }

    // On both Local and Bucket File Systems, errors shouldn't be sent to
    // observations based on their scope but based on the source the
    // observations are tied to.
    if (error) {
      observation.NotifyOfChanges(
          changes_or_error, base::PassKey<FileSystemAccessWatcherManager>());
      continue;
    }
    bool modified_url_in_scope = observation.scope().Contains(changed_url);
    bool moved_from_url_in_scope =
        is_move_event && observation.scope().Contains(moved_from_url.value());

    if (!modified_url_in_scope && !moved_from_url_in_scope) {
      continue;
    }

    if (is_move_event) {
      if (!moved_from_url_in_scope) {
        // If a file/dir is moved into the scope, the change should be reported
        // as ChangeType::kCreated.
        FileSystemAccessChangeSource::ChangeInfo updated_change_info(
            change_info.file_path_type,
            FileSystemAccessChangeSource::ChangeType::kCreated,
            change_info.modified_path);
        observation.NotifyOfChanges(
            std::list<Observation::Change>(
                {{changed_url, std::move(updated_change_info)}}),
            base::PassKey<FileSystemAccessWatcherManager>());
        continue;
      }
      if (!modified_url_in_scope) {
        // If a file/dir is moved out of the scope, the change should be
        // reported as ChangeType::kDeleted.
        FileSystemAccessChangeSource::ChangeInfo updated_change_info(
            change_info.file_path_type,
            FileSystemAccessChangeSource::ChangeType::kDeleted,
            change_info.moved_from_path.value());
        observation.NotifyOfChanges(
            std::list<Observation::Change>(
                {{moved_from_url.value(), std::move(updated_change_info)}}),
            base::PassKey<FileSystemAccessWatcherManager>());
        continue;
      }
    }

    // The default case, including move within scope, should notify the changes
    // as is.
    observation.NotifyOfChanges(
        changes_or_error, base::PassKey<FileSystemAccessWatcherManager>());
  }
}

void FileSystemAccessWatcherManager::OnSourceBeingDestroyed(
    FileSystemAccessChangeSource* source) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  source_observations_.RemoveObservation(source);
  size_t count_removed = std::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_.emplace_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(crbug.com/40105284): 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 raw_ref<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(crbug.com/40283894): Handle overlapping scopes and initializing
  // sources.

  FileSystemAccessChangeSource* raw_change_source = nullptr;
  auto it = base::ranges::find_if(
      all_sources_,
      [&scope](const raw_ref<FileSystemAccessChangeSource> source) {
        return source->scope().GetWatchType() ==
                       WatchType::kAllBucketFileSystems
                   ? source->scope().Contains(scope)
                   : source->scope() == scope;
      });
  if (it != all_sources_.end()) {
    raw_change_source = &it->get();
  } else {
    auto owned_change_source = CreateOwnedSourceForScope(scope);
    if (!owned_change_source) {
      // TODO(crbug.com/40105284): 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(crbug.com/40105284): 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(crbug.com/40283896): 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