summaryrefslogtreecommitdiffstats
path: root/content/browser/in_process_webkit/indexed_db_callbacks.cc
blob: 4f7fe94f38e506c3441c08c717342e8cfa9f0d1d (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
// Copyright (c) 2012 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/browser/in_process_webkit/indexed_db_callbacks.h"

#include <vector>

#include "content/common/indexed_db/indexed_db_messages.h"
#include "webkit/quota/quota_manager.h"

namespace content {

namespace {
const int32 kDatabaseNotAdded = -1;
}

IndexedDBCallbacksBase::IndexedDBCallbacksBase(
    IndexedDBDispatcherHost* dispatcher_host,
    int32 ipc_thread_id,
    int32 ipc_response_id)
    : dispatcher_host_(dispatcher_host),
      ipc_response_id_(ipc_response_id),
      ipc_thread_id_(ipc_thread_id) {
}

IndexedDBCallbacksBase::~IndexedDBCallbacksBase() {}

void IndexedDBCallbacksBase::onError(const WebKit::WebIDBDatabaseError& error) {
  dispatcher_host_->Send(new IndexedDBMsg_CallbacksError(
      ipc_thread_id_, ipc_response_id_, error.code(), error.message()));
}

void IndexedDBCallbacksBase::onBlocked(long long old_version) {
  dispatcher_host_->Send(new IndexedDBMsg_CallbacksIntBlocked(
      ipc_thread_id_, ipc_response_id_, old_version));
}

IndexedDBCallbacksDatabase::IndexedDBCallbacksDatabase(
    IndexedDBDispatcherHost* dispatcher_host,
    int32 ipc_thread_id,
    int32 ipc_response_id,
    int64 host_transaction_id,
    const GURL& origin_url)
    : IndexedDBCallbacksBase(dispatcher_host, ipc_thread_id, ipc_response_id),
      host_transaction_id_(host_transaction_id),
      origin_url_(origin_url),
      ipc_database_id_(kDatabaseNotAdded) {
}

void IndexedDBCallbacksDatabase::onSuccess(
    WebKit::WebIDBDatabase* idb_object,
    const WebKit::WebIDBMetadata& metadata) {
  int32 ipc_object_id = ipc_database_id_;
  if (ipc_object_id == kDatabaseNotAdded) {
    ipc_object_id = dispatcher_host()->Add(idb_object, ipc_thread_id(),
                                           origin_url_);
  } else {
    // We already have this database and don't need a new copy of it.
    delete idb_object;
  }
  IndexedDBDatabaseMetadata idb_metadata =
          IndexedDBDispatcherHost::ConvertMetadata(metadata);

  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessIDBDatabase(ipc_thread_id(),
                                                   ipc_response_id(),
                                                   ipc_object_id,
                                                   idb_metadata));
}

void IndexedDBCallbacksDatabase::onUpgradeNeeded(
    long long old_version,
    WebKit::WebIDBDatabase* database,
    const WebKit::WebIDBMetadata& metadata) {
  dispatcher_host()->RegisterTransactionId(host_transaction_id_, origin_url_);
  int32 ipc_database_id = dispatcher_host()->Add(database, ipc_thread_id(),
                                                 origin_url_);
  ipc_database_id_ = ipc_database_id;
  IndexedDBDatabaseMetadata idb_metadata =
          IndexedDBDispatcherHost::ConvertMetadata(metadata);
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksUpgradeNeeded(
          ipc_thread_id(), ipc_response_id(),
          ipc_database_id,
          old_version,
          idb_metadata));
}

void IndexedDBCallbacks<WebKit::WebIDBCursor>::onSuccess(
    WebKit::WebIDBCursor* idb_cursor,
    const WebKit::WebIDBKey& key,
    const WebKit::WebIDBKey& primaryKey,
    const WebKit::WebSerializedScriptValue& value) {
  int32 ipc_object_id = dispatcher_host()->Add(idb_cursor);
  IndexedDBMsg_CallbacksSuccessIDBCursor_Params params;
  params.ipc_thread_id = ipc_thread_id();
  params.ipc_response_id = ipc_response_id();
  params.ipc_cursor_id = ipc_object_id;
  params.key = IndexedDBKey(key);
  params.primary_key = IndexedDBKey(primaryKey);
  params.serialized_value = SerializedScriptValue(value);
  dispatcher_host()->Send(new IndexedDBMsg_CallbacksSuccessIDBCursor(params));
}

void IndexedDBCallbacks<WebKit::WebIDBCursor>::onSuccess(
    const WebKit::WebSerializedScriptValue& value) {
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessSerializedScriptValue(
          ipc_thread_id(), ipc_response_id(), SerializedScriptValue(value)));
}

void IndexedDBCallbacks<WebKit::WebIDBCursor>::onSuccess(
    const WebKit::WebIDBKey& key,
    const WebKit::WebIDBKey& primaryKey,
    const WebKit::WebSerializedScriptValue& value) {
  DCHECK_NE(ipc_cursor_id_, -1);
  WebKit::WebIDBCursor* idb_cursor = dispatcher_host()->GetCursorFromId(
      ipc_cursor_id_);

  DCHECK(idb_cursor);
  if (!idb_cursor)
    return;
  IndexedDBMsg_CallbacksSuccessCursorContinue_Params params;
  params.ipc_thread_id = ipc_thread_id();
  params.ipc_response_id = ipc_response_id();
  params.ipc_cursor_id = ipc_cursor_id_;
  params.key = IndexedDBKey(key);
  params.primary_key = IndexedDBKey(primaryKey);
  params.serialized_value = SerializedScriptValue(value);
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessCursorContinue(params));
}

void IndexedDBCallbacks<WebKit::WebIDBCursor>::onSuccessWithPrefetch(
    const WebKit::WebVector<WebKit::WebIDBKey>& keys,
    const WebKit::WebVector<WebKit::WebIDBKey>& primaryKeys,
    const WebKit::WebVector<WebKit::WebSerializedScriptValue>& values) {
  DCHECK_NE(ipc_cursor_id_, -1);

  std::vector<IndexedDBKey> msgKeys;
  std::vector<IndexedDBKey> msgPrimaryKeys;
  std::vector<SerializedScriptValue> msgValues;

  for (size_t i = 0; i < keys.size(); ++i) {
    msgKeys.push_back(IndexedDBKey(keys[i]));
    msgPrimaryKeys.push_back(IndexedDBKey(primaryKeys[i]));
    msgValues.push_back(SerializedScriptValue(values[i]));
  }

  IndexedDBMsg_CallbacksSuccessCursorPrefetch_Params params;
  params.ipc_thread_id = ipc_thread_id();
  params.ipc_response_id = ipc_response_id();
  params.ipc_cursor_id = ipc_cursor_id_;
  params.keys = msgKeys;
  params.primary_keys = msgPrimaryKeys;
  params.values = msgValues;
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessCursorPrefetch(params));
}

void IndexedDBCallbacks<WebKit::WebIDBKey>::onSuccess(
    const WebKit::WebIDBKey& value) {
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessIndexedDBKey(
          ipc_thread_id(), ipc_response_id(), IndexedDBKey(value)));
}

void IndexedDBCallbacks<WebKit::WebDOMStringList>::onSuccess(
    const WebKit::WebDOMStringList& value) {

  std::vector<string16> list;
  for (unsigned i = 0; i < value.length(); ++i)
      list.push_back(value.item(i));

  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessStringList(
          ipc_thread_id(), ipc_response_id(), list));
}

void IndexedDBCallbacks<WebKit::WebSerializedScriptValue>::onSuccess(
    const WebKit::WebSerializedScriptValue& value) {
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessSerializedScriptValue(
          ipc_thread_id(), ipc_response_id(), SerializedScriptValue(value)));
}

void IndexedDBCallbacks<WebKit::WebSerializedScriptValue>::onSuccess(
    const WebKit::WebSerializedScriptValue& value,
    const WebKit::WebIDBKey& primaryKey,
    const WebKit::WebIDBKeyPath& keyPath) {
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessSerializedScriptValueWithKey(
          ipc_thread_id(), ipc_response_id(), SerializedScriptValue(value),
          IndexedDBKey(primaryKey), IndexedDBKeyPath(keyPath)));
}

void IndexedDBCallbacks<WebKit::WebSerializedScriptValue>::onSuccess(
    long long value) {
    dispatcher_host()->Send(
        new IndexedDBMsg_CallbacksSuccessInteger(ipc_thread_id(),
                                                 ipc_response_id(),
                                                 value));
}

void IndexedDBCallbacks<WebKit::WebSerializedScriptValue>::onSuccess() {
    dispatcher_host()->Send(
        new IndexedDBMsg_CallbacksSuccessUndefined(ipc_thread_id(),
                                                   ipc_response_id()));
}

void IndexedDBCallbacks<WebKit::WebSerializedScriptValue>::onSuccess(
    const WebKit::WebIDBKey& value) {
  dispatcher_host()->Send(
      new IndexedDBMsg_CallbacksSuccessIndexedDBKey(
          ipc_thread_id(), ipc_response_id(), IndexedDBKey(value)));
}

}  // namespace content