Chromium Code Reviews| 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" |
| 11 #include "base/threading/non_thread_safe.h" | 11 #include "base/threading/non_thread_safe.h" |
| 12 #include "chrome/browser/profiles/profile_keyed_service.h" | 12 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 13 #include "chrome/common/extensions/extension.h" | |
|
Mihai Parparita -not on Chrome
2012/09/11 07:18:19
This include seems unncessary (you're just dealing
| |
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 14 | 15 |
| 15 using content::BrowserThread; | 16 using content::BrowserThread; |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 | 19 |
| 19 // An ApiResourceManager manages the lifetime of a set of resources that | 20 // An ApiResourceManager manages the lifetime of a set of resources that |
| 20 // ApiFunctions use. Examples are sockets or USB connections. | 21 // ApiFunctions use. Examples are sockets or USB connections. |
| 21 template <class T> | 22 template <class T> |
| 22 class ApiResourceManager : public ProfileKeyedService, | 23 class ApiResourceManager : public ProfileKeyedService, |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 44 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 45 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
| 45 int id = GenerateId(); | 46 int id = GenerateId(); |
| 46 if (id > 0) { | 47 if (id > 0) { |
| 47 linked_ptr<T> resource_ptr(api_resource); | 48 linked_ptr<T> resource_ptr(api_resource); |
| 48 (*api_resource_map_)[id] = resource_ptr; | 49 (*api_resource_map_)[id] = resource_ptr; |
| 49 return id; | 50 return id; |
| 50 } | 51 } |
| 51 return 0; | 52 return 0; |
| 52 } | 53 } |
| 53 | 54 |
| 54 void Remove(int api_resource_id) { | 55 void Remove(const std::string& extension_id, int api_resource_id) { |
| 55 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 56 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
| 56 api_resource_map_->erase(api_resource_id); | 57 if (GetOwnedResource(extension_id, api_resource_id) != NULL) { |
| 58 api_resource_map_->erase(api_resource_id); | |
| 59 } | |
| 57 } | 60 } |
| 58 | 61 |
| 59 T* Get(int api_resource_id) { | 62 T* Get(const std::string& extension_id, int api_resource_id) { |
| 60 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); | 63 DCHECK(BrowserThread::CurrentlyOn(thread_id_)); |
| 61 linked_ptr<T> ptr = (*api_resource_map_)[api_resource_id]; | 64 return GetOwnedResource(extension_id, api_resource_id); |
| 62 return ptr.get(); | |
| 63 } | 65 } |
| 64 | 66 |
| 65 private: | 67 private: |
| 66 // TODO(miket): consider partitioning the ID space by extension ID to make it | 68 // TODO(miket): consider partitioning the ID space by extension ID to make it |
|
Mihai Parparita -not on Chrome
2012/09/11 07:18:19
You can remove this TODO now.
| |
| 67 // harder for extensions to peek into each others' resources. | 69 // harder for extensions to peek into each others' resources. |
| 68 int GenerateId() { | 70 int GenerateId() { |
| 69 return next_id_++; | 71 return next_id_++; |
| 70 } | 72 } |
| 71 | 73 |
| 74 T* GetOwnedResource(const std::string& extension_id, | |
| 75 int api_resource_id) { | |
| 76 linked_ptr<T> ptr = (*api_resource_map_)[api_resource_id]; | |
| 77 T* resource = ptr.get(); | |
| 78 if (resource && | |
| 79 extension_id == resource->owner_extension_id()) | |
|
Mihai Parparita -not on Chrome
2012/09/11 07:18:19
This can fit on the previous line.
| |
| 80 return resource; | |
| 81 return NULL; | |
| 82 } | |
| 83 | |
| 72 int next_id_; | 84 int next_id_; |
| 73 BrowserThread::ID thread_id_; | 85 BrowserThread::ID thread_id_; |
| 74 | 86 |
| 75 // We need finer-grained control over the lifetime of this instance than RAII | 87 // We need finer-grained control over the lifetime of this instance than RAII |
| 76 // can give us. | 88 // can give us. |
| 77 std::map<int, linked_ptr<T> >* api_resource_map_; | 89 std::map<int, linked_ptr<T> >* api_resource_map_; |
| 78 }; | 90 }; |
| 79 | 91 |
| 80 } // namespace extensions | 92 } // namespace extensions |
| 81 | 93 |
| 82 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 94 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ |
| OLD | NEW |