Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 | |
| 10 #include "base/memory/linked_ptr.h" | |
| 11 #include "base/threading/non_thread_safe.h" | |
| 12 #include "chrome/browser/profiles/profile_keyed_service.h" | |
| 13 #include "content/public/browser/browser_thread.h" | |
| 14 | |
| 15 using content::BrowserThread; | |
| 16 | |
| 17 namespace extensions { | |
| 18 | |
| 19 template <class T> | |
| 20 class ApiResourceManager : public ProfileKeyedService, | |
|
asargent_no_longer_on_chrome
2012/07/16 18:08:25
nit: please add class overview comment
| |
| 21 public base::NonThreadSafe { | |
| 22 public: | |
| 23 ApiResourceManager() | |
| 24 : next_id_(1), | |
| 25 thread_id_(static_cast<BrowserThread::ID>(-1)), | |
| 26 api_resource_map_(new std::map<int, linked_ptr<T> >()) { | |
| 27 } | |
| 28 | |
| 29 virtual ~ApiResourceManager() { | |
| 30 DCHECK(CalledOnValidThread()); | |
| 31 | |
| 32 DCHECK(BrowserThread::IsMessageLoopValid(thread_id())) << | |
| 33 "A unit test is using an ApiResourceManager but didn't provide " | |
| 34 "the thread message loop needed for that kind of resource. " | |
| 35 "Please ensure that the appropriate message loop is operational."; | |
| 36 | |
| 37 BrowserThread::DeleteSoon(thread_id(), FROM_HERE, api_resource_map_); | |
| 38 } | |
| 39 | |
| 40 BrowserThread::ID thread_id() { | |
| 41 DCHECK(thread_id_ != -1) << "Before using this ApiResourceManager, " | |
| 42 "please set thread_id to the thread (IO, FILE) that the " | |
| 43 "managed resource requires."; | |
| 44 return thread_id_; | |
| 45 } | |
| 46 | |
| 47 void set_thread_id(BrowserThread::ID thread_id) { | |
| 48 thread_id_ = thread_id; | |
| 49 } | |
| 50 | |
| 51 // Takes ownership. | |
| 52 int Add(T* api_resource) { | |
| 53 DCHECK(BrowserThread::CurrentlyOn(thread_id())); | |
| 54 int id = GenerateId(); | |
| 55 if (id > 0) { | |
| 56 linked_ptr<T> resource_ptr(api_resource); | |
| 57 (*api_resource_map_)[id] = resource_ptr; | |
| 58 return id; | |
| 59 } | |
| 60 return 0; | |
| 61 } | |
| 62 | |
| 63 void Remove(int api_resource_id) { | |
| 64 DCHECK(BrowserThread::CurrentlyOn(thread_id())); | |
| 65 api_resource_map_->erase(api_resource_id); | |
| 66 } | |
| 67 | |
| 68 T* Get(int api_resource_id) { | |
| 69 DCHECK(BrowserThread::CurrentlyOn(thread_id())); | |
| 70 linked_ptr<T> ptr = (*api_resource_map_)[api_resource_id]; | |
| 71 return ptr.get(); | |
| 72 } | |
| 73 | |
| 74 private: | |
| 75 // TODO(miket): consider partitioning the ID space by extension ID to make it | |
| 76 // harder for extensions to peek into each others' resources. | |
|
asargent_no_longer_on_chrome
2012/07/16 18:08:25
Another possibility would be to have one ApiResour
miket_OOO
2012/07/16 19:46:02
I've entered crbug.com/137539 to track more loudly
| |
| 77 int GenerateId() { | |
| 78 return next_id_++; | |
| 79 } | |
| 80 | |
| 81 int next_id_; | |
| 82 BrowserThread::ID thread_id_; | |
| 83 | |
| 84 // We need finer-grained control over the lifetime of this instance than RAII | |
| 85 // can give us. | |
| 86 std::map<int, linked_ptr<T> >* api_resource_map_; | |
| 87 }; | |
| 88 | |
| 89 } // namespace extensions | |
| 90 | |
| 91 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | |
| OLD | NEW |