api_response_validator.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2018 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. #ifndef EXTENSIONS_RENDERER_BINDINGS_API_RESPONSE_VALIDATOR_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_RESPONSE_VALIDATOR_H_
  6. #include <set>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback.h"
  10. #include "v8/include/v8.h"
  11. namespace extensions {
  12. class APITypeReferenceMap;
  13. // A class to validate the responses to API calls sent by the browser. This
  14. // helps ensure that the browser returns values that match the expected schema
  15. // (which corresponds to the public documentation).
  16. // TODO(devlin): This is now used for both API method responses and event
  17. // arguments. Rename to APISignatureValidator?
  18. class APIResponseValidator {
  19. public:
  20. // Allow overriding the default failure behavior.
  21. class TestHandler {
  22. public:
  23. using HandlerMethod =
  24. base::RepeatingCallback<void(const std::string&, const std::string&)>;
  25. explicit TestHandler(HandlerMethod method);
  26. TestHandler(const TestHandler&) = delete;
  27. TestHandler& operator=(const TestHandler&) = delete;
  28. ~TestHandler();
  29. // Ignores the given `signature` for testing purposes.
  30. void IgnoreSignature(std::string signature);
  31. // Forwards the failure call to the handler `method_`.
  32. void HandleFailure(const std::string& signature_name,
  33. const std::string& error);
  34. // Returns true if the given `signature_name` should be ignored for
  35. // testing purposes.
  36. bool ShouldIgnoreSignature(const std::string& signature_name) const;
  37. private:
  38. HandlerMethod method_;
  39. std::set<std::string> signatures_to_ignore_;
  40. };
  41. // The origin of the callback passed to the response.
  42. enum class CallbackType {
  43. // NOTE(devlin): There's deliberately not a kNoCallback value here, since
  44. // ValidateResponse() is only invoked if there was some callback provided.
  45. // This is important, since some API implementations can adjust behavior
  46. // based on whether a callback is provided.
  47. // The callback was directly provided by the author script.
  48. kCallerProvided,
  49. // The callback was provided by the API, such as through custom bindings.
  50. kAPIProvided,
  51. };
  52. explicit APIResponseValidator(const APITypeReferenceMap* type_refs);
  53. APIResponseValidator(const APIResponseValidator&) = delete;
  54. APIResponseValidator& operator=(const APIResponseValidator&) = delete;
  55. ~APIResponseValidator();
  56. // Validates a response against the expected schema. By default, this will
  57. // NOTREACHED() in cases of validation failure.
  58. void ValidateResponse(
  59. v8::Local<v8::Context> context,
  60. const std::string& method_name,
  61. const std::vector<v8::Local<v8::Value>> response_arguments,
  62. const std::string& api_error,
  63. CallbackType callback_type);
  64. // Validates a collection of event arguments against the expected schema.
  65. // By default, this will NOTREACHED() in cases of validation failure.
  66. void ValidateEvent(v8::Local<v8::Context> context,
  67. const std::string& event_name,
  68. const std::vector<v8::Local<v8::Value>>& event_args);
  69. private:
  70. // The type reference map; guaranteed to outlive this object.
  71. const APITypeReferenceMap* type_refs_;
  72. };
  73. } // namespace extensions
  74. #endif // EXTENSIONS_RENDERER_BINDINGS_API_RESPONSE_VALIDATOR_H_