| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 #include <string> |
| 11 |
| 12 #include "base/callback.h" |
| 13 #include "base/macros.h" |
| 14 #include "extensions/renderer/api_binding_types.h" |
| 15 #include "v8/include/v8.h" |
| 16 |
| 17 namespace gin { |
| 18 class Arguments; |
| 19 } |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 // A class to register custom hooks for given API calls that need different |
| 24 // handling. An instance exists for a single API, but can be used across |
| 25 // multiple contexts (but only on the same thread). |
| 26 // TODO(devlin): We have both C++ and JS custom bindings, but this only allows |
| 27 // for registration of C++ handlers. Add JS support. |
| 28 class APIBindingHooks { |
| 29 public: |
| 30 // The callback to handle an API method. We pass in the expected signature |
| 31 // (so the caller can verify arguments, optionally after modifying/"massaging" |
| 32 // them) and the passed arguments. The handler is responsible for returning, |
| 33 // which depending on the API could mean either returning synchronously |
| 34 // through gin::Arguments::Return or asynchronously through a passed callback. |
| 35 // TODO(devlin): As we continue expanding the hooks interface, we should allow |
| 36 // handlers to register a request so that they don't have to maintain a |
| 37 // reference to the callback themselves. |
| 38 using HandleRequestHook = |
| 39 base::Callback<void(const binding::APISignature*, gin::Arguments*)>; |
| 40 |
| 41 APIBindingHooks(); |
| 42 ~APIBindingHooks(); |
| 43 |
| 44 // Register a custom binding to handle requests. |
| 45 void RegisterHandleRequest(const std::string& method_name, |
| 46 const HandleRequestHook& hook); |
| 47 |
| 48 // Returns the custom hook for the given method, or a null callback if none |
| 49 // exists. |
| 50 HandleRequestHook GetHandleRequest(const std::string& method_name); |
| 51 |
| 52 private: |
| 53 // Whether we've tried to use any hooks associated with this object. |
| 54 bool hooks_used_ = false; |
| 55 |
| 56 // All registered request handlers. |
| 57 std::map<std::string, HandleRequestHook> request_hooks_; |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(APIBindingHooks); |
| 60 }; |
| 61 |
| 62 } // namespace extensions |
| 63 |
| 64 #endif // EXTENSIONS_RENDERER_API_BINDING_HOOKS_H_ |
| OLD | NEW |