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 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 pending_request.callback_arguments.size()); | 137 pending_request.callback_arguments.size()); |
135 for (const auto& arg : pending_request.callback_arguments) | 138 for (const auto& arg : pending_request.callback_arguments) |
136 args.push_back(arg.Get(isolate)); | 139 args.push_back(arg.Get(isolate)); |
137 for (const auto& arg : response_args) | 140 for (const auto& arg : response_args) |
138 args.push_back(converter->ToV8Value(&arg, context)); | 141 args.push_back(converter->ToV8Value(&arg, context)); |
139 | 142 |
140 blink::WebScopedUserGesture user_gesture(pending_request.user_gesture_token); | 143 blink::WebScopedUserGesture user_gesture(pending_request.user_gesture_token); |
141 if (!error.empty()) | 144 if (!error.empty()) |
142 last_error_.SetError(context, error); | 145 last_error_.SetError(context, error); |
143 | 146 |
| 147 v8::TryCatch try_catch(isolate); |
144 // args.size() is converted to int, but args is controlled by chrome and is | 148 // args.size() is converted to int, but args is controlled by chrome and is |
145 // never close to std::numeric_limits<int>::max. | 149 // never close to std::numeric_limits<int>::max. |
146 call_js_.Run(pending_request.callback.Get(isolate), context, args.size(), | 150 call_js_.Run(pending_request.callback.Get(isolate), context, args.size(), |
147 args.data()); | 151 args.data()); |
| 152 if (try_catch.HasCaught()) { |
| 153 v8::Local<v8::Message> v8_message = try_catch.Message(); |
| 154 base::Optional<std::string> message; |
| 155 if (!v8_message.IsEmpty()) |
| 156 message = gin::V8ToString(v8_message->Get()); |
| 157 exception_handler_->HandleException(context, "Error handling response", |
| 158 &try_catch); |
| 159 } |
148 | 160 |
149 if (!error.empty()) | 161 if (!error.empty()) |
150 last_error_.ClearError(context, true); | 162 last_error_.ClearError(context, true); |
151 } | 163 } |
152 | 164 |
153 int APIRequestHandler::AddPendingRequest(v8::Local<v8::Context> context, | 165 int APIRequestHandler::AddPendingRequest(v8::Local<v8::Context> context, |
154 v8::Local<v8::Function> callback) { | 166 v8::Local<v8::Function> callback) { |
155 int request_id = next_request_id_++; | 167 int request_id = next_request_id_++; |
156 pending_requests_.emplace( | 168 pending_requests_.emplace( |
157 request_id, PendingRequest(context->GetIsolate(), callback, context, | 169 request_id, PendingRequest(context->GetIsolate(), callback, context, |
(...skipping 12 matching lines...) Expand all Loading... |
170 } | 182 } |
171 | 183 |
172 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { | 184 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { |
173 std::set<int> result; | 185 std::set<int> result; |
174 for (const auto& pair : pending_requests_) | 186 for (const auto& pair : pending_requests_) |
175 result.insert(pair.first); | 187 result.insert(pair.first); |
176 return result; | 188 return result; |
177 } | 189 } |
178 | 190 |
179 } // namespace extensions | 191 } // namespace extensions |
OLD | NEW |