summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/bindings/v8_interface_bridge.h
blob: fc4c903cae3e124d01730bb550cea7d2a19fa3ea (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2020 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_V8_INTERFACE_BRIDGE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_INTERFACE_BRIDGE_H_

#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_isolate_data.h"
#include "third_party/blink/renderer/platform/bindings/wrapper_type_info.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "v8/include/v8.h"

namespace blink {

class DOMWrapperWorld;

namespace bindings {

// The common base class of code-generated V8-Blink bridge class of IDL
// interfaces and namespaces.
class PLATFORM_EXPORT V8InterfaceBridgeBase {
  STATIC_ONLY(V8InterfaceBridgeBase);

 public:
  // Selects properties to be installed according to origin trial features.
  //
  // There are two usages.
  // 1) At the first call of V8T::InstallContextDependentProperties, install
  //   (a) properties that are not associated to origin trial features (e.g.
  //   secure contexts) plus (b) properties that are associated to origin trial
  //   features and are already enabled by the moment of the call.
  // 2) On 2nd+ call of V8T::InstallContextDependentProperties (when an origin
  //   trial feature gets enabled later on), install only (c) properties that
  //   are associated to the origin trial feature that has got enabled.
  //
  // FeatureSelector() is used for usage 1) and
  // FeatureSelector(feature) is used for usage 2).
  class FeatureSelector final {
   public:
    // Selects all properties not associated to any origin trial feature and
    // properties associated with the origin trial features that are already
    // enabled.
    FeatureSelector() : does_select_all_(true) {}
    // Selects only the properties that are associated to the given origin
    // trial feature.
    FeatureSelector(OriginTrialFeature feature) : selector_(feature) {}
    FeatureSelector(const FeatureSelector&) = default;
    FeatureSelector(FeatureSelector&&) = default;
    ~FeatureSelector() = default;

    FeatureSelector& operator=(const FeatureSelector&) = default;
    FeatureSelector& operator=(FeatureSelector&&) = default;

    // Returns true if properties should be installed.  Arguments |featureN|
    // represent the origin trial features to which the properties are
    // associated.  No argument means that the properties are not associated
    // with any origin trial feature.
    bool AnyOf() const { return does_select_all_; }
    bool AnyOf(OriginTrialFeature feature1) const {
      return does_select_all_ || selector_ == feature1;
    }
    bool AnyOf(OriginTrialFeature feature1, OriginTrialFeature feature2) const {
      return does_select_all_ || selector_ == feature1 || selector_ == feature2;
    }

   private:
    bool does_select_all_ = false;
    OriginTrialFeature selector_ = OriginTrialFeature::kNonExisting;
  };

  using InstallInterfaceTemplateFuncType =
      void (*)(v8::Isolate* isolate,
               const DOMWrapperWorld& world,
               v8::Local<v8::FunctionTemplate> interface_template);
  using InstallUnconditionalPropertiesFuncType =
      void (*)(v8::Isolate* isolate,
               const DOMWrapperWorld& world,
               v8::Local<v8::ObjectTemplate> instance_template,
               v8::Local<v8::ObjectTemplate> prototype_template,
               v8::Local<v8::FunctionTemplate> interface_template);
  using InstallContextIndependentPropertiesFuncType =
      void (*)(v8::Isolate* isolate,
               const DOMWrapperWorld& world,
               v8::Local<v8::ObjectTemplate> instance_template,
               v8::Local<v8::ObjectTemplate> prototype_template,
               v8::Local<v8::FunctionTemplate> interface_template);
  using InstallContextDependentPropertiesFuncType =
      void (*)(v8::Local<v8::Context> context,
               const DOMWrapperWorld& world,
               v8::Local<v8::Object> instance_object,
               v8::Local<v8::Object> prototype_object,
               v8::Local<v8::Function> interface_object,
               v8::Local<v8::FunctionTemplate> interface_template,
               FeatureSelector feature_selector);
};

template <class V8T, class T>
class V8InterfaceBridge : public V8InterfaceBridgeBase {
 public:
  static T* ToWrappable(v8::Isolate* isolate, v8::Local<v8::Value> value) {
    return HasInstance(isolate, value)
               ? ToWrappableUnsafe(value.As<v8::Object>())
               : nullptr;
  }

  static T* ToWrappableUnsafe(v8::Local<v8::Object> value) {
    return ToScriptWrappable(value)->ToImpl<T>();
  }

  static bool HasInstance(v8::Isolate* isolate, v8::Local<v8::Value> value) {
    return V8PerIsolateData::From(isolate)->HasInstance(
        V8T::GetWrapperTypeInfo(), value);
  }

  // Migration adapter
  static bool HasInstance(v8::Local<v8::Value> value, v8::Isolate* isolate) {
    return HasInstance(isolate, value);
  }

  static T* ToImpl(v8::Local<v8::Object> value) {
    return ToWrappableUnsafe(value);
  }

  static T* ToImplWithTypeCheck(v8::Isolate* isolate,
                                v8::Local<v8::Value> value) {
    return ToWrappable(isolate, value);
  }
};

}  // namespace bindings

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_INTERFACE_BRIDGE_H_