summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/indexed_db/webidbcursor_impl.cc
blob: 62811b6fd76d3e86af6887dfe5f4b403177ad669 (plain)
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
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/indexed_db/webidbcursor_impl.h"

#include <stddef.h>

#include <string>
#include <vector>

#include "content/renderer/indexed_db/indexed_db_dispatcher.h"
#include "content/renderer/indexed_db/indexed_db_key_builders.h"
#include "mojo/public/cpp/bindings/strong_associated_binding.h"
#include "third_party/blink/public/platform/modules/indexeddb/web_idb_value.h"

using blink::WebBlobInfo;
using blink::WebData;
using blink::WebIDBCallbacks;
using blink::WebIDBKey;
using blink::WebIDBKeyView;
using blink::WebIDBValue;
using indexed_db::mojom::CallbacksAssociatedPtrInfo;
using indexed_db::mojom::CursorAssociatedPtrInfo;

namespace content {

class WebIDBCursorImpl::IOThreadHelper {
 public:
  explicit IOThreadHelper(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  ~IOThreadHelper();

  void Bind(CursorAssociatedPtrInfo cursor_info);
  void Advance(uint32_t count,
               std::unique_ptr<IndexedDBCallbacksImpl> callbacks);
  void Continue(const IndexedDBKey& key,
                const IndexedDBKey& primary_key,
                std::unique_ptr<IndexedDBCallbacksImpl> callbacks);
  void Prefetch(int32_t count,
                std::unique_ptr<IndexedDBCallbacksImpl> callbacks);
  void PrefetchReset(int32_t used_prefetches, int32_t unused_prefetches);

 private:
  CallbacksAssociatedPtrInfo GetCallbacksProxy(
      std::unique_ptr<IndexedDBCallbacksImpl> callbacks);

  indexed_db::mojom::CursorAssociatedPtr cursor_;
  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

  DISALLOW_COPY_AND_ASSIGN(IOThreadHelper);
};

WebIDBCursorImpl::WebIDBCursorImpl(
    indexed_db::mojom::CursorAssociatedPtrInfo cursor_info,
    int64_t transaction_id,
    scoped_refptr<base::SingleThreadTaskRunner> io_runner,
    scoped_refptr<base::SingleThreadTaskRunner> callback_runner)
    : transaction_id_(transaction_id),
      helper_(new IOThreadHelper(io_runner)),
      io_runner_(std::move(io_runner)),
      callback_runner_(std::move(callback_runner)),
      continue_count_(0),
      used_prefetches_(0),
      pending_onsuccess_callbacks_(0),
      prefetch_amount_(kMinPrefetchAmount),
      weak_factory_(this) {
  IndexedDBDispatcher::ThreadSpecificInstance()->RegisterCursor(this);
  io_runner_->PostTask(FROM_HERE, base::BindOnce(&IOThreadHelper::Bind,
                                                 base::Unretained(helper_),
                                                 std::move(cursor_info)));
}

WebIDBCursorImpl::~WebIDBCursorImpl() {
  // It's not possible for there to be pending callbacks that address this
  // object since inside WebKit, they hold a reference to the object which owns
  // this object. But, if that ever changed, then we'd need to invalidate
  // any such pointers.
  IndexedDBDispatcher::ThreadSpecificInstance()->UnregisterCursor(this);
  io_runner_->DeleteSoon(FROM_HERE, helper_);
}

void WebIDBCursorImpl::Advance(unsigned long count,
                               WebIDBCallbacks* callbacks_ptr) {
  std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);
  if (count <= prefetch_keys_.size()) {
    CachedAdvance(count, callbacks.get());
    return;
  }
  ResetPrefetchCache();

  // Reset all cursor prefetch caches except for this cursor.
  IndexedDBDispatcher::ThreadSpecificInstance()->ResetCursorPrefetchCaches(
      transaction_id_, this);

  auto callbacks_impl = std::make_unique<IndexedDBCallbacksImpl>(
      std::move(callbacks), transaction_id_, weak_factory_.GetWeakPtr(),
      io_runner_, callback_runner_);
  io_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&IOThreadHelper::Advance, base::Unretained(helper_), count,
                     std::move(callbacks_impl)));
}

void WebIDBCursorImpl::Continue(WebIDBKeyView key,
                                WebIDBKeyView primary_key,
                                WebIDBCallbacks* callbacks_ptr) {
  std::unique_ptr<WebIDBCallbacks> callbacks(callbacks_ptr);

  if (key.KeyType() == blink::kWebIDBKeyTypeNull &&
      primary_key.KeyType() == blink::kWebIDBKeyTypeNull) {
    // No key(s), so this would qualify for a prefetch.
    ++continue_count_;

    if (!prefetch_keys_.empty()) {
      // We have a prefetch cache, so serve the result from that.
      CachedContinue(callbacks.get());
      return;
    }

    if (continue_count_ > kPrefetchContinueThreshold) {
      // Request pre-fetch.
      ++pending_onsuccess_callbacks_;

      auto callbacks_impl = std::make_unique<IndexedDBCallbacksImpl>(
          std::move(callbacks), transaction_id_, weak_factory_.GetWeakPtr(),
          io_runner_, callback_runner_);
      io_runner_->PostTask(
          FROM_HERE,
          base::BindOnce(&IOThreadHelper::Prefetch, base::Unretained(helper_),
                         prefetch_amount_, std::move(callbacks_impl)));

      // Increase prefetch_amount_ exponentially.
      prefetch_amount_ *= 2;
      if (prefetch_amount_ > kMaxPrefetchAmount)
        prefetch_amount_ = kMaxPrefetchAmount;

      return;
    }
  } else {
    // Key argument supplied. We couldn't prefetch this.
    ResetPrefetchCache();
  }

  // Reset all cursor prefetch caches except for this cursor.
  IndexedDBDispatcher::ThreadSpecificInstance()->ResetCursorPrefetchCaches(
      transaction_id_, this);

  auto callbacks_impl = std::make_unique<IndexedDBCallbacksImpl>(
      std::move(callbacks), transaction_id_, weak_factory_.GetWeakPtr(),
      io_runner_, callback_runner_);
  io_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&IOThreadHelper::Continue, base::Unretained(helper_),
                     IndexedDBKeyBuilder::Build(key),
                     IndexedDBKeyBuilder::Build(primary_key),
                     std::move(callbacks_impl)));
}

void WebIDBCursorImpl::PostSuccessHandlerCallback() {
  pending_onsuccess_callbacks_--;

  // If the onsuccess callback called continue()/advance() on the cursor
  // again, and that request was served by the prefetch cache, then
  // pending_onsuccess_callbacks_ would be incremented. If not, it means the
  // callback did something else, or nothing at all, in which case we need to
  // reset the cache.

  if (pending_onsuccess_callbacks_ == 0)
    ResetPrefetchCache();
}

void WebIDBCursorImpl::SetPrefetchData(
    const std::vector<IndexedDBKey>& keys,
    const std::vector<IndexedDBKey>& primary_keys,
    std::vector<WebIDBValue> values) {
  prefetch_keys_.assign(keys.begin(), keys.end());
  prefetch_primary_keys_.assign(primary_keys.begin(), primary_keys.end());
  prefetch_values_.assign(std::make_move_iterator(values.begin()),
                          std::make_move_iterator(values.end()));

  used_prefetches_ = 0;
  pending_onsuccess_callbacks_ = 0;
}

void WebIDBCursorImpl::CachedAdvance(unsigned long count,
                                     WebIDBCallbacks* callbacks) {
  DCHECK_GE(prefetch_keys_.size(), count);
  DCHECK_EQ(prefetch_primary_keys_.size(), prefetch_keys_.size());
  DCHECK_EQ(prefetch_values_.size(), prefetch_keys_.size());

  while (count > 1) {
    prefetch_keys_.pop_front();
    prefetch_primary_keys_.pop_front();
    prefetch_values_.pop_front();
    ++used_prefetches_;
    --count;
  }

  CachedContinue(callbacks);
}

void WebIDBCursorImpl::CachedContinue(WebIDBCallbacks* callbacks) {
  DCHECK_GT(prefetch_keys_.size(), 0ul);
  DCHECK_EQ(prefetch_primary_keys_.size(), prefetch_keys_.size());
  DCHECK_EQ(prefetch_values_.size(), prefetch_keys_.size());

  IndexedDBKey key = prefetch_keys_.front();
  IndexedDBKey primary_key = prefetch_primary_keys_.front();
  WebIDBValue value = std::move(prefetch_values_.front());

  prefetch_keys_.pop_front();
  prefetch_primary_keys_.pop_front();
  prefetch_values_.pop_front();
  ++used_prefetches_;

  ++pending_onsuccess_callbacks_;

  if (!continue_count_) {
    // The cache was invalidated by a call to ResetPrefetchCache()
    // after the RequestIDBCursorPrefetch() was made. Now that the
    // initiating continue() call has been satisfied, discard
    // the rest of the cache.
    ResetPrefetchCache();
  }

  callbacks->OnSuccess(WebIDBKeyBuilder::Build(key),
                       WebIDBKeyBuilder::Build(primary_key), std::move(value));
}

void WebIDBCursorImpl::ResetPrefetchCache() {
  continue_count_ = 0;
  prefetch_amount_ = kMinPrefetchAmount;

  if (prefetch_keys_.empty()) {
    // No prefetch cache, so no need to reset the cursor in the back-end.
    return;
  }

  // Reset the back-end cursor.
  io_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(&IOThreadHelper::PrefetchReset, base::Unretained(helper_),
                     used_prefetches_, prefetch_keys_.size()));

  // Reset the prefetch cache.
  prefetch_keys_.clear();
  prefetch_primary_keys_.clear();
  prefetch_values_.clear();

  pending_onsuccess_callbacks_ = 0;
}

WebIDBCursorImpl::IOThreadHelper::IOThreadHelper(
    scoped_refptr<base::SingleThreadTaskRunner> task_runner)
    : task_runner_(std::move(task_runner)) {}

WebIDBCursorImpl::IOThreadHelper::~IOThreadHelper() {}

void WebIDBCursorImpl::IOThreadHelper::Bind(
    CursorAssociatedPtrInfo cursor_info) {
  cursor_.Bind(std::move(cursor_info), task_runner_);
}

void WebIDBCursorImpl::IOThreadHelper::Advance(
    uint32_t count,
    std::unique_ptr<IndexedDBCallbacksImpl> callbacks) {
  cursor_->Advance(count, GetCallbacksProxy(std::move(callbacks)));
}

void WebIDBCursorImpl::IOThreadHelper::Continue(
    const IndexedDBKey& key,
    const IndexedDBKey& primary_key,
    std::unique_ptr<IndexedDBCallbacksImpl> callbacks) {
  cursor_->Continue(key, primary_key, GetCallbacksProxy(std::move(callbacks)));
}

void WebIDBCursorImpl::IOThreadHelper::Prefetch(
    int32_t count,
    std::unique_ptr<IndexedDBCallbacksImpl> callbacks) {
  cursor_->Prefetch(count, GetCallbacksProxy(std::move(callbacks)));
}

void WebIDBCursorImpl::IOThreadHelper::PrefetchReset(
    int32_t used_prefetches,
    int32_t unused_prefetches) {
  cursor_->PrefetchReset(used_prefetches, unused_prefetches);
}

CallbacksAssociatedPtrInfo WebIDBCursorImpl::IOThreadHelper::GetCallbacksProxy(
    std::unique_ptr<IndexedDBCallbacksImpl> callbacks) {
  CallbacksAssociatedPtrInfo ptr_info;
  auto request = mojo::MakeRequest(&ptr_info);
  mojo::MakeStrongAssociatedBinding(std::move(callbacks), std::move(request));
  return ptr_info;
}

}  // namespace content