OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "base/lazy_instance.h" | 5 #include "base/lazy_instance.h" |
6 #include "chrome/browser/browser_process.h" | 6 #include "chrome/browser/browser_process.h" |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_
operation.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/destroy_partitions_
operation.h" |
9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
11 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 11 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
12 #include "chrome/browser/extensions/api/image_writer_private/write_from_file_ope
ration.h" | 12 #include "chrome/browser/extensions/api/image_writer_private/write_from_file_ope
ration.h" |
13 #include "chrome/browser/extensions/api/image_writer_private/write_from_url_oper
ation.h" | 13 #include "chrome/browser/extensions/api/image_writer_private/write_from_url_oper
ation.h" |
14 #include "chrome/browser/extensions/event_router_forwarder.h" | 14 #include "chrome/browser/extensions/event_router_forwarder.h" |
15 #include "chrome/browser/extensions/extension_service.h" | 15 #include "chrome/browser/extensions/extension_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
18 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
19 #include "extensions/browser/event_router.h" | 19 #include "extensions/browser/event_router.h" |
20 #include "extensions/browser/extension_host.h" | 20 #include "extensions/browser/extension_host.h" |
| 21 #include "extensions/browser/extension_registry.h" |
21 | 22 |
22 namespace image_writer_api = extensions::api::image_writer_private; | 23 namespace image_writer_api = extensions::api::image_writer_private; |
23 | 24 |
24 namespace extensions { | 25 namespace extensions { |
25 namespace image_writer { | 26 namespace image_writer { |
26 | 27 |
27 using content::BrowserThread; | 28 using content::BrowserThread; |
28 | 29 |
29 OperationManager::OperationManager(content::BrowserContext* context) | 30 OperationManager::OperationManager(content::BrowserContext* context) |
30 : profile_(Profile::FromBrowserContext(context)), weak_factory_(this) { | 31 : browser_context_(context), |
31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNINSTALLED, | 32 extension_registry_observer_(this), |
32 content::Source<Profile>(profile_)); | 33 weak_factory_(this) { |
33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 34 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
34 content::Source<Profile>(profile_)); | 35 Profile* profile = Profile::FromBrowserContext(browser_context_); |
35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, | 36 registrar_.Add(this, |
36 content::Source<Profile>(profile_)); | 37 chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED, |
37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | 38 content::Source<Profile>(profile)); |
38 content::Source<Profile>(profile_)); | 39 registrar_.Add(this, |
39 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 40 chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
40 content::Source<Profile>(profile_)); | 41 content::Source<Profile>(profile)); |
| 42 registrar_.Add(this, |
| 43 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 44 content::Source<Profile>(profile)); |
41 } | 45 } |
42 | 46 |
43 OperationManager::~OperationManager() { | 47 OperationManager::~OperationManager() { |
44 } | 48 } |
45 | 49 |
46 void OperationManager::Shutdown() { | 50 void OperationManager::Shutdown() { |
47 for (OperationMap::iterator iter = operations_.begin(); | 51 for (OperationMap::iterator iter = operations_.begin(); |
48 iter != operations_.end(); | 52 iter != operations_.end(); |
49 iter++) { | 53 iter++) { |
50 BrowserThread::PostTask(BrowserThread::FILE, | 54 BrowserThread::PostTask(BrowserThread::FILE, |
(...skipping 11 matching lines...) Expand all Loading... |
62 const Operation::StartWriteCallback& callback) { | 66 const Operation::StartWriteCallback& callback) { |
63 OperationMap::iterator existing_operation = operations_.find(extension_id); | 67 OperationMap::iterator existing_operation = operations_.find(extension_id); |
64 | 68 |
65 if (existing_operation != operations_.end()) { | 69 if (existing_operation != operations_.end()) { |
66 return callback.Run(false, error::kOperationAlreadyInProgress); | 70 return callback.Run(false, error::kOperationAlreadyInProgress); |
67 } | 71 } |
68 | 72 |
69 scoped_refptr<Operation> operation( | 73 scoped_refptr<Operation> operation( |
70 new WriteFromUrlOperation(weak_factory_.GetWeakPtr(), | 74 new WriteFromUrlOperation(weak_factory_.GetWeakPtr(), |
71 extension_id, | 75 extension_id, |
72 profile_->GetRequestContext(), | 76 browser_context_->GetRequestContext(), |
73 url, | 77 url, |
74 hash, | 78 hash, |
75 device_path)); | 79 device_path)); |
76 operations_[extension_id] = operation; | 80 operations_[extension_id] = operation; |
77 BrowserThread::PostTask(BrowserThread::FILE, | 81 BrowserThread::PostTask(BrowserThread::FILE, |
78 FROM_HERE, | 82 FROM_HERE, |
79 base::Bind(&Operation::Start, operation)); | 83 base::Bind(&Operation::Start, operation)); |
80 callback.Run(true, ""); | 84 callback.Run(true, ""); |
81 } | 85 } |
82 | 86 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 146 |
143 image_writer_api::ProgressInfo info; | 147 image_writer_api::ProgressInfo info; |
144 info.stage = stage; | 148 info.stage = stage; |
145 info.percent_complete = progress; | 149 info.percent_complete = progress; |
146 | 150 |
147 scoped_ptr<base::ListValue> args( | 151 scoped_ptr<base::ListValue> args( |
148 image_writer_api::OnWriteProgress::Create(info)); | 152 image_writer_api::OnWriteProgress::Create(info)); |
149 scoped_ptr<Event> event(new Event( | 153 scoped_ptr<Event> event(new Event( |
150 image_writer_api::OnWriteProgress::kEventName, args.Pass())); | 154 image_writer_api::OnWriteProgress::kEventName, args.Pass())); |
151 | 155 |
152 EventRouter::Get(profile_) | 156 EventRouter::Get(browser_context_) |
153 ->DispatchEventToExtension(extension_id, event.Pass()); | 157 ->DispatchEventToExtension(extension_id, event.Pass()); |
154 } | 158 } |
155 | 159 |
156 void OperationManager::OnComplete(const ExtensionId& extension_id) { | 160 void OperationManager::OnComplete(const ExtensionId& extension_id) { |
157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 161 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
158 | 162 |
159 scoped_ptr<base::ListValue> args(image_writer_api::OnWriteComplete::Create()); | 163 scoped_ptr<base::ListValue> args(image_writer_api::OnWriteComplete::Create()); |
160 scoped_ptr<Event> event(new Event( | 164 scoped_ptr<Event> event(new Event( |
161 image_writer_api::OnWriteComplete::kEventName, args.Pass())); | 165 image_writer_api::OnWriteComplete::kEventName, args.Pass())); |
162 | 166 |
163 EventRouter::Get(profile_) | 167 EventRouter::Get(browser_context_) |
164 ->DispatchEventToExtension(extension_id, event.Pass()); | 168 ->DispatchEventToExtension(extension_id, event.Pass()); |
165 | 169 |
166 DeleteOperation(extension_id); | 170 DeleteOperation(extension_id); |
167 } | 171 } |
168 | 172 |
169 void OperationManager::OnError(const ExtensionId& extension_id, | 173 void OperationManager::OnError(const ExtensionId& extension_id, |
170 image_writer_api::Stage stage, | 174 image_writer_api::Stage stage, |
171 int progress, | 175 int progress, |
172 const std::string& error_message) { | 176 const std::string& error_message) { |
173 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
174 image_writer_api::ProgressInfo info; | 178 image_writer_api::ProgressInfo info; |
175 | 179 |
176 DLOG(ERROR) << "ImageWriter error: " << error_message; | 180 DLOG(ERROR) << "ImageWriter error: " << error_message; |
177 | 181 |
178 info.stage = stage; | 182 info.stage = stage; |
179 info.percent_complete = progress; | 183 info.percent_complete = progress; |
180 | 184 |
181 scoped_ptr<base::ListValue> args( | 185 scoped_ptr<base::ListValue> args( |
182 image_writer_api::OnWriteError::Create(info, error_message)); | 186 image_writer_api::OnWriteError::Create(info, error_message)); |
183 scoped_ptr<Event> event(new Event( | 187 scoped_ptr<Event> event(new Event( |
184 image_writer_api::OnWriteError::kEventName, args.Pass())); | 188 image_writer_api::OnWriteError::kEventName, args.Pass())); |
185 | 189 |
186 EventRouter::Get(profile_) | 190 EventRouter::Get(browser_context_) |
187 ->DispatchEventToExtension(extension_id, event.Pass()); | 191 ->DispatchEventToExtension(extension_id, event.Pass()); |
188 | 192 |
189 DeleteOperation(extension_id); | 193 DeleteOperation(extension_id); |
190 } | 194 } |
191 | 195 |
192 Operation* OperationManager::GetOperation(const ExtensionId& extension_id) { | 196 Operation* OperationManager::GetOperation(const ExtensionId& extension_id) { |
193 OperationMap::iterator existing_operation = operations_.find(extension_id); | 197 OperationMap::iterator existing_operation = operations_.find(extension_id); |
194 | 198 |
195 if (existing_operation == operations_.end()) | 199 if (existing_operation == operations_.end()) |
196 return NULL; | 200 return NULL; |
197 return existing_operation->second.get(); | 201 return existing_operation->second.get(); |
198 } | 202 } |
199 | 203 |
200 void OperationManager::DeleteOperation(const ExtensionId& extension_id) { | 204 void OperationManager::DeleteOperation(const ExtensionId& extension_id) { |
201 OperationMap::iterator existing_operation = operations_.find(extension_id); | 205 OperationMap::iterator existing_operation = operations_.find(extension_id); |
202 if (existing_operation != operations_.end()) { | 206 if (existing_operation != operations_.end()) { |
203 operations_.erase(existing_operation); | 207 operations_.erase(existing_operation); |
204 } | 208 } |
205 } | 209 } |
206 | 210 |
| 211 void OperationManager::OnExtensionUnloaded( |
| 212 content::BrowserContext* browser_context, |
| 213 const Extension* extension, |
| 214 UnloadedExtensionInfo::Reason reason) { |
| 215 DeleteOperation(extension->id()); |
| 216 } |
| 217 |
207 void OperationManager::Observe(int type, | 218 void OperationManager::Observe(int type, |
208 const content::NotificationSource& source, | 219 const content::NotificationSource& source, |
209 const content::NotificationDetails& details) { | 220 const content::NotificationDetails& details) { |
210 switch (type) { | 221 switch (type) { |
211 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | |
212 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); | |
213 break; | |
214 } | |
215 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | |
216 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); | |
217 break; | |
218 } | |
219 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: { | 222 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: { |
220 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); | 223 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); |
221 break; | 224 break; |
222 } | 225 } |
223 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { | 226 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { |
224 DeleteOperation( | 227 DeleteOperation( |
225 content::Details<ExtensionHost>(details)->extension()->id()); | 228 content::Details<ExtensionHost>(details)->extension()->id()); |
226 break; | 229 break; |
227 } | 230 } |
228 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 231 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
(...skipping 16 matching lines...) Expand all Loading... |
245 g_factory = LAZY_INSTANCE_INITIALIZER; | 248 g_factory = LAZY_INSTANCE_INITIALIZER; |
246 | 249 |
247 BrowserContextKeyedAPIFactory<OperationManager>* | 250 BrowserContextKeyedAPIFactory<OperationManager>* |
248 OperationManager::GetFactoryInstance() { | 251 OperationManager::GetFactoryInstance() { |
249 return g_factory.Pointer(); | 252 return g_factory.Pointer(); |
250 } | 253 } |
251 | 254 |
252 | 255 |
253 } // namespace image_writer | 256 } // namespace image_writer |
254 } // namespace extensions | 257 } // namespace extensions |
OLD | NEW |