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