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

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

Issue 2697363003: [Extensions Bindings] Move request dispatch to APIRequestHandler (Closed)
Patch Set: . Created 3 years, 10 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 "third_party/WebKit/public/web/WebScopedUserGesture.h" 13 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
13 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" 14 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
14 15
15 namespace extensions { 16 namespace extensions {
16 17
18 APIRequestHandler::Request::Request() {}
19 APIRequestHandler::Request::~Request() = default;
lazyboy 2017/02/17 19:47:13 Is there any particular reason to choose "default"
Devlin 2017/02/17 21:57:41 Good question. I've noticed a trend towards using
lazyboy 2017/02/17 22:06:55 This is fine, I was just curious if this was was i
jbroman 2017/02/21 00:55:55 I don't have a preference one way or another, and
20
17 APIRequestHandler::PendingRequest::PendingRequest( 21 APIRequestHandler::PendingRequest::PendingRequest(
18 v8::Isolate* isolate, 22 v8::Isolate* isolate,
19 v8::Local<v8::Function> callback, 23 v8::Local<v8::Function> callback,
20 v8::Local<v8::Context> context, 24 v8::Local<v8::Context> context,
21 const std::vector<v8::Local<v8::Value>>& local_callback_args) 25 const std::vector<v8::Local<v8::Value>>& local_callback_args)
22 : isolate(isolate), 26 : isolate(isolate),
23 context(isolate, context), 27 context(isolate, context),
24 callback(isolate, callback), 28 callback(isolate, callback),
25 user_gesture_token( 29 user_gesture_token(
26 blink::WebUserGestureIndicator::currentUserGestureToken()) { 30 blink::WebUserGestureIndicator::currentUserGestureToken()) {
27 if (!local_callback_args.empty()) { 31 if (!local_callback_args.empty()) {
28 callback_arguments.reserve(local_callback_args.size()); 32 callback_arguments.reserve(local_callback_args.size());
29 for (const auto& arg : local_callback_args) 33 for (const auto& arg : local_callback_args)
30 callback_arguments.push_back(v8::Global<v8::Value>(isolate, arg)); 34 callback_arguments.push_back(v8::Global<v8::Value>(isolate, arg));
31 } 35 }
32 } 36 }
33 37
34 APIRequestHandler::PendingRequest::~PendingRequest() {} 38 APIRequestHandler::PendingRequest::~PendingRequest() {}
35 APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default; 39 APIRequestHandler::PendingRequest::PendingRequest(PendingRequest&&) = default;
36 APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=( 40 APIRequestHandler::PendingRequest& APIRequestHandler::PendingRequest::operator=(
37 PendingRequest&&) = default; 41 PendingRequest&&) = default;
38 42
39 APIRequestHandler::APIRequestHandler(const CallJSFunction& call_js, 43 APIRequestHandler::APIRequestHandler(const SendRequestMethod& send_request,
44 const CallJSFunction& call_js,
40 APILastError last_error) 45 APILastError last_error)
41 : call_js_(call_js), last_error_(std::move(last_error)) {} 46 : send_request_(send_request),
47 call_js_(call_js),
48 last_error_(std::move(last_error)) {}
42 49
43 APIRequestHandler::~APIRequestHandler() {} 50 APIRequestHandler::~APIRequestHandler() {}
44 51
45 int APIRequestHandler::AddPendingRequest( 52 int APIRequestHandler::StartRequest(v8::Local<v8::Context> context,
46 v8::Isolate* isolate, 53 const std::string& method,
47 v8::Local<v8::Function> callback, 54 std::unique_ptr<base::ListValue> arguments,
48 v8::Local<v8::Context> context, 55 v8::Local<v8::Function> callback,
49 const std::vector<v8::Local<v8::Value>>& callback_args) { 56 v8::Local<v8::Function> custom_callback) {
50 // TODO(devlin): We could *probably* get away with just using an integer here, 57 auto request = base::MakeUnique<Request>();
51 // but it's a little less foolproof. How slow is GenerateGUID? Should we use 58
52 // that instead? It means updating the IPC (ExtensionHostMsg_Request). 59 if (!custom_callback.IsEmpty() || !callback.IsEmpty()) {
53 // base::UnguessableToken is another good option. 60 v8::Isolate* isolate = context->GetIsolate();
54 int id = next_request_id_++; 61 // In the JS bindings, custom callbacks are called with the arguments of
55 pending_requests_.insert(std::make_pair( 62 // name, the full request object (see below), the original callback, and
56 id, PendingRequest(isolate, callback, context, callback_args))); 63 // the responses from the API. The responses from the API are handled by the
64 // APIRequestHandler, but we need to curry in the other values.
65 std::vector<v8::Local<v8::Value>> callback_args;
66 if (!custom_callback.IsEmpty()) {
67 // TODO(devlin): The |request| object in the JS bindings includes
68 // properties for callback, callbackSchema, args, stack, id, and
69 // customCallback. Of those, it appears that we only use stack, args, and
70 // id (since callback is curried in separately). We may be able to update
71 // bindings to get away from some of those. For now, just pass in an empty
72 // object (most APIs don't rely on it).
73 v8::Local<v8::Object> request = v8::Object::New(isolate);
74 callback_args = {gin::StringToSymbol(isolate, method), request, callback};
75 callback = custom_callback;
76 }
77
78 // TODO(devlin): We could *probably* get away with just using an integer
79 // here, but it's a little less foolproof. How slow is GenerateGUID? Should
80 // we use that instead? It means updating the IPC
81 // (ExtensionHostMsg_Request).
82 // base::UnguessableToken is another good option.
83 request->request_id = next_request_id_++;
84 request->has_callback = true;
85 pending_requests_.insert(std::make_pair(
86 request->request_id,
87 PendingRequest(isolate, callback, context, callback_args)));
88 }
89
90 request->has_user_gesture =
91 blink::WebUserGestureIndicator::isProcessingUserGestureThreadSafe();
92 request->arguments = std::move(arguments);
93 request->method_name = method;
94
95 int id = request->request_id;
96 send_request_.Run(std::move(request), context);
57 return id; 97 return id;
58 } 98 }
59 99
60 void APIRequestHandler::CompleteRequest(int request_id, 100 void APIRequestHandler::CompleteRequest(int request_id,
61 const base::ListValue& response_args, 101 const base::ListValue& response_args,
62 const std::string& error) { 102 const std::string& error) {
63 auto iter = pending_requests_.find(request_id); 103 auto iter = pending_requests_.find(request_id);
64 // The request may have been removed if the context was invalidated before a 104 // The request may have been removed if the context was invalidated before a
65 // response is ready. 105 // response is ready.
66 if (iter == pending_requests_.end()) 106 if (iter == pending_requests_.end())
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 } 147 }
108 148
109 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const { 149 std::set<int> APIRequestHandler::GetPendingRequestIdsForTesting() const {
110 std::set<int> result; 150 std::set<int> result;
111 for (const auto& pair : pending_requests_) 151 for (const auto& pair : pending_requests_)
112 result.insert(pair.first); 152 result.insert(pair.first);
113 return result; 153 return result;
114 } 154 }
115 155
116 } // namespace extensions 156 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/renderer/api_request_handler.h ('k') | extensions/renderer/api_request_handler_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698