1 /*
2 * Copyright (C) 2007 Apple 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 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "config.h"
30 #include "JSDatabase.h"
31
32 #if ENABLE(DATABASE)
33
34 #include "DOMWindow.h"
35 #include "Database.h"
36 #include "Document.h"
37 #include "ExceptionCode.h"
38 #include "JSCustomSQLTransactionCallback.h"
39 #include "JSCustomSQLTransactionErrorCallback.h"
40 #include "JSCustomVoidCallback.h"
41 #include "JSDOMWindowCustom.h"
42 #include "PlatformString.h"
43 #include "SQLValue.h"
44 #include <runtime/JSArray.h>
45
46 namespace WebCore {
47
48 using namespace JSC;
49
changeVersion(ExecState * exec,const ArgList & args)50 JSValue JSDatabase::changeVersion(ExecState* exec, const ArgList& args)
51 {
52 String oldVersion = args.at(0).toString(exec);
53 String newVersion = args.at(1).toString(exec);
54
55 JSObject* object;
56 if (!(object = args.at(2).getObject())) {
57 setDOMException(exec, TYPE_MISMATCH_ERR);
58 return jsUndefined();
59 }
60
61 RefPtr<SQLTransactionCallback> callback(JSCustomSQLTransactionCallback::create(object, static_cast<JSDOMGlobalObject*>(exec->dynamicGlobalObject())));
62
63 RefPtr<SQLTransactionErrorCallback> errorCallback;
64 if (!args.at(3).isNull()) {
65 if (!(object = args.at(3).getObject())) {
66 setDOMException(exec, TYPE_MISMATCH_ERR);
67 return jsUndefined();
68 }
69
70 errorCallback = JSCustomSQLTransactionErrorCallback::create(object, static_cast<JSDOMGlobalObject*>(exec->dynamicGlobalObject()));
71 }
72
73 RefPtr<VoidCallback> successCallback;
74 if (!args.at(4).isNull()) {
75 if (!(object = args.at(4).getObject())) {
76 setDOMException(exec, TYPE_MISMATCH_ERR);
77 return jsUndefined();
78 }
79
80 successCallback = JSCustomVoidCallback::create(object, static_cast<JSDOMGlobalObject*>(exec->dynamicGlobalObject()));
81 }
82
83 m_impl->changeVersion(oldVersion, newVersion, callback.release(), errorCallback.release(), successCallback.release());
84
85 return jsUndefined();
86 }
87
createTransaction(ExecState * exec,const ArgList & args,Database * database,JSDOMGlobalObject * globalObject,bool readOnly)88 static JSValue createTransaction(ExecState* exec, const ArgList& args, Database* database, JSDOMGlobalObject* globalObject, bool readOnly)
89 {
90 JSObject* object;
91
92 if (!(object = args.at(0).getObject())) {
93 setDOMException(exec, TYPE_MISMATCH_ERR);
94 return jsUndefined();
95 }
96
97 RefPtr<SQLTransactionCallback> callback(JSCustomSQLTransactionCallback::create(object, globalObject));
98 RefPtr<SQLTransactionErrorCallback> errorCallback;
99
100 if (args.size() > 1 && !args.at(1).isNull()) {
101 if (!(object = args.at(1).getObject())) {
102 setDOMException(exec, TYPE_MISMATCH_ERR);
103 return jsUndefined();
104 }
105
106 errorCallback = JSCustomSQLTransactionErrorCallback::create(object, globalObject);
107 }
108
109 RefPtr<VoidCallback> successCallback;
110 if (args.size() > 2 && !args.at(2).isNull()) {
111 if (!(object = args.at(2).getObject())) {
112 setDOMException(exec, TYPE_MISMATCH_ERR);
113 return jsUndefined();
114 }
115
116 successCallback = JSCustomVoidCallback::create(object, globalObject);
117 }
118
119 database->transaction(callback.release(), errorCallback.release(), successCallback.release(), readOnly);
120 return jsUndefined();
121 }
122
transaction(ExecState * exec,const ArgList & args)123 JSValue JSDatabase::transaction(ExecState* exec, const ArgList& args)
124 {
125 return createTransaction(exec, args, m_impl.get(), static_cast<JSDOMGlobalObject*>(exec->dynamicGlobalObject()), false);
126 }
127
readTransaction(ExecState * exec,const ArgList & args)128 JSValue JSDatabase::readTransaction(ExecState* exec, const ArgList& args)
129 {
130 return createTransaction(exec, args, m_impl.get(), static_cast<JSDOMGlobalObject*>(exec->dynamicGlobalObject()), true);
131 }
132
133 }
134
135 #endif // ENABLE(DATABASE)
136