Chromium Code Reviews| 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" |
| 11 #include "content/public/child/v8_value_converter.h" | 11 #include "content/public/child/v8_value_converter.h" |
| 12 #include "gin/converter.h" | 12 #include "gin/converter.h" |
| 13 #include "gin/data_object_builder.h" | |
| 13 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" | 14 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" |
| 14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | 15 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" |
| 15 | 16 |
| 16 namespace extensions { | 17 namespace extensions { |
| 17 | 18 |
| 18 APIRequestHandler::Request::Request() {} | 19 APIRequestHandler::Request::Request() {} |
| 19 APIRequestHandler::Request::~Request() = default; | 20 APIRequestHandler::Request::~Request() = default; |
| 20 | 21 |
| 21 APIRequestHandler::PendingRequest::PendingRequest( | 22 APIRequestHandler::PendingRequest::PendingRequest( |
| 22 v8::Isolate* isolate, | 23 v8::Isolate* isolate, |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 50 APIRequestHandler::~APIRequestHandler() {} | 51 APIRequestHandler::~APIRequestHandler() {} |
| 51 | 52 |
| 52 int APIRequestHandler::StartRequest(v8::Local<v8::Context> context, | 53 int APIRequestHandler::StartRequest(v8::Local<v8::Context> context, |
| 53 const std::string& method, | 54 const std::string& method, |
| 54 std::unique_ptr<base::ListValue> arguments, | 55 std::unique_ptr<base::ListValue> arguments, |
| 55 v8::Local<v8::Function> callback, | 56 v8::Local<v8::Function> callback, |
| 56 v8::Local<v8::Function> custom_callback, | 57 v8::Local<v8::Function> custom_callback, |
| 57 binding::RequestThread thread) { | 58 binding::RequestThread thread) { |
| 58 auto request = base::MakeUnique<Request>(); | 59 auto request = base::MakeUnique<Request>(); |
| 59 | 60 |
| 61 // The request id is primarily used in the renderer to associate an API | |
| 62 // request with the associated callback, but it's also used in the browser as | |
| 63 // an identifier for the extension function (e.g. by the pageCapture API). | |
| 64 // TODO(devlin): We should probably fix this, since the request id is only | |
| 65 // unique per-isolate, rather than globally. | |
| 66 int request_id = next_request_id_++; | |
| 67 | |
| 60 if (!custom_callback.IsEmpty() || !callback.IsEmpty()) { | 68 if (!custom_callback.IsEmpty() || !callback.IsEmpty()) { |
| 61 v8::Isolate* isolate = context->GetIsolate(); | 69 v8::Isolate* isolate = context->GetIsolate(); |
| 62 // In the JS bindings, custom callbacks are called with the arguments of | 70 // In the JS bindings, custom callbacks are called with the arguments of |
| 63 // name, the full request object (see below), the original callback, and | 71 // name, the full request object (see below), the original callback, and |
| 64 // the responses from the API. The responses from the API are handled by the | 72 // the responses from the API. The responses from the API are handled by the |
| 65 // APIRequestHandler, but we need to curry in the other values. | 73 // APIRequestHandler, but we need to curry in the other values. |
| 66 std::vector<v8::Local<v8::Value>> callback_args; | 74 std::vector<v8::Local<v8::Value>> callback_args; |
| 67 if (!custom_callback.IsEmpty()) { | 75 if (!custom_callback.IsEmpty()) { |
| 68 // TODO(devlin): The |request| object in the JS bindings includes | 76 // TODO(devlin): The |request| object in the JS bindings includes |
| 69 // properties for callback, callbackSchema, args, stack, id, and | 77 // properties for callback, callbackSchema, args, stack, id, and |
| 70 // customCallback. Of those, it appears that we only use stack, args, and | 78 // customCallback. Of those, it appears that we only use stack, args, and |
| 71 // id (since callback is curried in separately). We may be able to update | 79 // id (since callback is curried in separately). We may be able to update |
| 72 // bindings to get away from some of those. For now, just pass in an empty | 80 // bindings to get away from some of those. For now, just pass in an |
| 73 // object (most APIs don't rely on it). | 81 // object with the request id. |
| 74 v8::Local<v8::Object> request = v8::Object::New(isolate); | 82 v8::Local<v8::Object> request = |
| 83 gin::DataObjectBuilder(isolate).Set("id", request_id).Build(); | |
| 75 v8::Local<v8::Value> callback_to_pass = callback; | 84 v8::Local<v8::Value> callback_to_pass = callback; |
| 76 if (callback_to_pass.IsEmpty()) | 85 if (callback_to_pass.IsEmpty()) |
| 77 callback_to_pass = v8::Undefined(isolate); | 86 callback_to_pass = v8::Undefined(isolate); |
| 78 callback_args = {gin::StringToSymbol(isolate, method), request, | 87 callback_args = {gin::StringToSymbol(isolate, method), request, |
| 79 callback_to_pass}; | 88 callback_to_pass}; |
| 80 callback = custom_callback; | 89 callback = custom_callback; |
| 81 } | 90 } |
| 82 | 91 |
| 83 // TODO(devlin): We could *probably* get away with just using an integer | 92 // TODO(devlin): We could *probably* get away with just using an integer |
| 84 // here, but it's a little less foolproof. How slow is GenerateGUID? Should | 93 // here, but it's a little less foolproof. How slow is GenerateGUID? Should |
| 85 // we use that instead? It means updating the IPC | 94 // we use that instead? It means updating the IPC |
| 86 // (ExtensionHostMsg_Request). | 95 // (ExtensionHostMsg_Request). |
| 87 // base::UnguessableToken is another good option. | 96 // base::UnguessableToken is another good option. |
| 88 request->request_id = next_request_id_++; | 97 request->request_id = request_id; |
| 89 request->has_callback = true; | 98 request->has_callback = true; |
| 90 pending_requests_.insert(std::make_pair( | 99 pending_requests_.insert(std::make_pair( |
| 91 request->request_id, | 100 request->request_id, |
| 92 PendingRequest(isolate, callback, context, callback_args))); | 101 PendingRequest(isolate, callback, context, callback_args))); |
| 93 } | 102 } |
| 94 | 103 |
| 95 request->has_user_gesture = | 104 request->has_user_gesture = |
| 96 blink::WebUserGestureIndicator::IsProcessingUserGestureThreadSafe(); | 105 blink::WebUserGestureIndicator::IsProcessingUserGestureThreadSafe(); |
| 97 request->arguments = std::move(arguments); | 106 request->arguments = std::move(arguments); |
| 98 request->method_name = method; | 107 request->method_name = method; |
| 99 request->thread = thread; | 108 request->thread = thread; |
| 100 | 109 |
| 101 int id = request->request_id; | 110 int id = request->request_id; |
|
jbroman
2017/05/24 16:54:38
To clarify my question, |request_id| isn't used he
Devlin
2017/05/24 17:31:40
Oh, I see. Good catch; this was a bug. Fixed and
| |
| 102 send_request_.Run(std::move(request), context); | 111 send_request_.Run(std::move(request), context); |
| 103 return id; | 112 return id; |
| 104 } | 113 } |
| 105 | 114 |
| 106 void APIRequestHandler::CompleteRequest(int request_id, | 115 void APIRequestHandler::CompleteRequest(int request_id, |
| 107 const base::ListValue& response_args, | 116 const base::ListValue& response_args, |
| 108 const std::string& error) { | 117 const std::string& error) { |
| 109 auto iter = pending_requests_.find(request_id); | 118 auto iter = pending_requests_.find(request_id); |
| 110 // The request may have been removed if the context was invalidated before a | 119 // The request may have been removed if the context was invalidated before a |
| 111 // response is ready. | 120 // response is ready. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 153 } | 162 } |
| 154 | 163 |
| 155 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { | 164 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { |
| 156 std::set<int> result; | 165 std::set<int> result; |
| 157 for (const auto& pair : pending_requests_) | 166 for (const auto& pair : pending_requests_) |
| 158 result.insert(pair.first); | 167 result.insert(pair.first); |
| 159 return result; | 168 return result; |
| 160 } | 169 } |
| 161 | 170 |
| 162 } // namespace extensions | 171 } // namespace extensions |
| OLD | NEW |