1 /*
2 * Copyright (C) 2012 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
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "modules/indexeddb/IDBOpenDBRequest.h"
28
29 #include "bindings/core/v8/Nullable.h"
30 #include "core/dom/ExceptionCode.h"
31 #include "core/dom/ExecutionContext.h"
32 #include "modules/indexeddb/IDBDatabase.h"
33 #include "modules/indexeddb/IDBDatabaseCallbacks.h"
34 #include "modules/indexeddb/IDBTracing.h"
35 #include "modules/indexeddb/IDBVersionChangeEvent.h"
36
37 using blink::WebIDBDatabase;
38
39 namespace blink {
40
create(ScriptState * scriptState,IDBDatabaseCallbacks * callbacks,int64_t transactionId,int64_t version)41 IDBOpenDBRequest* IDBOpenDBRequest::create(ScriptState* scriptState, IDBDatabaseCallbacks* callbacks, int64_t transactionId, int64_t version)
42 {
43 IDBOpenDBRequest* request = adoptRefCountedGarbageCollectedWillBeNoop(new IDBOpenDBRequest(scriptState, callbacks, transactionId, version));
44 request->suspendIfNeeded();
45 return request;
46 }
47
IDBOpenDBRequest(ScriptState * scriptState,IDBDatabaseCallbacks * callbacks,int64_t transactionId,int64_t version)48 IDBOpenDBRequest::IDBOpenDBRequest(ScriptState* scriptState, IDBDatabaseCallbacks* callbacks, int64_t transactionId, int64_t version)
49 : IDBRequest(scriptState, IDBAny::createNull(), 0)
50 , m_databaseCallbacks(callbacks)
51 , m_transactionId(transactionId)
52 , m_version(version)
53 {
54 ASSERT(!resultAsAny());
55 }
56
~IDBOpenDBRequest()57 IDBOpenDBRequest::~IDBOpenDBRequest()
58 {
59 }
60
trace(Visitor * visitor)61 void IDBOpenDBRequest::trace(Visitor* visitor)
62 {
63 visitor->trace(m_databaseCallbacks);
64 IDBRequest::trace(visitor);
65 }
66
interfaceName() const67 const AtomicString& IDBOpenDBRequest::interfaceName() const
68 {
69 return EventTargetNames::IDBOpenDBRequest;
70 }
71
onBlocked(int64_t oldVersion)72 void IDBOpenDBRequest::onBlocked(int64_t oldVersion)
73 {
74 IDB_TRACE("IDBOpenDBRequest::onBlocked()");
75 if (!shouldEnqueueEvent())
76 return;
77 Nullable<unsigned long long> newVersionNullable = (m_version == IDBDatabaseMetadata::DefaultIntVersion) ? Nullable<unsigned long long>() : Nullable<unsigned long long>(m_version);
78 enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::blocked, oldVersion, newVersionNullable));
79 }
80
onUpgradeNeeded(int64_t oldVersion,PassOwnPtr<WebIDBDatabase> backend,const IDBDatabaseMetadata & metadata,WebIDBDataLoss dataLoss,String dataLossMessage)81 void IDBOpenDBRequest::onUpgradeNeeded(int64_t oldVersion, PassOwnPtr<WebIDBDatabase> backend, const IDBDatabaseMetadata& metadata, WebIDBDataLoss dataLoss, String dataLossMessage)
82 {
83 IDB_TRACE("IDBOpenDBRequest::onUpgradeNeeded()");
84 if (m_contextStopped || !executionContext()) {
85 OwnPtr<WebIDBDatabase> db = backend;
86 db->abort(m_transactionId);
87 db->close();
88 return;
89 }
90 if (!shouldEnqueueEvent())
91 return;
92
93 ASSERT(m_databaseCallbacks);
94
95 IDBDatabase* idbDatabase = IDBDatabase::create(executionContext(), backend, m_databaseCallbacks.release());
96 idbDatabase->setMetadata(metadata);
97
98 if (oldVersion == IDBDatabaseMetadata::NoIntVersion) {
99 // This database hasn't had an integer version before.
100 oldVersion = IDBDatabaseMetadata::DefaultIntVersion;
101 }
102 IDBDatabaseMetadata oldMetadata(metadata);
103 oldMetadata.intVersion = oldVersion;
104
105 m_transaction = IDBTransaction::create(scriptState(), m_transactionId, idbDatabase, this, oldMetadata);
106 setResult(IDBAny::create(idbDatabase));
107
108 if (m_version == IDBDatabaseMetadata::NoIntVersion)
109 m_version = 1;
110 enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::upgradeneeded, oldVersion, m_version, dataLoss, dataLossMessage));
111 }
112
onSuccess(PassOwnPtr<WebIDBDatabase> backend,const IDBDatabaseMetadata & metadata)113 void IDBOpenDBRequest::onSuccess(PassOwnPtr<WebIDBDatabase> backend, const IDBDatabaseMetadata& metadata)
114 {
115 IDB_TRACE("IDBOpenDBRequest::onSuccess()");
116 if (m_contextStopped || !executionContext()) {
117 OwnPtr<WebIDBDatabase> db = backend;
118 if (db)
119 db->close();
120 return;
121 }
122 if (!shouldEnqueueEvent())
123 return;
124
125 IDBDatabase* idbDatabase = 0;
126 if (resultAsAny()) {
127 // Previous onUpgradeNeeded call delivered the backend.
128 ASSERT(!backend.get());
129 idbDatabase = resultAsAny()->idbDatabase();
130 ASSERT(idbDatabase);
131 ASSERT(!m_databaseCallbacks);
132 } else {
133 ASSERT(backend.get());
134 ASSERT(m_databaseCallbacks);
135 idbDatabase = IDBDatabase::create(executionContext(), backend, m_databaseCallbacks.release());
136 setResult(IDBAny::create(idbDatabase));
137 }
138 idbDatabase->setMetadata(metadata);
139 enqueueEvent(Event::create(EventTypeNames::success));
140 }
141
onSuccess(int64_t oldVersion)142 void IDBOpenDBRequest::onSuccess(int64_t oldVersion)
143 {
144 IDB_TRACE("IDBOpenDBRequest::onSuccess()");
145 if (!shouldEnqueueEvent())
146 return;
147 if (oldVersion == IDBDatabaseMetadata::NoIntVersion) {
148 // This database hasn't had an integer version before.
149 oldVersion = IDBDatabaseMetadata::DefaultIntVersion;
150 }
151 setResult(IDBAny::createUndefined());
152 enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::success, oldVersion, Nullable<unsigned long long>()));
153 }
154
shouldEnqueueEvent() const155 bool IDBOpenDBRequest::shouldEnqueueEvent() const
156 {
157 if (m_contextStopped || !executionContext())
158 return false;
159 ASSERT(m_readyState == PENDING || m_readyState == DONE);
160 if (m_requestAborted)
161 return false;
162 return true;
163 }
164
dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)165 bool IDBOpenDBRequest::dispatchEvent(PassRefPtrWillBeRawPtr<Event> event)
166 {
167 // If the connection closed between onUpgradeNeeded and the delivery of the "success" event,
168 // an "error" event should be fired instead.
169 if (event->type() == EventTypeNames::success && resultAsAny()->type() == IDBAny::IDBDatabaseType && resultAsAny()->idbDatabase()->isClosePending()) {
170 dequeueEvent(event.get());
171 setResult(0);
172 onError(DOMError::create(AbortError, "The connection was closed."));
173 return false;
174 }
175
176 return IDBRequest::dispatchEvent(event);
177 }
178
179 } // namespace blink
180