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 |
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 | 20 |
21 explicit ScopedPersistent(v8::Handle<T> handle) { | 21 explicit ScopedPersistent(v8::Handle<T> handle) { |
22 reset(handle); | 22 reset(handle); |
23 } | 23 } |
24 | 24 |
25 ~ScopedPersistent() { | 25 ~ScopedPersistent() { |
26 reset(); | 26 reset(); |
27 } | 27 } |
28 | 28 |
29 void reset(v8::Handle<T> handle) { | 29 void reset(v8::Handle<T> handle) { |
30 reset(); | 30 handle_.Reset(GetIsolate(handle), handle); |
31 handle_ = v8::Persistent<T>::New(GetIsolate(handle), handle); | |
32 } | 31 } |
33 | 32 |
34 void reset() { | 33 void reset() { |
35 if (handle_.IsEmpty()) | 34 if (handle_.IsEmpty()) |
36 return; | 35 return; |
37 handle_.Dispose(GetIsolate(handle_)); | 36 handle_.Dispose(GetIsolate(handle_)); |
38 handle_.Clear(); | 37 handle_.Clear(); |
39 } | 38 } |
40 | 39 |
41 v8::Persistent<T> operator->() const { | 40 v8::Persistent<T> operator->() const { |
(...skipping 24 matching lines...) Expand all Loading... |
66 } | 65 } |
67 | 66 |
68 v8::Persistent<T> handle_; | 67 v8::Persistent<T> handle_; |
69 | 68 |
70 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); | 69 DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); |
71 }; | 70 }; |
72 | 71 |
73 } // namespace extensions | 72 } // namespace extensions |
74 | 73 |
75 #endif // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ | 74 #endif // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ |
OLD | NEW |