Chromium Code Reviews| 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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 friend class api::UDPSocketEventDispatcher; | 173 friend class api::UDPSocketEventDispatcher; |
| 174 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; | 174 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; |
| 175 | 175 |
| 176 static const bool kServiceHasOwnInstanceInIncognito = true; | 176 static const bool kServiceHasOwnInstanceInIncognito = true; |
| 177 static const bool kServiceIsNULLWhileTesting = true; | 177 static const bool kServiceIsNULLWhileTesting = true; |
| 178 | 178 |
| 179 // ApiResourceData class handles resource bookkeeping on a thread | 179 // ApiResourceData class handles resource bookkeeping on a thread |
| 180 // where resource lifetime is handled. | 180 // where resource lifetime is handled. |
| 181 class ApiResourceData : public base::RefCountedThreadSafe<ApiResourceData> { | 181 class ApiResourceData : public base::RefCountedThreadSafe<ApiResourceData> { |
| 182 public: | 182 public: |
| 183 typedef std::map<int, linked_ptr<T> > ApiResourceMap; | 183 typedef std::map<int, std::unique_ptr<T>> ApiResourceMap; |
| 184 // Lookup map from extension id's to allocated resource id's. | 184 // Lookup map from extension id's to allocated resource id's. |
| 185 typedef std::map<std::string, base::hash_set<int> > ExtensionToResourceMap; | 185 typedef std::map<std::string, base::hash_set<int> > ExtensionToResourceMap; |
| 186 | 186 |
| 187 ApiResourceData() : next_id_(1) {} | 187 ApiResourceData() : next_id_(1) {} |
| 188 | 188 |
| 189 int Add(T* api_resource) { | 189 int Add(T* api_resource) { |
|
Devlin
2016/08/30 19:21:53
here, too, add a TODO to make this (and Add() on l
lazyboy
2016/08/30 20:30:56
Done.
| |
| 190 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 190 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 191 int id = GenerateId(); | 191 int id = GenerateId(); |
| 192 if (id > 0) { | 192 if (id > 0) { |
| 193 linked_ptr<T> resource_ptr(api_resource); | 193 api_resource_map_[id] = base::WrapUnique<T>(api_resource); |
| 194 api_resource_map_[id] = resource_ptr; | |
| 195 | 194 |
| 196 const std::string& extension_id = api_resource->owner_extension_id(); | 195 const std::string& extension_id = api_resource->owner_extension_id(); |
| 197 ExtensionToResourceMap::iterator it = | 196 ExtensionToResourceMap::iterator it = |
| 198 extension_resource_map_.find(extension_id); | 197 extension_resource_map_.find(extension_id); |
| 199 if (it == extension_resource_map_.end()) { | 198 if (it == extension_resource_map_.end()) { |
| 200 it = extension_resource_map_.insert( | 199 it = extension_resource_map_.insert( |
| 201 std::make_pair(extension_id, base::hash_set<int>())).first; | 200 std::make_pair(extension_id, base::hash_set<int>())).first; |
| 202 } | 201 } |
| 203 it->second.insert(id); | 202 it->second.insert(id); |
| 204 return id; | 203 return id; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 224 // Change the resource mapped to this |extension_id| at this | 223 // Change the resource mapped to this |extension_id| at this |
| 225 // |api_resource_id| to |resource|. Returns true and succeeds unless | 224 // |api_resource_id| to |resource|. Returns true and succeeds unless |
| 226 // |api_resource_id| does not already identify a resource held by | 225 // |api_resource_id| does not already identify a resource held by |
| 227 // |extension_id|. | 226 // |extension_id|. |
| 228 bool Replace(const std::string& extension_id, | 227 bool Replace(const std::string& extension_id, |
| 229 int api_resource_id, | 228 int api_resource_id, |
| 230 T* api_resource) { | 229 T* api_resource) { |
| 231 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 230 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 232 T* old_resource = api_resource_map_[api_resource_id].get(); | 231 T* old_resource = api_resource_map_[api_resource_id].get(); |
| 233 if (old_resource && extension_id == old_resource->owner_extension_id()) { | 232 if (old_resource && extension_id == old_resource->owner_extension_id()) { |
| 234 api_resource_map_[api_resource_id] = linked_ptr<T>(api_resource); | 233 api_resource_map_[api_resource_id] = base::WrapUnique<T>(api_resource); |
| 235 return true; | 234 return true; |
| 236 } | 235 } |
| 237 return false; | 236 return false; |
| 238 } | 237 } |
| 239 | 238 |
| 240 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { | 239 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { |
| 241 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 240 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 242 return GetOwnedResourceIds(extension_id); | 241 return GetOwnedResourceIds(extension_id); |
| 243 } | 242 } |
| 244 | 243 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 274 FROM_HERE, base::Bind(&ApiResourceData::Cleanup, this)); | 273 FROM_HERE, base::Bind(&ApiResourceData::Cleanup, this)); |
| 275 } | 274 } |
| 276 } | 275 } |
| 277 | 276 |
| 278 private: | 277 private: |
| 279 friend class base::RefCountedThreadSafe<ApiResourceData>; | 278 friend class base::RefCountedThreadSafe<ApiResourceData>; |
| 280 | 279 |
| 281 virtual ~ApiResourceData() {} | 280 virtual ~ApiResourceData() {} |
| 282 | 281 |
| 283 T* GetOwnedResource(const std::string& extension_id, int api_resource_id) { | 282 T* GetOwnedResource(const std::string& extension_id, int api_resource_id) { |
| 284 linked_ptr<T> ptr = api_resource_map_[api_resource_id]; | 283 const std::unique_ptr<T>& ptr = api_resource_map_[api_resource_id]; |
| 285 T* resource = ptr.get(); | 284 T* resource = ptr.get(); |
| 286 if (resource && extension_id == resource->owner_extension_id()) | 285 if (resource && extension_id == resource->owner_extension_id()) |
| 287 return resource; | 286 return resource; |
| 288 return NULL; | 287 return NULL; |
| 289 } | 288 } |
| 290 | 289 |
| 291 base::hash_set<int>* GetOwnedResourceIds(const std::string& extension_id) { | 290 base::hash_set<int>* GetOwnedResourceIds(const std::string& extension_id) { |
| 292 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 291 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 293 ExtensionToResourceMap::iterator it = | 292 ExtensionToResourceMap::iterator it = |
| 294 extension_resource_map_.find(extension_id); | 293 extension_resource_map_.find(extension_id); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 318 | 317 |
| 319 // Remove all resources, or the non persistent ones only if |remove_all| | 318 // Remove all resources, or the non persistent ones only if |remove_all| |
| 320 // is false. | 319 // is false. |
| 321 base::hash_set<int>& resource_ids = it->second; | 320 base::hash_set<int>& resource_ids = it->second; |
| 322 for (base::hash_set<int>::iterator it = resource_ids.begin(); | 321 for (base::hash_set<int>::iterator it = resource_ids.begin(); |
| 323 it != resource_ids.end();) { | 322 it != resource_ids.end();) { |
| 324 bool erase = false; | 323 bool erase = false; |
| 325 if (remove_all) { | 324 if (remove_all) { |
| 326 erase = true; | 325 erase = true; |
| 327 } else { | 326 } else { |
| 328 linked_ptr<T> ptr = api_resource_map_[*it]; | 327 std::unique_ptr<T>& ptr = api_resource_map_[*it]; |
| 329 T* resource = ptr.get(); | 328 T* resource = ptr.get(); |
| 330 erase = (resource && !resource->IsPersistent()); | 329 erase = (resource && !resource->IsPersistent()); |
| 331 } | 330 } |
| 332 | 331 |
| 333 if (erase) { | 332 if (erase) { |
| 334 api_resource_map_.erase(*it); | 333 api_resource_map_.erase(*it); |
| 335 resource_ids.erase(it++); | 334 resource_ids.erase(it++); |
| 336 } else { | 335 } else { |
| 337 ++it; | 336 ++it; |
| 338 } | 337 } |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 429 ->GetSequencedTaskRunnerWithShutdownBehavior( | 428 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 430 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( | 429 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( |
| 431 T::kSequenceToken), | 430 T::kSequenceToken), |
| 432 T::kShutdownBehavior); | 431 T::kShutdownBehavior); |
| 433 } | 432 } |
| 434 }; | 433 }; |
| 435 | 434 |
| 436 } // namespace extensions | 435 } // namespace extensions |
| 437 | 436 |
| 438 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 437 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
| OLD | NEW |