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_UNINSTALLED, |
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_PROCESS_TERMINATED, |
40 content::Source<Profile>(profile_)); | 41 content::Source<Profile>(profile)); |
42 registrar_.Add(this, | |
43 chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | |
44 content::Source<Profile>(profile)); | |
45 registrar_.Add(this, | |
46 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
47 content::Source<Profile>(profile)); | |
41 } | 48 } |
42 | 49 |
43 OperationManager::~OperationManager() { | 50 OperationManager::~OperationManager() { |
44 } | 51 } |
45 | 52 |
46 void OperationManager::Shutdown() { | 53 void OperationManager::Shutdown() { |
47 for (OperationMap::iterator iter = operations_.begin(); | 54 for (OperationMap::iterator iter = operations_.begin(); |
48 iter != operations_.end(); | 55 iter != operations_.end(); |
49 iter++) { | 56 iter++) { |
50 BrowserThread::PostTask(BrowserThread::FILE, | 57 BrowserThread::PostTask(BrowserThread::FILE, |
(...skipping 11 matching lines...) Expand all Loading... | |
62 const Operation::StartWriteCallback& callback) { | 69 const Operation::StartWriteCallback& callback) { |
63 OperationMap::iterator existing_operation = operations_.find(extension_id); | 70 OperationMap::iterator existing_operation = operations_.find(extension_id); |
64 | 71 |
65 if (existing_operation != operations_.end()) { | 72 if (existing_operation != operations_.end()) { |
66 return callback.Run(false, error::kOperationAlreadyInProgress); | 73 return callback.Run(false, error::kOperationAlreadyInProgress); |
67 } | 74 } |
68 | 75 |
69 scoped_refptr<Operation> operation( | 76 scoped_refptr<Operation> operation( |
70 new WriteFromUrlOperation(weak_factory_.GetWeakPtr(), | 77 new WriteFromUrlOperation(weak_factory_.GetWeakPtr(), |
71 extension_id, | 78 extension_id, |
72 profile_->GetRequestContext(), | 79 browser_context_->GetRequestContext(), |
73 url, | 80 url, |
74 hash, | 81 hash, |
75 device_path)); | 82 device_path)); |
76 operations_[extension_id] = operation; | 83 operations_[extension_id] = operation; |
77 BrowserThread::PostTask(BrowserThread::FILE, | 84 BrowserThread::PostTask(BrowserThread::FILE, |
78 FROM_HERE, | 85 FROM_HERE, |
79 base::Bind(&Operation::Start, operation)); | 86 base::Bind(&Operation::Start, operation)); |
80 callback.Run(true, ""); | 87 callback.Run(true, ""); |
81 } | 88 } |
82 | 89 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
142 | 149 |
143 image_writer_api::ProgressInfo info; | 150 image_writer_api::ProgressInfo info; |
144 info.stage = stage; | 151 info.stage = stage; |
145 info.percent_complete = progress; | 152 info.percent_complete = progress; |
146 | 153 |
147 scoped_ptr<base::ListValue> args( | 154 scoped_ptr<base::ListValue> args( |
148 image_writer_api::OnWriteProgress::Create(info)); | 155 image_writer_api::OnWriteProgress::Create(info)); |
149 scoped_ptr<Event> event(new Event( | 156 scoped_ptr<Event> event(new Event( |
150 image_writer_api::OnWriteProgress::kEventName, args.Pass())); | 157 image_writer_api::OnWriteProgress::kEventName, args.Pass())); |
151 | 158 |
152 EventRouter::Get(profile_) | 159 EventRouter::Get(browser_context_) |
153 ->DispatchEventToExtension(extension_id, event.Pass()); | 160 ->DispatchEventToExtension(extension_id, event.Pass()); |
154 } | 161 } |
155 | 162 |
156 void OperationManager::OnComplete(const ExtensionId& extension_id) { | 163 void OperationManager::OnComplete(const ExtensionId& extension_id) { |
157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 164 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
158 | 165 |
159 scoped_ptr<base::ListValue> args(image_writer_api::OnWriteComplete::Create()); | 166 scoped_ptr<base::ListValue> args(image_writer_api::OnWriteComplete::Create()); |
160 scoped_ptr<Event> event(new Event( | 167 scoped_ptr<Event> event(new Event( |
161 image_writer_api::OnWriteComplete::kEventName, args.Pass())); | 168 image_writer_api::OnWriteComplete::kEventName, args.Pass())); |
162 | 169 |
163 EventRouter::Get(profile_) | 170 EventRouter::Get(browser_context_) |
164 ->DispatchEventToExtension(extension_id, event.Pass()); | 171 ->DispatchEventToExtension(extension_id, event.Pass()); |
165 | 172 |
166 DeleteOperation(extension_id); | 173 DeleteOperation(extension_id); |
167 } | 174 } |
168 | 175 |
169 void OperationManager::OnError(const ExtensionId& extension_id, | 176 void OperationManager::OnError(const ExtensionId& extension_id, |
170 image_writer_api::Stage stage, | 177 image_writer_api::Stage stage, |
171 int progress, | 178 int progress, |
172 const std::string& error_message) { | 179 const std::string& error_message) { |
173 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 180 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
174 image_writer_api::ProgressInfo info; | 181 image_writer_api::ProgressInfo info; |
175 | 182 |
176 DLOG(ERROR) << "ImageWriter error: " << error_message; | 183 DLOG(ERROR) << "ImageWriter error: " << error_message; |
177 | 184 |
178 info.stage = stage; | 185 info.stage = stage; |
179 info.percent_complete = progress; | 186 info.percent_complete = progress; |
180 | 187 |
181 scoped_ptr<base::ListValue> args( | 188 scoped_ptr<base::ListValue> args( |
182 image_writer_api::OnWriteError::Create(info, error_message)); | 189 image_writer_api::OnWriteError::Create(info, error_message)); |
183 scoped_ptr<Event> event(new Event( | 190 scoped_ptr<Event> event(new Event( |
184 image_writer_api::OnWriteError::kEventName, args.Pass())); | 191 image_writer_api::OnWriteError::kEventName, args.Pass())); |
185 | 192 |
186 EventRouter::Get(profile_) | 193 EventRouter::Get(browser_context_) |
187 ->DispatchEventToExtension(extension_id, event.Pass()); | 194 ->DispatchEventToExtension(extension_id, event.Pass()); |
188 | 195 |
189 DeleteOperation(extension_id); | 196 DeleteOperation(extension_id); |
190 } | 197 } |
191 | 198 |
192 Operation* OperationManager::GetOperation(const ExtensionId& extension_id) { | 199 Operation* OperationManager::GetOperation(const ExtensionId& extension_id) { |
193 OperationMap::iterator existing_operation = operations_.find(extension_id); | 200 OperationMap::iterator existing_operation = operations_.find(extension_id); |
194 | 201 |
195 if (existing_operation == operations_.end()) | 202 if (existing_operation == operations_.end()) |
196 return NULL; | 203 return NULL; |
197 return existing_operation->second.get(); | 204 return existing_operation->second.get(); |
198 } | 205 } |
199 | 206 |
200 void OperationManager::DeleteOperation(const ExtensionId& extension_id) { | 207 void OperationManager::DeleteOperation(const ExtensionId& extension_id) { |
201 OperationMap::iterator existing_operation = operations_.find(extension_id); | 208 OperationMap::iterator existing_operation = operations_.find(extension_id); |
202 if (existing_operation != operations_.end()) { | 209 if (existing_operation != operations_.end()) { |
203 operations_.erase(existing_operation); | 210 operations_.erase(existing_operation); |
204 } | 211 } |
205 } | 212 } |
206 | 213 |
214 void OperationManager::OnExtensionUnloaded( | |
215 content::BrowserContext* browser_context, | |
216 const Extension* extension, | |
217 UnloadedExtensionInfo::Reason reason) { | |
218 DeleteOperation(extension->id()); | |
219 } | |
220 | |
207 void OperationManager::Observe(int type, | 221 void OperationManager::Observe(int type, |
208 const content::NotificationSource& source, | 222 const content::NotificationSource& source, |
209 const content::NotificationDetails& details) { | 223 const content::NotificationDetails& details) { |
210 switch (type) { | 224 switch (type) { |
211 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { | 225 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED: { |
212 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); | 226 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); |
213 break; | 227 break; |
214 } | 228 } |
not at google - send to devlin
2014/05/06 15:04:11
this case looks wrong to me, it should be covered
limasdf
2014/05/06 15:19:10
Done.
| |
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: { | 229 case chrome::NOTIFICATION_EXTENSION_PROCESS_TERMINATED: { |
220 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); | 230 DeleteOperation(content::Details<const Extension>(details).ptr()->id()); |
221 break; | 231 break; |
222 } | 232 } |
223 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { | 233 case chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE: { |
224 DeleteOperation( | 234 DeleteOperation( |
225 content::Details<ExtensionHost>(details)->extension()->id()); | 235 content::Details<ExtensionHost>(details)->extension()->id()); |
226 break; | 236 break; |
227 } | 237 } |
228 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 238 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
(...skipping 16 matching lines...) Expand all Loading... | |
245 g_factory = LAZY_INSTANCE_INITIALIZER; | 255 g_factory = LAZY_INSTANCE_INITIALIZER; |
246 | 256 |
247 BrowserContextKeyedAPIFactory<OperationManager>* | 257 BrowserContextKeyedAPIFactory<OperationManager>* |
248 OperationManager::GetFactoryInstance() { | 258 OperationManager::GetFactoryInstance() { |
249 return g_factory.Pointer(); | 259 return g_factory.Pointer(); |
250 } | 260 } |
251 | 261 |
252 | 262 |
253 } // namespace image_writer | 263 } // namespace image_writer |
254 } // namespace extensions | 264 } // namespace extensions |
OLD | NEW |