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 "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
15 | 16 |
16 namespace base { | 17 namespace base { |
17 class ListValue; | 18 class ListValue; |
18 } | 19 } |
19 | 20 |
20 namespace extensions { | 21 namespace extensions { |
21 | 22 |
22 // A wrapper around a map for extension API calls. Contains all pending requests | 23 // A wrapper around a map for extension API calls. Contains all pending requests |
23 // and the associated context and callback. Designed to be used on a single | 24 // and the associated context and callback. Designed to be used on a single |
24 // thread, but amongst multiple contexts. | 25 // thread, but amongst multiple contexts. |
25 class APIRequestHandler { | 26 class APIRequestHandler { |
26 public: | 27 public: |
27 using CallJSFunction = base::Callback<void(v8::Local<v8::Function>, | 28 using CallJSFunction = base::Callback<void(v8::Local<v8::Function>, |
28 v8::Local<v8::Context>, | 29 v8::Local<v8::Context>, |
29 int argc, | 30 int argc, |
30 v8::Local<v8::Value>[])>; | 31 v8::Local<v8::Value>[])>; |
31 | 32 |
32 explicit APIRequestHandler(const CallJSFunction& call_js); | 33 APIRequestHandler(const CallJSFunction& call_js, APILastError last_error); |
33 ~APIRequestHandler(); | 34 ~APIRequestHandler(); |
34 | 35 |
35 // Adds a pending request to the map. Returns a unique identifier for that | 36 // Adds a pending request to the map. Returns a unique identifier for that |
36 // request. | 37 // request. |
37 int AddPendingRequest(v8::Isolate* isolate, | 38 int AddPendingRequest(v8::Isolate* isolate, |
38 v8::Local<v8::Function> callback, | 39 v8::Local<v8::Function> callback, |
39 v8::Local<v8::Context> context, | 40 v8::Local<v8::Context> context, |
40 const std::vector<v8::Local<v8::Value>>& callback_args); | 41 const std::vector<v8::Local<v8::Value>>& callback_args); |
41 | 42 |
42 // Responds to the request with the given |request_id|, calling the callback | 43 // Responds to the request with the given |request_id|, calling the callback |
43 // with the given |response| arguments. | 44 // with the given |response| arguments. |
44 // Invalid ids are ignored. | 45 // Invalid ids are ignored. |
45 void CompleteRequest(int request_id, const base::ListValue& response); | 46 void CompleteRequest(int request_id, |
| 47 const base::ListValue& response, |
| 48 const std::string& error); |
46 | 49 |
47 // Invalidates any requests that are associated with |context|. | 50 // Invalidates any requests that are associated with |context|. |
48 void InvalidateContext(v8::Local<v8::Context> context); | 51 void InvalidateContext(v8::Local<v8::Context> context); |
49 | 52 |
50 std::set<int> GetPendingRequestIdsForTesting() const; | 53 std::set<int> GetPendingRequestIdsForTesting() const; |
51 | 54 |
52 private: | 55 private: |
53 struct PendingRequest { | 56 struct PendingRequest { |
54 PendingRequest(v8::Isolate* isolate, | 57 PendingRequest(v8::Isolate* isolate, |
55 v8::Local<v8::Function> callback, | 58 v8::Local<v8::Function> callback, |
(...skipping 14 matching lines...) Expand all Loading... |
70 | 73 |
71 // A map of all pending requests. | 74 // A map of all pending requests. |
72 std::map<int, PendingRequest> pending_requests_; | 75 std::map<int, PendingRequest> pending_requests_; |
73 | 76 |
74 // The method to call into a JS with specific arguments. We curry this in | 77 // The method to call into a JS with specific arguments. We curry this in |
75 // because the manner we want to do this is a unittest (e.g. | 78 // because the manner we want to do this is a unittest (e.g. |
76 // v8::Function::Call) can be significantly different than in production | 79 // v8::Function::Call) can be significantly different than in production |
77 // (where we have to deal with e.g. blocking javascript). | 80 // (where we have to deal with e.g. blocking javascript). |
78 CallJSFunction call_js_; | 81 CallJSFunction call_js_; |
79 | 82 |
| 83 APILastError last_error_; |
| 84 |
80 DISALLOW_COPY_AND_ASSIGN(APIRequestHandler); | 85 DISALLOW_COPY_AND_ASSIGN(APIRequestHandler); |
81 }; | 86 }; |
82 | 87 |
83 } // namespace extensions | 88 } // namespace extensions |
84 | 89 |
85 #endif // EXTENSIONS_RENDERER_API_REQUEST_HANDLER_H_ | 90 #endif // EXTENSIONS_RENDERER_API_REQUEST_HANDLER_H_ |
OLD | NEW |