V8DatabaseCustom.cpp revision d0825bca7fe65beaee391d30da42e937db621564
1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32
33#if ENABLE(DATABASE)
34#include "V8Database.h"
35
36#include "Database.h"
37#include "V8Binding.h"
38#include "V8CustomBinding.h"
39#include "V8CustomSQLTransactionCallback.h"
40#include "V8CustomSQLTransactionErrorCallback.h"
41#include "V8CustomVoidCallback.h"
42#include "V8Proxy.h"
43
44namespace WebCore {
45
46v8::Handle<v8::Value> V8Database::changeVersionCallback(const v8::Arguments& args)
47{
48    INC_STATS("DOM.Database.changeVersion()");
49
50    if (args.Length() < 2)
51        return throwError("The old and new version strings are required.", V8Proxy::SyntaxError);
52
53    if (!(args[0]->IsString() && args[1]->IsString()))
54        return throwError("The old and new versions must be strings.");
55
56    Database* database = V8Database::toNative(args.Holder());
57
58    Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
59    if (!frame)
60        return v8::Undefined();
61
62    RefPtr<V8CustomSQLTransactionCallback> callback;
63    if (args.Length() > 2) {
64        if (!args[2]->IsObject())
65            return throwError("changeVersion transaction callback must be of valid type.");
66
67        callback = V8CustomSQLTransactionCallback::create(args[2], frame);
68    }
69
70    RefPtr<V8CustomSQLTransactionErrorCallback> errorCallback;
71    if (args.Length() > 3) {
72        if (!args[3]->IsObject())
73            return throwError("changeVersion error callback must be of valid type.");
74
75        errorCallback = V8CustomSQLTransactionErrorCallback::create(args[3], frame);
76    }
77
78    RefPtr<V8CustomVoidCallback> successCallback;
79    if (args.Length() > 4) {
80        if (!args[4]->IsObject())
81            return throwError("changeVersion success callback must be of valid type.");
82
83        successCallback = V8CustomVoidCallback::create(args[4], frame);
84    }
85
86    database->changeVersion(toWebCoreString(args[0]), toWebCoreString(args[1]), callback.release(), errorCallback.release(), successCallback.release());
87
88    return v8::Undefined();
89}
90
91static v8::Handle<v8::Value> createTransaction(const v8::Arguments& args, bool readOnly)
92{
93    if (!args.Length())
94        return throwError("Transaction callback is required.", V8Proxy::SyntaxError);
95
96    if (!args[0]->IsObject())
97        return throwError("Transaction callback must be of valid type.");
98
99    Database* database = V8Database::toNative(args.Holder());
100
101    Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
102    if (!frame)
103        return v8::Undefined();
104
105    RefPtr<V8CustomSQLTransactionCallback> callback = V8CustomSQLTransactionCallback::create(args[0], frame);
106
107    RefPtr<V8CustomSQLTransactionErrorCallback> errorCallback;
108    if (args.Length() > 1) {
109        if (!args[1]->IsObject())
110            return throwError("Transaction error callback must be of valid type.");
111
112        errorCallback = V8CustomSQLTransactionErrorCallback::create(args[1], frame);
113    }
114
115    RefPtr<V8CustomVoidCallback> successCallback;
116    if (args.Length() > 2) {
117        if (!args[2]->IsObject())
118            return throwError("Transaction success callback must be of valid type.");
119
120        successCallback = V8CustomVoidCallback::create(args[2], frame);
121    }
122
123    database->transaction(callback.release(), errorCallback.release(), successCallback.release(), readOnly);
124    return v8::Undefined();
125}
126
127v8::Handle<v8::Value> V8Database::transactionCallback(const v8::Arguments& args)
128{
129    INC_STATS("DOM.Database.transaction()");
130    return createTransaction(args, false);
131}
132
133v8::Handle<v8::Value> V8Database::readTransactionCallback(const v8::Arguments& args)
134{
135    INC_STATS("DOM.Database.readTransaction()");
136    return createTransaction(args, true);
137}
138
139} // namespace WebCore
140
141#endif
142