summaryrefslogtreecommitdiffstats
path: root/ppapi/proxy/plugin_resource_tracker.h
blob: b96831ca6094a54ccf19512091eed657c2a8f20c (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 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.

#ifndef PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_
#define PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_

#include <map>
#include <utility>

#include "base/compiler_specific.h"
#include "base/memory/linked_ptr.h"
#include "ppapi/c/pp_completion_callback.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_stdint.h"
#include "ppapi/c/pp_resource.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/proxy/host_resource.h"
#include "ppapi/proxy/plugin_var_tracker.h"
#include "ppapi/shared_impl/tracker_base.h"

template<typename T> struct DefaultSingletonTraits;

namespace ppapi {
class Var;
}

namespace pp {
namespace proxy {

class PluginDispatcher;
class PluginResource;

class PluginResourceTracker : public ::ppapi::TrackerBase {
 public:
  // Called by tests that want to specify a specific ResourceTracker. This
  // allows them to use a unique one each time and avoids singletons sticking
  // around across tests.
  static void SetInstanceForTest(PluginResourceTracker* tracker);

  // Returns the global singleton resource tracker for the plugin.
  static PluginResourceTracker* GetInstance();
  static ::ppapi::TrackerBase* GetTrackerBaseInstance();

  // Returns the object associated with the given resource ID, or NULL if
  // there isn't one.
  PluginResource* GetResourceObject(PP_Resource pp_resource);

  // Adds the given resource object to the tracked list, and returns the
  // plugin-local PP_Resource ID that identifies the resource. Note that this
  // PP_Resource is not valid to send to the host, use
  // PluginResource.host_resource() to get that.
  PP_Resource AddResource(linked_ptr<PluginResource> object);

  void AddRefResource(PP_Resource resource);
  void ReleaseResource(PP_Resource resource);

  // Given a host resource, maps it to an existing plugin resource ID if it
  // exists, or returns 0 on failure.
  PP_Resource PluginResourceForHostResource(
      const HostResource& resource) const;

  PluginVarTracker& var_tracker() {
    return var_tracker_test_override_ ? *var_tracker_test_override_
                                      : var_tracker_;
  }

  void set_var_tracker_test_override(PluginVarTracker* t) {
    var_tracker_test_override_ = t;
  }

  // TrackerBase.
  virtual ppapi::ResourceObjectBase* GetResourceAPI(
      PP_Resource res) OVERRIDE;
  virtual ppapi::FunctionGroupBase* GetFunctionAPI(
      PP_Instance inst,
      pp::proxy::InterfaceID id) OVERRIDE;
  virtual PP_Instance GetInstanceForResource(PP_Resource resource) OVERRIDE;
  virtual ppapi::VarTracker* GetVarTracker() OVERRIDE;

 private:
  friend struct DefaultSingletonTraits<PluginResourceTracker>;
  friend class PluginResourceTrackerTest;
  friend class PluginProxyTestHarness;

  PluginResourceTracker();
  virtual ~PluginResourceTracker();

  struct ResourceInfo {
    ResourceInfo();
    ResourceInfo(int ref_count, linked_ptr<PluginResource> r);
    ResourceInfo(const ResourceInfo& other);
    ~ResourceInfo();

    ResourceInfo& operator=(const ResourceInfo& other);

    int ref_count;
    linked_ptr<PluginResource> resource;  // May be NULL.
  };

  void ReleasePluginResourceRef(const PP_Resource& var,
                                bool notify_browser_on_release);

  // Use the var_tracker_test_override_ instead if it's non-NULL.
  //
  // TODO(brettw) this should be somehow separated out from here. I'm thinking
  // of some global object that manages PPAPI globals, including separate var
  // and resource trackers.
  PluginVarTracker var_tracker_;

  // Non-owning pointer to a var tracker mock used by tests. NULL when no
  // test implementation is provided.
  PluginVarTracker* var_tracker_test_override_;

  // Map of plugin resource IDs to the information tracking that resource.
  typedef std::map<PP_Resource, ResourceInfo> ResourceMap;
  ResourceMap resource_map_;

  // Map of host instance/resource pairs to a plugin resource ID.
  typedef std::map<HostResource, PP_Resource> HostResourceMap;
  HostResourceMap host_resource_map_;

  // Tracks the last ID we've sent out as a plugin resource so we don't send
  // duplicates.
  PP_Resource last_resource_id_;

  DISALLOW_COPY_AND_ASSIGN(PluginResourceTracker);
};

}  // namespace proxy
}  // namespace pp

#endif  // PPAPI_PROXY_PLUGIN_RESOURCE_TRACKER_H_