Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: ppapi/proxy/plugin_resource_tracker.cc

Issue 6334016: Refactor PPAPI proxy resource handling to maintain which host they came from,... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/proxy/plugin_resource_tracker.h ('k') | ppapi/proxy/plugin_resource_tracker_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/proxy/plugin_resource_tracker.h" 5 #include "ppapi/proxy/plugin_resource_tracker.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/singleton.h" 8 #include "base/singleton.h"
9 #include "ppapi/proxy/plugin_dispatcher.h" 9 #include "ppapi/proxy/plugin_dispatcher.h"
10 #include "ppapi/proxy/ppapi_messages.h" 10 #include "ppapi/proxy/ppapi_messages.h"
11 #include "ppapi/proxy/plugin_resource.h" 11 #include "ppapi/proxy/plugin_resource.h"
12 #include "ppapi/proxy/serialized_var.h" 12 #include "ppapi/proxy/serialized_var.h"
13 13
14 namespace pp { 14 namespace pp {
15 namespace proxy { 15 namespace proxy {
16 16
17 namespace {
18
19 // When non-NULL, this object overrides the ResourceTrackerSingleton.
20 PluginResourceTracker* g_resource_tracker_override = NULL;
21
22 } // namespace
23
17 PluginResourceTracker::ResourceInfo::ResourceInfo() : ref_count(0) { 24 PluginResourceTracker::ResourceInfo::ResourceInfo() : ref_count(0) {
18 } 25 }
19 26
20 PluginResourceTracker::ResourceInfo::ResourceInfo(int rc, 27 PluginResourceTracker::ResourceInfo::ResourceInfo(int rc,
21 linked_ptr<PluginResource> r) 28 linked_ptr<PluginResource> r)
22 : ref_count(rc), 29 : ref_count(rc),
23 resource(r) { 30 resource(r) {
24 } 31 }
25 32
26 PluginResourceTracker::ResourceInfo::ResourceInfo(const ResourceInfo& other) 33 PluginResourceTracker::ResourceInfo::ResourceInfo(const ResourceInfo& other)
27 : ref_count(other.ref_count), 34 : ref_count(other.ref_count),
28 resource(other.resource) { 35 resource(other.resource) {
29 } 36 }
30 37
31 PluginResourceTracker::ResourceInfo::~ResourceInfo() { 38 PluginResourceTracker::ResourceInfo::~ResourceInfo() {
32 } 39 }
33 40
34 PluginResourceTracker::ResourceInfo& 41 PluginResourceTracker::ResourceInfo&
35 PluginResourceTracker::ResourceInfo::operator=( 42 PluginResourceTracker::ResourceInfo::operator=(
36 const ResourceInfo& other) { 43 const ResourceInfo& other) {
37 ref_count = other.ref_count; 44 ref_count = other.ref_count;
38 resource = other.resource; 45 resource = other.resource;
39 return *this; 46 return *this;
40 } 47 }
41 48
42 PluginResourceTracker::PluginResourceTracker() { 49 // Start counting resources at a high number to avoid collisions with vars (to
50 // help debugging).
51 PluginResourceTracker::PluginResourceTracker()
52 : last_resource_id_(0x00100000) {
43 } 53 }
44 54
45 PluginResourceTracker::~PluginResourceTracker() { 55 PluginResourceTracker::~PluginResourceTracker() {
46 } 56 }
47 57
48 // static 58 // static
59 void PluginResourceTracker::SetInstanceForTest(PluginResourceTracker* tracker) {
60 g_resource_tracker_override = tracker;
61 }
62
63 // static
49 PluginResourceTracker* PluginResourceTracker::GetInstance() { 64 PluginResourceTracker* PluginResourceTracker::GetInstance() {
65 if (g_resource_tracker_override)
66 return g_resource_tracker_override;
50 return Singleton<PluginResourceTracker>::get(); 67 return Singleton<PluginResourceTracker>::get();
51 } 68 }
52 69
53 PluginResource* PluginResourceTracker::GetResourceObject( 70 PluginResource* PluginResourceTracker::GetResourceObject(
54 PP_Resource pp_resource) { 71 PP_Resource pp_resource) {
55 ResourceMap::iterator found = resource_map_.find(pp_resource); 72 ResourceMap::iterator found = resource_map_.find(pp_resource);
56 if (found == resource_map_.end()) 73 if (found == resource_map_.end())
57 return NULL; 74 return NULL;
58 return found->second.resource.get(); 75 return found->second.resource.get();
59 } 76 }
60 77
61 void PluginResourceTracker::AddResource(PP_Resource pp_resource, 78 PP_Resource PluginResourceTracker::AddResource(
62 linked_ptr<PluginResource> object) { 79 linked_ptr<PluginResource> object) {
63 DCHECK(resource_map_.find(pp_resource) == resource_map_.end()); 80 if (object->host_resource().is_null()) {
64 resource_map_[pp_resource] = ResourceInfo(1, object); 81 // Prevent adding null resources or GetResourceObject(0) will return a
82 // valid pointer!
83 NOTREACHED();
84 return 0;
85 }
86
87 PP_Resource plugin_resource = ++last_resource_id_;
88 DCHECK(resource_map_.find(plugin_resource) == resource_map_.end());
89 resource_map_[plugin_resource] = ResourceInfo(1, object);
90 host_resource_map_[object->host_resource()] = plugin_resource;
91 return plugin_resource;
65 } 92 }
66 93
67 void PluginResourceTracker::AddRefResource(PP_Resource resource) { 94 void PluginResourceTracker::AddRefResource(PP_Resource resource) {
68 resource_map_[resource].ref_count++; 95 ResourceMap::iterator found = resource_map_.find(resource);
96 if (found == resource_map_.end()) {
97 NOTREACHED();
98 return;
99 }
100 found->second.ref_count++;
69 } 101 }
70 102
71 void PluginResourceTracker::ReleaseResource(PP_Resource resource) { 103 void PluginResourceTracker::ReleaseResource(PP_Resource resource) {
72 ReleasePluginResourceRef(resource, true); 104 ReleasePluginResourceRef(resource, true);
73 } 105 }
74 106
75 bool PluginResourceTracker::PreparePreviouslyTrackedResource( 107 PP_Resource PluginResourceTracker::PluginResourceForHostResource(
76 PP_Resource resource) { 108 const HostResource& resource) const {
77 ResourceMap::iterator found = resource_map_.find(resource); 109 HostResourceMap::const_iterator found = host_resource_map_.find(resource);
78 if (found == resource_map_.end()) 110 if (found == host_resource_map_.end())
79 return false; // We've not seen this resource before. 111 return 0;
80 112 return found->second;
81 // We have already seen this resource and the caller wants the plugin to
82 // have one more ref to the object (this function is called when retuning
83 // a resource).
84 //
85 // This is like the PluginVarTracker::ReceiveObjectPassRef. We do an AddRef
86 // in the plugin for the additional ref, and then a Release in the renderer
87 // because the code in the renderer addrefed on behalf of the caller.
88 found->second.ref_count++;
89
90 SendReleaseResourceToHost(resource, found->second.resource.get());
91 return true;
92 } 113 }
93 114
94 void PluginResourceTracker::ReleasePluginResourceRef( 115 void PluginResourceTracker::ReleasePluginResourceRef(
95 const PP_Resource& resource, 116 const PP_Resource& resource,
96 bool notify_browser_on_release) { 117 bool notify_browser_on_release) {
97 ResourceMap::iterator found = resource_map_.find(resource); 118 ResourceMap::iterator found = resource_map_.find(resource);
98 if (found == resource_map_.end()) 119 if (found == resource_map_.end())
99 return; 120 return;
100 found->second.ref_count--; 121 found->second.ref_count--;
101 if (found->second.ref_count == 0) { 122 if (found->second.ref_count == 0) {
123 PluginResource* plugin_resource = found->second.resource.get();
102 if (notify_browser_on_release) 124 if (notify_browser_on_release)
103 SendReleaseResourceToHost(resource, found->second.resource.get()); 125 SendReleaseResourceToHost(resource, plugin_resource);
126 host_resource_map_.erase(plugin_resource->host_resource());
104 resource_map_.erase(found); 127 resource_map_.erase(found);
105 } 128 }
106 } 129 }
107 130
108 void PluginResourceTracker::SendReleaseResourceToHost( 131 void PluginResourceTracker::SendReleaseResourceToHost(
109 PP_Resource resource_id, 132 PP_Resource resource_id,
110 PluginResource* resource) { 133 PluginResource* resource) {
111 PluginDispatcher* dispatcher = 134 PluginDispatcher* dispatcher =
112 PluginDispatcher::GetForInstance(resource->instance()); 135 PluginDispatcher::GetForInstance(resource->instance());
113 if (dispatcher) { 136 if (dispatcher) {
114 dispatcher->Send(new PpapiHostMsg_PPBCore_ReleaseResource( 137 dispatcher->Send(new PpapiHostMsg_PPBCore_ReleaseResource(
115 INTERFACE_ID_PPB_CORE, resource_id)); 138 INTERFACE_ID_PPB_CORE, resource->host_resource()));
116 } else { 139 } else {
117 NOTREACHED(); 140 NOTREACHED();
118 } 141 }
119 } 142 }
120 143
121 } // namespace proxy 144 } // namespace proxy
122 } // namespace pp 145 } // namespace pp
OLDNEW
« no previous file with comments | « ppapi/proxy/plugin_resource_tracker.h ('k') | ppapi/proxy/plugin_resource_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698