File: api_response_validator.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (96 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_RENDERER_BINDINGS_API_RESPONSE_VALIDATOR_H_
#define EXTENSIONS_RENDERER_BINDINGS_API_RESPONSE_VALIDATOR_H_

#include <set>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "v8/include/v8.h"

namespace extensions {
class APITypeReferenceMap;

// A class to validate the responses to API calls sent by the browser. This
// helps ensure that the browser returns values that match the expected schema
// (which corresponds to the public documentation).
// TODO(devlin): This is now used for both API method responses and event
// arguments. Rename to APISignatureValidator?
class APIResponseValidator {
 public:
  // Allow overriding the default failure behavior.
  class TestHandler {
   public:
    using HandlerMethod =
        base::RepeatingCallback<void(const std::string&, const std::string&)>;

    explicit TestHandler(HandlerMethod method);

    TestHandler(const TestHandler&) = delete;
    TestHandler& operator=(const TestHandler&) = delete;

    ~TestHandler();

    // Ignores the given `signature` for testing purposes.
    void IgnoreSignature(std::string signature);

    // Forwards the failure call to the handler `method_`.
    void HandleFailure(const std::string& signature_name,
                       const std::string& error);

    // Returns true if the given `signature_name` should be ignored for
    // testing purposes.
    bool ShouldIgnoreSignature(const std::string& signature_name) const;

   private:
    HandlerMethod method_;

    std::set<std::string> signatures_to_ignore_;
  };

  // The origin of the callback passed to the response.
  enum class CallbackType {
    // NOTE(devlin): There's deliberately not a kNoCallback value here, since
    // ValidateResponse() is only invoked if there was some callback provided.
    // This is important, since some API implementations can adjust behavior
    // based on whether a callback is provided.

    // The callback was directly provided by the author script.
    kCallerProvided,
    // The callback was provided by the API, such as through custom bindings.
    kAPIProvided,
  };

  explicit APIResponseValidator(const APITypeReferenceMap* type_refs);

  APIResponseValidator(const APIResponseValidator&) = delete;
  APIResponseValidator& operator=(const APIResponseValidator&) = delete;

  ~APIResponseValidator();

  // Validates a response against the expected schema. By default, this will
  // NOTREACHED() in cases of validation failure.
  void ValidateResponse(v8::Local<v8::Context> context,
                        const std::string& method_name,
                        const v8::LocalVector<v8::Value>& response_arguments,
                        const std::string& api_error,
                        CallbackType callback_type);

  // Validates a collection of event arguments against the expected schema.
  // By default, this will NOTREACHED() in cases of validation failure.
  void ValidateEvent(v8::Local<v8::Context> context,
                     const std::string& event_name,
                     const v8::LocalVector<v8::Value>& event_args);

 private:
  // The type reference map; guaranteed to outlive this object.
  raw_ptr<const APITypeReferenceMap> type_refs_;
};

}  // namespace extensions

#endif  // EXTENSIONS_RENDERER_BINDINGS_API_RESPONSE_VALIDATOR_H_