OLD | NEW |
(Empty) | |
| 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 |
| 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/shared/ppapi_proxy/plugin_resource.h" |
| 11 #include "ppapi/c/pp_resource.h" |
| 12 |
| 13 namespace ppapi_proxy { |
| 14 |
| 15 scoped_refptr<PluginResource> |
| 16 PluginResourceTracker::GetResource(PP_Resource res) const { |
| 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 } |
| 23 |
| 24 PluginResourceTracker::PluginResourceTracker() |
| 25 : last_id_(0) { |
| 26 } |
| 27 |
| 28 PluginResourceTracker::~PluginResourceTracker() { |
| 29 } |
| 30 |
| 31 PP_Resource PluginResourceTracker::AddResource(PluginResource* resource) { |
| 32 // If the plugin manages to create 4B resources... |
| 33 if (last_id_ == std::numeric_limits<PP_Resource>::max()) { |
| 34 return 0; |
| 35 } |
| 36 // Add the resource with plugin use-count 1. |
| 37 ++last_id_; |
| 38 live_resources_.insert(std::make_pair(last_id_, std::make_pair(resource, 1))); |
| 39 return last_id_; |
| 40 } |
| 41 |
| 42 bool PluginResourceTracker::AddRefResource(PP_Resource res) { |
| 43 ResourceMap::iterator i = live_resources_.find(res); |
| 44 if (i == live_resources_.end()) { |
| 45 return false; |
| 46 } else { |
| 47 // 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 |
| 49 // one time too many. |
| 50 ++i->second.second; |
| 51 return true; |
| 52 } |
| 53 } |
| 54 |
| 55 bool PluginResourceTracker::UnrefResource(PP_Resource res) { |
| 56 ResourceMap::iterator i = live_resources_.find(res); |
| 57 if (i != live_resources_.end()) { |
| 58 if (!--i->second.second) { |
| 59 i->second.first->StoppedTracking(); |
| 60 live_resources_.erase(i); |
| 61 } |
| 62 return true; |
| 63 } else { |
| 64 return false; |
| 65 } |
| 66 } |
| 67 |
| 68 } // namespace ppapi_proxy |
| 69 |
OLD | NEW |