| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 EXTENSIONS_RENDERER_SCOPED_PERSISTENT_H_ | 5 #ifndef EXTENSIONS_RENDERER_SCOPED_PERSISTENT_H_ |
| 6 #define EXTENSIONS_RENDERER_SCOPED_PERSISTENT_H_ | 6 #define EXTENSIONS_RENDERER_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 |
| 11 namespace extensions { | 11 namespace extensions { |
| 12 | 12 |
| 13 // A v8::Persistent handle to a V8 value which destroys and clears the | 13 // A v8::Persistent handle to a V8 value which destroys and clears the |
| 14 // underlying handle on destruction. | 14 // underlying handle on destruction. |
| 15 template <typename T> | 15 template <typename T> |
| 16 class ScopedPersistent { | 16 class ScopedPersistent { |
| 17 public: | 17 public: |
| 18 ScopedPersistent() {} | 18 ScopedPersistent() {} |
| 19 | 19 |
| 20 explicit ScopedPersistent(v8::Handle<T> handle) { reset(handle); } | 20 explicit ScopedPersistent(v8::Handle<T> handle) { reset(handle); } |
| 21 | 21 |
| 22 ScopedPersistent(v8::Isolate* isolate, v8::Handle<T> handle) { |
| 23 reset(isolate, handle); |
| 24 } |
| 25 |
| 22 ~ScopedPersistent() { reset(); } | 26 ~ScopedPersistent() { reset(); } |
| 23 | 27 |
| 24 void reset(v8::Handle<T> handle) { | 28 void reset(v8::Isolate* isolate, v8::Handle<T> handle) { |
| 25 if (!handle.IsEmpty()) | 29 if (!handle.IsEmpty()) |
| 26 handle_.Reset(GetIsolate(handle), handle); | 30 handle_.Reset(isolate, handle); |
| 27 else | 31 else |
| 28 reset(); | 32 reset(); |
| 29 } | 33 } |
| 30 | 34 |
| 35 void reset(v8::Handle<T> handle) { reset(GetIsolate(handle), handle); } |
| 36 |
| 31 void reset() { handle_.Reset(); } | 37 void reset() { handle_.Reset(); } |
| 32 | 38 |
| 33 bool IsEmpty() const { return handle_.IsEmpty(); } | 39 bool IsEmpty() const { return handle_.IsEmpty(); } |
| 34 | 40 |
| 35 v8::Handle<T> NewHandle() const { | 41 v8::Handle<T> NewHandle() const { |
| 36 if (handle_.IsEmpty()) | 42 if (handle_.IsEmpty()) |
| 37 return v8::Local<T>(); | 43 return v8::Local<T>(); |
| 38 return v8::Local<T>::New(GetIsolate(handle_), handle_); | 44 return v8::Local<T>::New(GetIsolate(handle_), handle_); |
| 39 } | 45 } |
| 40 | 46 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 70 } | 76 } |
| 71 | 77 |
| 72 v8::Persistent<T> handle_; | 78 v8::Persistent<T> handle_; |
| 73 | 79 |
| 74 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); | 80 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); |
| 75 }; | 81 }; |
| 76 | 82 |
| 77 } // namespace extensions | 83 } // namespace extensions |
| 78 | 84 |
| 79 #endif // EXTENSIONS_RENDERER_SCOPED_PERSISTENT_H_ | 85 #endif // EXTENSIONS_RENDERER_SCOPED_PERSISTENT_H_ |
| OLD | NEW |