JSCustomSQLTransactionCallback.cpp 5.27 KB
Newer Older
andersca's avatar
andersca committed
1
/*
darin@apple.com's avatar
darin@apple.com committed
2
 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
andersca's avatar
andersca committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1.  Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 * 2.  Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
 *     its contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "JSCustomSQLTransactionCallback.h"

#include "CString.h"
33 34
#include "Console.h"
#include "DOMWindow.h"
andersca's avatar
andersca committed
35
#include "Frame.h"
ggaren@apple.com's avatar
ggaren@apple.com committed
36
#include "Logging.h"
darin@apple.com's avatar
darin@apple.com committed
37
#include "ScriptController.h"
andersca's avatar
andersca committed
38 39
#include "JSSQLTransaction.h"
#include "Page.h"
40
#include <wtf/MainThread.h>
andersca's avatar
andersca committed
41 42 43 44 45

namespace WebCore {
    
using namespace KJS;
    
ggaren@apple.com's avatar
ggaren@apple.com committed
46
#ifndef NDEBUG
darin@apple.com's avatar
darin@apple.com committed
47 48

WTFLogChannel LogWebCoreSQLLeaks = { 0x00000000, "", WTFLogChannelOn };
ggaren@apple.com's avatar
ggaren@apple.com committed
49 50 51 52 53 54 55 56 57

struct JSCustomSQLTransactionCallbackCounter { 
    static int count; 
    ~JSCustomSQLTransactionCallbackCounter() 
    { 
        if (count)
            LOG(WebCoreSQLLeaks, "LEAK: %d JSCustomSQLTransactionCallback\n", count);
    }
};
darin@apple.com's avatar
darin@apple.com committed
58

ggaren@apple.com's avatar
ggaren@apple.com committed
59 60
int JSCustomSQLTransactionCallbackCounter::count = 0;
static JSCustomSQLTransactionCallbackCounter counter;
darin@apple.com's avatar
darin@apple.com committed
61

ggaren@apple.com's avatar
ggaren@apple.com committed
62 63
#endif

64 65 66
// We have to clean up the data on the main thread for two reasons:
//
//     1) Can't deref a Frame on a non-main thread.
67 68
//     2) Unprotecting the JSObject on a non-main thread would register that thread
//        for JavaScript garbage collection, which could unnecessarily slow things down.
69 70 71 72 73 74 75 76 77 78 79 80

class JSCustomSQLTransactionCallback::Data {
public:
    Data(JSObject* callback, Frame* frame) : m_callback(callback), m_frame(frame) { }
    JSObject* callback() { return m_callback; }
    Frame* frame() { return m_frame.get(); }

private:
    ProtectedPtr<JSObject> m_callback;
    RefPtr<Frame> m_frame;
};

andersca's avatar
andersca committed
81
JSCustomSQLTransactionCallback::JSCustomSQLTransactionCallback(JSObject* callback, Frame* frame)
82
    : m_data(new Data(callback, frame))
andersca's avatar
andersca committed
83
{
ggaren@apple.com's avatar
ggaren@apple.com committed
84 85 86
#ifndef NDEBUG
    ++JSCustomSQLTransactionCallbackCounter::count;
#endif
andersca's avatar
andersca committed
87
}
darin@apple.com's avatar
darin@apple.com committed
88

89
void JSCustomSQLTransactionCallback::deleteData(void* context)
darin@apple.com's avatar
darin@apple.com committed
90
{
91
    delete static_cast<Data*>(context);
darin@apple.com's avatar
darin@apple.com committed
92 93
}

ggaren@apple.com's avatar
ggaren@apple.com committed
94 95
JSCustomSQLTransactionCallback::~JSCustomSQLTransactionCallback()
{
96
    callOnMainThread(deleteData, m_data);
ggaren@apple.com's avatar
ggaren@apple.com committed
97
#ifndef NDEBUG
98
    m_data = 0;
ggaren@apple.com's avatar
ggaren@apple.com committed
99 100 101 102
    --JSCustomSQLTransactionCallbackCounter::count;
#endif
}

andersca's avatar
andersca committed
103 104
void JSCustomSQLTransactionCallback::handleEvent(SQLTransaction* transaction, bool& raisedException)
{
105 106 107 108
    ASSERT(m_data);
    ASSERT(m_data->callback());
    ASSERT(m_data->frame());

darin@apple.com's avatar
darin@apple.com committed
109
    if (!m_data->frame()->script()->isEnabled())
andersca's avatar
andersca committed
110 111
        return;
        
darin@apple.com's avatar
darin@apple.com committed
112
    JSGlobalObject* globalObject = m_data->frame()->script()->globalObject();
ggaren@apple.com's avatar
ggaren@apple.com committed
113
    ExecState* exec = globalObject->globalExec();
andersca's avatar
andersca committed
114 115 116
        
    KJS::JSLock lock;
        
117
    JSValue* handleEventFuncValue = m_data->callback()->get(exec, "handleEvent");
andersca's avatar
andersca committed
118 119 120 121 122 123 124
    JSObject* handleEventFunc = 0;
    if (handleEventFuncValue->isObject()) {
        handleEventFunc = static_cast<JSObject*>(handleEventFuncValue);
        if (!handleEventFunc->implementsCall())
            handleEventFunc = 0;
    }
        
125
    if (!handleEventFunc && !m_data->callback()->implementsCall()) {
andersca's avatar
andersca committed
126 127 128 129 130 131
        // FIXME: Should an exception be thrown here?
        return;
    }
        
    RefPtr<JSCustomSQLTransactionCallback> protect(this);
        
darin@apple.com's avatar
darin@apple.com committed
132
    ArgList args;
andersca's avatar
andersca committed
133 134
    args.append(toJS(exec, transaction));

ggaren@apple.com's avatar
ggaren@apple.com committed
135
    globalObject->startTimeoutCheck();
andersca's avatar
andersca committed
136
    if (handleEventFunc)
ggaren@apple.com's avatar
ggaren@apple.com committed
137
        handleEventFunc->callAsFunction(exec, m_data->callback(), args);
andersca's avatar
andersca committed
138
    else
ggaren@apple.com's avatar
ggaren@apple.com committed
139
        m_data->callback()->callAsFunction(exec, m_data->callback(), args);
ggaren@apple.com's avatar
ggaren@apple.com committed
140
    globalObject->stopTimeoutCheck();
andersca's avatar
andersca committed
141 142 143 144 145 146
        
    if (exec->hadException()) {
        JSObject* exception = exec->exception()->toObject(exec);
        String message = exception->get(exec, exec->propertyNames().message)->toString(exec);
        int lineNumber = exception->get(exec, "line")->toInt32(exec);
        String sourceURL = exception->get(exec, "sourceURL")->toString(exec);
147
        m_data->frame()->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, message, lineNumber, sourceURL);
andersca's avatar
andersca committed
148 149 150 151 152 153 154 155 156
        exec->clearException();
        
        raisedException = true;
    }
        
    Document::updateDocumentsRendering();
}
    
}