[M120 Migration] Set IO|GPU thread type with higher priorites
[platform/framework/web/chromium-efl.git] / gin / wrappable.cc
1 // Copyright 2013 The Chromium Authors
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 "gin/wrappable.h"
6
7 #include "base/check_op.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_isolate_data.h"
10
11 namespace gin {
12
13 WrappableBase::WrappableBase() = default;
14
15 WrappableBase::~WrappableBase() {
16   wrapper_.Reset();
17 }
18
19 ObjectTemplateBuilder WrappableBase::GetObjectTemplateBuilder(
20     v8::Isolate* isolate) {
21   return ObjectTemplateBuilder(isolate, GetTypeName());
22 }
23
24 const char* WrappableBase::GetTypeName() {
25   return nullptr;
26 }
27
28 void WrappableBase::FirstWeakCallback(
29     const v8::WeakCallbackInfo<WrappableBase>& data) {
30   WrappableBase* wrappable = data.GetParameter();
31   wrappable->dead_ = true;
32   wrappable->wrapper_.Reset();
33   data.SetSecondPassCallback(SecondWeakCallback);
34 }
35
36 void WrappableBase::SecondWeakCallback(
37     const v8::WeakCallbackInfo<WrappableBase>& data) {
38   WrappableBase* wrappable = data.GetParameter();
39   delete wrappable;
40 }
41
42 v8::MaybeLocal<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate,
43                                                          WrapperInfo* info) {
44   if (!wrapper_.IsEmpty()) {
45     return v8::MaybeLocal<v8::Object>(
46         v8::Local<v8::Object>::New(isolate, wrapper_));
47   }
48
49   if (dead_)
50     return v8::MaybeLocal<v8::Object>();
51
52   PerIsolateData* data = PerIsolateData::From(isolate);
53   v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info);
54   if (templ.IsEmpty()) {
55     templ = GetObjectTemplateBuilder(isolate).Build();
56     CHECK(!templ.IsEmpty());
57     data->SetObjectTemplate(info, templ);
58   }
59   CHECK_EQ(kNumberOfInternalFields, templ->InternalFieldCount());
60   v8::Local<v8::Object> wrapper;
61   // |wrapper| may be empty in some extreme cases, e.g., when
62   // Object.prototype.constructor is overwritten.
63   if (!templ->NewInstance(isolate->GetCurrentContext()).ToLocal(&wrapper)) {
64     // The current wrappable object will be no longer managed by V8. Delete this
65     // now.
66     delete this;
67     return v8::MaybeLocal<v8::Object>(wrapper);
68   }
69
70   int indices[] = {kWrapperInfoIndex, kEncodedValueIndex};
71   void* values[] = {info, this};
72   wrapper->SetAlignedPointerInInternalFields(2, indices, values);
73   wrapper_.Reset(isolate, wrapper);
74   wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
75   return v8::MaybeLocal<v8::Object>(wrapper);
76 }
77
78 namespace internal {
79
80 void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val,
81                  WrapperInfo* wrapper_info) {
82   if (!val->IsObject())
83     return NULL;
84   v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
85   WrapperInfo* info = WrapperInfo::From(obj);
86
87   // If this fails, the object is not managed by Gin. It is either a normal JS
88   // object that's not wrapping any external C++ object, or it is wrapping some
89   // C++ object, but that object isn't managed by Gin (maybe Blink).
90   if (!info)
91     return NULL;
92
93   // If this fails, the object is managed by Gin, but it's not wrapping an
94   // instance of the C++ class associated with wrapper_info.
95   if (info != wrapper_info)
96     return NULL;
97
98   return obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
99 }
100
101 }  // namespace internal
102
103 }  // namespace gin