blob: 7cf7caf6546c081f0edd3f2a87860d0ef61bee1e [file] [log] [blame]
[email protected]73097562012-01-12 19:38:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c2932f5e2010-11-03 03:22:332// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/proxy/plugin_resource_tracker.h"
6
7#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:158#include "base/memory/singleton.h"
[email protected]c2932f5e2010-11-03 03:22:339#include "ppapi/proxy/plugin_dispatcher.h"
[email protected]794d83cd2011-10-20 19:09:2010#include "ppapi/proxy/plugin_globals.h"
[email protected]c2932f5e2010-11-03 03:22:3311#include "ppapi/proxy/ppapi_messages.h"
[email protected]c2932f5e2010-11-03 03:22:3312#include "ppapi/proxy/serialized_var.h"
[email protected]67c125d2011-10-11 18:58:2213#include "ppapi/shared_impl/proxy_lock.h"
[email protected]7f8b26b2011-08-18 15:41:0114#include "ppapi/shared_impl/resource.h"
[email protected]ce701cd2011-08-01 21:47:0415#include "ppapi/shared_impl/var.h"
[email protected]c2932f5e2010-11-03 03:22:3316
[email protected]4d2efd22011-08-18 21:58:0217namespace ppapi {
[email protected]c2932f5e2010-11-03 03:22:3318namespace proxy {
19
[email protected]b73c6592013-03-30 17:08:1320PluginResourceTracker::PluginResourceTracker() : ResourceTracker(THREAD_SAFE) {
[email protected]3eaf5432013-08-13 19:18:1121 UseOddResourceValueInDebugMode();
[email protected]c2932f5e2010-11-03 03:22:3322}
23
24PluginResourceTracker::~PluginResourceTracker() {
25}
26
[email protected]f24448db2011-01-27 20:40:3927PP_Resource PluginResourceTracker::PluginResourceForHostResource(
28 const HostResource& resource) const {
29 HostResourceMap::const_iterator found = host_resource_map_.find(resource);
30 if (found == host_resource_map_.end())
31 return 0;
32 return found->second;
[email protected]799d1ab2010-11-09 17:16:2833}
34
raymes1087a3e12015-05-22 04:34:0535void PluginResourceTracker::AbandonResource(PP_Resource res) {
36 DCHECK(GetResource(res));
37 bool inserted = abandoned_resources_.insert(res).second;
38 DCHECK(inserted);
39
40 ReleaseResource(res);
41}
42
[email protected]7f8b26b2011-08-18 15:41:0143PP_Resource PluginResourceTracker::AddResource(Resource* object) {
[email protected]d72f41d2012-12-19 02:24:1844 // If there's a HostResource, it must not be added twice.
45 DCHECK(!object->host_resource().host_resource() ||
46 (host_resource_map_.find(object->host_resource()) ==
47 host_resource_map_.end()));
48
[email protected]7f8b26b2011-08-18 15:41:0149 PP_Resource ret = ResourceTracker::AddResource(object);
50
51 // Some resources are plugin-only, so they don't have a host resource.
52 if (object->host_resource().host_resource())
53 host_resource_map_.insert(std::make_pair(object->host_resource(), ret));
54 return ret;
55}
56
57void PluginResourceTracker::RemoveResource(Resource* object) {
58 ResourceTracker::RemoveResource(object);
59
60 if (!object->host_resource().is_null()) {
61 // The host_resource will be NULL for proxy-only resources, which we
62 // obviously don't need to tell the host about.
63 DCHECK(host_resource_map_.find(object->host_resource()) !=
64 host_resource_map_.end());
65 host_resource_map_.erase(object->host_resource());
66
raymes1087a3e12015-05-22 04:34:0567 bool abandoned = false;
68 auto it = abandoned_resources_.find(object->pp_resource());
69 if (it != abandoned_resources_.end()) {
70 abandoned = true;
71 abandoned_resources_.erase(it);
72 }
73
[email protected]7f8b26b2011-08-18 15:41:0174 PluginDispatcher* dispatcher =
75 PluginDispatcher::GetForInstance(object->pp_instance());
raymes1087a3e12015-05-22 04:34:0576 if (dispatcher && !abandoned) {
[email protected]7f8b26b2011-08-18 15:41:0177 // The dispatcher can be NULL if the plugin held on to a resource after
78 // the instance was destroyed. In that case the browser-side resource has
79 // already been freed correctly on the browser side.
[email protected]96fe50e32011-05-10 03:47:5880 dispatcher->Send(new PpapiHostMsg_PPBCore_ReleaseResource(
[email protected]ac4b54d2011-10-20 23:09:2881 API_ID_PPB_CORE, object->host_resource()));
[email protected]12dbac942011-04-01 18:20:4282 }
[email protected]4614f192011-01-21 00:26:4383 }
84}
85
[email protected]c2932f5e2010-11-03 03:22:3386} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:0287} // namespace ppapi