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 "JSCustomSQLStatementErrorCallback.h"
31
32 #if ENABLE(DATABASE)
33
34 #include "Frame.h"
35 #include "ScriptController.h"
36 #include "JSSQLError.h"
37 #include "JSSQLTransaction.h"
38 #include <runtime/JSLock.h>
39
40 namespace WebCore {
41
42 using namespace JSC;
43
JSCustomSQLStatementErrorCallback(JSObject * callback,Frame * frame)44 JSCustomSQLStatementErrorCallback::JSCustomSQLStatementErrorCallback(JSObject* callback, Frame* frame)
45 : m_callback(callback)
46 , m_frame(frame)
47 {
48 }
49
handleEvent(SQLTransaction * transaction,SQLError * error)50 bool JSCustomSQLStatementErrorCallback::handleEvent(SQLTransaction* transaction, SQLError* error)
51 {
52 ASSERT(m_callback);
53 ASSERT(m_frame);
54
55 if (!m_frame->script()->isEnabled())
56 return true;
57
58 // FIXME: This is likely the wrong globalObject (for prototype chains at least)
59 JSGlobalObject* globalObject = m_frame->script()->globalObject();
60 ExecState* exec = globalObject->globalExec();
61
62 JSC::JSLock lock(SilenceAssertionsOnly);
63
64 JSValue handleEventFunction = m_callback->get(exec, Identifier(exec, "handleEvent"));
65 CallData handleEventCallData;
66 CallType handleEventCallType = handleEventFunction.getCallData(handleEventCallData);
67 CallData callbackCallData;
68 CallType callbackCallType = CallTypeNone;
69
70 if (handleEventCallType == CallTypeNone) {
71 callbackCallType = m_callback->getCallData(callbackCallData);
72 if (callbackCallType == CallTypeNone) {
73 // FIXME: Should an exception be thrown here?
74 return true;
75 }
76 }
77
78 RefPtr<JSCustomSQLStatementErrorCallback> protect(this);
79
80 MarkedArgumentBuffer args;
81 args.append(toJS(exec, deprecatedGlobalObjectForPrototype(exec), transaction));
82 args.append(toJS(exec, deprecatedGlobalObjectForPrototype(exec), error));
83
84 JSValue result;
85 globalObject->globalData()->timeoutChecker.start();
86 if (handleEventCallType != CallTypeNone)
87 result = call(exec, handleEventFunction, handleEventCallType, handleEventCallData, m_callback, args);
88 else
89 result = call(exec, m_callback, callbackCallType, callbackCallData, m_callback, args);
90 globalObject->globalData()->timeoutChecker.stop();
91
92 if (exec->hadException()) {
93 reportCurrentException(exec);
94
95 // The spec says:
96 // "If the error callback returns false, then move on to the next statement..."
97 // "Otherwise, the error callback did not return false, or there was no error callback"
98 // Therefore an exception and returning true are the same thing - so, return true on an exception
99 return true;
100 }
101
102 Document::updateStyleForAllDocuments();
103
104 return result.toBoolean(exec);
105 }
106
107 }
108
109 #endif // ENABLE(DATABASE)
110