summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/bindings/callback_method_retriever.h
blob: 652ff1385577f85b271c9dbcceb8dfe87ee1b594 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_CALLBACK_METHOD_RETRIEVER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_CALLBACK_METHOD_RETRIEVER_H_

#include "third_party/blink/renderer/platform/bindings/callback_function_base.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "v8/include/v8.h"

namespace blink {

class CallbackFunctionBase;
class ExceptionState;

// Helper class to run step 10 of custom element definition, which part of the
// algorithm is expected to be defined in Web IDL so that other standards can
// apply the same algorithm.
//
// https://html.spec.whatwg.org/C/custom-elements.html#element-definition
// step 10. Run the following substeps while catching any exceptions: ...
class PLATFORM_EXPORT CallbackMethodRetriever {
  STACK_ALLOCATED();

 public:
  explicit CallbackMethodRetriever(CallbackFunctionBase* constructor);

  // Get the prototype object from the callback function. Must be invoked prior
  // to GetMethod or GetStaticMethod.
  v8::Local<v8::Object> GetPrototypeObject(ExceptionState&);

  // Returns a function extracted from the prototype chain, or undefined.
  // Throws if the property is neither of function nor undefined.
  v8::Local<v8::Value> GetMethodOrUndefined(const StringView& method_name,
                                            ExceptionState& exception_state) {
    return GetFunctionOrUndefined(prototype_object_, method_name,
                                  exception_state);
  }

  // Returns a function extracted from the prototype chain, or throw a TypeError
  // if a function doesn't exist.
  v8::Local<v8::Function> GetMethodOrThrow(const StringView& method_name,
                                           ExceptionState& exception_state) {
    return GetFunctionOrThrow(prototype_object_, method_name, exception_state);
  }

  // Returns a function extracted from the callback function, or undefined.
  // Throws if the property is neither of function nor undefined.
  v8::Local<v8::Value> GetStaticMethodOrUndefined(
      const StringView& method_name,
      ExceptionState& exception_state) {
    return GetFunctionOrUndefined(constructor_->CallbackObject(), method_name,
                                  exception_state);
  }

  // Returns a function extracted from the callback function, or throw a
  // TypeError if a function doesn't exist.
  v8::Local<v8::Function> GetStaticMethodOrThrow(
      const StringView& method_name,
      ExceptionState& exception_state) {
    return GetFunctionOrThrow(constructor_->CallbackObject(), method_name,
                              exception_state);
  }

 private:
  // Gets |property| from |object|. Throws an exception if the property is
  // neither of function nor undefined.
  v8::Local<v8::Value> GetFunctionOrUndefined(v8::Local<v8::Object> object,
                                              const StringView& property,
                                              ExceptionState&);
  // Gets |property| from |object|. Throws an exception if the property is
  // not a function.
  v8::Local<v8::Function> GetFunctionOrThrow(v8::Local<v8::Object> object,
                                             const StringView& property,
                                             ExceptionState&);

  CallbackFunctionBase* constructor_;
  v8::Isolate* isolate_;
  v8::Local<v8::Context> current_context_;
  v8::Local<v8::Object> prototype_object_;

  DISALLOW_COPY_AND_ASSIGN(CallbackMethodRetriever);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_CALLBACK_METHOD_RETRIEVER_H_