Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2010 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" | |
| 11 #include "native_client/src/include/portability.h" | |
| 12 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" | |
| 10 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" | 13 #include "native_client/src/shared/ppapi_proxy/plugin_resource.h" |
| 11 #include "ppapi/c/pp_resource.h" | 14 #include "ppapi/c/pp_resource.h" |
| 15 #include "srpcgen/ppb_rpc.h" | |
| 12 | 16 |
| 13 namespace ppapi_proxy { | 17 namespace ppapi_proxy { |
| 14 | 18 |
| 15 scoped_refptr<PluginResource> | 19 PluginResourceTracker::ResourceAndRefCounts::ResourceAndRefCounts( |
| 16 PluginResourceTracker::GetResource(PP_Resource res) const { | 20 PluginResource* r) : resource(r), browser_refcount(1), plugin_refcount(1) { |
| 17 ResourceMap::const_iterator result = live_resources_.find(res); | |
| 18 if (result == live_resources_.end()) { | |
| 19 return scoped_refptr<PluginResource>(); | |
| 20 } | |
| 21 return result->second.first; | |
| 22 } | 21 } |
| 23 | 22 |
| 24 PluginResourceTracker::PluginResourceTracker() | 23 PluginResourceTracker::ResourceAndRefCounts::~ResourceAndRefCounts() { |
| 25 : last_id_(0) { | |
| 26 } | 24 } |
| 27 | 25 |
| 28 PluginResourceTracker::~PluginResourceTracker() { | 26 scoped_refptr<PluginResource> PluginResourceTracker::GetExistingResource( |
| 27 PP_Resource res) const { | |
| 28 ResourceMap::const_iterator result = live_resources_.find(res); | |
| 29 if (result == live_resources_.end()) | |
| 30 return scoped_refptr<PluginResource>(); | |
| 31 else | |
| 32 return result->second.resource; | |
| 29 } | 33 } |
| 30 | 34 |
| 31 PP_Resource PluginResourceTracker::AddResource(PluginResource* resource) { | 35 PluginResourceTracker::PluginResourceTracker() : last_id_(0) { |
| 32 // If the plugin manages to create 4B resources... | 36 } |
| 33 if (last_id_ == std::numeric_limits<PP_Resource>::max()) { | 37 |
| 34 return 0; | 38 void PluginResourceTracker::AddResource(PluginResource* resource, |
| 35 } | 39 PP_Resource id) { |
| 36 // Add the resource with plugin use-count 1. | 40 // Add the resource with plugin use-count 1. |
| 37 ++last_id_; | 41 live_resources_.insert(std::make_pair(id, ResourceAndRefCounts(resource))); |
| 38 live_resources_.insert(std::make_pair(last_id_, std::make_pair(resource, 1))); | |
| 39 return last_id_; | |
| 40 } | 42 } |
| 41 | 43 |
| 42 bool PluginResourceTracker::AddRefResource(PP_Resource res) { | 44 bool PluginResourceTracker::AddRefResource(PP_Resource res) { |
| 43 ResourceMap::iterator i = live_resources_.find(res); | 45 ResourceMap::iterator i = live_resources_.find(res); |
| 44 if (i == live_resources_.end()) { | 46 if (i == live_resources_.end()) { |
| 45 return false; | 47 return false; |
| 46 } else { | 48 } else { |
| 47 // We don't protect against overflow, since a plugin as malicious as to ref | 49 // We don't protect against overflow, since a plugin as malicious as to ref |
| 48 // once per every byte in the address space could have just as well unrefed | 50 // once per every byte in the address space could have just as well unrefed |
| 49 // one time too many. | 51 // one time too many. |
| 50 ++i->second.second; | 52 ++i->second.plugin_refcount; |
|
nfullagar
2010/12/14 00:39:28
nit: would prefer
i->second.plugin_refcount += 1
neb
2010/12/14 23:03:46
http://google-styleguide.googlecode.com/svn/trunk/
| |
| 51 return true; | 53 return true; |
| 52 } | 54 } |
| 53 } | 55 } |
| 54 | 56 |
| 55 bool PluginResourceTracker::UnrefResource(PP_Resource res) { | 57 bool PluginResourceTracker::UnrefResource(PP_Resource res) { |
| 56 ResourceMap::iterator i = live_resources_.find(res); | 58 ResourceMap::iterator i = live_resources_.find(res); |
| 57 if (i != live_resources_.end()) { | 59 if (i != live_resources_.end()) { |
| 58 if (!--i->second.second) { | 60 if (!--i->second.plugin_refcount) { |
|
nfullagar
2010/12/14 00:39:28
I'm not a huge fan of !--i->second.plugin_refcount
neb
2010/12/14 23:03:46
Done.
| |
| 59 i->second.first->StoppedTracking(); | 61 size_t browser_refcount = i->second.browser_refcount; |
| 62 i->second.resource->StoppedTracking(); | |
| 60 live_resources_.erase(i); | 63 live_resources_.erase(i); |
| 64 | |
| 65 // Release all browser references. | |
| 66 ReleaseBrowserResource(res, browser_refcount); | |
| 61 } | 67 } |
| 62 return true; | 68 return true; |
| 63 } else { | 69 } else { |
| 64 return false; | 70 return false; |
| 65 } | 71 } |
| 66 } | 72 } |
| 67 | 73 |
| 74 void PluginResourceTracker::ObtainBrowserResource(PP_Resource res) { | |
| 75 if (res) { | |
| 76 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); | |
| 77 PpbCoreRpcClient::PPB_Core_AddRefResource(channel, res); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void PluginResourceTracker::ReleaseBrowserResource(PP_Resource res, | |
| 82 size_t browser_refcount) { | |
| 83 // Release all browser references. | |
| 84 if (res) { | |
| 85 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); | |
| 86 PpbCoreRpcClient::ReleaseResourceMultipleTimes(channel, res, | |
| 87 browser_refcount); | |
|
sehr (please use chromium)
2010/12/10 01:09:18
What will happen if the NaCl module releases more
neb
2010/12/10 20:43:31
NaCl module doesn't call this function directly, b
| |
| 88 } | |
| 89 } | |
| 90 | |
| 68 } // namespace ppapi_proxy | 91 } // namespace ppapi_proxy |
| 69 | 92 |
| OLD | NEW |