OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_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/api/profile_keyed_api_factory.h" | |
17 #include "chrome/browser/extensions/extension_host.h" | 16 #include "chrome/browser/extensions/extension_host.h" |
18 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | 17 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" |
19 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
22 #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" |
23 #include "extensions/common/extension.h" | 23 #include "extensions/common/extension.h" |
24 | 24 |
25 namespace extensions { | 25 namespace extensions { |
26 namespace api { | 26 namespace api { |
27 class SerialEventDispatcher; | 27 class SerialEventDispatcher; |
28 class TCPServerSocketEventDispatcher; | 28 class TCPServerSocketEventDispatcher; |
29 class TCPSocketEventDispatcher; | 29 class TCPSocketEventDispatcher; |
30 class UDPSocketEventDispatcher; | 30 class UDPSocketEventDispatcher; |
31 } | 31 } |
32 } | 32 } |
(...skipping 19 matching lines...) Expand all Loading... |
52 // static const BrowserThread::ID kThreadId = BrowserThread::FILE; | 52 // static const BrowserThread::ID kThreadId = BrowserThread::FILE; |
53 // private: | 53 // private: |
54 // friend class ApiResourceManager<Resource>; | 54 // friend class ApiResourceManager<Resource>; |
55 // static const char* service_name() { | 55 // static const char* service_name() { |
56 // return "ResourceManager"; | 56 // return "ResourceManager"; |
57 // } | 57 // } |
58 // }; | 58 // }; |
59 // | 59 // |
60 // In the cc file: | 60 // In the cc file: |
61 // | 61 // |
62 // static base::LazyInstance<ProfileKeyedAPIFactory< | 62 // static base::LazyInstance<BrowserContextKeyedAPIFactory< |
63 // ApiResourceManager<Resource> > > | 63 // ApiResourceManager<Resource> > > |
64 // g_factory = LAZY_INSTANCE_INITIALIZER; | 64 // g_factory = LAZY_INSTANCE_INITIALIZER; |
65 // | 65 // |
66 // | 66 // |
67 // template <> | 67 // template <> |
68 // ProfileKeyedAPIFactory<ApiResourceManager<Resource> >* | 68 // BrowserContextKeyedAPIFactory<ApiResourceManager<Resource> >* |
69 // ApiResourceManager<Resource>::GetFactoryInstance() { | 69 // ApiResourceManager<Resource>::GetFactoryInstance() { |
70 // return g_factory.Pointer(); | 70 // return g_factory.Pointer(); |
71 // } | 71 // } |
72 template <class T> | 72 template <class T> |
73 class ApiResourceManager : public ProfileKeyedAPI, | 73 class ApiResourceManager : public BrowserContextKeyedAPI, |
74 public base::NonThreadSafe, | 74 public base::NonThreadSafe, |
75 public content::NotificationObserver { | 75 public content::NotificationObserver { |
76 public: | 76 public: |
77 explicit ApiResourceManager(content::BrowserContext* context) | 77 explicit ApiResourceManager(content::BrowserContext* context) |
78 : thread_id_(T::kThreadId), data_(new ApiResourceData(thread_id_)) { | 78 : thread_id_(T::kThreadId), data_(new ApiResourceData(thread_id_)) { |
79 registrar_.Add( | 79 registrar_.Add( |
80 this, | 80 this, |
81 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 81 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
82 content::NotificationService::AllSources()); | 82 content::NotificationService::AllSources()); |
83 registrar_.Add( | 83 registrar_.Add( |
(...skipping 15 matching lines...) Expand all Loading... |
99 virtual ~ApiResourceManager() { | 99 virtual ~ApiResourceManager() { |
100 DCHECK(CalledOnValidThread()); | 100 DCHECK(CalledOnValidThread()); |
101 DCHECK(content::BrowserThread::IsMessageLoopValid(thread_id_)) << | 101 DCHECK(content::BrowserThread::IsMessageLoopValid(thread_id_)) << |
102 "A unit test is using an ApiResourceManager but didn't provide " | 102 "A unit test is using an ApiResourceManager but didn't provide " |
103 "the thread message loop needed for that kind of resource. " | 103 "the thread message loop needed for that kind of resource. " |
104 "Please ensure that the appropriate message loop is operational."; | 104 "Please ensure that the appropriate message loop is operational."; |
105 | 105 |
106 data_->InititateCleanup(); | 106 data_->InititateCleanup(); |
107 } | 107 } |
108 | 108 |
109 // ProfileKeyedAPI implementation. | 109 // BrowserContextKeyedAPI implementation. |
110 static ProfileKeyedAPIFactory<ApiResourceManager<T> >* GetFactoryInstance(); | 110 static BrowserContextKeyedAPIFactory<ApiResourceManager<T> >* |
| 111 GetFactoryInstance(); |
111 | 112 |
112 // Convenience method to get the ApiResourceManager for a profile. | 113 // Convenience method to get the ApiResourceManager for a profile. |
113 static ApiResourceManager<T>* Get(content::BrowserContext* context) { | 114 static ApiResourceManager<T>* Get(content::BrowserContext* context) { |
114 return ProfileKeyedAPIFactory<ApiResourceManager<T> >::GetForProfile( | 115 return BrowserContextKeyedAPIFactory<ApiResourceManager<T> >::Get(context); |
115 context); | |
116 } | 116 } |
117 | 117 |
118 // Takes ownership. | 118 // Takes ownership. |
119 int Add(T* api_resource) { | 119 int Add(T* api_resource) { |
120 return data_->Add(api_resource); | 120 return data_->Add(api_resource); |
121 } | 121 } |
122 | 122 |
123 void Remove(const std::string& extension_id, int api_resource_id) { | 123 void Remove(const std::string& extension_id, int api_resource_id) { |
124 data_->Remove(extension_id, api_resource_id); | 124 data_->Remove(extension_id, api_resource_id); |
125 } | 125 } |
(...skipping 25 matching lines...) Expand all Loading... |
151 break; | 151 break; |
152 } | 152 } |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 private: | 156 private: |
157 friend class api::SerialEventDispatcher; | 157 friend class api::SerialEventDispatcher; |
158 friend class api::TCPServerSocketEventDispatcher; | 158 friend class api::TCPServerSocketEventDispatcher; |
159 friend class api::TCPSocketEventDispatcher; | 159 friend class api::TCPSocketEventDispatcher; |
160 friend class api::UDPSocketEventDispatcher; | 160 friend class api::UDPSocketEventDispatcher; |
161 friend class ProfileKeyedAPIFactory<ApiResourceManager<T> >; | 161 friend class BrowserContextKeyedAPIFactory<ApiResourceManager<T> >; |
162 // ProfileKeyedAPI implementation. | 162 // BrowserContextKeyedAPI implementation. |
163 static const char* service_name() { | 163 static const char* service_name() { |
164 return T::service_name(); | 164 return T::service_name(); |
165 } | 165 } |
166 static const bool kServiceHasOwnInstanceInIncognito = true; | 166 static const bool kServiceHasOwnInstanceInIncognito = true; |
167 static const bool kServiceIsNULLWhileTesting = true; | 167 static const bool kServiceIsNULLWhileTesting = true; |
168 | 168 |
169 // ApiResourceData class handles resource bookkeeping on a thread | 169 // ApiResourceData class handles resource bookkeeping on a thread |
170 // where resource lifetime is handled. | 170 // where resource lifetime is handled. |
171 class ApiResourceData : public base::RefCountedThreadSafe<ApiResourceData> { | 171 class ApiResourceData : public base::RefCountedThreadSafe<ApiResourceData> { |
172 public: | 172 public: |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
324 }; | 324 }; |
325 | 325 |
326 content::BrowserThread::ID thread_id_; | 326 content::BrowserThread::ID thread_id_; |
327 content::NotificationRegistrar registrar_; | 327 content::NotificationRegistrar registrar_; |
328 scoped_refptr<ApiResourceData> data_; | 328 scoped_refptr<ApiResourceData> data_; |
329 }; | 329 }; |
330 | 330 |
331 } // namespace extensions | 331 } // namespace extensions |
332 | 332 |
333 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ | 333 #endif // CHROME_BROWSER_EXTENSIONS_API_API_RESOURCE_MANAGER_H_ |
OLD | NEW |