summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/v8object_var.cc
blob: 9e7ee57c3c1b0b9b2a676e26f7415ba09e4dc55b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/pepper/v8object_var.h"

#include "base/logging.h"
#include "content/public/renderer/pepper_plugin_instance.h"
#include "content/renderer/pepper/host_globals.h"
#include "content/renderer/pepper/pepper_plugin_instance_impl.h"
#include "ppapi/c/pp_var.h"

namespace ppapi {

// V8ObjectVar -----------------------------------------------------------------

V8ObjectVar::V8ObjectVar(PP_Instance instance,
                         v8::Local<v8::Object> v8_object)
    : instance_(content::HostGlobals::Get()->GetInstance(instance)) {
  v8_object_.Reset(instance_->GetIsolate(), v8_object);
  content::HostGlobals::Get()->host_var_tracker()->AddV8ObjectVar(this);
}

V8ObjectVar::~V8ObjectVar() {
  if (instance_)
    content::HostGlobals::Get()->host_var_tracker()->RemoveV8ObjectVar(this);
  v8_object_.Reset();
}

V8ObjectVar* V8ObjectVar::AsV8ObjectVar() {
  return this;
}

PP_VarType V8ObjectVar::GetType() const {
  return PP_VARTYPE_OBJECT;
}

v8::Local<v8::Object> V8ObjectVar::GetHandle() const {
  if (instance_)
    return v8::Local<v8::Object>::New(instance_->GetIsolate(), v8_object_);
  return v8::Local<v8::Object>();
}

void V8ObjectVar::InstanceDeleted() {
  // This is called by the HostVarTracker which will take care of removing us
  // from its set.
  DCHECK(instance_);
  instance_ = nullptr;
}

// static
scoped_refptr<V8ObjectVar> V8ObjectVar::FromPPVar(PP_Var var) {
  if (var.type != PP_VARTYPE_OBJECT)
    return scoped_refptr<V8ObjectVar>(nullptr);
  scoped_refptr<Var> var_object(
      PpapiGlobals::Get()->GetVarTracker()->GetVar(var));
  if (!var_object.get())
    return scoped_refptr<V8ObjectVar>();
  return scoped_refptr<V8ObjectVar>(var_object->AsV8ObjectVar());
}

}  // namespace ppapi