| 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_BINDING_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDING_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDING_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 base::Callback<void(const std::string& name, | 43 base::Callback<void(const std::string& name, |
| 44 std::unique_ptr<base::ListValue> arguments, | 44 std::unique_ptr<base::ListValue> arguments, |
| 45 v8::Isolate*, | 45 v8::Isolate*, |
| 46 v8::Local<v8::Context>, | 46 v8::Local<v8::Context>, |
| 47 v8::Local<v8::Function>)>; | 47 v8::Local<v8::Function>)>; |
| 48 | 48 |
| 49 // The callback type for handling an API call. | 49 // The callback type for handling an API call. |
| 50 using HandlerCallback = base::Callback<void(gin::Arguments*)>; | 50 using HandlerCallback = base::Callback<void(gin::Arguments*)>; |
| 51 | 51 |
| 52 // The ArgumentSpec::RefMap is required to outlive this object. | 52 // The ArgumentSpec::RefMap is required to outlive this object. |
| 53 // |type_definitions| may be null if the API does not specify any types. |
| 53 APIBinding(const std::string& name, | 54 APIBinding(const std::string& name, |
| 54 const base::ListValue& function_definitions, | 55 const base::ListValue& function_definitions, |
| 55 const base::ListValue& type_definitions, | 56 const base::ListValue* type_definitions, |
| 56 const APIMethodCallback& callback, | 57 const APIMethodCallback& callback, |
| 57 ArgumentSpec::RefMap* type_refs); | 58 ArgumentSpec::RefMap* type_refs); |
| 58 ~APIBinding(); | 59 ~APIBinding(); |
| 59 | 60 |
| 60 // Returns a new v8::Object for the API this APIBinding represents. | 61 // Returns a new v8::Object for the API this APIBinding represents. |
| 61 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context, | 62 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context, |
| 62 v8::Isolate* isolate); | 63 v8::Isolate* isolate); |
| 63 | 64 |
| 64 private: | 65 private: |
| 65 using APISignature = std::vector<std::unique_ptr<ArgumentSpec>>; | 66 using APISignature = std::vector<std::unique_ptr<ArgumentSpec>>; |
| 66 | 67 |
| 67 // Per-context data that stores the callbacks that are used within the | 68 // Per-context data that stores the callbacks that are used within the |
| 68 // context. Since these callbacks are used within v8::Externals, v8 itself | 69 // context. Since these callbacks are used within v8::Externals, v8 itself |
| 69 // does not clean them up. | 70 // does not clean them up. |
| 70 struct APIPerContextData : public base::SupportsUserData::Data { | 71 struct APIPerContextData : public base::SupportsUserData::Data { |
| 71 APIPerContextData(); | 72 APIPerContextData(); |
| 72 ~APIPerContextData() override; | 73 ~APIPerContextData() override; |
| 73 | 74 |
| 74 std::vector<std::unique_ptr<HandlerCallback>> context_callbacks; | 75 std::vector<std::unique_ptr<HandlerCallback>> context_callbacks; |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 // Handles a call an API method with the given |name| and matches the | 78 // Handles a call an API method with the given |name| and matches the |
| 78 // arguments against |signature|. | 79 // arguments against |signature|. |
| 79 void HandleCall(const std::string& name, | 80 void HandleCall(const std::string& name, |
| 80 const APISignature* signature, | 81 const APISignature* signature, |
| 81 gin::Arguments* args); | 82 gin::Arguments* args); |
| 82 | 83 |
| 84 // The root name of the API, e.g. "tabs" for chrome.tabs. |
| 85 std::string api_name_; |
| 86 |
| 83 // A map from method name to expected signature. | 87 // A map from method name to expected signature. |
| 84 std::map<std::string, std::unique_ptr<APISignature>> signatures_; | 88 std::map<std::string, std::unique_ptr<APISignature>> signatures_; |
| 85 | 89 |
| 86 // The callback to use when an API is invoked with valid arguments. | 90 // The callback to use when an API is invoked with valid arguments. |
| 87 APIMethodCallback method_callback_; | 91 APIMethodCallback method_callback_; |
| 88 | 92 |
| 89 // The reference map for all known types; required to outlive this object. | 93 // The reference map for all known types; required to outlive this object. |
| 90 const ArgumentSpec::RefMap* type_refs_; | 94 const ArgumentSpec::RefMap* type_refs_; |
| 91 | 95 |
| 92 base::WeakPtrFactory<APIBinding> weak_factory_; | 96 base::WeakPtrFactory<APIBinding> weak_factory_; |
| 93 | 97 |
| 94 DISALLOW_COPY_AND_ASSIGN(APIBinding); | 98 DISALLOW_COPY_AND_ASSIGN(APIBinding); |
| 95 }; | 99 }; |
| 96 | 100 |
| 97 } // namespace extensions | 101 } // namespace extensions |
| 98 | 102 |
| 99 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ | 103 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ |
| OLD | NEW |