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

Side by Side Diff: src/shared/ppapi_proxy/plugin_resource_tracker.cc

Issue 5581011: Resource tracking done right. (Closed) Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Updated for CoreInterface->PPBCoreInterface rename Created 10 years 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 | « src/shared/ppapi_proxy/plugin_resource_tracker.h ('k') | src/shared/ppapi_proxy/ppb_core.srpc » ('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 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++;
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 i->second.plugin_refcount--;
59 i->second.first->StoppedTracking(); 61 if (0 == i->second.plugin_refcount) {
62 size_t browser_refcount = i->second.browser_refcount;
63 i->second.resource->StoppedTracking();
60 live_resources_.erase(i); 64 live_resources_.erase(i);
65
66 // Release all browser references.
67 ReleaseBrowserResource(res, browser_refcount);
61 } 68 }
62 return true; 69 return true;
63 } else { 70 } else {
64 return false; 71 return false;
65 } 72 }
66 } 73 }
67 74
75 void PluginResourceTracker::ObtainBrowserResource(PP_Resource res) {
76 if (res) {
77 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel();
78 PpbCoreRpcClient::PPB_Core_AddRefResource(channel, res);
79 }
80 }
81
82 void PluginResourceTracker::ReleaseBrowserResource(PP_Resource res,
83 size_t browser_refcount) {
84 // Release all browser references.
85 if (res) {
86 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel();
87 PpbCoreRpcClient::ReleaseResourceMultipleTimes(channel, res,
88 browser_refcount);
89 }
90 }
91
68 } // namespace ppapi_proxy 92 } // namespace ppapi_proxy
69 93
OLDNEW
« no previous file with comments | « src/shared/ppapi_proxy/plugin_resource_tracker.h ('k') | src/shared/ppapi_proxy/ppb_core.srpc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698