OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef GIN_WRAPPABLE_H_ |
| 6 #define GIN_WRAPPABLE_H_ |
| 7 |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "gin/converter.h" |
| 10 #include "gin/public/wrapper_info.h" |
| 11 |
| 12 namespace gin { |
| 13 |
| 14 class Wrappable : public base::RefCounted<Wrappable> { |
| 15 public: |
| 16 virtual WrapperInfo* GetWrapperInfo() = 0; |
| 17 |
| 18 protected: |
| 19 Wrappable(); |
| 20 virtual ~Wrappable(); |
| 21 |
| 22 private: |
| 23 friend class base::RefCounted<Wrappable>; |
| 24 friend struct Converter<Wrappable*>; |
| 25 |
| 26 static void WeakCallback( |
| 27 const v8::WeakCallbackData<v8::Object, Wrappable>& data); |
| 28 v8::Handle<v8::Object> CreateWrapper(v8::Isolate* isolate); |
| 29 |
| 30 v8::Persistent<v8::Object> wrapper_; // Weak |
| 31 |
| 32 DISALLOW_COPY_AND_ASSIGN(Wrappable); |
| 33 }; |
| 34 |
| 35 template<> |
| 36 struct Converter<Wrappable*> { |
| 37 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 38 Wrappable* val); |
| 39 static bool FromV8(v8::Handle<v8::Value> val, |
| 40 Wrappable** out); |
| 41 }; |
| 42 |
| 43 template<typename T> |
| 44 struct WrappableConverter { |
| 45 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { |
| 46 return Converter<Wrappable*>::ToV8(isolate, val); |
| 47 } |
| 48 static bool FromV8(v8::Handle<v8::Value> val, T** out) { |
| 49 Wrappable* wrappable = 0; |
| 50 if (!Converter<Wrappable*>::FromV8(val, &wrappable) |
| 51 || wrappable->GetWrapperInfo() != &T::kWrapperInfo) |
| 52 return false; |
| 53 *out = static_cast<T*>(wrappable); |
| 54 return true; |
| 55 } |
| 56 }; |
| 57 |
| 58 } // namespace gin |
| 59 |
| 60 #endif // GIN_WRAPPABLE_H_ |
OLD | NEW |