Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / ScriptWrappable.cpp
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "bindings/core/v8/ScriptWrappable.h"
7
8 #include "bindings/core/v8/DOMDataStore.h"
9 #include "bindings/core/v8/V8DOMWrapper.h"
10
11 namespace blink {
12
13 #if COMPILER(MSVC)
14 __declspec(align(4))
15 #endif
16 struct SameSizeAsScriptWrappableBase { };
17
18 COMPILE_ASSERT(sizeof(ScriptWrappableBase) <= sizeof(SameSizeAsScriptWrappableBase), ScriptWrappableBase_should_stay_small);
19
20 struct SameSizeAsScriptWrappable : public ScriptWrappableBase {
21     virtual ~SameSizeAsScriptWrappable() { }
22     v8::Object* m_wrapper;
23 };
24
25 COMPILE_ASSERT(sizeof(ScriptWrappable) <= sizeof(SameSizeAsScriptWrappable), ScriptWrappable_should_stay_small);
26
27 namespace {
28
29 class ScriptWrappableBaseProtector final {
30     WTF_MAKE_NONCOPYABLE(ScriptWrappableBaseProtector);
31 public:
32     ScriptWrappableBaseProtector(ScriptWrappableBase* scriptWrappableBase, const WrapperTypeInfo* wrapperTypeInfo)
33         : m_scriptWrappableBase(scriptWrappableBase), m_wrapperTypeInfo(wrapperTypeInfo)
34     {
35         m_wrapperTypeInfo->refObject(m_scriptWrappableBase);
36     }
37     ~ScriptWrappableBaseProtector()
38     {
39         m_wrapperTypeInfo->derefObject(m_scriptWrappableBase);
40     }
41
42 private:
43     ScriptWrappableBase* m_scriptWrappableBase;
44     const WrapperTypeInfo* m_wrapperTypeInfo;
45 };
46
47 } // namespace
48
49 // ScriptWrappableBase
50
51 v8::Handle<v8::Object> ScriptWrappableBase::wrap(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, const WrapperTypeInfo* wrapperTypeInfo)
52 {
53     // It's possible that no one except for the new wrapper owns this object at
54     // this moment, so we have to prevent GC to collect this object until the
55     // object gets associated with the wrapper.
56     ScriptWrappableBaseProtector protect(this, wrapperTypeInfo);
57
58     ASSERT(!DOMDataStore::containsWrapper(this, isolate));
59
60     v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, wrapperTypeInfo, this, isolate);
61     if (UNLIKELY(wrapper.IsEmpty()))
62         return wrapper;
63
64     wrapperTypeInfo->installConditionallyEnabledProperties(wrapper, isolate);
65     return V8DOMWrapper::associateObjectWithWrapper(isolate, this, wrapperTypeInfo, wrapper);
66 }
67
68 // ScriptWrappable
69
70 v8::Handle<v8::Object> ScriptWrappable::wrap(v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
71 {
72     const WrapperTypeInfo* wrapperTypeInfo = this->wrapperTypeInfo();
73
74     // It's possible that no one except for the new wrapper owns this object at
75     // this moment, so we have to prevent GC to collect this object until the
76     // object gets associated with the wrapper.
77     ScriptWrappableBaseProtector protect(this, wrapperTypeInfo);
78
79     ASSERT(!DOMDataStore::containsWrapper(this, isolate));
80
81     v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, wrapperTypeInfo, toScriptWrappableBase(), isolate);
82     if (UNLIKELY(wrapper.IsEmpty()))
83         return wrapper;
84
85     wrapperTypeInfo->installConditionallyEnabledProperties(wrapper, isolate);
86     return associateWithWrapper(wrapperTypeInfo, wrapper, isolate);
87 }
88
89 v8::Handle<v8::Object> ScriptWrappable::associateWithWrapper(const WrapperTypeInfo* wrapperTypeInfo, v8::Handle<v8::Object> wrapper, v8::Isolate* isolate)
90 {
91     return V8DOMWrapper::associateObjectWithWrapper(isolate, this, wrapperTypeInfo, wrapper);
92 }
93
94 } // namespace blink