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

content / browser / file_system_access / file_system_access_file_modification_host_impl.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_FILE_MODIFICATION_HOST_IMPL_H_
#define CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_FILE_MODIFICATION_HOST_IMPL_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/thread_annotations.h"
#include "base/types/pass_key.h"
#include "content/browser/file_system_access/file_system_access_manager_impl.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "storage/browser/file_system/file_system_context.h"
#include "storage/browser/file_system/file_system_url.h"
#include "third_party/blink/public/mojom/file_system_access/file_system_access_file_modification_host.mojom.h"

namespace storage {
class QuotaManagerProxy;
}

namespace content {

class FileSystemAccessFileModificationHostImplTest;

// This is the browser side implementation of the
// FileSystemAccessFileModificationHost mojom interface. Instances of this
// class are owned by the FileSystemAccessHandleHost instance passed in to the
// constructor.
class CONTENT_EXPORT FileSystemAccessFileModificationHostImpl
    : public blink::mojom::FileSystemAccessFileModificationHost {
 public:
  // Creates a FileSystemAccessFileModificationHost that manages capacity
  // reservations for the file. FileModificationHosts should only be created
  // via the FileSystemAccessHandleHost.
  FileSystemAccessFileModificationHostImpl(
      FileSystemAccessManagerImpl* manager,
      const storage::FileSystemURL& url,
      base::PassKey<FileSystemAccessAccessHandleHostImpl> pass_key,
      mojo::PendingReceiver<blink::mojom::FileSystemAccessFileModificationHost>
          receiver,
      int64_t file_size);

  // Constructor for testing
  FileSystemAccessFileModificationHostImpl(
      FileSystemAccessManagerImpl* manager,
      const storage::FileSystemURL& url,
      base::PassKey<FileSystemAccessFileModificationHostImplTest> pass_key,
      mojo::PendingReceiver<blink::mojom::FileSystemAccessFileModificationHost>
          receiver,
      int64_t file_size);

  FileSystemAccessFileModificationHostImpl(
      const FileSystemAccessFileModificationHostImpl&) = delete;
  FileSystemAccessFileModificationHostImpl& operator=(
      const FileSystemAccessFileModificationHostImpl&) = delete;

  ~FileSystemAccessFileModificationHostImpl() override;

  // blink::mojom::FileSystemAccessFileModificationHost:
  void RequestCapacityChange(int64_t capacity_delta,
                             RequestCapacityChangeCallback callback) override;
  void OnContentsModified() override;

  // Returns the the total space allocated for the file whose capacity is
  // managed through this host. Initially, this is the file's size.
  int64_t granted_capacity() const {
    DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
    return granted_capacity_;
  }

 private:
  const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy() const {
    return manager_->context()->quota_manager_proxy();
  }

  // Called when the receiver is disconnected.
  void OnReceiverDisconnect();

  void DidGetUsageAndQuota(int64_t capacity_delta,
                           RequestCapacityChangeCallback callback,
                           blink::mojom::QuotaStatusCode status,
                           int64_t usage,
                           int64_t quota);

  SEQUENCE_CHECKER(sequence_checker_);

  // Raw pointer use is safe, since the manager owns the
  // FileSystemAccessAccessHandleHostImpl which owns this class.
  const raw_ptr<FileSystemAccessManagerImpl> manager_ = nullptr;

  // URL of the file whose capacity is managed through this host.
  const storage::FileSystemURL url_;

  mojo::Receiver<blink::mojom::FileSystemAccessFileModificationHost> receiver_
      GUARDED_BY_CONTEXT(sequence_checker_);

  // Total capacity granted to the file managed through this host. Initially,
  // this is the file's size. Later, this value is modified through mojo calls
  // reaching `RequestCapacityChange()`.
  int64_t granted_capacity_ GUARDED_BY_CONTEXT(sequence_checker_);

  base::WeakPtrFactory<FileSystemAccessFileModificationHostImpl> weak_factory_
      GUARDED_BY_CONTEXT(sequence_checker_){this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_FILE_SYSTEM_ACCESS_FILE_SYSTEM_ACCESS_FILE_MODIFICATION_HOST_IMPL_H_