OLD | NEW |
| (Empty) |
1 // Copyright 2014 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. | |
4 | |
5 #ifndef EXTENSIONS_RENDERER_UNSAFE_PERSISTENT_H_ | |
6 #define EXTENSIONS_RENDERER_UNSAFE_PERSISTENT_H_ | |
7 | |
8 #include "v8/include/v8.h" | |
9 | |
10 namespace extensions { | |
11 | |
12 // An unsafe way to pass Persistent handles around. Do not use unless you know | |
13 // what you're doing. UnsafePersistent is only safe to use when we know that the | |
14 // memory pointed by it is not going away: 1) When GC cannot happen while the | |
15 // UnsafePersistent is alive or 2) when there is a strong Persistent keeping the | |
16 // memory alive while the UnsafePersistent is alive. | |
17 template <typename T> | |
18 class UnsafePersistent { | |
19 public: | |
20 UnsafePersistent() : value_(0) {} | |
21 | |
22 explicit UnsafePersistent(v8::Persistent<T>* handle) { | |
23 value_ = handle->ClearAndLeak(); | |
24 } | |
25 | |
26 UnsafePersistent(v8::Isolate* isolate, const v8::Handle<T>& handle) { | |
27 v8::Persistent<T> persistent(isolate, handle); | |
28 value_ = persistent.ClearAndLeak(); | |
29 } | |
30 | |
31 // Usage of this function requires | |
32 // V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR to be defined | |
33 void dispose() { | |
34 v8::Persistent<T> handle(value_); | |
35 handle.Reset(); | |
36 value_ = 0; | |
37 } | |
38 | |
39 // Usage of this function requires | |
40 // V8_ALLOW_ACCESS_TO_RAW_HANDLE_CONSTRUCTOR to be defined | |
41 v8::Local<T> newLocal(v8::Isolate* isolate) { | |
42 return v8::Local<T>::New(isolate, v8::Local<T>(value_)); | |
43 } | |
44 | |
45 private: | |
46 T* value_; | |
47 }; | |
48 | |
49 } // namespace extensions | |
50 | |
51 #endif // EXTENSIONS_RENDERER_UNSAFE_PERSISTENT_H_ | |
OLD | NEW |