summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/bindings/v8_value_or_script_wrappable_adapter.h
blob: b2d53160dfb958b0143266602f73ad551f10a16d (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
// Copyright 2019 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_VALUE_OR_SCRIPT_WRAPPABLE_ADAPTER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_VALUE_OR_SCRIPT_WRAPPABLE_ADAPTER_H_

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

namespace blink {

class ScriptState;
class ScriptWrappable;

namespace bindings {

// V8ValueOrScriptWrappableAdapter is an adapter to make bindings functions take
// either of v8::Local<v8::Value> or ScriptWrappable*.
//
// This class is only designed to be used as function argument types in bindings
// layer.  Please refrain from abuse for other purpose and/or outside bindings.
class PLATFORM_EXPORT V8ValueOrScriptWrappableAdapter {
  STACK_ALLOCATED();

 public:
  // Supports implicit conversions from v8::Value and ScriptWrappable type
  // family so that the call sites do not need to recognize this helper class.
  V8ValueOrScriptWrappableAdapter(std::nullptr_t) {}
  V8ValueOrScriptWrappableAdapter(v8::Local<v8::Value> v8_value)
      : v8_value_(v8_value) {
    DCHECK(!v8_value_.IsEmpty());
  }
  V8ValueOrScriptWrappableAdapter(ScriptWrappable* script_wrappable)
      : script_wrappable_(script_wrappable) {}
  template <typename T>
  V8ValueOrScriptWrappableAdapter(Persistent<T> script_wrappable)
      : script_wrappable_(script_wrappable) {
    static_assert(std::is_base_of<ScriptWrappable, T>::value,
                  "script_wrappable must be a ScriptWrappable");
  }

  // Returns the specified v8::Value or the V8 wrapper object of the specified
  // ScriptWrappable.  In the latter case, the wrapper may be created in
  // |creation_context|.
  v8::Local<v8::Value> V8Value(ScriptState* creation_context) const;

  // Returns true when none of v8::Value nor ScriptWrappable is specified.
  bool IsEmpty() const { return v8_value_.IsEmpty() && !script_wrappable_; }

 private:
  v8::Local<v8::Value> v8_value_;
  Member<ScriptWrappable> script_wrappable_;
};

}  // namespace bindings

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_VALUE_OR_SCRIPT_WRAPPABLE_ADAPTER_H_