OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ | 5 #ifndef CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ |
6 #define CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ | 6 #define CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ |
7 | 7 |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "v8/include/v8.h" | 9 #include "v8/include/v8.h" |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... |
33 reset(); | 33 reset(); |
34 } | 34 } |
35 | 35 |
36 void reset() { | 36 void reset() { |
37 if (handle_.IsEmpty()) | 37 if (handle_.IsEmpty()) |
38 return; | 38 return; |
39 handle_.Dispose(); | 39 handle_.Dispose(); |
40 handle_.Clear(); | 40 handle_.Clear(); |
41 } | 41 } |
42 | 42 |
43 bool IsEmpty() const { | 43 v8::Handle<T> operator->() const { |
44 return handle_.IsEmpty(); | 44 return get(); |
| 45 } |
| 46 |
| 47 // TODO(dcarney): Remove this function |
| 48 // This is an unsafe access to the underlying handle |
| 49 v8::Handle<T> get() const { |
| 50 return *reinterpret_cast<v8::Handle<T>*>( |
| 51 const_cast<v8::Persistent<T>* >(&handle_)); |
45 } | 52 } |
46 | 53 |
47 v8::Handle<T> NewHandle() const { | 54 v8::Handle<T> NewHandle() const { |
48 if (handle_.IsEmpty()) | 55 if (handle_.IsEmpty()) |
49 return v8::Local<T>(); | 56 return v8::Local<T>(); |
50 return v8::Local<T>::New(GetIsolate(handle_), handle_); | 57 return v8::Local<T>::New(GetIsolate(handle_), handle_); |
51 } | 58 } |
52 | 59 |
53 v8::Handle<T> NewHandle(v8::Isolate* isolate) const { | |
54 if (handle_.IsEmpty()) | |
55 return v8::Local<T>(); | |
56 return v8::Local<T>::New(isolate, handle_); | |
57 } | |
58 | |
59 template<typename P> | 60 template<typename P> |
60 void MakeWeak(P* parameters, | 61 void MakeWeak(P* parameters, |
61 typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) { | 62 typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) { |
62 handle_.MakeWeak(parameters, callback); | 63 handle_.MakeWeak(parameters, callback); |
63 } | 64 } |
64 | 65 |
65 private: | 66 private: |
66 template <typename U> | 67 template <typename U> |
67 static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) { | 68 static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) { |
68 // Only works for v8::Object and its subclasses. Add specialisations for | 69 // Only works for v8::Object and its subclasses. Add specialisations for |
(...skipping 13 matching lines...) Expand all Loading... |
82 } | 83 } |
83 | 84 |
84 v8::Persistent<T> handle_; | 85 v8::Persistent<T> handle_; |
85 | 86 |
86 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); | 87 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); |
87 }; | 88 }; |
88 | 89 |
89 } // namespace extensions | 90 } // namespace extensions |
90 | 91 |
91 #endif // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ | 92 #endif // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ |
OLD | NEW |