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" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/scoped_observer.h" |
14 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
15 #include "components/keyed_service/core/keyed_service.h" | 16 #include "components/keyed_service/core/keyed_service.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
18 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
19 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
20 #include "extensions/browser/browser_context_keyed_api_factory.h" | 21 #include "extensions/browser/browser_context_keyed_api_factory.h" |
21 #include "extensions/browser/extension_host.h" | 22 #include "extensions/browser/extension_host.h" |
| 23 #include "extensions/browser/extension_registry.h" |
| 24 #include "extensions/browser/extension_registry_observer.h" |
22 #include "extensions/browser/notification_types.h" | 25 #include "extensions/browser/notification_types.h" |
23 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
24 | 27 |
25 namespace extensions { | 28 namespace extensions { |
26 | 29 |
27 namespace api { | 30 namespace api { |
28 class BluetoothSocketApiFunction; | 31 class BluetoothSocketApiFunction; |
29 class BluetoothSocketEventDispatcher; | 32 class BluetoothSocketEventDispatcher; |
30 } | 33 } |
31 | 34 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 // | 108 // |
106 // | 109 // |
107 // template <> | 110 // template <> |
108 // BrowserContextKeyedAPIFactory<ApiResourceManager<Resource> >* | 111 // BrowserContextKeyedAPIFactory<ApiResourceManager<Resource> >* |
109 // ApiResourceManager<Resource>::GetFactoryInstance() { | 112 // ApiResourceManager<Resource>::GetFactoryInstance() { |
110 // return g_factory.Pointer(); | 113 // return g_factory.Pointer(); |
111 // } | 114 // } |
112 template <class T, typename ThreadingTraits = NamedThreadTraits<T> > | 115 template <class T, typename ThreadingTraits = NamedThreadTraits<T> > |
113 class ApiResourceManager : public BrowserContextKeyedAPI, | 116 class ApiResourceManager : public BrowserContextKeyedAPI, |
114 public base::NonThreadSafe, | 117 public base::NonThreadSafe, |
115 public content::NotificationObserver { | 118 public content::NotificationObserver, |
| 119 public ExtensionRegistryObserver { |
116 public: | 120 public: |
117 explicit ApiResourceManager(content::BrowserContext* context) | 121 explicit ApiResourceManager(content::BrowserContext* context) |
118 : data_(new ApiResourceData()) { | 122 : data_(new ApiResourceData()), extension_registry_observer_(this) { |
119 registrar_.Add(this, | 123 extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
120 extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
121 content::NotificationService::AllSources()); | |
122 registrar_.Add(this, | 124 registrar_.Add(this, |
123 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 125 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
124 content::NotificationService::AllSources()); | 126 content::NotificationService::AllSources()); |
125 } | 127 } |
126 // For Testing. | 128 // For Testing. |
127 static ApiResourceManager<T, TestThreadTraits<T> >* | 129 static ApiResourceManager<T, TestThreadTraits<T> >* |
128 CreateApiResourceManagerForTest(content::BrowserContext* context, | 130 CreateApiResourceManagerForTest(content::BrowserContext* context, |
129 content::BrowserThread::ID thread_id) { | 131 content::BrowserThread::ID thread_id) { |
130 TestThreadTraits<T>::thread_id_ = thread_id; | 132 TestThreadTraits<T>::thread_id_ = thread_id; |
131 ApiResourceManager<T, TestThreadTraits<T> >* manager = | 133 ApiResourceManager<T, TestThreadTraits<T> >* manager = |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 int api_resource_id, | 180 int api_resource_id, |
179 T* resource) { | 181 T* resource) { |
180 return data_->Replace(extension_id, api_resource_id, resource); | 182 return data_->Replace(extension_id, api_resource_id, resource); |
181 } | 183 } |
182 | 184 |
183 protected: | 185 protected: |
184 // content::NotificationObserver: | 186 // content::NotificationObserver: |
185 virtual void Observe(int type, | 187 virtual void Observe(int type, |
186 const content::NotificationSource& source, | 188 const content::NotificationSource& source, |
187 const content::NotificationDetails& details) OVERRIDE { | 189 const content::NotificationDetails& details) OVERRIDE { |
188 switch (type) { | 190 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type); |
189 case extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 191 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
190 std::string id = content::Details<extensions::UnloadedExtensionInfo>( | 192 data_->InitiateExtensionSuspendedCleanup(host->extension_id()); |
191 details)->extension->id(); | 193 } |
192 data_->InitiateExtensionUnloadedCleanup(id); | 194 |
193 break; | 195 // ExtensionRegistryObserver: |
194 } | 196 virtual void OnExtensionUnloaded( |
195 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 197 content::BrowserContext* browser_context, |
196 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 198 const Extension* extension, |
197 data_->InitiateExtensionSuspendedCleanup(host->extension_id()); | 199 UnloadedExtensionInfo::Reason reason) OVERRIDE { |
198 break; | 200 data_->InitiateExtensionUnloadedCleanup(extension->id()); |
199 } | |
200 } | |
201 } | 201 } |
202 | 202 |
203 private: | 203 private: |
204 // TODO(rockot): ApiResourceData could be moved out of ApiResourceManager and | 204 // TODO(rockot): ApiResourceData could be moved out of ApiResourceManager and |
205 // we could avoid maintaining a friends list here. | 205 // we could avoid maintaining a friends list here. |
206 friend class BluetoothAPI; | 206 friend class BluetoothAPI; |
207 friend class api::BluetoothSocketApiFunction; | 207 friend class api::BluetoothSocketApiFunction; |
208 friend class api::BluetoothSocketEventDispatcher; | 208 friend class api::BluetoothSocketEventDispatcher; |
209 friend class core_api::SerialEventDispatcher; | 209 friend class core_api::SerialEventDispatcher; |
210 friend class core_api::TCPServerSocketEventDispatcher; | 210 friend class core_api::TCPServerSocketEventDispatcher; |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 | 391 |
392 int GenerateId() { return next_id_++; } | 392 int GenerateId() { return next_id_++; } |
393 | 393 |
394 int next_id_; | 394 int next_id_; |
395 ApiResourceMap api_resource_map_; | 395 ApiResourceMap api_resource_map_; |
396 ExtensionToResourceMap extension_resource_map_; | 396 ExtensionToResourceMap extension_resource_map_; |
397 }; | 397 }; |
398 | 398 |
399 content::NotificationRegistrar registrar_; | 399 content::NotificationRegistrar registrar_; |
400 scoped_refptr<ApiResourceData> data_; | 400 scoped_refptr<ApiResourceData> data_; |
| 401 |
| 402 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 403 extension_registry_observer_; |
401 }; | 404 }; |
402 | 405 |
403 // With WorkerPoolThreadTraits, ApiResourceManager can be used to manage the | 406 // With WorkerPoolThreadTraits, ApiResourceManager can be used to manage the |
404 // lifetime of a set of resources that live on sequenced task runner threads | 407 // lifetime of a set of resources that live on sequenced task runner threads |
405 // which ApiFunctions use. Examples of such resources are temporary file | 408 // which ApiFunctions use. Examples of such resources are temporary file |
406 // resources produced by certain API calls. | 409 // resources produced by certain API calls. |
407 // | 410 // |
408 // Instead of kThreadId. classes used for tracking such resources should define | 411 // Instead of kThreadId. classes used for tracking such resources should define |
409 // kSequenceToken and kShutdownBehavior to identify sequence task runner for | 412 // kSequenceToken and kShutdownBehavior to identify sequence task runner for |
410 // ApiResourceManager to work on and how pending tasks should behave on | 413 // ApiResourceManager to work on and how pending tasks should behave on |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 ->GetSequencedTaskRunnerWithShutdownBehavior( | 465 ->GetSequencedTaskRunnerWithShutdownBehavior( |
463 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( | 466 content::BrowserThread::GetBlockingPool()->GetNamedSequenceToken( |
464 T::kSequenceToken), | 467 T::kSequenceToken), |
465 T::kShutdownBehavior); | 468 T::kShutdownBehavior); |
466 } | 469 } |
467 }; | 470 }; |
468 | 471 |
469 } // namespace extensions | 472 } // namespace extensions |
470 | 473 |
471 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 474 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
OLD | NEW |