File: storage_bucket.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (361 lines) | stat: -rw-r--r-- 13,470 bytes parent folder | download | duplicates (9)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/buckets/storage_bucket.h"

#include "base/time/time.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/bindings/core/v8/active_script_wrappable_creation_key.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_storage_bucket_durability.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_storage_estimate.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_storage_usage_details.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/dom/dom_high_res_time_stamp.h"
#include "third_party/blink/renderer/core/fetch/global_fetch.h"
#include "third_party/blink/renderer/core/frame/navigator.h"
#include "third_party/blink/renderer/modules/cache_storage/cache_storage.h"
#include "third_party/blink/renderer/modules/cache_storage/global_cache_storage.h"
#include "third_party/blink/renderer/modules/file_system_access/storage_manager_file_system_access.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_factory.h"
#include "third_party/blink/renderer/modules/locks/lock_manager.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"

namespace blink {

StorageBucket::StorageBucket(
    NavigatorBase* navigator,
    const String& name,
    mojo::PendingRemote<mojom::blink::BucketHost> remote)
    : ExecutionContextClient(navigator->GetExecutionContext()),
      name_(name),
      remote_(GetExecutionContext()),
      navigator_base_(navigator) {
  remote_.Bind(std::move(remote), GetExecutionContext()->GetTaskRunner(
                                      TaskType::kInternalDefault));
}

const String& StorageBucket::name() {
  return name_;
}

ScriptPromise<IDLBoolean> StorageBucket::persist(ScriptState* script_state) {
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLBoolean>>(script_state);
  auto promise = resolver->Promise();

  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return promise;
  }

  remote_->Persist(WTF::BindOnce(&StorageBucket::DidRequestPersist,
                                 WrapPersistent(this),
                                 WrapPersistent(resolver)));
  return promise;
}

ScriptPromise<IDLBoolean> StorageBucket::persisted(ScriptState* script_state) {
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLBoolean>>(script_state);
  auto promise = resolver->Promise();

  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return promise;
  }

  remote_->Persisted(WTF::BindOnce(&StorageBucket::DidGetPersisted,
                                   WrapPersistent(this),
                                   WrapPersistent(resolver)));
  return promise;
}

ScriptPromise<StorageEstimate> StorageBucket::estimate(
    ScriptState* script_state) {
  auto* resolver = MakeGarbageCollected<ScriptPromiseResolver<StorageEstimate>>(
      script_state);
  auto promise = resolver->Promise();

  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return promise;
  }

  remote_->Estimate(WTF::BindOnce(&StorageBucket::DidGetEstimate,
                                  WrapPersistent(this),
                                  WrapPersistent(resolver)));
  return promise;
}

ScriptPromise<V8StorageBucketDurability> StorageBucket::durability(
    ScriptState* script_state) {
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<V8StorageBucketDurability>>(
          script_state);
  auto promise = resolver->Promise();

  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return promise;
  }

  remote_->Durability(WTF::BindOnce(&StorageBucket::DidGetDurability,
                                    WrapPersistent(this),
                                    WrapPersistent(resolver)));
  return promise;
}

ScriptPromise<IDLUndefined> StorageBucket::setExpires(
    ScriptState* script_state,
    const DOMHighResTimeStamp& expires) {
  auto* resolver =
      MakeGarbageCollected<ScriptPromiseResolver<IDLUndefined>>(script_state);
  auto promise = resolver->Promise();

  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return promise;
  }

  remote_->SetExpires(
      base::Time::FromMillisecondsSinceUnixEpoch(expires),
      WTF::BindOnce(&StorageBucket::DidSetExpires, WrapPersistent(this),
                    WrapPersistent(resolver)));
  return promise;
}

ScriptPromise<IDLNullable<IDLDOMHighResTimeStamp>> StorageBucket::expires(
    ScriptState* script_state) {
  auto* resolver = MakeGarbageCollected<
      ScriptPromiseResolver<IDLNullable<IDLDOMHighResTimeStamp>>>(script_state);
  auto promise = resolver->Promise();

  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return promise;
  }

  remote_->Expires(WTF::BindOnce(&StorageBucket::DidGetExpires,
                                 WrapPersistent(this),
                                 WrapPersistent(resolver)));
  return promise;
}

IDBFactory* StorageBucket::indexedDB() {
  if (!idb_factory_) {
    idb_factory_ = MakeGarbageCollected<IDBFactory>(GetExecutionContext());
    mojo::PendingRemote<mojom::blink::IDBFactory> remote_factory;
    remote_->GetIdbFactory(remote_factory.InitWithNewPipeAndPassReceiver());
    idb_factory_->SetRemote(std::move(remote_factory));
  }
  return idb_factory_.Get();
}

LockManager* StorageBucket::locks() {
  if (!lock_manager_) {
    mojo::PendingRemote<mojom::blink::LockManager> lock_manager;
    remote_->GetLockManager(lock_manager.InitWithNewPipeAndPassReceiver());
    lock_manager_ = MakeGarbageCollected<LockManager>(*navigator_base_);
    lock_manager_->SetManager(std::move(lock_manager), GetExecutionContext());
  }
  return lock_manager_.Get();
}

CacheStorage* StorageBucket::caches(ExceptionState& exception_state) {
  if (!caches_ && GlobalCacheStorage::CanCreateCacheStorage(
                      GetExecutionContext(), exception_state)) {
    mojo::PendingRemote<mojom::blink::CacheStorage> cache_storage;
    remote_->GetCaches(cache_storage.InitWithNewPipeAndPassReceiver());
    caches_ = MakeGarbageCollected<CacheStorage>(
        GetExecutionContext(),
        GlobalFetch::ScopedFetcher::From(*navigator_base_),
        std::move(cache_storage));
  }

  return caches_.Get();
}

ScriptPromise<FileSystemDirectoryHandle> StorageBucket::getDirectory(
    ScriptState* script_state,
    ExceptionState& exception_state) {
  return StorageManagerFileSystemAccess::CheckStorageAccessIsAllowed(
      script_state, exception_state,
      WTF::BindOnce(&StorageBucket::GetSandboxedFileSystem,
                    WrapWeakPersistent(this)));
}

void StorageBucket::GetDirectoryForDevTools(
    ExecutionContext* context,
    Vector<String> directory_path_components,
    base::OnceCallback<void(mojom::blink::FileSystemAccessErrorPtr,
                            FileSystemDirectoryHandle*)> callback) {
  StorageManagerFileSystemAccess::CheckStorageAccessIsAllowed(
      context,
      WTF::BindOnce(&StorageBucket::GetSandboxedFileSystemForDevtools,
                    WrapWeakPersistent(this), WrapWeakPersistent(context),
                    std::move(directory_path_components), std::move(callback)));
}

void StorageBucket::Trace(Visitor* visitor) const {
  visitor->Trace(remote_);
  visitor->Trace(idb_factory_);
  visitor->Trace(lock_manager_);
  visitor->Trace(navigator_base_);
  visitor->Trace(caches_);
  ScriptWrappable::Trace(visitor);
  ExecutionContextClient::Trace(visitor);
}

void StorageBucket::DidRequestPersist(
    ScriptPromiseResolver<IDLBoolean>* resolver,
    bool persisted,
    bool success) {
  if (!success) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kUnknownError,
        "Unknown error occurred while requesting persist."));
    return;
  }

  resolver->Resolve(persisted);
}

void StorageBucket::DidGetPersisted(ScriptPromiseResolver<IDLBoolean>* resolver,
                                    bool persisted,
                                    bool success) {
  if (!success) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kUnknownError,
        "Unknown error occurred while getting persisted."));
    return;
  }

  resolver->Resolve(persisted);
}

void StorageBucket::DidGetEstimate(
    ScriptPromiseResolver<StorageEstimate>* resolver,
    int64_t current_usage,
    int64_t current_quota,
    bool success) {
  if (!success) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kUnknownError,
        "Unknown error occurred while getting estimate."));
    return;
  }

  StorageEstimate* estimate = StorageEstimate::Create();
  estimate->setUsage(current_usage);
  estimate->setQuota(current_quota);
  StorageUsageDetails* details = StorageUsageDetails::Create();
  estimate->setUsageDetails(details);
  resolver->Resolve(estimate);
}

void StorageBucket::DidGetDurability(
    ScriptPromiseResolver<V8StorageBucketDurability>* resolver,
    mojom::blink::BucketDurability durability,
    bool success) {
  if (!success) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kUnknownError,
        "Unknown error occurred while getting durability."));
    return;
  }

  if (durability == mojom::blink::BucketDurability::kRelaxed) {
    resolver->Resolve(
        V8StorageBucketDurability(V8StorageBucketDurability::Enum::kRelaxed));
  } else {
    resolver->Resolve(
        V8StorageBucketDurability(V8StorageBucketDurability::Enum::kStrict));
  }
}

void StorageBucket::DidSetExpires(ScriptPromiseResolver<IDLUndefined>* resolver,
                                  bool success) {
  if (success) {
    resolver->Resolve();
  } else {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kUnknownError,
        "Unknown error occurred while setting expires."));
  }
}

void StorageBucket::DidGetExpires(
    ScriptPromiseResolver<IDLNullable<IDLDOMHighResTimeStamp>>* resolver,
    const std::optional<base::Time> expires,
    bool success) {
  if (!success) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kUnknownError,
        "Unknown error occurred while getting expires."));
  } else {
    resolver->Resolve(expires);
  }
}

void StorageBucket::GetSandboxedFileSystem(
    ScriptPromiseResolver<FileSystemDirectoryHandle>* resolver) {
  // The context may be destroyed and the mojo connection unbound. However the
  // object may live on, reject any requests after the context is destroyed.
  if (!remote_.is_bound()) {
    resolver->Reject(MakeGarbageCollected<DOMException>(
        DOMExceptionCode::kInvalidStateError));
    return;
  }

  remote_->GetDirectory(
      WTF::BindOnce(&StorageManagerFileSystemAccess::DidGetSandboxedFileSystem,
                    WrapPersistent(resolver)));
}

void StorageBucket::GetSandboxedFileSystemForDevtools(
    ExecutionContext* context,
    const Vector<String>& directory_path_components,
    base::OnceCallback<void(mojom::blink::FileSystemAccessErrorPtr,
                            FileSystemDirectoryHandle*)> callback,
    mojom::blink::FileSystemAccessErrorPtr result) {
  if (result->status != mojom::blink::FileSystemAccessStatus::kOk) {
    std::move(callback).Run(std::move(result), nullptr);
    return;
  }

  if (!remote_.is_bound()) {
    std::move(callback).Run(
        mojom::blink::FileSystemAccessError::New(
            mojom::blink::FileSystemAccessStatus::kInvalidState,
            base::File::Error::FILE_ERROR_FAILED, "Invalid state Error."),
        nullptr);
    return;
  }

  remote_->GetDirectoryForDevtools(
      directory_path_components,
      WTF::BindOnce(
          &StorageManagerFileSystemAccess::DidGetSandboxedFileSystemForDevtools,
          WrapWeakPersistent(context), std::move(callback)));
}
}  // namespace blink