Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1273)

Side by Side Diff: extensions/renderer/api_request_handler.cc

Issue 2894923003: [Extensions Bindings] Include request id in a custom callback response (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 int request_id = next_request_id_++;
jbroman 2017/05/23 22:15:03 Should this be inside the if condition? It's not u
Devlin 2017/05/24 16:08:33 The browser side uses these identifiers, even thou
62
60 if (!custom_callback.IsEmpty() || !callback.IsEmpty()) { 63 if (!custom_callback.IsEmpty() || !callback.IsEmpty()) {
61 v8::Isolate* isolate = context->GetIsolate(); 64 v8::Isolate* isolate = context->GetIsolate();
62 // In the JS bindings, custom callbacks are called with the arguments of 65 // In the JS bindings, custom callbacks are called with the arguments of
63 // name, the full request object (see below), the original callback, and 66 // 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 67 // 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. 68 // APIRequestHandler, but we need to curry in the other values.
66 std::vector<v8::Local<v8::Value>> callback_args; 69 std::vector<v8::Local<v8::Value>> callback_args;
67 if (!custom_callback.IsEmpty()) { 70 if (!custom_callback.IsEmpty()) {
68 // TODO(devlin): The |request| object in the JS bindings includes 71 // TODO(devlin): The |request| object in the JS bindings includes
69 // properties for callback, callbackSchema, args, stack, id, and 72 // properties for callback, callbackSchema, args, stack, id, and
70 // customCallback. Of those, it appears that we only use stack, args, and 73 // 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 74 // 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 75 // bindings to get away from some of those. For now, just pass in an
73 // object (most APIs don't rely on it). 76 // object with the request id.
74 v8::Local<v8::Object> request = v8::Object::New(isolate); 77 v8::Local<v8::Object> request =
78 gin::DataObjectBuilder(isolate).Set("id", request_id).Build();
75 v8::Local<v8::Value> callback_to_pass = callback; 79 v8::Local<v8::Value> callback_to_pass = callback;
76 if (callback_to_pass.IsEmpty()) 80 if (callback_to_pass.IsEmpty())
77 callback_to_pass = v8::Undefined(isolate); 81 callback_to_pass = v8::Undefined(isolate);
78 callback_args = {gin::StringToSymbol(isolate, method), request, 82 callback_args = {gin::StringToSymbol(isolate, method), request,
79 callback_to_pass}; 83 callback_to_pass};
80 callback = custom_callback; 84 callback = custom_callback;
81 } 85 }
82 86
83 // TODO(devlin): We could *probably* get away with just using an integer 87 // 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 88 // here, but it's a little less foolproof. How slow is GenerateGUID? Should
85 // we use that instead? It means updating the IPC 89 // we use that instead? It means updating the IPC
86 // (ExtensionHostMsg_Request). 90 // (ExtensionHostMsg_Request).
87 // base::UnguessableToken is another good option. 91 // base::UnguessableToken is another good option.
88 request->request_id = next_request_id_++; 92 request->request_id = request_id;
89 request->has_callback = true; 93 request->has_callback = true;
90 pending_requests_.insert(std::make_pair( 94 pending_requests_.insert(std::make_pair(
91 request->request_id, 95 request->request_id,
92 PendingRequest(isolate, callback, context, callback_args))); 96 PendingRequest(isolate, callback, context, callback_args)));
93 } 97 }
94 98
95 request->has_user_gesture = 99 request->has_user_gesture =
96 blink::WebUserGestureIndicator::IsProcessingUserGestureThreadSafe(); 100 blink::WebUserGestureIndicator::IsProcessingUserGestureThreadSafe();
97 request->arguments = std::move(arguments); 101 request->arguments = std::move(arguments);
98 request->method_name = method; 102 request->method_name = method;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 157 }
154 158
155 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { 159 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const {
156 std::set<int> result; 160 std::set<int> result;
157 for (const auto& pair : pending_requests_) 161 for (const auto& pair : pending_requests_)
158 result.insert(pair.first); 162 result.insert(pair.first);
159 return result; 163 return result;
160 } 164 }
161 165
162 } // namespace extensions 166 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698