| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
| 6 #define EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 6 #define EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 | 10 |
| 11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/ptr_util.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/scoped_observer.h" | 14 #include "base/scoped_observer.h" |
| 15 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
| 16 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "extensions/browser/browser_context_keyed_api_factory.h" | 18 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 19 #include "extensions/browser/extension_registry.h" | 19 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/browser/extension_registry_observer.h" | 20 #include "extensions/browser/extension_registry_observer.h" |
| 21 #include "extensions/browser/process_manager.h" | 21 #include "extensions/browser/process_manager.h" |
| 22 #include "extensions/browser/process_manager_observer.h" | 22 #include "extensions/browser/process_manager_observer.h" |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 virtual ~ApiResourceManager() { | 103 virtual ~ApiResourceManager() { |
| 104 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 105 DCHECK(ThreadingTraits::IsMessageLoopValid()) | 105 DCHECK(ThreadingTraits::IsMessageLoopValid()) |
| 106 << "A unit test is using an ApiResourceManager but didn't provide " | 106 << "A unit test is using an ApiResourceManager but didn't provide " |
| 107 "the thread message loop needed for that kind of resource. " | 107 "the thread message loop needed for that kind of resource. " |
| 108 "Please ensure that the appropriate message loop is operational."; | 108 "Please ensure that the appropriate message loop is operational."; |
| 109 | 109 |
| 110 data_->InititateCleanup(); | 110 data_->InititateCleanup(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // TODO(lazyboy): Pass unique_ptr<T> instead of T*. |
| 113 // Takes ownership. | 114 // Takes ownership. |
| 114 int Add(T* api_resource) { return data_->Add(api_resource); } | 115 int Add(T* api_resource) { return data_->Add(api_resource); } |
| 115 | 116 |
| 116 void Remove(const std::string& extension_id, int api_resource_id) { | 117 void Remove(const std::string& extension_id, int api_resource_id) { |
| 117 data_->Remove(extension_id, api_resource_id); | 118 data_->Remove(extension_id, api_resource_id); |
| 118 } | 119 } |
| 119 | 120 |
| 120 T* Get(const std::string& extension_id, int api_resource_id) { | 121 T* Get(const std::string& extension_id, int api_resource_id) { |
| 121 return data_->Get(extension_id, api_resource_id); | 122 return data_->Get(extension_id, api_resource_id); |
| 122 } | 123 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 friend class api::UDPSocketEventDispatcher; | 174 friend class api::UDPSocketEventDispatcher; |
| 174 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; | 175 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; |
| 175 | 176 |
| 176 static const bool kServiceHasOwnInstanceInIncognito = true; | 177 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 177 static const bool kServiceIsNULLWhileTesting = true; | 178 static const bool kServiceIsNULLWhileTesting = true; |
| 178 | 179 |
| 179 // ApiResourceData class handles resource bookkeeping on a thread | 180 // ApiResourceData class handles resource bookkeeping on a thread |
| 180 // where resource lifetime is handled. | 181 // where resource lifetime is handled. |
| 181 class ApiResourceData : public base::RefCountedThreadSafe<ApiResourceData> { | 182 class ApiResourceData : public base::RefCountedThreadSafe<ApiResourceData> { |
| 182 public: | 183 public: |
| 183 typedef std::map<int, linked_ptr<T> > ApiResourceMap; | 184 typedef std::map<int, std::unique_ptr<T>> ApiResourceMap; |
| 184 // Lookup map from extension id's to allocated resource id's. | 185 // Lookup map from extension id's to allocated resource id's. |
| 185 typedef std::map<std::string, base::hash_set<int> > ExtensionToResourceMap; | 186 typedef std::map<std::string, base::hash_set<int> > ExtensionToResourceMap; |
| 186 | 187 |
| 187 ApiResourceData() : next_id_(1) {} | 188 ApiResourceData() : next_id_(1) {} |
| 188 | 189 |
| 190 // TODO(lazyboy): Pass unique_ptr<T> instead of T*. |
| 189 int Add(T* api_resource) { | 191 int Add(T* api_resource) { |
| 190 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 192 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 191 int id = GenerateId(); | 193 int id = GenerateId(); |
| 192 if (id > 0) { | 194 if (id > 0) { |
| 193 linked_ptr<T> resource_ptr(api_resource); | 195 api_resource_map_[id] = base::WrapUnique<T>(api_resource); |
| 194 api_resource_map_[id] = resource_ptr; | |
| 195 | 196 |
| 196 const std::string& extension_id = api_resource->owner_extension_id(); | 197 const std::string& extension_id = api_resource->owner_extension_id(); |
| 197 ExtensionToResourceMap::iterator it = | 198 ExtensionToResourceMap::iterator it = |
| 198 extension_resource_map_.find(extension_id); | 199 extension_resource_map_.find(extension_id); |
| 199 if (it == extension_resource_map_.end()) { | 200 if (it == extension_resource_map_.end()) { |
| 200 it = extension_resource_map_.insert( | 201 it = extension_resource_map_.insert( |
| 201 std::make_pair(extension_id, base::hash_set<int>())).first; | 202 std::make_pair(extension_id, base::hash_set<int>())).first; |
| 202 } | 203 } |
| 203 it->second.insert(id); | 204 it->second.insert(id); |
| 204 return id; | 205 return id; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 224 // Change the resource mapped to this |extension_id| at this | 225 // Change the resource mapped to this |extension_id| at this |
| 225 // |api_resource_id| to |resource|. Returns true and succeeds unless | 226 // |api_resource_id| to |resource|. Returns true and succeeds unless |
| 226 // |api_resource_id| does not already identify a resource held by | 227 // |api_resource_id| does not already identify a resource held by |
| 227 // |extension_id|. | 228 // |extension_id|. |
| 228 bool Replace(const std::string& extension_id, | 229 bool Replace(const std::string& extension_id, |
| 229 int api_resource_id, | 230 int api_resource_id, |
| 230 T* api_resource) { | 231 T* api_resource) { |
| 231 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 232 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 232 T* old_resource = api_resource_map_[api_resource_id].get(); | 233 T* old_resource = api_resource_map_[api_resource_id].get(); |
| 233 if (old_resource && extension_id == old_resource->owner_extension_id()) { | 234 if (old_resource && extension_id == old_resource->owner_extension_id()) { |
| 234 api_resource_map_[api_resource_id] = linked_ptr<T>(api_resource); | 235 api_resource_map_[api_resource_id] = base::WrapUnique<T>(api_resource); |
| 235 return true; | 236 return true; |
| 236 } | 237 } |
| 237 return false; | 238 return false; |
| 238 } | 239 } |
| 239 | 240 |
| 240 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { | 241 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { |
| 241 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 242 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 242 return GetOwnedResourceIds(extension_id); | 243 return GetOwnedResourceIds(extension_id); |
| 243 } | 244 } |
| 244 | 245 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 274 FROM_HERE, base::Bind(&ApiResourceData::Cleanup, this)); | 275 FROM_HERE, base::Bind(&ApiResourceData::Cleanup, this)); |
| 275 } | 276 } |
| 276 } | 277 } |
| 277 | 278 |
| 278 private: | 279 private: |
| 279 friend class base::RefCountedThreadSafe<ApiResourceData>; | 280 friend class base::RefCountedThreadSafe<ApiResourceData>; |
| 280 | 281 |
| 281 virtual ~ApiResourceData() {} | 282 virtual ~ApiResourceData() {} |
| 282 | 283 |
| 283 T* GetOwnedResource(const std::string& extension_id, int api_resource_id) { | 284 T* GetOwnedResource(const std::string& extension_id, int api_resource_id) { |
| 284 linked_ptr<T> ptr = api_resource_map_[api_resource_id]; | 285 const std::unique_ptr<T>& ptr = api_resource_map_[api_resource_id]; |
| 285 T* resource = ptr.get(); | 286 T* resource = ptr.get(); |
| 286 if (resource && extension_id == resource->owner_extension_id()) | 287 if (resource && extension_id == resource->owner_extension_id()) |
| 287 return resource; | 288 return resource; |
| 288 return NULL; | 289 return NULL; |
| 289 } | 290 } |
| 290 | 291 |
| 291 base::hash_set<int>* GetOwnedResourceIds(const std::string& extension_id) { | 292 base::hash_set<int>* GetOwnedResourceIds(const std::string& extension_id) { |
| 292 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 293 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 293 ExtensionToResourceMap::iterator it = | 294 ExtensionToResourceMap::iterator it = |
| 294 extension_resource_map_.find(extension_id); | 295 extension_resource_map_.find(extension_id); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 318 | 319 |
| 319 // Remove all resources, or the non persistent ones only if |remove_all| | 320 // Remove all resources, or the non persistent ones only if |remove_all| |
| 320 // is false. | 321 // is false. |
| 321 base::hash_set<int>& resource_ids = it->second; | 322 base::hash_set<int>& resource_ids = it->second; |
| 322 for (base::hash_set<int>::iterator it = resource_ids.begin(); | 323 for (base::hash_set<int>::iterator it = resource_ids.begin(); |
| 323 it != resource_ids.end();) { | 324 it != resource_ids.end();) { |
| 324 bool erase = false; | 325 bool erase = false; |
| 325 if (remove_all) { | 326 if (remove_all) { |
| 326 erase = true; | 327 erase = true; |
| 327 } else { | 328 } else { |
| 328 linked_ptr<T> ptr = api_resource_map_[*it]; | 329 std::unique_ptr<T>& ptr = api_resource_map_[*it]; |
| 329 T* resource = ptr.get(); | 330 T* resource = ptr.get(); |
| 330 erase = (resource && !resource->IsPersistent()); | 331 erase = (resource && !resource->IsPersistent()); |
| 331 } | 332 } |
| 332 | 333 |
| 333 if (erase) { | 334 if (erase) { |
| 334 api_resource_map_.erase(*it); | 335 api_resource_map_.erase(*it); |
| 335 resource_ids.erase(it++); | 336 resource_ids.erase(it++); |
| 336 } else { | 337 } else { |
| 337 ++it; | 338 ++it; |
| 338 } | 339 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 ->GetSequencedTaskRunnerWithShutdownBehavior( | 430 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 430 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( | 431 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( |
| 431 T::kSequenceToken), | 432 T::kSequenceToken), |
| 432 T::kShutdownBehavior); | 433 T::kShutdownBehavior); |
| 433 } | 434 } |
| 434 }; | 435 }; |
| 435 | 436 |
| 436 } // namespace extensions | 437 } // namespace extensions |
| 437 | 438 |
| 438 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 439 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
| OLD | NEW |