| 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/threading/non_thread_safe.h" | 14 #include "base/threading/non_thread_safe.h" |
| 15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
| 16 #include "chrome/browser/extensions/extension_host.h" | 16 #include "chrome/browser/extensions/extension_host.h" |
| 17 #include "components/keyed_service/core/keyed_service.h" | 17 #include "components/keyed_service/core/keyed_service.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
| 21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 22 #include "extensions/browser/browser_context_keyed_api_factory.h" | 22 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 23 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
| 24 | 24 |
| 25 namespace extensions { | 25 namespace extensions { |
| 26 | 26 |
| 27 namespace api { | 27 namespace api { |
| 28 class BluetoothSocketApiFunction; |
| 29 class BluetoothSocketEventDispatcher; |
| 28 class SerialEventDispatcher; | 30 class SerialEventDispatcher; |
| 29 } | 31 } |
| 30 | 32 |
| 31 namespace core_api { | 33 namespace core_api { |
| 32 class TCPServerSocketEventDispatcher; | 34 class TCPServerSocketEventDispatcher; |
| 33 class TCPSocketEventDispatcher; | 35 class TCPSocketEventDispatcher; |
| 34 class UDPSocketEventDispatcher; | 36 class UDPSocketEventDispatcher; |
| 35 } | 37 } |
| 36 | 38 |
| 37 // An ApiResourceManager manages the lifetime of a set of resources that | 39 // An ApiResourceManager manages the lifetime of a set of resources that |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 147 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| 146 data_->InitiateExtensionSuspendedCleanup(host->extension_id()); | 148 data_->InitiateExtensionSuspendedCleanup(host->extension_id()); |
| 147 break; | 149 break; |
| 148 } | 150 } |
| 149 } | 151 } |
| 150 } | 152 } |
| 151 | 153 |
| 152 private: | 154 private: |
| 153 // TODO(rockot): ApiResourceData could be moved out of ApiResourceManager and | 155 // TODO(rockot): ApiResourceData could be moved out of ApiResourceManager and |
| 154 // we could avoid maintaining a friends list here. | 156 // we could avoid maintaining a friends list here. |
| 157 friend class BluetoothAPI; |
| 158 friend class api::BluetoothSocketApiFunction; |
| 159 friend class api::BluetoothSocketEventDispatcher; |
| 155 friend class api::SerialEventDispatcher; | 160 friend class api::SerialEventDispatcher; |
| 156 friend class core_api::TCPServerSocketEventDispatcher; | 161 friend class core_api::TCPServerSocketEventDispatcher; |
| 157 friend class core_api::TCPSocketEventDispatcher; | 162 friend class core_api::TCPSocketEventDispatcher; |
| 158 friend class core_api::UDPSocketEventDispatcher; | 163 friend class core_api::UDPSocketEventDispatcher; |
| 159 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; | 164 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; |
| 160 | 165 |
| 161 // BrowserContextKeyedAPI implementation. | 166 // BrowserContextKeyedAPI implementation. |
| 162 static const char* service_name() { return T::service_name(); } | 167 static const char* service_name() { return T::service_name(); } |
| 163 | 168 |
| 164 static const bool kServiceHasOwnInstanceInIncognito = true; | 169 static const bool kServiceHasOwnInstanceInIncognito = true; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 DCHECK(content::BrowserThread::CurrentlyOn(thread_id_)); | 213 DCHECK(content::BrowserThread::CurrentlyOn(thread_id_)); |
| 209 return GetOwnedResource(extension_id, api_resource_id); | 214 return GetOwnedResource(extension_id, api_resource_id); |
| 210 } | 215 } |
| 211 | 216 |
| 212 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { | 217 base::hash_set<int>* GetResourceIds(const std::string& extension_id) { |
| 213 DCHECK(content::BrowserThread::CurrentlyOn(thread_id_)); | 218 DCHECK(content::BrowserThread::CurrentlyOn(thread_id_)); |
| 214 return GetOwnedResourceIds(extension_id); | 219 return GetOwnedResourceIds(extension_id); |
| 215 } | 220 } |
| 216 | 221 |
| 217 void InitiateExtensionUnloadedCleanup(const std::string& extension_id) { | 222 void InitiateExtensionUnloadedCleanup(const std::string& extension_id) { |
| 218 content::BrowserThread::PostTask( | 223 if (content::BrowserThread::CurrentlyOn(thread_id_)) { |
| 219 thread_id_, | 224 CleanupResourcesFromUnloadedExtension(extension_id); |
| 220 FROM_HERE, | 225 } else { |
| 221 base::Bind(&ApiResourceData::CleanupResourcesFromUnloadedExtension, | 226 content::BrowserThread::PostTask( |
| 222 this, | 227 thread_id_, |
| 223 extension_id)); | 228 FROM_HERE, |
| 229 base::Bind(&ApiResourceData::CleanupResourcesFromUnloadedExtension, |
| 230 this, |
| 231 extension_id)); |
| 232 } |
| 224 } | 233 } |
| 225 | 234 |
| 226 void InitiateExtensionSuspendedCleanup(const std::string& extension_id) { | 235 void InitiateExtensionSuspendedCleanup(const std::string& extension_id) { |
| 227 content::BrowserThread::PostTask( | 236 if (content::BrowserThread::CurrentlyOn(thread_id_)) { |
| 228 thread_id_, | 237 CleanupResourcesFromSuspendedExtension(extension_id); |
| 229 FROM_HERE, | 238 } else { |
| 230 base::Bind(&ApiResourceData::CleanupResourcesFromSuspendedExtension, | 239 content::BrowserThread::PostTask( |
| 231 this, | 240 thread_id_, |
| 232 extension_id)); | 241 FROM_HERE, |
| 242 base::Bind(&ApiResourceData::CleanupResourcesFromSuspendedExtension, |
| 243 this, |
| 244 extension_id)); |
| 245 } |
| 233 } | 246 } |
| 234 | 247 |
| 235 void InititateCleanup() { | 248 void InititateCleanup() { |
| 236 content::BrowserThread::PostTask( | 249 if (content::BrowserThread::CurrentlyOn(thread_id_)) { |
| 237 thread_id_, FROM_HERE, base::Bind(&ApiResourceData::Cleanup, this)); | 250 Cleanup(); |
| 251 } else { |
| 252 content::BrowserThread::PostTask( |
| 253 thread_id_, FROM_HERE, base::Bind(&ApiResourceData::Cleanup, this)); |
| 254 } |
| 238 } | 255 } |
| 239 | 256 |
| 240 private: | 257 private: |
| 241 friend class base::RefCountedThreadSafe<ApiResourceData>; | 258 friend class base::RefCountedThreadSafe<ApiResourceData>; |
| 242 | 259 |
| 243 virtual ~ApiResourceData() {} | 260 virtual ~ApiResourceData() {} |
| 244 | 261 |
| 245 T* GetOwnedResource(const std::string& extension_id, int api_resource_id) { | 262 T* GetOwnedResource(const std::string& extension_id, int api_resource_id) { |
| 246 linked_ptr<T> ptr = api_resource_map_[api_resource_id]; | 263 linked_ptr<T> ptr = api_resource_map_[api_resource_id]; |
| 247 T* resource = ptr.get(); | 264 T* resource = ptr.get(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 }; | 339 }; |
| 323 | 340 |
| 324 content::BrowserThread::ID thread_id_; | 341 content::BrowserThread::ID thread_id_; |
| 325 content::NotificationRegistrar registrar_; | 342 content::NotificationRegistrar registrar_; |
| 326 scoped_refptr<ApiResourceData> data_; | 343 scoped_refptr<ApiResourceData> data_; |
| 327 }; | 344 }; |
| 328 | 345 |
| 329 } // namespace extensions | 346 } // namespace extensions |
| 330 | 347 |
| 331 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ | 348 #endif // EXTENSIONS_BROWSER_API_API_RESOURCE_MANAGER_H_ |
| OLD | NEW |