summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/npobject_var.cc
blob: 0616907ea468327f5302a3a3cfedd2f3d4e93044 (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
// Copyright (c) 2011 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 "webkit/plugins/ppapi/npobject_var.h"

#include "base/logging.h"
#include "ppapi/c/pp_var.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebBindings.h"
#include "webkit/plugins/ppapi/host_globals.h"
#include "webkit/plugins/ppapi/host_var_tracker.h"

using webkit::ppapi::HostGlobals;
using WebKit::WebBindings;

namespace ppapi {

// NPObjectVar -----------------------------------------------------------------

NPObjectVar::NPObjectVar(PP_Instance instance,
                         NPObject* np_object)
    : pp_instance_(instance),
      np_object_(np_object) {
  WebBindings::retainObject(np_object_);
  HostGlobals::Get()->host_var_tracker()->AddNPObjectVar(this);
}

NPObjectVar::~NPObjectVar() {
  if (pp_instance())
    HostGlobals::Get()->host_var_tracker()->RemoveNPObjectVar(this);
  WebBindings::releaseObject(np_object_);
}

NPObjectVar* NPObjectVar::AsNPObjectVar() {
  return this;
}

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

void NPObjectVar::InstanceDeleted() {
  DCHECK(pp_instance_);
  HostGlobals::Get()->host_var_tracker()->RemoveNPObjectVar(this);
  pp_instance_ = 0;
}

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

}  // namespace ppapi