File: idb_request_loader.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 (177 lines) | stat: -rw-r--r-- 6,285 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 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/indexeddb/idb_request_loader.h"

#include <algorithm>

#include "base/compiler_specific.h"
#include "base/metrics/histogram_functions.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/renderer/core/dom/dom_exception.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/fileapi/file_reader_loader.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_value.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_value_wrapping.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread_scheduler.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"

namespace blink {

IDBRequestLoader::IDBRequestLoader(
    Vector<std::unique_ptr<IDBValue>>&& values,
    ExecutionContext* execution_context,
    LoadCompleteCallback&& load_complete_callback)
    : values_(std::move(values)),
      execution_context_(execution_context),
      load_complete_callback_(std::move(load_complete_callback)) {
  DCHECK(IDBValueUnwrapper::IsWrapped(values_));
}

IDBRequestLoader::~IDBRequestLoader() {}

void IDBRequestLoader::Start() {
#if DCHECK_IS_ON()
  DCHECK(!started_) << "Start() was already called";
  started_ = true;
#endif  // DCHECK_IS_ON()

  // TODO(pwnall): Start() / StartNextValue() unwrap large values sequentially.
  //               Consider parallelizing. The main issue is that the Blob reads
  //               will have to be throttled somewhere, and the extra complexity
  //               only benefits applications that use getAll().
  current_value_ = values_.begin();
  StartNextValue();
}

Vector<std::unique_ptr<IDBValue>>&& IDBRequestLoader::Cancel() {
#if DCHECK_IS_ON()
  DCHECK(started_) << "Cancel() called on a loader that hasn't been Start()ed";
  DCHECK(!canceled_) << "Cancel() was already called";
  canceled_ = true;

  DCHECK(file_reader_loading_);
  file_reader_loading_ = false;
#endif  // DCHECK_IS_ON()
  if (loader_) {
    loader_->Cancel();
  }
  // We're not expected to unwrap any more values or run the callback.
  load_complete_callback_.Reset();
  execution_context_.Clear();
  return std::move(values_);
}

void IDBRequestLoader::StartNextValue() {
  IDBValueUnwrapper unwrapper;

  while (true) {
    if (current_value_ == values_.end()) {
      OnLoadComplete(FileErrorCode::kOK);
      return;
    }
    if (unwrapper.Parse(current_value_->get())) {
      break;
    }
    UNSAFE_TODO(++current_value_);
  }

  DCHECK(current_value_ != values_.end());

  // The execution context was torn down. The loader will eventually get a
  // Cancel() call. Note that WeakMember sets `execution_context_` to null
  // only when the ExecutionContext is GC-ed, but the context destruction may
  // have happened earlier. Hence, check `IsContextDestroyed()` explicitly.
  if (!execution_context_ || execution_context_->IsContextDestroyed()) {
    return;
  }

  wrapped_data_.reserve(unwrapper.WrapperBlobSize());
#if DCHECK_IS_ON()
  DCHECK(!file_reader_loading_);
  file_reader_loading_ = true;
#endif  // DCHECK_IS_ON()
  loader_ = MakeGarbageCollected<FileReaderLoader>(
      this, execution_context_->GetTaskRunner(TaskType::kDatabaseAccess));
  start_loading_time_ = base::TimeTicks::Now();
  loader_->Start(unwrapper.WrapperBlobHandle());
}

FileErrorCode IDBRequestLoader::DidStartLoading(uint64_t) {
  return FileErrorCode::kOK;
}

FileErrorCode IDBRequestLoader::DidReceiveData(base::span<const uint8_t> data) {
  DCHECK_LE(wrapped_data_.size() + data.size(), wrapped_data_.capacity())
      << "The reader returned more data than we were prepared for";

  wrapped_data_.AppendSpan(base::as_chars(data));
  return FileErrorCode::kOK;
}

void IDBRequestLoader::DidFinishLoading() {
#if DCHECK_IS_ON()
  DCHECK(started_)
      << "FileReaderLoader called DidFinishLoading() before it was Start()ed";
  DCHECK(!canceled_)
      << "FileReaderLoader called DidFinishLoading() after it was Cancel()ed";

  DCHECK(file_reader_loading_);
  file_reader_loading_ = false;
#endif  // DCHECK_IS_ON()

  base::UmaHistogramTimes("IndexedDB.WrappedBlobLoadTime",
                          base::TimeTicks::Now() - start_loading_time_);
  IDBValueUnwrapper::Unwrap(std::move(wrapped_data_), **current_value_);
  UNSAFE_TODO(++current_value_);

  StartNextValue();
}

void IDBRequestLoader::DidFail(FileErrorCode error_code) {
#if DCHECK_IS_ON()
  DCHECK(started_)
      << "FileReaderLoader called DidFail() before it was Start()ed";
  DCHECK(!canceled_)
      << "FileReaderLoader called DidFail() after it was Cancel()ed";

  DCHECK(file_reader_loading_);
  file_reader_loading_ = false;
#endif  // DCHECK_IS_ON()
  OnLoadComplete(error_code);
}

void IDBRequestLoader::OnLoadComplete(FileErrorCode error_code) {
#if DCHECK_IS_ON()
  DCHECK(started_);
  DCHECK(!canceled_);
#endif  // DCHECK_IS_ON()
  base::UmaHistogramEnumeration("IndexedDB.LargeValueReadResult", error_code);
  DOMException* exception = nullptr;
  // Translate the error code from the internal file read operation to an
  // appropriate `DOMException` for the IndexedDB operation.
  switch (error_code) {
    case FileErrorCode::kOK:
      break;
    case FileErrorCode::kNotFoundErr:
      // A file containing IndexedDB data is now missing from the disk. Report
      // this as a `NotReadableError` per the spec.
      exception = MakeGarbageCollected<DOMException>(
          DOMExceptionCode::kNotReadableError,
          "Data lost due to missing file. Affected record should be considered "
          "irrecoverable");
      break;
    default:
      // Report all other errors, including `FileErrorCode::kNotReadableErr`, as
      // `UnknownError` since these are internal, likely transient, errors.
      exception = MakeGarbageCollected<DOMException>(
          DOMExceptionCode::kUnknownError,
          "Failed to read large IndexedDB value");
      break;
  }
  std::move(load_complete_callback_).Run(std::move(values_), exception);
}

}  // namespace blink