| 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 16 matching lines...) Expand all  Loading... | 
|  27   } |  27   } | 
|  28  |  28  | 
|  29   void reset(v8::Handle<T> handle) { |  29   void reset(v8::Handle<T> handle) { | 
|  30     if (!handle.IsEmpty()) |  30     if (!handle.IsEmpty()) | 
|  31       handle_.Reset(GetIsolate(handle), handle); |  31       handle_.Reset(GetIsolate(handle), handle); | 
|  32     else |  32     else | 
|  33       reset(); |  33       reset(); | 
|  34   } |  34   } | 
|  35  |  35  | 
|  36   void reset() { |  36   void reset() { | 
|  37     if (handle_.IsEmpty()) |  37     handle_.Reset(); | 
|  38       return; |  | 
|  39     handle_.Dispose(); |  | 
|  40     handle_.Clear(); |  | 
|  41   } |  38   } | 
|  42  |  39  | 
|  43   bool IsEmpty() const { |  40   bool IsEmpty() const { | 
|  44     return handle_.IsEmpty(); |  41     return handle_.IsEmpty(); | 
|  45   } |  42   } | 
|  46  |  43  | 
|  47   v8::Handle<T> NewHandle() const { |  44   v8::Handle<T> NewHandle() const { | 
|  48     if (handle_.IsEmpty()) |  45     if (handle_.IsEmpty()) | 
|  49       return v8::Local<T>(); |  46       return v8::Local<T>(); | 
|  50     return v8::Local<T>::New(GetIsolate(handle_), handle_); |  47     return v8::Local<T>::New(GetIsolate(handle_), handle_); | 
|  51   } |  48   } | 
|  52  |  49  | 
|  53   v8::Handle<T> NewHandle(v8::Isolate* isolate) const { |  50   v8::Handle<T> NewHandle(v8::Isolate* isolate) const { | 
|  54     if (handle_.IsEmpty()) |  51     if (handle_.IsEmpty()) | 
|  55       return v8::Local<T>(); |  52       return v8::Local<T>(); | 
|  56     return v8::Local<T>::New(isolate, handle_); |  53     return v8::Local<T>::New(isolate, handle_); | 
|  57   } |  54   } | 
|  58  |  55  | 
|  59   template<typename P> |  56   template <typename P> | 
|  60   void MakeWeak(P* parameters, |  57   void SetWeak(P* parameters, | 
|  61                 typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) { |  58                typename v8::WeakCallbackData<T, P>::Callback callback) { | 
|  62     handle_.MakeWeak(parameters, callback); |  59     handle_.SetWeak(parameters, callback); | 
|  63   } |  60   } | 
|  64  |  61  | 
|  65  private: |  62  private: | 
|  66   template <typename U> |  63   template <typename U> | 
|  67   static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) { |  64   static v8::Isolate* GetIsolate(v8::Handle<U> object_handle) { | 
|  68     // Only works for v8::Object and its subclasses. Add specialisations for |  65     // Only works for v8::Object and its subclasses. Add specialisations for | 
|  69     // anything else. |  66     // anything else. | 
|  70     if (!object_handle.IsEmpty()) |  67     if (!object_handle.IsEmpty()) | 
|  71       return GetIsolate(object_handle->CreationContext()); |  68       return GetIsolate(object_handle->CreationContext()); | 
|  72     return v8::Isolate::GetCurrent(); |  69     return v8::Isolate::GetCurrent(); | 
|  73   } |  70   } | 
|  74   static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) { |  71   static v8::Isolate* GetIsolate(v8::Handle<v8::Context> context_handle) { | 
|  75     if (!context_handle.IsEmpty()) |  72     if (!context_handle.IsEmpty()) | 
|  76       return context_handle->GetIsolate(); |  73       return context_handle->GetIsolate(); | 
|  77     return v8::Isolate::GetCurrent(); |  74     return v8::Isolate::GetCurrent(); | 
|  78   } |  75   } | 
|  79   static v8::Isolate* GetIsolate( |  76   static v8::Isolate* GetIsolate( | 
|  80       v8::Handle<v8::ObjectTemplate> template_handle) { |  77       v8::Handle<v8::ObjectTemplate> template_handle) { | 
|  81     return v8::Isolate::GetCurrent(); |  78     return v8::Isolate::GetCurrent(); | 
|  82   } |  79   } | 
|  83  |  80  | 
|  84   v8::Persistent<T> handle_; |  81   v8::Persistent<T> handle_; | 
|  85  |  82  | 
|  86   DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); |  83   DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); | 
|  87 }; |  84 }; | 
|  88  |  85  | 
|  89 }  // namespace extensions |  86 }  // namespace extensions | 
|  90  |  87  | 
|  91 #endif  // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ |  88 #endif  // CHROME_RENDERER_EXTENSIONS_SCOPED_PERSISTENT_H_ | 
| OLD | NEW |