OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/bindings/api_request_handler.h" | 5 #include "extensions/renderer/bindings/api_request_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "content/public/child/v8_value_converter.h" | 11 #include "content/public/child/v8_value_converter.h" |
| 12 #include "extensions/renderer/bindings/exception_handler.h" |
12 #include "gin/converter.h" | 13 #include "gin/converter.h" |
13 #include "gin/data_object_builder.h" | 14 #include "gin/data_object_builder.h" |
14 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 15 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 16 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
16 | 17 |
17 namespace extensions { | 18 namespace extensions { |
18 | 19 |
19 APIRequestHandler::Request::Request() {} | 20 APIRequestHandler::Request::Request() {} |
20 APIRequestHandler::Request::~Request() = default; | 21 APIRequestHandler::Request::~Request() = default; |
21 | 22 |
(...skipping 14 matching lines...) Expand all Loading... |
36 } | 37 } |
37 } | 38 } |
38 | 39 |
39 APIRequestHandler::PendingRequest::~PendingRequest() {} | 40 APIRequestHandler::PendingRequest::~PendingRequest() {} |
40 APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default; | 41 APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default; |
41 APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=( | 42 APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=( |
42 PendingRequest&&) = default; | 43 PendingRequest&&) = default; |
43 | 44 |
44 APIRequestHandler::APIRequestHandler(const SendRequestMethod& send_request, | 45 APIRequestHandler::APIRequestHandler(const SendRequestMethod& send_request, |
45 const CallJSFunction& call_js, | 46 const CallJSFunction& call_js, |
46 APILastError last_error) | 47 APILastError last_error, |
| 48 ExceptionHandler* exception_handler) |
47 : send_request_(send_request), | 49 : send_request_(send_request), |
48 call_js_(call_js), | 50 call_js_(call_js), |
49 last_error_(std::move(last_error)) {} | 51 last_error_(std::move(last_error)), |
| 52 exception_handler_(exception_handler) {} |
50 | 53 |
51 APIRequestHandler::~APIRequestHandler() {} | 54 APIRequestHandler::~APIRequestHandler() {} |
52 | 55 |
53 int APIRequestHandler::StartRequest(v8::Local<v8::Context> context, | 56 int APIRequestHandler::StartRequest(v8::Local<v8::Context> context, |
54 const std::string& method, | 57 const std::string& method, |
55 std::unique_ptr<base::ListValue> arguments, | 58 std::unique_ptr<base::ListValue> arguments, |
56 v8::Local<v8::Function> callback, | 59 v8::Local<v8::Function> callback, |
57 v8::Local<v8::Function> custom_callback, | 60 v8::Local<v8::Function> custom_callback, |
58 binding::RequestThread thread) { | 61 binding::RequestThread thread) { |
59 auto request = base::MakeUnique<Request>(); | 62 auto request = base::MakeUnique<Request>(); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 pending_request.callback_arguments.size()); | 136 pending_request.callback_arguments.size()); |
134 for (const auto& arg : pending_request.callback_arguments) | 137 for (const auto& arg : pending_request.callback_arguments) |
135 args.push_back(arg.Get(isolate)); | 138 args.push_back(arg.Get(isolate)); |
136 for (const auto& arg : response_args) | 139 for (const auto& arg : response_args) |
137 args.push_back(converter->ToV8Value(&arg, context)); | 140 args.push_back(converter->ToV8Value(&arg, context)); |
138 | 141 |
139 blink::WebScopedUserGesture user_gesture(pending_request.user_gesture_token); | 142 blink::WebScopedUserGesture user_gesture(pending_request.user_gesture_token); |
140 if (!error.empty()) | 143 if (!error.empty()) |
141 last_error_.SetError(context, error); | 144 last_error_.SetError(context, error); |
142 | 145 |
| 146 v8::TryCatch try_catch(isolate); |
143 // args.size() is converted to int, but args is controlled by chrome and is | 147 // args.size() is converted to int, but args is controlled by chrome and is |
144 // never close to std::numeric_limits<int>::max. | 148 // never close to std::numeric_limits<int>::max. |
145 call_js_.Run(pending_request.callback.Get(isolate), context, args.size(), | 149 call_js_.Run(pending_request.callback.Get(isolate), context, args.size(), |
146 args.data()); | 150 args.data()); |
| 151 if (try_catch.HasCaught()) { |
| 152 v8::Local<v8::Message> v8_message = try_catch.Message(); |
| 153 base::Optional<std::string> message; |
| 154 if (!v8_message.IsEmpty()) |
| 155 message = gin::V8ToString(v8_message->Get()); |
| 156 exception_handler_->HandleException(context, "Error handling response", |
| 157 &try_catch); |
| 158 } |
147 | 159 |
148 if (!error.empty()) | 160 if (!error.empty()) |
149 last_error_.ClearError(context, true); | 161 last_error_.ClearError(context, true); |
150 } | 162 } |
151 | 163 |
152 int APIRequestHandler::AddPendingRequest(v8::Local<v8::Context> context, | 164 int APIRequestHandler::AddPendingRequest(v8::Local<v8::Context> context, |
153 v8::Local<v8::Function> callback) { | 165 v8::Local<v8::Function> callback) { |
154 int request_id = next_request_id_++; | 166 int request_id = next_request_id_++; |
155 pending_requests_.emplace( | 167 pending_requests_.emplace( |
156 request_id, PendingRequest(context->GetIsolate(), callback, context, | 168 request_id, PendingRequest(context->GetIsolate(), callback, context, |
(...skipping 12 matching lines...) Expand all Loading... |
169 } | 181 } |
170 | 182 |
171 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { | 183 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { |
172 std::set<int> result; | 184 std::set<int> result; |
173 for (const auto& pair : pending_requests_) | 185 for (const auto& pair : pending_requests_) |
174 result.insert(pair.first); | 186 result.insert(pair.first); |
175 return result; | 187 return result; |
176 } | 188 } |
177 | 189 |
178 } // namespace extensions | 190 } // namespace extensions |
OLD | NEW |