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