1/*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. AND ITS CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC.
20 * OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include "config.h"
30#include "modules/indexeddb/WebIDBCallbacksImpl.h"
31
32#include "core/dom/DOMError.h"
33#include "core/inspector/InspectorInstrumentation.h"
34#include "modules/indexeddb/IDBMetadata.h"
35#include "modules/indexeddb/IDBRequest.h"
36#include "platform/SharedBuffer.h"
37#include "public/platform/WebBlobInfo.h"
38#include "public/platform/WebData.h"
39#include "public/platform/WebIDBCursor.h"
40#include "public/platform/WebIDBDatabase.h"
41#include "public/platform/WebIDBDatabaseError.h"
42#include "public/platform/WebIDBKey.h"
43
44using blink::WebBlobInfo;
45using blink::WebData;
46using blink::WebIDBCursor;
47using blink::WebIDBDatabase;
48using blink::WebIDBDatabaseError;
49using blink::WebIDBIndex;
50using blink::WebIDBKey;
51using blink::WebIDBKeyPath;
52using blink::WebIDBMetadata;
53using blink::WebVector;
54
55namespace blink {
56
57// static
58PassOwnPtr<WebIDBCallbacksImpl> WebIDBCallbacksImpl::create(IDBRequest* request)
59{
60    return adoptPtr(new WebIDBCallbacksImpl(request));
61}
62
63WebIDBCallbacksImpl::WebIDBCallbacksImpl(IDBRequest* request)
64    : m_request(request)
65{
66    m_asyncOperationId = InspectorInstrumentation::traceAsyncOperationStarting(m_request->executionContext(), "IndexedDB");
67}
68
69WebIDBCallbacksImpl::~WebIDBCallbacksImpl()
70{
71    InspectorInstrumentation::traceAsyncOperationCompleted(m_request->executionContext(), m_asyncOperationId);
72}
73
74static PassOwnPtr<Vector<WebBlobInfo> > ConvertBlobInfo(const WebVector<WebBlobInfo>& webBlobInfo)
75{
76    OwnPtr<Vector<WebBlobInfo> > blobInfo = adoptPtr(new Vector<WebBlobInfo>(webBlobInfo.size()));
77    for (size_t i = 0; i < webBlobInfo.size(); ++i)
78        (*blobInfo)[i] = webBlobInfo[i];
79    return blobInfo.release();
80}
81
82void WebIDBCallbacksImpl::onError(const WebIDBDatabaseError& error)
83{
84    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
85    m_request->onError(error);
86    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
87}
88
89void WebIDBCallbacksImpl::onSuccess(const WebVector<blink::WebString>& webStringList)
90{
91    Vector<String> stringList;
92    for (size_t i = 0; i < webStringList.size(); ++i)
93        stringList.append(webStringList[i]);
94    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
95    m_request->onSuccess(stringList);
96    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
97}
98
99void WebIDBCallbacksImpl::onSuccess(WebIDBCursor* cursor, const WebIDBKey& key, const WebIDBKey& primaryKey, const WebData& value, const WebVector<WebBlobInfo>& webBlobInfo)
100{
101    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
102    m_request->onSuccess(adoptPtr(cursor), key, primaryKey, value, ConvertBlobInfo(webBlobInfo));
103    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
104}
105
106void WebIDBCallbacksImpl::onSuccess(WebIDBDatabase* backend, const WebIDBMetadata& metadata)
107{
108    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
109    m_request->onSuccess(adoptPtr(backend), metadata);
110    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
111}
112
113void WebIDBCallbacksImpl::onSuccess(const WebIDBKey& key)
114{
115    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
116    m_request->onSuccess(key);
117    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
118}
119
120void WebIDBCallbacksImpl::onSuccess(const WebData& value, const WebVector<WebBlobInfo>& webBlobInfo)
121{
122    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
123    m_request->onSuccess(value, ConvertBlobInfo(webBlobInfo));
124    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
125}
126
127void WebIDBCallbacksImpl::onSuccess(const WebData& value, const WebVector<WebBlobInfo>& webBlobInfo, const WebIDBKey& key, const WebIDBKeyPath& keyPath)
128{
129    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
130    m_request->onSuccess(value, ConvertBlobInfo(webBlobInfo), key, keyPath);
131    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
132}
133
134void WebIDBCallbacksImpl::onSuccess(long long value)
135{
136    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
137    m_request->onSuccess(value);
138    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
139}
140
141void WebIDBCallbacksImpl::onSuccess()
142{
143    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
144    m_request->onSuccess();
145    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
146}
147
148void WebIDBCallbacksImpl::onSuccess(const WebIDBKey& key, const WebIDBKey& primaryKey, const WebData& value, const WebVector<WebBlobInfo>& webBlobInfo)
149{
150    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
151    m_request->onSuccess(key, primaryKey, value, ConvertBlobInfo(webBlobInfo));
152    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
153}
154
155void WebIDBCallbacksImpl::onBlocked(long long oldVersion)
156{
157    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
158    m_request->onBlocked(oldVersion);
159    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
160}
161
162void WebIDBCallbacksImpl::onUpgradeNeeded(long long oldVersion, WebIDBDatabase* database, const WebIDBMetadata& metadata, unsigned short dataLoss, blink::WebString dataLossMessage)
163{
164    InspectorInstrumentationCookie cookie = InspectorInstrumentation::traceAsyncCallbackStarting(m_request->executionContext(), m_asyncOperationId);
165    m_request->onUpgradeNeeded(oldVersion, adoptPtr(database), metadata, static_cast<blink::WebIDBDataLoss>(dataLoss), dataLossMessage);
166    InspectorInstrumentation::traceAsyncCallbackCompleted(cookie);
167}
168
169} // namespace blink
170