1 /*
2 * Copyright (C) 2007, 2013 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 "third_party/blink/renderer/modules/webdatabase/sql_statement.h"
30
31 #include "third_party/blink/renderer/bindings/modules/v8/v8_sql_statement_callback.h"
32 #include "third_party/blink/renderer/bindings/modules/v8/v8_sql_statement_error_callback.h"
33 #include "third_party/blink/renderer/core/probe/core_probes.h"
34 #include "third_party/blink/renderer/modules/webdatabase/database.h"
35 #include "third_party/blink/renderer/modules/webdatabase/database_manager.h"
36 #include "third_party/blink/renderer/modules/webdatabase/sql_error.h"
37 #include "third_party/blink/renderer/modules/webdatabase/sql_statement_backend.h"
38 #include "third_party/blink/renderer/modules/webdatabase/sql_transaction.h"
39 #include "third_party/blink/renderer/modules/webdatabase/sqlite/sqlite_database.h"
40 #include "third_party/blink/renderer/modules/webdatabase/sqlite/sqlite_statement.h"
41
42 namespace blink {
43
Trace(Visitor * visitor)44 void SQLStatement::OnSuccessV8Impl::Trace(Visitor* visitor) {
45 visitor->Trace(callback_);
46 OnSuccessCallback::Trace(visitor);
47 }
48
OnSuccess(SQLTransaction * transaction,SQLResultSet * result_set)49 bool SQLStatement::OnSuccessV8Impl::OnSuccess(SQLTransaction* transaction,
50 SQLResultSet* result_set) {
51 v8::TryCatch try_catch(callback_->GetIsolate());
52 try_catch.SetVerbose(true);
53
54 // An exception if any is killed with the v8::TryCatch above and reported
55 // to the global exception handler.
56 return callback_->handleEvent(nullptr, transaction, result_set).IsJust();
57 }
58
Trace(Visitor * visitor)59 void SQLStatement::OnErrorV8Impl::Trace(Visitor* visitor) {
60 visitor->Trace(callback_);
61 OnErrorCallback::Trace(visitor);
62 }
63
OnError(SQLTransaction * transaction,SQLError * error)64 bool SQLStatement::OnErrorV8Impl::OnError(SQLTransaction* transaction,
65 SQLError* error) {
66 v8::TryCatch try_catch(callback_->GetIsolate());
67 try_catch.SetVerbose(true);
68
69 // 4.3.2 Processing model
70 // https://www.w3.org/TR/webdatabase/#sqlstatementcallback
71 // step 6.(In case of error).2. If the error callback returns false, then move
72 // on to the next statement, if any, or onto the next overall step otherwise.
73 // step 6.(In case of error).3. Otherwise, the error callback did not return
74 // false, or there was no error callback. Jump to the last step in the overall
75 // steps.
76 bool return_value;
77 // An exception if any is killed with the v8::TryCatch above and reported
78 // to the global exception handler.
79 if (!callback_->handleEvent(nullptr, transaction, error).To(&return_value)) {
80 return true;
81 }
82 return return_value;
83 }
84
SQLStatement(Database * database,OnSuccessCallback * callback,OnErrorCallback * error_callback)85 SQLStatement::SQLStatement(Database* database,
86 OnSuccessCallback* callback,
87 OnErrorCallback* error_callback)
88 : success_callback_(callback), error_callback_(error_callback) {
89 DCHECK(IsMainThread());
90
91 if (HasCallback() || HasErrorCallback()) {
92 probe::AsyncTaskScheduled(database->GetExecutionContext(), "SQLStatement",
93 &async_task_id_);
94 }
95 }
96
Trace(Visitor * visitor)97 void SQLStatement::Trace(Visitor* visitor) {
98 visitor->Trace(backend_);
99 visitor->Trace(success_callback_);
100 visitor->Trace(error_callback_);
101 }
102
SetBackend(SQLStatementBackend * backend)103 void SQLStatement::SetBackend(SQLStatementBackend* backend) {
104 backend_ = backend;
105 }
106
HasCallback()107 bool SQLStatement::HasCallback() {
108 return success_callback_;
109 }
110
HasErrorCallback()111 bool SQLStatement::HasErrorCallback() {
112 return error_callback_;
113 }
114
PerformCallback(SQLTransaction * transaction)115 bool SQLStatement::PerformCallback(SQLTransaction* transaction) {
116 DCHECK(transaction);
117 DCHECK(backend_);
118
119 bool callback_error = false;
120
121 OnSuccessCallback* callback = success_callback_.Release();
122 OnErrorCallback* error_callback = error_callback_.Release();
123 SQLErrorData* error = backend_->SqlError();
124
125 probe::AsyncTask async_task(transaction->GetDatabase()->GetExecutionContext(),
126 &async_task_id_);
127
128 // Call the appropriate statement callback and track if it resulted in an
129 // error, because then we need to jump to the transaction error callback.
130 if (error) {
131 if (error_callback) {
132 callback_error = error_callback->OnError(
133 transaction, MakeGarbageCollected<SQLError>(*error));
134 }
135 } else if (callback) {
136 callback_error =
137 !callback->OnSuccess(transaction, backend_->SqlResultSet());
138 }
139
140 return callback_error;
141 }
142
143 } // namespace blink
144