Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / indexeddb / IDBOpenDBRequest.cpp
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/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/IDBPendingTransactionMonitor.h"
35 #include "modules/indexeddb/IDBTracing.h"
36 #include "modules/indexeddb/IDBVersionChangeEvent.h"
37
38 using blink::WebIDBDatabase;
39
40 namespace WebCore {
41
42 PassRefPtr<IDBOpenDBRequest> IDBOpenDBRequest::create(ExecutionContext* context, PassRefPtr<IDBDatabaseCallbacks> callbacks, int64_t transactionId, int64_t version)
43 {
44     RefPtr<IDBOpenDBRequest> request(adoptRef(new IDBOpenDBRequest(context, callbacks, transactionId, version)));
45     request->suspendIfNeeded();
46     return request.release();
47 }
48
49 IDBOpenDBRequest::IDBOpenDBRequest(ExecutionContext* context, PassRefPtr<IDBDatabaseCallbacks> callbacks, int64_t transactionId, int64_t version)
50     : IDBRequest(context, IDBAny::createNull(), 0)
51     , m_databaseCallbacks(callbacks)
52     , m_transactionId(transactionId)
53     , m_version(version)
54 {
55     ASSERT(!resultAsAny());
56     ScriptWrappable::init(this);
57 }
58
59 IDBOpenDBRequest::~IDBOpenDBRequest()
60 {
61 }
62
63 const AtomicString& IDBOpenDBRequest::interfaceName() const
64 {
65     return EventTargetNames::IDBOpenDBRequest;
66 }
67
68 void IDBOpenDBRequest::onBlocked(int64_t oldVersion)
69 {
70     IDB_TRACE("IDBOpenDBRequest::onBlocked()");
71     if (!shouldEnqueueEvent())
72         return;
73     Nullable<unsigned long long> newVersionNullable = (m_version == IDBDatabaseMetadata::DefaultIntVersion) ? Nullable<unsigned long long>() : Nullable<unsigned long long>(m_version);
74     enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::blocked, oldVersion, newVersionNullable));
75 }
76
77 void IDBOpenDBRequest::onUpgradeNeeded(int64_t oldVersion, PassOwnPtr<WebIDBDatabase> backend, const IDBDatabaseMetadata& metadata, blink::WebIDBDataLoss dataLoss, String dataLossMessage)
78 {
79     IDB_TRACE("IDBOpenDBRequest::onUpgradeNeeded()");
80     if (m_contextStopped || !executionContext()) {
81         OwnPtr<WebIDBDatabase> db = backend;
82         db->abort(m_transactionId);
83         db->close();
84         return;
85     }
86     if (!shouldEnqueueEvent())
87         return;
88
89     ASSERT(m_databaseCallbacks);
90
91     RefPtr<IDBDatabase> idbDatabase = IDBDatabase::create(executionContext(), backend, m_databaseCallbacks.release());
92     idbDatabase->setMetadata(metadata);
93
94     if (oldVersion == IDBDatabaseMetadata::NoIntVersion) {
95         // This database hasn't had an integer version before.
96         oldVersion = IDBDatabaseMetadata::DefaultIntVersion;
97     }
98     IDBDatabaseMetadata oldMetadata(metadata);
99     oldMetadata.intVersion = oldVersion;
100
101     m_transaction = IDBTransaction::create(executionContext(), m_transactionId, idbDatabase.get(), this, oldMetadata);
102     setResult(IDBAny::create(idbDatabase.release()));
103
104     if (m_version == IDBDatabaseMetadata::NoIntVersion)
105         m_version = 1;
106     enqueueEvent(IDBVersionChangeEvent::create(EventTypeNames::upgradeneeded, oldVersion, m_version, dataLoss, dataLossMessage));
107 }
108
109 void IDBOpenDBRequest::onSuccess(PassOwnPtr<WebIDBDatabase> backend, const IDBDatabaseMetadata& metadata)
110 {
111     IDB_TRACE("IDBOpenDBRequest::onSuccess()");
112     if (m_contextStopped || !executionContext()) {
113         OwnPtr<WebIDBDatabase> db = backend;
114         if (db)
115             db->close();
116         return;
117     }
118     if (!shouldEnqueueEvent())
119         return;
120
121     RefPtr<IDBDatabase> idbDatabase;
122     if (resultAsAny()) {
123         // Previous onUpgradeNeeded call delivered the backend.
124         ASSERT(!backend.get());
125         idbDatabase = resultAsAny()->idbDatabase();
126         ASSERT(idbDatabase);
127         ASSERT(!m_databaseCallbacks);
128     } else {
129         ASSERT(backend.get());
130         ASSERT(m_databaseCallbacks);
131         idbDatabase = IDBDatabase::create(executionContext(), backend, m_databaseCallbacks.release());
132         setResult(IDBAny::create(idbDatabase.get()));
133     }
134     idbDatabase->setMetadata(metadata);
135     enqueueEvent(Event::create(EventTypeNames::success));
136 }
137
138 bool IDBOpenDBRequest::shouldEnqueueEvent() const
139 {
140     if (m_contextStopped || !executionContext())
141         return false;
142     ASSERT(m_readyState == PENDING || m_readyState == DONE);
143     if (m_requestAborted)
144         return false;
145     return true;
146 }
147
148 bool IDBOpenDBRequest::dispatchEvent(PassRefPtr<Event> event)
149 {
150     // If the connection closed between onUpgradeNeeded and the delivery of the "success" event,
151     // an "error" event should be fired instead.
152     if (event->type() == EventTypeNames::success && resultAsAny()->type() == IDBAny::IDBDatabaseType && resultAsAny()->idbDatabase()->isClosePending()) {
153         dequeueEvent(event.get());
154         setResult(nullptr);
155         onError(DOMError::create(AbortError, "The connection was closed."));
156         return false;
157     }
158
159     return IDBRequest::dispatchEvent(event);
160 }
161
162 } // namespace WebCore