1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef STORAGE_BROWSER_FILE_SYSTEM_QUOTA_QUOTA_BACKEND_IMPL_H_
6 #define STORAGE_BROWSER_FILE_SYSTEM_QUOTA_QUOTA_BACKEND_IMPL_H_
7 
8 #include <stdint.h>
9 
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "storage/browser/file_system/quota/quota_reservation_manager.h"
15 #include "storage/browser/file_system/sandbox_file_system_backend_delegate.h"
16 #include "third_party/blink/public/mojom/quota/quota_types.mojom.h"
17 #include "url/origin.h"
18 
19 namespace base {
20 class SequencedTaskRunner;
21 }
22 
23 namespace storage {
24 
25 class FileSystemUsageCache;
26 class ObfuscatedFileUtil;
27 class QuotaManagerProxy;
28 
29 // An instance of this class is owned by QuotaReservationManager.
COMPONENT_EXPORT(STORAGE_BROWSER)30 class COMPONENT_EXPORT(STORAGE_BROWSER) QuotaBackendImpl
31     : public QuotaReservationManager::QuotaBackend {
32  public:
33   using ReserveQuotaCallback = QuotaReservationManager::ReserveQuotaCallback;
34 
35   QuotaBackendImpl(base::SequencedTaskRunner* file_task_runner,
36                    ObfuscatedFileUtil* obfuscated_file_util,
37                    FileSystemUsageCache* file_system_usage_cache,
38                    QuotaManagerProxy* quota_manager_proxy);
39   ~QuotaBackendImpl() override;
40 
41   // QuotaReservationManager::QuotaBackend overrides.
42   void ReserveQuota(const url::Origin& origin,
43                     FileSystemType type,
44                     int64_t delta,
45                     ReserveQuotaCallback callback) override;
46   void ReleaseReservedQuota(const url::Origin& origin,
47                             FileSystemType type,
48                             int64_t size) override;
49   void CommitQuotaUsage(const url::Origin& origin,
50                         FileSystemType type,
51                         int64_t delta) override;
52   void IncrementDirtyCount(const url::Origin& origin,
53                            FileSystemType type) override;
54   void DecrementDirtyCount(const url::Origin& origin,
55                            FileSystemType type) override;
56 
57  private:
58   friend class QuotaBackendImplTest;
59 
60   struct QuotaReservationInfo {
61     QuotaReservationInfo(const url::Origin& origin,
62                          FileSystemType type,
63                          int64_t delta);
64     ~QuotaReservationInfo();
65 
66     url::Origin origin;
67     FileSystemType type;
68     int64_t delta;
69   };
70 
71   void DidGetUsageAndQuotaForReserveQuota(const QuotaReservationInfo& info,
72                                           ReserveQuotaCallback callback,
73                                           blink::mojom::QuotaStatusCode status,
74                                           int64_t usage,
75                                           int64_t quota);
76 
77   void ReserveQuotaInternal(const QuotaReservationInfo& info);
78   base::File::Error GetUsageCachePath(const url::Origin& origin,
79                                       FileSystemType type,
80                                       base::FilePath* usage_file_path);
81 
82   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
83 
84   // Owned by SandboxFileSystemBackendDelegate.
85   ObfuscatedFileUtil* obfuscated_file_util_;
86   FileSystemUsageCache* file_system_usage_cache_;
87 
88   scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;
89 
90   base::WeakPtrFactory<QuotaBackendImpl> weak_ptr_factory_{this};
91 
92   DISALLOW_COPY_AND_ASSIGN(QuotaBackendImpl);
93 };
94 
95 }  // namespace storage
96 
97 #endif  // STORAGE_BROWSER_FILE_SYSTEM_QUOTA_QUOTA_BACKEND_IMPL_H_
98