Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / pepper / v8object_var.cc
blobfaf8bef8e7a492d034afa289b5b259bac846cab2
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.
5 #include "content/renderer/pepper/v8object_var.h"
7 #include "base/logging.h"
8 #include "content/public/renderer/pepper_plugin_instance.h"
9 #include "content/renderer/pepper/host_globals.h"
10 #include "content/renderer/pepper/pepper_plugin_instance_impl.h"
11 #include "ppapi/c/pp_var.h"
13 namespace ppapi {
15 // V8ObjectVar -----------------------------------------------------------------
17 V8ObjectVar::V8ObjectVar(PP_Instance instance,
18 v8::Handle<v8::Object> v8_object)
19 : instance_(content::HostGlobals::Get()->GetInstance(instance)) {
20 v8_object_.Reset(instance_->GetIsolate(), v8_object);
21 content::HostGlobals::Get()->host_var_tracker()->AddV8ObjectVar(this);
24 V8ObjectVar::~V8ObjectVar() {
25 if (instance_)
26 content::HostGlobals::Get()->host_var_tracker()->RemoveV8ObjectVar(this);
27 v8_object_.Reset();
30 V8ObjectVar* V8ObjectVar::AsV8ObjectVar() {
31 return this;
34 PP_VarType V8ObjectVar::GetType() const {
35 return PP_VARTYPE_OBJECT;
38 v8::Local<v8::Object> V8ObjectVar::GetHandle() const {
39 if (instance_)
40 return v8::Local<v8::Object>::New(instance_->GetIsolate(), v8_object_);
41 return v8::Local<v8::Object>();
44 void V8ObjectVar::InstanceDeleted() {
45 // This is called by the HostVarTracker which will take care of removing us
46 // from its set.
47 DCHECK(instance_);
48 instance_ = NULL;
51 // static
52 scoped_refptr<V8ObjectVar> V8ObjectVar::FromPPVar(PP_Var var) {
53 if (var.type != PP_VARTYPE_OBJECT)
54 return scoped_refptr<V8ObjectVar>(NULL);
55 scoped_refptr<Var> var_object(
56 PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
57 if (!var_object.get())
58 return scoped_refptr<V8ObjectVar>();
59 return scoped_refptr<V8ObjectVar>(var_object->AsV8ObjectVar());
62 } // namespace ppapi