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 #include "DOMWindow.h"
33 #include "Database.h"
34 #include "Document.h"
35 #include "ExceptionCode.h"
36 #include "JSCustomSQLTransactionCallback.h"
37 #include "JSCustomSQLTransactionErrorCallback.h"
38 #include "JSCustomVoidCallback.h"
39 #include "JSDOMWindowCustom.h"
40 #include "PlatformString.h"
41 #include "SQLValue.h"
42 #include <runtime/JSArray.h>
43
44 namespace WebCore {
45
46 using namespace JSC;
47
changeVersion(ExecState * exec,const ArgList & args)48 JSValuePtr JSDatabase::changeVersion(ExecState* exec, const ArgList& args)
49 {
50 String oldVersion = args.at(exec, 0).toString(exec);
51 String newVersion = args.at(exec, 1).toString(exec);
52
53 Frame* frame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
54 if (!frame)
55 return jsUndefined();
56
57 JSObject* object;
58 if (!(object = args.at(exec, 2).getObject())) {
59 setDOMException(exec, TYPE_MISMATCH_ERR);
60 return jsUndefined();
61 }
62
63 RefPtr<SQLTransactionCallback> callback(JSCustomSQLTransactionCallback::create(object, frame));
64
65 RefPtr<SQLTransactionErrorCallback> errorCallback;
66 if (!args.at(exec, 3).isNull()) {
67 if (!(object = args.at(exec, 3).getObject())) {
68 setDOMException(exec, TYPE_MISMATCH_ERR);
69 return jsUndefined();
70 }
71
72 errorCallback = JSCustomSQLTransactionErrorCallback::create(object, frame);
73 }
74
75 RefPtr<VoidCallback> successCallback;
76 if (!args.at(exec, 4).isNull()) {
77 successCallback = toVoidCallback(exec, args.at(exec, 4));
78 if (!successCallback) {
79 setDOMException(exec, TYPE_MISMATCH_ERR);
80 return jsUndefined();
81 }
82 }
83
84 m_impl->changeVersion(oldVersion, newVersion, callback.release(), errorCallback.release(), successCallback.release());
85
86 return jsUndefined();
87 }
88
transaction(ExecState * exec,const ArgList & args)89 JSValuePtr JSDatabase::transaction(ExecState* exec, const ArgList& args)
90 {
91 JSObject* object;
92
93 if (!(object = args.at(exec, 0).getObject())) {
94 setDOMException(exec, TYPE_MISMATCH_ERR);
95 return jsUndefined();
96 }
97
98 Frame* frame = asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame();
99 if (!frame)
100 return jsUndefined();
101
102 RefPtr<SQLTransactionCallback> callback(JSCustomSQLTransactionCallback::create(object, frame));
103 RefPtr<SQLTransactionErrorCallback> errorCallback;
104
105 if (args.size() > 1 && !args.at(exec, 1).isNull()) {
106 if (!(object = args.at(exec, 1).getObject())) {
107 setDOMException(exec, TYPE_MISMATCH_ERR);
108 return jsUndefined();
109 }
110
111 errorCallback = JSCustomSQLTransactionErrorCallback::create(object, frame);
112 }
113
114 RefPtr<VoidCallback> successCallback;
115 if (args.size() > 2 && !args.at(exec, 2).isNull()) {
116 successCallback = toVoidCallback(exec, args.at(exec, 2));
117 if (!successCallback) {
118 setDOMException(exec, TYPE_MISMATCH_ERR);
119 return jsUndefined();
120 }
121 }
122
123 m_impl->transaction(callback.release(), errorCallback.release(), successCallback.release());
124
125 return jsUndefined();
126 }
127
128 }
129