OLD | NEW |
1 // Copyright (c) 2010 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2011 The Native Client 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 "native_client/src/shared/ppapi_proxy/plugin_resource_tracker.h" | 5 #include "native_client/src/shared/ppapi_proxy/plugin_resource_tracker.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "native_client/src/include/nacl_macros.h" | 10 #include "native_client/src/include/nacl_macros.h" |
11 #include "native_client/src/include/portability.h" | 11 #include "native_client/src/include/portability.h" |
12 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" | 12 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
13 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" | 13 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/untrusted/srpcgen/ppb_rpc.h" |
14 #include "native_client/src/third_party/ppapi/c/pp_resource.h" | 15 #include "native_client/src/third_party/ppapi/c/pp_resource.h" |
15 #include "srpcgen/ppb_rpc.h" | |
16 | 16 |
17 namespace ppapi_proxy { | 17 namespace ppapi_proxy { |
18 | 18 |
19 PluginResourceTracker::ResourceAndRefCounts::ResourceAndRefCounts( | 19 PluginResourceTracker::ResourceAndRefCounts::ResourceAndRefCounts( |
20 PluginResource* r) : resource(r), browser_refcount(1), plugin_refcount(1) { | 20 PluginResource* r, size_t browser_count) |
| 21 : resource(r), browser_refcount(browser_count), plugin_refcount(1) { |
21 } | 22 } |
22 | 23 |
23 PluginResourceTracker::ResourceAndRefCounts::~ResourceAndRefCounts() { | 24 PluginResourceTracker::ResourceAndRefCounts::~ResourceAndRefCounts() { |
24 } | 25 } |
25 | 26 |
26 scoped_refptr<PluginResource> PluginResourceTracker::GetExistingResource( | 27 scoped_refptr<PluginResource> PluginResourceTracker::GetExistingResource( |
27 PP_Resource res) const { | 28 PP_Resource res) const { |
28 ResourceMap::const_iterator result = live_resources_.find(res); | 29 ResourceMap::const_iterator result = live_resources_.find(res); |
29 if (result == live_resources_.end()) | 30 if (result == live_resources_.end()) |
30 return scoped_refptr<PluginResource>(); | 31 return scoped_refptr<PluginResource>(); |
31 else | 32 else |
32 return result->second.resource; | 33 return result->second.resource; |
33 } | 34 } |
34 | 35 |
35 PluginResourceTracker::PluginResourceTracker() : last_id_(0) { | 36 PluginResourceTracker::PluginResourceTracker() { |
36 } | 37 } |
37 | 38 |
38 void PluginResourceTracker::AddResource(PluginResource* resource, | 39 void PluginResourceTracker::AddResource(PluginResource* resource, |
39 PP_Resource id) { | 40 PP_Resource id, |
| 41 size_t browser_refcount) { |
40 // Add the resource with plugin use-count 1. | 42 // Add the resource with plugin use-count 1. |
41 live_resources_.insert(std::make_pair(id, ResourceAndRefCounts(resource))); | 43 live_resources_.insert( |
| 44 std::make_pair(id, ResourceAndRefCounts(resource, browser_refcount))); |
42 } | 45 } |
43 | 46 |
44 bool PluginResourceTracker::AddRefResource(PP_Resource res) { | 47 bool PluginResourceTracker::AddRefResource(PP_Resource res) { |
45 ResourceMap::iterator i = live_resources_.find(res); | 48 ResourceMap::iterator i = live_resources_.find(res); |
46 if (i == live_resources_.end()) { | 49 if (i == live_resources_.end()) { |
47 return false; | 50 return false; |
48 } else { | 51 } else { |
49 // We don't protect against overflow, since a plugin as malicious as to ref | 52 // We don't protect against overflow, since a plugin as malicious as to ref |
50 // once per every byte in the address space could have just as well unrefed | 53 // once per every byte in the address space could have just as well unrefed |
51 // one time too many. | 54 // one time too many. |
52 i->second.plugin_refcount++; | 55 i->second.plugin_refcount++; |
| 56 if (i->second.browser_refcount == 0) { |
| 57 // If we don't currently have any refcount with the browser, try to |
| 58 // obtain one. |
| 59 i->second.browser_refcount++; |
| 60 ObtainBrowserResource(res); |
| 61 } |
53 return true; | 62 return true; |
54 } | 63 } |
55 } | 64 } |
56 | 65 |
57 bool PluginResourceTracker::UnrefResource(PP_Resource res) { | 66 bool PluginResourceTracker::UnrefResource(PP_Resource res) { |
58 ResourceMap::iterator i = live_resources_.find(res); | 67 ResourceMap::iterator i = live_resources_.find(res); |
59 if (i != live_resources_.end()) { | 68 if (i != live_resources_.end()) { |
60 i->second.plugin_refcount--; | 69 i->second.plugin_refcount--; |
61 if (0 == i->second.plugin_refcount) { | 70 if (0 == i->second.plugin_refcount) { |
62 size_t browser_refcount = i->second.browser_refcount; | 71 size_t browser_refcount = i->second.browser_refcount; |
63 i->second.resource->StoppedTracking(); | |
64 live_resources_.erase(i); | 72 live_resources_.erase(i); |
65 | 73 |
66 // Release all browser references. | 74 // Release all browser references. |
67 ReleaseBrowserResource(res, browser_refcount); | 75 ReleaseBrowserResource(res, browser_refcount); |
68 } | 76 } |
69 return true; | 77 return true; |
70 } else { | 78 } else { |
71 return false; | 79 return false; |
72 } | 80 } |
73 } | 81 } |
74 | 82 |
75 void PluginResourceTracker::ObtainBrowserResource(PP_Resource res) { | 83 void PluginResourceTracker::ObtainBrowserResource(PP_Resource res) { |
76 if (res) { | 84 if (res) { |
77 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); | 85 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); |
78 PpbCoreRpcClient::PPB_Core_AddRefResource(channel, res); | 86 PpbCoreRpcClient::PPB_Core_AddRefResource(channel, res); |
79 } | 87 } |
80 } | 88 } |
81 | 89 |
82 void PluginResourceTracker::ReleaseBrowserResource(PP_Resource res, | 90 void PluginResourceTracker::ReleaseBrowserResource(PP_Resource res, |
83 size_t browser_refcount) { | 91 size_t browser_refcount) { |
84 // Release all browser references. | 92 // Release all browser references. |
85 if (res) { | 93 if (res && (browser_refcount > 0)) { |
86 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); | 94 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); |
87 PpbCoreRpcClient::ReleaseResourceMultipleTimes(channel, res, | 95 PpbCoreRpcClient::ReleaseResourceMultipleTimes(channel, res, |
88 browser_refcount); | 96 browser_refcount); |
89 } | 97 } |
90 } | 98 } |
91 | 99 |
92 } // namespace ppapi_proxy | 100 } // namespace ppapi_proxy |
93 | 101 |
OLD | NEW |