| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/memory/linked_ptr.h" | 10 #include "base/memory/linked_ptr.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 44 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
| 45 int id = GenerateId(); | 45 int id = GenerateId(); |
| 46 if (id > 0) { | 46 if (id > 0) { |
| 47 linked_ptr<T> resource_ptr(api_resource); | 47 linked_ptr<T> resource_ptr(api_resource); |
| 48 (*api_resource_map_)[id] = resource_ptr; | 48 (*api_resource_map_)[id] = resource_ptr; |
| 49 return id; | 49 return id; |
| 50 } | 50 } |
| 51 return 0; | 51 return 0; |
| 52 } | 52 } |
| 53 | 53 |
| 54 void Remove(int api_resource_id) { | 54 void Remove(const std::string& extension_id, int api_resource_id) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 55 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
| 56 api_resource_map_->erase(api_resource_id); | 56 if (GetOwnedResource(extension_id, api_resource_id) != NULL) { |
| 57 api_resource_map_->erase(api_resource_id); |
| 58 } |
| 57 } | 59 } |
| 58 | 60 |
| 59 T* Get(int api_resource_id) { | 61 T* Get(const std::string& extension_id, int api_resource_id) { |
| 60 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 62 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
| 61 linked_ptr<T> ptr = (*api_resource_map_)[api_resource_id]; | 63 return GetOwnedResource(extension_id, api_resource_id); |
| 62 return ptr.get(); | |
| 63 } | 64 } |
| 64 | 65 |
| 65 private: | 66 private: |
| 66 // TODO(miket): consider partitioning the ID space by extension ID to make it | |
| 67 // harder for extensions to peek into each others' resources. | |
| 68 int GenerateId() { | 67 int GenerateId() { |
| 69 return next_id_++; | 68 return next_id_++; |
| 70 } | 69 } |
| 71 | 70 |
| 71 T* GetOwnedResource(const std::string& extension_id, |
| 72 int api_resource_id) { |
| 73 linked_ptr<T> ptr = (*api_resource_map_)[api_resource_id]; |
| 74 T* resource = ptr.get(); |
| 75 if (resource && extension_id == resource->owner_extension_id()) |
| 76 return resource; |
| 77 return NULL; |
| 78 } |
| 79 |
| 72 int next_id_; | 80 int next_id_; |
| 73 BrowserThread::ID thread_id_; | 81 BrowserThread::ID thread_id_; |
| 74 | 82 |
| 75 // We need finer-grained control over the lifetime of this instance than RAII | 83 // We need finer-grained control over the lifetime of this instance than RAII |
| 76 // can give us. | 84 // can give us. |
| 77 std::map<int, linked_ptr<T> >* api_resource_map_; | 85 std::map<int, linked_ptr<T> >* api_resource_map_; |
| 78 }; | 86 }; |
| 79 | 87 |
| 80 } // namespace extensions | 88 } // namespace extensions |
| 81 | 89 |
| 82 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 90 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ |
| OLD | NEW |