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