[email protected] | 7309756 | 2012-01-12 19:38:55 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 2 | // 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] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 8 | #include "base/memory/singleton.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 9 | #include "ppapi/proxy/plugin_dispatcher.h" |
[email protected] | 794d83cd | 2011-10-20 19:09:20 | [diff] [blame] | 10 | #include "ppapi/proxy/plugin_globals.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 11 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 12 | #include "ppapi/proxy/serialized_var.h" |
[email protected] | 67c125d | 2011-10-11 18:58:22 | [diff] [blame] | 13 | #include "ppapi/shared_impl/proxy_lock.h" |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 14 | #include "ppapi/shared_impl/resource.h" |
[email protected] | ce701cd | 2011-08-01 21:47:04 | [diff] [blame] | 15 | #include "ppapi/shared_impl/var.h" |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 16 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 17 | namespace ppapi { |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 18 | namespace proxy { |
19 | |||||
[email protected] | b73c659 | 2013-03-30 17:08:13 | [diff] [blame] | 20 | PluginResourceTracker::PluginResourceTracker() : ResourceTracker(THREAD_SAFE) { |
[email protected] | 3eaf543 | 2013-08-13 19:18:11 | [diff] [blame] | 21 | UseOddResourceValueInDebugMode(); |
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 22 | } |
23 | |||||
24 | PluginResourceTracker::~PluginResourceTracker() { | ||||
25 | } | ||||
26 | |||||
[email protected] | f24448db | 2011-01-27 20:40:39 | [diff] [blame] | 27 | PP_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] | 799d1ab | 2010-11-09 17:16:28 | [diff] [blame] | 33 | } |
34 | |||||
raymes | 1087a3e1 | 2015-05-22 04:34:05 | [diff] [blame] | 35 | void 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] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 43 | PP_Resource PluginResourceTracker::AddResource(Resource* object) { |
[email protected] | d72f41d | 2012-12-19 02:24:18 | [diff] [blame] | 44 | // 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] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 49 | 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 | |||||
57 | void 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 | |||||
raymes | 1087a3e1 | 2015-05-22 04:34:05 | [diff] [blame] | 67 | 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] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 74 | PluginDispatcher* dispatcher = |
75 | PluginDispatcher::GetForInstance(object->pp_instance()); | ||||
raymes | 1087a3e1 | 2015-05-22 04:34:05 | [diff] [blame] | 76 | if (dispatcher && !abandoned) { |
[email protected] | 7f8b26b | 2011-08-18 15:41:01 | [diff] [blame] | 77 | // 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] | 96fe50e3 | 2011-05-10 03:47:58 | [diff] [blame] | 80 | dispatcher->Send(new PpapiHostMsg_PPBCore_ReleaseResource( |
[email protected] | ac4b54d | 2011-10-20 23:09:28 | [diff] [blame] | 81 | API_ID_PPB_CORE, object->host_resource())); |
[email protected] | 12dbac94 | 2011-04-01 18:20:42 | [diff] [blame] | 82 | } |
[email protected] | 4614f19 | 2011-01-21 00:26:43 | [diff] [blame] | 83 | } |
84 | } | ||||
85 | |||||
[email protected] | c2932f5e | 2010-11-03 03:22:33 | [diff] [blame] | 86 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 87 | } // namespace ppapi |