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 CONTENT_BROWSER_QUOTA_QUOTA_MANAGER_HOST_H_
6 #define CONTENT_BROWSER_QUOTA_QUOTA_MANAGER_HOST_H_
7 
8 #include "base/memory/scoped_refptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/browser/quota/quota_change_dispatcher.h"
11 #include "content/public/browser/quota_permission_context.h"
12 #include "mojo/public/cpp/bindings/pending_receiver.h"
13 #include "storage/browser/quota/quota_manager.h"
14 #include "third_party/blink/public/mojom/quota/quota_manager_host.mojom.h"
15 
16 namespace storage {
17 class QuotaManager;
18 }
19 
20 namespace url {
21 class Origin;
22 }
23 
24 namespace content {
25 class QuotaPermissionContext;
26 
27 // Implements the Quota (Storage) API for a single origin.
28 //
29 // QuotaContext indirectly owns all QuotaManagerHost instances associated with a
30 // StoragePartition. A new instance is created for every incoming mojo
31 // connection from a frame or worker. Instances are destroyed when their
32 // corresponding mojo connections are closed, or when QuotaContext is destroyed.
33 //
34 // This class is thread-hostile and must only be used on the browser's IO
35 // thread. This requirement is a consequence of interacting with
36 // storage::QuotaManager, which must be used from the IO thread. This situation
37 // is likely to change when QuotaManager moves to the Storage Service.
38 class QuotaManagerHost : public blink::mojom::QuotaManagerHost {
39  public:
40   // The owner must guarantee that |quota_manager| and |permission_context|
41   // outlive this instance.
42   QuotaManagerHost(
43       int process_id,
44       int render_frame_id,
45       const url::Origin& origin,
46       storage::QuotaManager* quota_manager,
47       QuotaPermissionContext* permission_context,
48       scoped_refptr<QuotaChangeDispatcher> quota_change_dispatcher);
49 
50   QuotaManagerHost(const QuotaManagerHost&) = delete;
51   QuotaManagerHost& operator=(const QuotaManagerHost&) = delete;
52 
53   ~QuotaManagerHost() override;
54 
55   // blink::mojom::QuotaManagerHost:
56   void AddChangeListener(
57       mojo::PendingRemote<blink::mojom::QuotaChangeListener> mojo_listener,
58       AddChangeListenerCallback callback) override;
59   void QueryStorageUsageAndQuota(
60       blink::mojom::StorageType storage_type,
61       QueryStorageUsageAndQuotaCallback callback) override;
62   void RequestStorageQuota(blink::mojom::StorageType storage_type,
63                            uint64_t requested_size,
64                            RequestStorageQuotaCallback callback) override;
65 
66  private:
67   void DidQueryStorageUsageAndQuota(QueryStorageUsageAndQuotaCallback callback,
68                                     blink::mojom::QuotaStatusCode status,
69                                     int64_t usage,
70                                     int64_t quota,
71                                     blink::mojom::UsageBreakdownPtr);
72   void DidGetPersistentUsageAndQuota(blink::mojom::StorageType storage_type,
73                                      uint64_t requested_quota,
74                                      RequestStorageQuotaCallback callback,
75                                      blink::mojom::QuotaStatusCode status,
76                                      int64_t usage,
77                                      int64_t quota);
78   void DidGetPermissionResponse(
79       uint64_t requested_quota,
80       int64_t current_usage,
81       int64_t current_quota,
82       RequestStorageQuotaCallback callback,
83       QuotaPermissionContext::QuotaPermissionResponse response);
84   void DidSetHostQuota(int64_t current_usage,
85                        RequestStorageQuotaCallback callback,
86                        blink::mojom::QuotaStatusCode status,
87                        int64_t new_quota);
88   void DidGetTemporaryUsageAndQuota(int64_t requested_quota,
89                                     RequestStorageQuotaCallback callback,
90                                     blink::mojom::QuotaStatusCode status,
91                                     int64_t usage,
92                                     int64_t quota);
93 
94   // The ID of the renderer process connected to this host.
95   const int process_id_;
96 
97   // The ID of the RenderFrame connected to this host.
98   //
99   // MSG_ROUTING_NONE if this host is connected to a worker.
100   const int render_frame_id_;
101 
102   // The origin of the frame or worker connected to this host.
103   const url::Origin origin_;
104 
105   // Raw pointer use is safe because the QuotaContext that indirectly owns this
106   // QuotaManagerHost owner holds a reference to the QuotaManager. Therefore
107   // the QuotaManager is guaranteed to outlive this QuotaManagerHost.
108   storage::QuotaManager* const quota_manager_;
109 
110   // Raw pointer use is safe because the QuotaContext that indirectly owns this
111   // QuotaManagerHost owner holds a reference to the QuotaPermissionContext.
112   // Therefore the QuotaPermissionContext is guaranteed to outlive this
113   // QuotaManagerHost.
114   QuotaPermissionContext* const permission_context_;
115 
116   scoped_refptr<QuotaChangeDispatcher> quota_change_dispatcher_;
117 
118   base::WeakPtrFactory<QuotaManagerHost> weak_factory_{this};
119 };
120 
121 }  // namespace content
122 
123 #endif  // CONTENT_BROWSER_QUOTA_QUOTA_MANAGER_HOST_H_
124