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 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 GetFactoryInstance(); | 163 GetFactoryInstance(); |
164 | 164 |
165 // Convenience method to get the ApiResourceManager for a profile. | 165 // Convenience method to get the ApiResourceManager for a profile. |
166 static ApiResourceManager<T>* Get(content::BrowserContext* context) { | 166 static ApiResourceManager<T>* Get(content::BrowserContext* context) { |
167 return BrowserContextKeyedAPIFactory<ApiResourceManager<T> >::Get(context); | 167 return BrowserContextKeyedAPIFactory<ApiResourceManager<T> >::Get(context); |
168 } | 168 } |
169 | 169 |
170 // BrowserContextKeyedAPI implementation. | 170 // BrowserContextKeyedAPI implementation. |
171 static const char* service_name() { return T::service_name(); } | 171 static const char* service_name() { return T::service_name(); } |
172 | 172 |
| 173 // Change the resource mapped to this |extension_id| at this |
| 174 // |api_resource_id| to |resource|. Returns true and succeeds unless |
| 175 // |api_resource_id| does not already identify a resource held by |
| 176 // |extension_id|. |
| 177 bool Replace(const std::string& extension_id, |
| 178 int api_resource_id, |
| 179 T* resource) { |
| 180 return data_->Replace(extension_id, api_resource_id, resource); |
| 181 } |
| 182 |
173 protected: | 183 protected: |
174 // content::NotificationObserver: | 184 // content::NotificationObserver: |
175 virtual void Observe(int type, | 185 virtual void Observe(int type, |
176 const content::NotificationSource& source, | 186 const content::NotificationSource& source, |
177 const content::NotificationDetails& details) OVERRIDE { | 187 const content::NotificationDetails& details) OVERRIDE { |
178 switch (type) { | 188 switch (type) { |
179 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 189 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { |
180 std::string id = content::Details<extensions::UnloadedExtensionInfo>( | 190 std::string id = content::Details<extensions::UnloadedExtensionInfo>( |
181 details)->extension->id(); | 191 details)->extension->id(); |
182 data_->InitiateExtensionUnloadedCleanup(id); | 192 data_->InitiateExtensionUnloadedCleanup(id); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 extension_resource_map_[extension_id].erase(api_resource_id); | 252 extension_resource_map_[extension_id].erase(api_resource_id); |
243 api_resource_map_.erase(api_resource_id); | 253 api_resource_map_.erase(api_resource_id); |
244 } | 254 } |
245 } | 255 } |
246 | 256 |
247 T* Get(const std::string& extension_id, int api_resource_id) { | 257 T* Get(const std::string& extension_id, int api_resource_id) { |
248 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 258 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
249 return GetOwnedResource(extension_id, api_resource_id); | 259 return GetOwnedResource(extension_id, api_resource_id); |
250 } | 260 } |
251 | 261 |
| 262 // Change the resource mapped to this |extension_id| at this |
| 263 // |api_resource_id| to |resource|. Returns true and succeeds unless |
| 264 // |api_resource_id| does not already identify a resource held by |
| 265 // |extension_id|. |
| 266 bool Replace(const std::string& extension_id, |
| 267 int api_resource_id, |
| 268 T* api_resource) { |
| 269 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
| 270 T* old_resource = api_resource_map_[api_resource_id].get(); |
| 271 if (old_resource && extension_id == old_resource->owner_extension_id()) { |
| 272 api_resource_map_[api_resource_id] = linked_ptr<T>(api_resource); |
| 273 return true; |
| 274 } |
| 275 return false; |
| 276 } |
| 277 |
252 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { | 278 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { |
253 DCHECK(ThreadingTraits::IsCalledOnValidThread()); | 279 DCHECK(ThreadingTraits::IsCalledOnValidThread()); |
254 return GetOwnedResourceIds(extension_id); | 280 return GetOwnedResourceIds(extension_id); |
255 } | 281 } |
256 | 282 |
257 void InitiateExtensionUnloadedCleanup(const std::string& extension_id) { | 283 void InitiateExtensionUnloadedCleanup(const std::string& extension_id) { |
258 if (ThreadingTraits::IsCalledOnValidThread()) { | 284 if (ThreadingTraits::IsCalledOnValidThread()) { |
259 CleanupResourcesFromUnloadedExtension(extension_id); | 285 CleanupResourcesFromUnloadedExtension(extension_id); |
260 } else { | 286 } else { |
261 ThreadingTraits::GetSequencedTaskRunner()->PostTask( | 287 ThreadingTraits::GetSequencedTaskRunner()->PostTask( |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
436 ->GetSequencedTaskRunnerWithShutdownBehavior( | 462 ->GetSequencedTaskRunnerWithShutdownBehavior( |
437 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( | 463 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( |
438 T::kSequenceToken), | 464 T::kSequenceToken), |
439 T::kShutdownBehavior); | 465 T::kShutdownBehavior); |
440 } | 466 } |
441 }; | 467 }; |
442 | 468 |
443 } // namespace extensions | 469 } // namespace extensions |
444 | 470 |
445 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 471 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
OLD | NEW |