• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007, 2008 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 "JSCustomSQLTransactionCallback.h"
31 
32 #if ENABLE(DATABASE)
33 
34 #include "Frame.h"
35 #include "ScriptController.h"
36 #include "JSSQLTransaction.h"
37 #include "Page.h"
38 #include <runtime/JSLock.h>
39 #include <wtf/MainThread.h>
40 #include <wtf/RefCountedLeakCounter.h>
41 
42 namespace WebCore {
43 
44 using namespace JSC;
45 
46 #ifndef NDEBUG
47 static WTF::RefCountedLeakCounter counter("JSCustomSQLTransactionCallback");
48 #endif
49 
50 // We have to clean up the data on the main thread for two reasons:
51 //
52 //     1) Can't deref a Frame on a non-main thread.
53 //     2) Unprotecting the JSObject on a non-main thread would register that thread
54 //        for JavaScript garbage collection, which could unnecessarily slow things down.
55 
56 class JSCustomSQLTransactionCallback::Data {
57 public:
Data(JSObject * callback,Frame * frame)58     Data(JSObject* callback, Frame* frame) : m_callback(callback), m_frame(frame) { }
callback()59     JSObject* callback() { return m_callback; }
frame()60     Frame* frame() { return m_frame.get(); }
61 
62 private:
63     ProtectedPtr<JSObject> m_callback;
64     RefPtr<Frame> m_frame;
65 };
66 
JSCustomSQLTransactionCallback(JSObject * callback,Frame * frame)67 JSCustomSQLTransactionCallback::JSCustomSQLTransactionCallback(JSObject* callback, Frame* frame)
68     : m_data(new Data(callback, frame))
69 {
70 #ifndef NDEBUG
71     counter.increment();
72 #endif
73 }
74 
deleteData(void * context)75 void JSCustomSQLTransactionCallback::deleteData(void* context)
76 {
77     delete static_cast<Data*>(context);
78 }
79 
~JSCustomSQLTransactionCallback()80 JSCustomSQLTransactionCallback::~JSCustomSQLTransactionCallback()
81 {
82     callOnMainThread(deleteData, m_data);
83 #ifndef NDEBUG
84     m_data = 0;
85     counter.decrement();
86 #endif
87 }
88 
handleEvent(SQLTransaction * transaction,bool & raisedException)89 void JSCustomSQLTransactionCallback::handleEvent(SQLTransaction* transaction, bool& raisedException)
90 {
91     ASSERT(m_data);
92     ASSERT(m_data->callback());
93     ASSERT(m_data->frame());
94 
95     if (!m_data->frame()->script()->isEnabled())
96         return;
97 
98     // FIXME: This is likely the wrong globalObject (for prototype chains at least)
99     JSGlobalObject* globalObject = m_data->frame()->script()->globalObject();
100     ExecState* exec = globalObject->globalExec();
101 
102     JSC::JSLock lock(SilenceAssertionsOnly);
103 
104     JSValue handleEventFunction = m_data->callback()->get(exec, Identifier(exec, "handleEvent"));
105     CallData handleEventCallData;
106     CallType handleEventCallType = handleEventFunction.getCallData(handleEventCallData);
107     CallData callbackCallData;
108     CallType callbackCallType = CallTypeNone;
109 
110     if (handleEventCallType == CallTypeNone) {
111         callbackCallType = m_data->callback()->getCallData(callbackCallData);
112         if (callbackCallType == CallTypeNone) {
113             // FIXME: Should an exception be thrown here?
114             return;
115         }
116     }
117 
118     RefPtr<JSCustomSQLTransactionCallback> protect(this);
119 
120     MarkedArgumentBuffer args;
121     args.append(toJS(exec, deprecatedGlobalObjectForPrototype(exec), transaction));
122 
123     globalObject->globalData()->timeoutChecker.start();
124     if (handleEventCallType != CallTypeNone)
125         call(exec, handleEventFunction, handleEventCallType, handleEventCallData, m_data->callback(), args);
126     else
127         call(exec, m_data->callback(), callbackCallType, callbackCallData, m_data->callback(), args);
128     globalObject->globalData()->timeoutChecker.stop();
129 
130     if (exec->hadException()) {
131         reportCurrentException(exec);
132 
133         raisedException = true;
134     }
135 
136     Document::updateStyleForAllDocuments();
137 }
138 
139 }
140 
141 #endif // ENABLE(DATABASE)
142