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 | |
jochen (gone - plz use gerrit)
2013/11/21 13:06:28
if you end up copying a weak persistent, bad thing
| |
31 }; | |
32 | |
33 template<> | |
34 struct Converter<Wrappable*> { | |
35 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | |
36 Wrappable* val); | |
37 static bool FromV8(v8::Handle<v8::Value> val, | |
38 Wrappable** out); | |
39 }; | |
40 | |
41 template<typename T> | |
42 struct WrappableConverter { | |
43 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, T* val) { | |
44 return Converter<Wrappable*>::ToV8(isolate, val); | |
45 } | |
46 static bool FromV8(v8::Handle<v8::Value> val, T** out) { | |
47 Wrappable* wrappable = 0; | |
48 if (!Converter<Wrappable*>::FromV8(val, &wrappable) | |
49 || wrappable->GetWrapperInfo() != &T::kWrapperInfo) | |
50 return false; | |
51 *out = static_cast<T*>(wrappable); | |
52 return true; | |
53 } | |
54 }; | |
55 | |
56 } // namespace gin | |
57 | |
58 #endif // GIN_WRAPPABLE_H_ | |
OLD | NEW |