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
|
// Copyright 2016 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 "third_party/blink/renderer/modules/indexeddb/idb_observer.h"
#include <bitset>
#include "third_party/blink/renderer/bindings/modules/v8/to_v8_for_modules.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_binding_for_modules.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_idb_observer_callback.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_idb_observer_init.h"
#include "third_party/blink/renderer/modules/indexed_db_names.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_database.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_observer_changes.h"
#include "third_party/blink/renderer/modules/indexeddb/idb_transaction.h"
#include "third_party/blink/renderer/platform/bindings/enumeration_base.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"
namespace blink {
IDBObserver* IDBObserver::Create(V8IDBObserverCallback* callback) {
return MakeGarbageCollected<IDBObserver>(callback);
}
IDBObserver::IDBObserver(V8IDBObserverCallback* callback)
: callback_(callback) {}
void IDBObserver::observe(IDBDatabase* database,
IDBTransaction* transaction,
const IDBObserverInit* options,
ExceptionState& exception_state) {
if (!transaction->IsActive()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kTransactionInactiveError,
transaction->InactiveErrorMessage());
return;
}
if (transaction->IsVersionChange()) {
exception_state.ThrowDOMException(
DOMExceptionCode::kTransactionInactiveError,
IDBDatabase::kCannotObserveVersionChangeTransaction);
return;
}
if (!database->Backend()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
IDBDatabase::kDatabaseClosedErrorMessage);
return;
}
if (!options->hasOperationTypes()) {
exception_state.ThrowTypeError(
"operationTypes not specified in observe options->");
return;
}
if (options->operationTypes().IsEmpty()) {
exception_state.ThrowTypeError("operationTypes must be populated.");
return;
}
std::bitset<kIDBOperationTypeCount> types;
for (const auto& operation_type : options->operationTypes()) {
if (operation_type == indexed_db_names::kAdd) {
types[static_cast<size_t>(mojom::IDBOperationType::Add)] = true;
} else if (operation_type == indexed_db_names::kPut) {
types[static_cast<size_t>(mojom::IDBOperationType::Put)] = true;
} else if (operation_type == indexed_db_names::kDelete) {
types[static_cast<size_t>(mojom::IDBOperationType::Delete)] = true;
} else if (operation_type == indexed_db_names::kClear) {
types[static_cast<size_t>(mojom::IDBOperationType::Clear)] = true;
} else {
exception_state.ThrowTypeError(
"Unknown operation type in observe options: " +
IDLEnumAsString(operation_type));
return;
}
}
int32_t observer_id =
database->AddObserver(this, transaction->Id(), options->transaction(),
options->noRecords(), options->values(), types);
observer_ids_.insert(observer_id, database);
}
void IDBObserver::unobserve(IDBDatabase* database,
ExceptionState& exception_state) {
if (!database->Backend()) {
exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
IDBDatabase::kDatabaseClosedErrorMessage);
return;
}
Vector<int32_t> observer_ids_to_remove;
for (const auto& it : observer_ids_) {
if (it.value == database)
observer_ids_to_remove.push_back(it.key);
}
observer_ids_.RemoveAll(observer_ids_to_remove);
if (!observer_ids_to_remove.IsEmpty())
database->RemoveObservers(observer_ids_to_remove);
}
void IDBObserver::Trace(Visitor* visitor) const {
visitor->Trace(callback_);
visitor->Trace(observer_ids_);
ScriptWrappable::Trace(visitor);
}
} // namespace blink
|