Have pretty_print.py find histograms.xml.
[chromium-blink-merge.git] / gin / wrappable.cc
blob8d41ce06c4dfc4b5abb0a08b50f5ac84929777c2
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.
5 #include "gin/wrappable.h"
7 #include "base/logging.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_isolate_data.h"
11 namespace gin {
13 WrappableBase::WrappableBase() {
16 WrappableBase::~WrappableBase() {
17 wrapper_.Reset();
20 ObjectTemplateBuilder WrappableBase::GetObjectTemplateBuilder(
21 v8::Isolate* isolate) {
22 return ObjectTemplateBuilder(isolate);
25 void WrappableBase::WeakCallback(
26 const v8::WeakCallbackData<v8::Object, WrappableBase>& data) {
27 WrappableBase* wrappable = data.GetParameter();
28 wrappable->wrapper_.Reset();
29 delete wrappable;
32 v8::Handle<v8::Object> WrappableBase::GetWrapperImpl(v8::Isolate* isolate,
33 WrapperInfo* info) {
34 if (!wrapper_.IsEmpty()) {
35 return v8::Local<v8::Object>::New(isolate, wrapper_);
38 PerIsolateData* data = PerIsolateData::From(isolate);
39 v8::Local<v8::ObjectTemplate> templ = data->GetObjectTemplate(info);
40 if (templ.IsEmpty()) {
41 templ = GetObjectTemplateBuilder(isolate).Build();
42 CHECK(!templ.IsEmpty());
43 data->SetObjectTemplate(info, templ);
45 CHECK_EQ(kNumberOfInternalFields, templ->InternalFieldCount());
46 v8::Handle<v8::Object> wrapper = templ->NewInstance();
47 wrapper->SetAlignedPointerInInternalField(kWrapperInfoIndex, info);
48 wrapper->SetAlignedPointerInInternalField(kEncodedValueIndex, this);
49 wrapper_.Reset(isolate, wrapper);
50 wrapper_.SetWeak(this, WeakCallback);
51 return wrapper;
54 namespace internal {
56 void* FromV8Impl(v8::Isolate* isolate, v8::Handle<v8::Value> val,
57 WrapperInfo* wrapper_info) {
58 if (!val->IsObject())
59 return NULL;
60 v8::Handle<v8::Object> obj = v8::Handle<v8::Object>::Cast(val);
61 WrapperInfo* info = WrapperInfo::From(obj);
63 // If this fails, the object is not managed by Gin. It is either a normal JS
64 // object that's not wrapping any external C++ object, or it is wrapping some
65 // C++ object, but that object isn't managed by Gin (maybe Blink).
66 if (!info)
67 return NULL;
69 // If this fails, the object is managed by Gin, but it's not wrapping an
70 // instance of the C++ class associated with wrapper_info.
71 if (info != wrapper_info)
72 return NULL;
74 return obj->GetAlignedPointerFromInternalField(kEncodedValueIndex);
77 } // namespace internal
79 } // namespace gin