| 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 "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
| 20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 21 #include "extensions/browser/browser_context_keyed_api_factory.h" | 22 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 22 #include "extensions/browser/extension_host.h" | 23 #include "extensions/browser/extension_host.h" |
| 24 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/browser/extension_registry_observer.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 class SerialEventDispatcher; | 33 class SerialEventDispatcher; |
| 31 } | 34 } |
| 32 | 35 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 // | 71 // |
| 69 // | 72 // |
| 70 // template <> | 73 // template <> |
| 71 // BrowserContextKeyedAPIFactory<ApiResourceManager<Resource> >* | 74 // BrowserContextKeyedAPIFactory<ApiResourceManager<Resource> >* |
| 72 // ApiResourceManager<Resource>::GetFactoryInstance() { | 75 // ApiResourceManager<Resource>::GetFactoryInstance() { |
| 73 // return g_factory.Pointer(); | 76 // return g_factory.Pointer(); |
| 74 // } | 77 // } |
| 75 template <class T> | 78 template <class T> |
| 76 class ApiResourceManager : public BrowserContextKeyedAPI, | 79 class ApiResourceManager : public BrowserContextKeyedAPI, |
| 77 public base::NonThreadSafe, | 80 public base::NonThreadSafe, |
| 78 public content::NotificationObserver { | 81 public content::NotificationObserver, |
| 82 public ExtensionRegistryObserver { |
| 79 public: | 83 public: |
| 80 explicit ApiResourceManager(content::BrowserContext* context) | 84 explicit ApiResourceManager(content::BrowserContext* context) |
| 81 : thread_id_(T::kThreadId), data_(new ApiResourceData(thread_id_)) { | 85 : thread_id_(T::kThreadId), |
| 82 registrar_.Add(this, | 86 data_(new ApiResourceData(thread_id_)), |
| 83 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 87 extension_registry_observer_(this) { |
| 84 content::NotificationService::AllSources()); | 88 extension_registry_observer_.Add(ExtensionRegistry::Get(context)); |
| 85 registrar_.Add(this, | 89 registrar_.Add(this, |
| 86 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 90 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 87 content::NotificationService::AllSources()); | 91 content::NotificationService::AllSources()); |
| 88 } | 92 } |
| 89 | 93 |
| 90 // For Testing. | 94 // For Testing. |
| 91 static ApiResourceManager<T>* CreateApiResourceManagerForTest( | 95 static ApiResourceManager<T>* CreateApiResourceManagerForTest( |
| 92 content::BrowserContext* context, | 96 content::BrowserContext* context, |
| 93 content::BrowserThread::ID thread_id) { | 97 content::BrowserThread::ID thread_id) { |
| 94 ApiResourceManager* manager = new ApiResourceManager<T>(context); | 98 ApiResourceManager* manager = new ApiResourceManager<T>(context); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 133 |
| 130 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { | 134 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { |
| 131 return data_->GetResourceIds(extension_id); | 135 return data_->GetResourceIds(extension_id); |
| 132 } | 136 } |
| 133 | 137 |
| 134 protected: | 138 protected: |
| 135 // content::NotificationObserver: | 139 // content::NotificationObserver: |
| 136 virtual void Observe(int type, | 140 virtual void Observe(int type, |
| 137 const content::NotificationSource& source, | 141 const content::NotificationSource& source, |
| 138 const content::NotificationDetails& details) OVERRIDE { | 142 const content::NotificationDetails& details) OVERRIDE { |
| 139 switch (type) { | 143 DCHECK_EQ(chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, type); |
| 140 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 144 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| 141 std::string id = content::Details<extensions::UnloadedExtensionInfo>( | 145 data_->InitiateExtensionSuspendedCleanup(host->extension_id()); |
| 142 details)->extension->id(); | 146 } |
| 143 data_->InitiateExtensionUnloadedCleanup(id); | 147 |
| 144 break; | 148 // ExtensionRegistryObserver: |
| 145 } | 149 virtual void OnExtensionUnloaded( |
| 146 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 150 content::BrowserContext* browser_context, |
| 147 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 151 const Extension* extension, |
| 148 data_->InitiateExtensionSuspendedCleanup(host->extension_id()); | 152 UnloadedExtensionInfo::Reason reason) OVERRIDE { |
| 149 break; | 153 data_->InitiateExtensionUnloadedCleanup(extension->id()); |
| 150 } | |
| 151 } | |
| 152 } | 154 } |
| 153 | 155 |
| 154 private: | 156 private: |
| 155 // TODO(rockot): ApiResourceData could be moved out of ApiResourceManager and | 157 // TODO(rockot): ApiResourceData could be moved out of ApiResourceManager and |
| 156 // we could avoid maintaining a friends list here. | 158 // we could avoid maintaining a friends list here. |
| 157 friend class BluetoothAPI; | 159 friend class BluetoothAPI; |
| 158 friend class api::BluetoothSocketApiFunction; | 160 friend class api::BluetoothSocketApiFunction; |
| 159 friend class api::BluetoothSocketEventDispatcher; | 161 friend class api::BluetoothSocketEventDispatcher; |
| 160 friend class api::SerialEventDispatcher; | 162 friend class api::SerialEventDispatcher; |
| 161 friend class core_api::TCPServerSocketEventDispatcher; | 163 friend class core_api::TCPServerSocketEventDispatcher; |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 336 |
| 335 int next_id_; | 337 int next_id_; |
| 336 const content::BrowserThread::ID thread_id_; | 338 const content::BrowserThread::ID thread_id_; |
| 337 ApiResourceMap api_resource_map_; | 339 ApiResourceMap api_resource_map_; |
| 338 ExtensionToResourceMap extension_resource_map_; | 340 ExtensionToResourceMap extension_resource_map_; |
| 339 }; | 341 }; |
| 340 | 342 |
| 341 content::BrowserThread::ID thread_id_; | 343 content::BrowserThread::ID thread_id_; |
| 342 content::NotificationRegistrar registrar_; | 344 content::NotificationRegistrar registrar_; |
| 343 scoped_refptr<ApiResourceData> data_; | 345 scoped_refptr<ApiResourceData> data_; |
| 346 |
| 347 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 348 extension_registry_observer_; |
| 344 }; | 349 }; |
| 345 | 350 |
| 346 } // namespace extensions | 351 } // namespace extensions |
| 347 | 352 |
| 348 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 353 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
| OLD | NEW |