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/api_request_handler.h" | 5 #include "extensions/renderer/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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 for (const auto& arg : local_callback_args) | 23 for (const auto& arg : local_callback_args) |
24 callback_arguments.push_back(v8::Global<v8::Value>(isolate, arg)); | 24 callback_arguments.push_back(v8::Global<v8::Value>(isolate, arg)); |
25 } | 25 } |
26 } | 26 } |
27 | 27 |
28 APIRequestHandler::PendingRequest::~PendingRequest() {} | 28 APIRequestHandler::PendingRequest::~PendingRequest() {} |
29 APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default; | 29 APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default; |
30 APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=( | 30 APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=( |
31 PendingRequest&&) = default; | 31 PendingRequest&&) = default; |
32 | 32 |
33 APIRequestHandler::APIRequestHandler(const CallJSFunction& call_js) | 33 APIRequestHandler::APIRequestHandler(const CallJSFunction& call_js, |
34 : call_js_(call_js) {} | 34 APILastError last_error) |
| 35 : call_js_(call_js), last_error_(std::move(last_error)) {} |
35 | 36 |
36 APIRequestHandler::~APIRequestHandler() {} | 37 APIRequestHandler::~APIRequestHandler() {} |
37 | 38 |
38 int APIRequestHandler::AddPendingRequest( | 39 int APIRequestHandler::AddPendingRequest( |
39 v8::Isolate* isolate, | 40 v8::Isolate* isolate, |
40 v8::Local<v8::Function> callback, | 41 v8::Local<v8::Function> callback, |
41 v8::Local<v8::Context> context, | 42 v8::Local<v8::Context> context, |
42 const std::vector<v8::Local<v8::Value>>& callback_args) { | 43 const std::vector<v8::Local<v8::Value>>& callback_args) { |
43 // TODO(devlin): We could *probably* get away with just using an integer here, | 44 // TODO(devlin): We could *probably* get away with just using an integer here, |
44 // but it's a little less foolproof. How slow is GenerateGUID? Should we use | 45 // but it's a little less foolproof. How slow is GenerateGUID? Should we use |
45 // that instead? It means updating the IPC (ExtensionHostMsg_Request). | 46 // that instead? It means updating the IPC (ExtensionHostMsg_Request). |
46 // base::UnguessableToken is another good option. | 47 // base::UnguessableToken is another good option. |
47 int id = next_request_id_++; | 48 int id = next_request_id_++; |
48 pending_requests_.insert(std::make_pair( | 49 pending_requests_.insert(std::make_pair( |
49 id, PendingRequest(isolate, callback, context, callback_args))); | 50 id, PendingRequest(isolate, callback, context, callback_args))); |
50 return id; | 51 return id; |
51 } | 52 } |
52 | 53 |
53 void APIRequestHandler::CompleteRequest(int request_id, | 54 void APIRequestHandler::CompleteRequest(int request_id, |
54 const base::ListValue& response_args) { | 55 const base::ListValue& response_args, |
| 56 const std::string& error) { |
55 auto iter = pending_requests_.find(request_id); | 57 auto iter = pending_requests_.find(request_id); |
56 // The request may have been removed if the context was invalidated before a | 58 // The request may have been removed if the context was invalidated before a |
57 // response is ready. | 59 // response is ready. |
58 if (iter == pending_requests_.end()) | 60 if (iter == pending_requests_.end()) |
59 return; | 61 return; |
60 | 62 |
61 PendingRequest pending_request = std::move(iter->second); | 63 PendingRequest pending_request = std::move(iter->second); |
62 pending_requests_.erase(iter); | 64 pending_requests_.erase(iter); |
63 | 65 |
64 v8::Isolate* isolate = pending_request.isolate; | 66 v8::Isolate* isolate = pending_request.isolate; |
65 v8::HandleScope handle_scope(isolate); | 67 v8::HandleScope handle_scope(isolate); |
66 v8::Local<v8::Context> context = pending_request.context.Get(isolate); | 68 v8::Local<v8::Context> context = pending_request.context.Get(isolate); |
| 69 v8::Context::Scope context_scope(context); |
67 std::unique_ptr<content::V8ValueConverter> converter( | 70 std::unique_ptr<content::V8ValueConverter> converter( |
68 content::V8ValueConverter::create()); | 71 content::V8ValueConverter::create()); |
69 std::vector<v8::Local<v8::Value>> args; | 72 std::vector<v8::Local<v8::Value>> args; |
70 args.reserve(response_args.GetSize() + | 73 args.reserve(response_args.GetSize() + |
71 pending_request.callback_arguments.size()); | 74 pending_request.callback_arguments.size()); |
72 for (const auto& arg : pending_request.callback_arguments) | 75 for (const auto& arg : pending_request.callback_arguments) |
73 args.push_back(arg.Get(isolate)); | 76 args.push_back(arg.Get(isolate)); |
74 for (const auto& arg : response_args) | 77 for (const auto& arg : response_args) |
75 args.push_back(converter->ToV8Value(arg.get(), context)); | 78 args.push_back(converter->ToV8Value(arg.get(), context)); |
76 | 79 |
| 80 if (!error.empty()) |
| 81 last_error_.SetError(context, error); |
| 82 |
77 // args.size() is converted to int, but args is controlled by chrome and is | 83 // args.size() is converted to int, but args is controlled by chrome and is |
78 // never close to std::numeric_limits<int>::max. | 84 // never close to std::numeric_limits<int>::max. |
79 call_js_.Run(pending_request.callback.Get(isolate), context, args.size(), | 85 call_js_.Run(pending_request.callback.Get(isolate), context, args.size(), |
80 args.data()); | 86 args.data()); |
| 87 |
| 88 if (!error.empty()) |
| 89 last_error_.ClearError(context, true); |
81 } | 90 } |
82 | 91 |
83 void APIRequestHandler::InvalidateContext(v8::Local<v8::Context> context) { | 92 void APIRequestHandler::InvalidateContext(v8::Local<v8::Context> context) { |
84 for (auto iter = pending_requests_.begin(); | 93 for (auto iter = pending_requests_.begin(); |
85 iter != pending_requests_.end();) { | 94 iter != pending_requests_.end();) { |
86 if (iter->second.context == context) | 95 if (iter->second.context == context) |
87 iter = pending_requests_.erase(iter); | 96 iter = pending_requests_.erase(iter); |
88 else | 97 else |
89 ++iter; | 98 ++iter; |
90 } | 99 } |
91 } | 100 } |
92 | 101 |
93 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { | 102 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { |
94 std::set<int> result; | 103 std::set<int> result; |
95 for (const auto& pair : pending_requests_) | 104 for (const auto& pair : pending_requests_) |
96 result.insert(pair.first); | 105 result.insert(pair.first); |
97 return result; | 106 return result; |
98 } | 107 } |
99 | 108 |
100 } // namespace extensions | 109 } // namespace extensions |
OLD | NEW |