summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/bindings/v8_global_value_map.h
blob: 6ca82aea230e43cc389e6662087d529b4a0e7418 (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
// Copyright 2015 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_GLOBAL_VALUE_MAP_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_GLOBAL_VALUE_MAP_H_

#include "third_party/blink/renderer/platform/wtf/allocator.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"
#include "v8/include/v8-util.h"
#include "v8/include/v8.h"

namespace blink {

/**
 * A Traits class for v8::GlobalValueMap that uses wtf/HashMap as a
 * backing store.
 *
 * The parameter is_weak will determine whether the references are 'weak'.
 * If so, entries will be removed from the map as the weak references are
 * collected.
 */
template <typename KeyType,
          typename ValueType,
          v8::PersistentContainerCallbackType type>
class V8GlobalValueMapTraits {
  STATIC_ONLY(V8GlobalValueMapTraits);

 public:
  // Map traits:
  typedef HashMap<KeyType, v8::PersistentContainerValue> Impl;
  typedef typename Impl::iterator Iterator;
  static size_t Size(const Impl* impl) { return impl->size(); }
  static bool Empty(Impl* impl) { return impl->IsEmpty(); }
  static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
  static Iterator Begin(Impl* impl) { return impl->begin(); }
  static Iterator End(Impl* impl) { return impl->end(); }
  static v8::PersistentContainerValue Value(Iterator& iter) {
    return iter->value;
  }
  static KeyType Key(Iterator& iter) { return iter->key; }
  static v8::PersistentContainerValue Set(Impl* impl,
                                          KeyType key,
                                          v8::PersistentContainerValue value) {
    v8::PersistentContainerValue old_value = Get(impl, key);
    impl->Set(key, value);
    return old_value;
  }
  static v8::PersistentContainerValue Get(const Impl* impl, KeyType key) {
    return impl->at(key);
  }

  static v8::PersistentContainerValue Remove(Impl* impl, KeyType key) {
    return impl->Take(key);
  }

  // Weak traits:
  static const v8::PersistentContainerCallbackType kCallbackType = type;
  typedef v8::GlobalValueMap<KeyType,
                             ValueType,
                             V8GlobalValueMapTraits<KeyType, ValueType, type>>
      MapType;

  typedef void WeakCallbackDataType;

  static WeakCallbackDataType* WeakCallbackParameter(
      MapType* map,
      KeyType key,
      const v8::Local<ValueType>& value) {
    return nullptr;
  }

  static void DisposeCallbackData(WeakCallbackDataType* callback_data) {}

  static MapType* MapFromWeakCallbackInfo(
      const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
    return nullptr;
  }

  static KeyType KeyFromWeakCallbackInfo(
      const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
    return KeyType();
  }

  static void OnWeakCallback(
      const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}

  // Dispose traits:
  static void Dispose(v8::Isolate* isolate,
                      v8::Global<ValueType> value,
                      KeyType key) {}
  static void DisposeWeak(
      const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
};

/**
 * A map for safely storing persistent V8 values, based on
 * v8::GlobalValueMap.
 */
template <typename KeyType,
          typename ValueType,
          v8::PersistentContainerCallbackType type>
class V8GlobalValueMap : public v8::GlobalValueMap<
                             KeyType,
                             ValueType,
                             V8GlobalValueMapTraits<KeyType, ValueType, type>> {
  DISALLOW_NEW();

 public:
  typedef V8GlobalValueMapTraits<KeyType, ValueType, type> Traits;
  explicit V8GlobalValueMap(v8::Isolate* isolate)
      : v8::GlobalValueMap<KeyType, ValueType, Traits>(isolate) {}
  V8GlobalValueMap(v8::Isolate* isolate, const char* label)
      : v8::GlobalValueMap<KeyType, ValueType, Traits>(isolate, label) {}
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_V8_GLOBAL_VALUE_MAP_H_