| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef EXTENSIONS_RENDERER_API_REQUEST_HANDLER_H_ | |
| 6 #define EXTENSIONS_RENDERER_API_REQUEST_HANDLER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <memory> | |
| 10 #include <set> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "extensions/renderer/api_binding_types.h" | |
| 15 #include "extensions/renderer/api_last_error.h" | |
| 16 #include "third_party/WebKit/public/web/WebUserGestureToken.h" | |
| 17 #include "v8/include/v8.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class ListValue; | |
| 21 } | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 // A wrapper around a map for extension API calls. Contains all pending requests | |
| 26 // and the associated context and callback. Designed to be used on a single | |
| 27 // thread, but amongst multiple contexts. | |
| 28 class APIRequestHandler { | |
| 29 public: | |
| 30 // TODO(devlin): We may want to coalesce this with the | |
| 31 // ExtensionHostMsg_Request_Params IPC struct. | |
| 32 struct Request { | |
| 33 Request(); | |
| 34 ~Request(); | |
| 35 | |
| 36 int request_id = -1; | |
| 37 std::string method_name; | |
| 38 bool has_callback = false; | |
| 39 bool has_user_gesture = false; | |
| 40 binding::RequestThread thread = binding::RequestThread::UI; | |
| 41 std::unique_ptr<base::ListValue> arguments; | |
| 42 | |
| 43 private: | |
| 44 DISALLOW_COPY_AND_ASSIGN(Request); | |
| 45 }; | |
| 46 | |
| 47 using SendRequestMethod = | |
| 48 base::Callback<void(std::unique_ptr<Request>, v8::Local<v8::Context>)>; | |
| 49 | |
| 50 using CallJSFunction = base::Callback<void(v8::Local<v8::Function>, | |
| 51 v8::Local<v8::Context>, | |
| 52 int argc, | |
| 53 v8::Local<v8::Value>[])>; | |
| 54 | |
| 55 APIRequestHandler(const SendRequestMethod& send_request, | |
| 56 const CallJSFunction& call_js, | |
| 57 APILastError last_error); | |
| 58 ~APIRequestHandler(); | |
| 59 | |
| 60 // Begins the process of processing the request. Returns the identifier of the | |
| 61 // pending request, or -1 if no pending request was added (which can happen if | |
| 62 // no callback was specified). | |
| 63 int StartRequest(v8::Local<v8::Context> context, | |
| 64 const std::string& method, | |
| 65 std::unique_ptr<base::ListValue> arguments, | |
| 66 v8::Local<v8::Function> callback, | |
| 67 v8::Local<v8::Function> custom_callback, | |
| 68 binding::RequestThread thread); | |
| 69 | |
| 70 // Adds a pending request for the request handler to manage (and complete via | |
| 71 // CompleteRequest). This is used by renderer-side implementations that | |
| 72 // shouldn't be dispatched to the browser in the normal flow, but means other | |
| 73 // classes don't have to worry about context invalidation. | |
| 74 int AddPendingRequest(v8::Local<v8::Context> context, | |
| 75 v8::Local<v8::Function> callback); | |
| 76 | |
| 77 // Responds to the request with the given |request_id|, calling the callback | |
| 78 // with the given |response| arguments. | |
| 79 // Invalid ids are ignored. | |
| 80 void CompleteRequest(int request_id, | |
| 81 const base::ListValue& response, | |
| 82 const std::string& error); | |
| 83 | |
| 84 // Invalidates any requests that are associated with |context|. | |
| 85 void InvalidateContext(v8::Local<v8::Context> context); | |
| 86 | |
| 87 APILastError* last_error() { return &last_error_; } | |
| 88 | |
| 89 std::set<int> GetPendingRequestIdsForTesting() const; | |
| 90 | |
| 91 private: | |
| 92 struct PendingRequest { | |
| 93 PendingRequest(v8::Isolate* isolate, | |
| 94 v8::Local<v8::Function> callback, | |
| 95 v8::Local<v8::Context> context, | |
| 96 const std::vector<v8::Local<v8::Value>>& callback_args); | |
| 97 ~PendingRequest(); | |
| 98 PendingRequest(PendingRequest&&); | |
| 99 PendingRequest& operator=(PendingRequest&&); | |
| 100 | |
| 101 v8::Isolate* isolate; | |
| 102 v8::Global<v8::Context> context; | |
| 103 v8::Global<v8::Function> callback; | |
| 104 std::vector<v8::Global<v8::Value>> callback_arguments; | |
| 105 blink::WebUserGestureToken user_gesture_token; | |
| 106 }; | |
| 107 | |
| 108 // The next available request identifier. | |
| 109 int next_request_id_ = 0; | |
| 110 | |
| 111 // A map of all pending requests. | |
| 112 std::map<int, PendingRequest> pending_requests_; | |
| 113 | |
| 114 SendRequestMethod send_request_; | |
| 115 | |
| 116 // The method to call into a JS with specific arguments. We curry this in | |
| 117 // because the manner we want to do this is a unittest (e.g. | |
| 118 // v8::Function::Call) can be significantly different than in production | |
| 119 // (where we have to deal with e.g. blocking javascript). | |
| 120 CallJSFunction call_js_; | |
| 121 | |
| 122 APILastError last_error_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(APIRequestHandler); | |
| 125 }; | |
| 126 | |
| 127 } // namespace extensions | |
| 128 | |
| 129 #endif // EXTENSIONS_RENDERER_API_REQUEST_HANDLER_H_ | |
| OLD | NEW |