1 // Copyright 2013 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 CHROME_RENDERER_EXTENSIONS_PEPPER_REQUEST_PROXY_H_ 6 #define CHROME_RENDERER_EXTENSIONS_PEPPER_REQUEST_PROXY_H_ 7 8 #include <map> 9 #include <string> 10 11 #include "base/callback.h" 12 13 namespace base { 14 class ListValue; 15 } 16 17 namespace v8 { 18 class Isolate; 19 } 20 21 namespace extensions { 22 23 class ChromeV8Context; 24 25 // A proxy that forwards pepper apps API calls through the Javascript API 26 // bindings. 27 class PepperRequestProxy { 28 public: 29 // A callback to be called with the result of an API call. If |success| is 30 // true, |response| will contain the response value. Otherwise, |error| will 31 // contain the error message. 32 typedef base::Callback<void(bool success, 33 const base::ListValue& response, 34 const std::string& error)> ResponseCallback; 35 36 explicit PepperRequestProxy(ChromeV8Context* context); 37 ~PepperRequestProxy(); 38 39 // Starts an API request. Returns whether the call was successful. On failure, 40 // |error| will contain the error message. |callback| will only be called on 41 // success. 42 bool StartRequest(const ResponseCallback& callback, 43 const std::string& request_name, 44 const base::ListValue& args, 45 std::string* error); 46 47 void OnResponseReceived(int request_id, 48 bool success, 49 const base::ListValue& args, 50 const std::string& error); 51 52 private: 53 typedef std::map<int, ResponseCallback> PendingRequestMap; 54 55 // Non-owning pointer. 56 ChromeV8Context* context_; 57 // Non-owning pointer. 58 v8::Isolate* isolate_; 59 PendingRequestMap pending_request_map_; 60 int next_request_id_; 61 }; 62 63 } // namespace extensions 64 65 #endif // CHROME_RENDERER_EXTENSIONS_PEPPER_REQUEST_PROXY_H_ 66