Suppression for crbug/241044.
[chromium-blink-merge.git] / chrome / renderer / extensions / scoped_persistent.h
blob20925e4d8b73f9873c7da9b6e40ad1e916c68803
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_
6 #define CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_
8 #include "base/logging.h"
9 #include "v8/include/v8.h"
11 namespace extensions {
13 // A v8::Persistent handle to a V8 value which destroys and clears the
14 // underlying handle on destruction.
15 template <typename T>
16 class ScopedPersistent {
17 public:
18 ScopedPersistent() {
21 explicit ScopedPersistent(v8::Handle<T> handle) {
22 reset(handle);
25 ~ScopedPersistent() {
26 reset();
29 void reset(v8::Handle<T> handle) {
30 handle_.Reset(GetIsolate(handle), handle);
33 void reset() {
34 if (handle_.IsEmpty())
35 return;
36 handle_.Dispose(GetIsolate(handle_));
37 handle_.Clear();
40 v8::Persistent<T> operator->() const {
41 return handle_;
44 v8::Persistent<T> get() const {
45 return handle_;
48 void MakeWeak(void* parameters, v8::NearDeathCallback callback) {
49 handle_.MakeWeak(GetIsolate(handle_), parameters, callback);
52 private:
53 template <typename U>
54 static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) {
55 // Only works for v8::Object and its subclasses. Add specialisations for
56 // anything else.
57 return GetIsolate(object_handle->CreationContext());
59 static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) {
60 return context_handle->GetIsolate();
62 static v8::Isolate* GetIsolate(
63 v8::Handle<v8::ObjectTemplate> template_handle) {
64 return v8::Isolate::GetCurrent();
67 v8::Persistent<T> handle_;
69 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
72 } // namespace extensions
74 #endif // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_