summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/npobject_var.h
blob: 6f31e3d3b190d2d4b8215d0bcd34cb004e6ae076 (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
63
64
65
66
67
68
69
70
71
// Copyright (c) 2012 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.

#ifndef WEBKIT_PLUGINS_PPAPI_NPOBJECT_VAR_H_
#define WEBKIT_PLUGINS_PPAPI_NPOBJECT_VAR_H_

#include <string>

#include "base/compiler_specific.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/shared_impl/var.h"
#include "webkit/plugins/webkit_plugins_export.h"

typedef struct NPObject NPObject;
typedef struct _NPVariant NPVariant;
typedef void* NPIdentifier;

namespace ppapi {

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

// Represents a JavaScript object Var. By itself, this represents random
// NPObjects that a given plugin (identified by the resource's module) wants to
// reference. If two different modules reference the same NPObject (like the
// "window" object), then there will be different NPObjectVar's (and hence
// PP_Var IDs) for each module. This allows us to track all references owned by
// a given module and free them when the plugin exits independently of other
// plugins that may be running at the same time.
class NPObjectVar : public Var {
 public:
  // You should always use FromNPObject to create an NPObjectVar. This function
  // guarantees that we maintain the 1:1 mapping between NPObject and
  // NPObjectVar.
  NPObjectVar(PP_Instance instance, NPObject* np_object);

  virtual ~NPObjectVar();

  // Var overrides.
  virtual NPObjectVar* AsNPObjectVar() OVERRIDE;
  virtual PP_VarType GetType() const OVERRIDE;

  // Returns the underlying NPObject corresponding to this NPObjectVar.
  // Guaranteed non-NULL.
  NPObject* np_object() const { return np_object_; }

  // Notification that the instance was deleted, the internal reference will be
  // zeroed out.
  void InstanceDeleted();

  // Possibly 0 if the object has outlived its instance.
  PP_Instance pp_instance() const { return pp_instance_; }

  // Helper function that converts a PP_Var to an object. This will return NULL
  // if the PP_Var is not of object type or the object is invalid.
  WEBKIT_PLUGINS_EXPORT static scoped_refptr<NPObjectVar> FromPPVar(PP_Var var);

 private:
  // Possibly 0 if the object has outlived its instance.
  PP_Instance pp_instance_;

  // Guaranteed non-NULL, this is the underlying object used by WebKit. We
  // hold a reference to this object.
  NPObject* np_object_;

  DISALLOW_COPY_AND_ASSIGN(NPObjectVar);
};

}  // namespace

#endif  // WEBKIT_PLUGINS_PPAPI_NPOBJECT_VAR_H_