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 #include "chrome/browser/extensions/webstore_installer.h" | 5 #include "chrome/browser/extensions/webstore_installer.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 content::Source<Profile>(profile->GetOriginalProfile())); | 194 content::Source<Profile>(profile->GetOriginalProfile())); |
195 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, | 195 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR, |
196 content::Source<CrxInstaller>(NULL)); | 196 content::Source<CrxInstaller>(NULL)); |
197 } | 197 } |
198 | 198 |
199 void WebstoreInstaller::Start() { | 199 void WebstoreInstaller::Start() { |
200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 200 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
201 AddRef(); // Balanced in ReportSuccess and ReportFailure. | 201 AddRef(); // Balanced in ReportSuccess and ReportFailure. |
202 | 202 |
203 if (!Extension::IdIsValid(id_)) { | 203 if (!Extension::IdIsValid(id_)) { |
204 ReportFailure(kInvalidIdError); | 204 ReportFailure(kInvalidIdError, false); |
205 return; | 205 return; |
206 } | 206 } |
207 | 207 |
208 FilePath download_path = DownloadPrefs::FromDownloadManager( | 208 FilePath download_path = DownloadPrefs::FromDownloadManager( |
209 BrowserContext::GetDownloadManager(profile_))->DownloadPath(); | 209 BrowserContext::GetDownloadManager(profile_))->DownloadPath(); |
210 BrowserThread::PostTask( | 210 BrowserThread::PostTask( |
211 BrowserThread::FILE, FROM_HERE, | 211 BrowserThread::FILE, FROM_HERE, |
212 base::Bind(&GetDownloadFilePath, download_path, id_, | 212 base::Bind(&GetDownloadFilePath, download_path, id_, |
213 base::Bind(&WebstoreInstaller::StartDownload, this))); | 213 base::Bind(&WebstoreInstaller::StartDownload, this))); |
214 } | 214 } |
215 | 215 |
216 void WebstoreInstaller::Observe(int type, | 216 void WebstoreInstaller::Observe(int type, |
217 const content::NotificationSource& source, | 217 const content::NotificationSource& source, |
218 const content::NotificationDetails& details) { | 218 const content::NotificationDetails& details) { |
219 switch (type) { | 219 switch (type) { |
220 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { | 220 case chrome::NOTIFICATION_CRX_INSTALLER_DONE: { |
221 const Extension* extension = | 221 const Extension* extension = |
222 content::Details<const Extension>(details).ptr(); | 222 content::Details<const Extension>(details).ptr(); |
223 CrxInstaller* installer = content::Source<CrxInstaller>(source).ptr(); | 223 CrxInstaller* installer = content::Source<CrxInstaller>(source).ptr(); |
224 if (extension == NULL && download_item_ != NULL && | 224 if (extension == NULL && download_item_ != NULL && |
225 installer->download_url() == download_item_->GetURL() && | 225 installer->download_url() == download_item_->GetURL() && |
226 installer->profile()->IsSameProfile(profile_)) { | 226 installer->profile()->IsSameProfile(profile_)) { |
227 ReportFailure(kInstallCanceledError); | 227 ReportFailure(kInstallCanceledError, true); |
Steve McKay
2012/09/25 18:56:04
Add a method name ReportCancelled, call that inste
sail
2012/10/02 18:57:01
I changed ReportFailure to take a enum. Do you sti
Greg Billock
2012/10/02 19:20:38
This is fine.
On 2012/10/02 18:57:01, sail wrote:
| |
228 } | 228 } |
229 break; | 229 break; |
230 } | 230 } |
231 | 231 |
232 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { | 232 case chrome::NOTIFICATION_EXTENSION_INSTALLED: { |
233 CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); | 233 CHECK(profile_->IsSameProfile(content::Source<Profile>(source).ptr())); |
234 const Extension* extension = | 234 const Extension* extension = |
235 content::Details<const Extension>(details).ptr(); | 235 content::Details<const Extension>(details).ptr(); |
236 if (id_ == extension->id()) | 236 if (id_ == extension->id()) |
237 ReportSuccess(); | 237 ReportSuccess(); |
238 break; | 238 break; |
239 } | 239 } |
240 | 240 |
241 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { | 241 case chrome::NOTIFICATION_EXTENSION_INSTALL_ERROR: { |
242 CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); | 242 CrxInstaller* crx_installer = content::Source<CrxInstaller>(source).ptr(); |
243 CHECK(crx_installer); | 243 CHECK(crx_installer); |
244 if (!profile_->IsSameProfile(crx_installer->profile())) | 244 if (!profile_->IsSameProfile(crx_installer->profile())) |
245 return; | 245 return; |
246 | 246 |
247 // TODO(rdevlin.cronin): Continue removing std::string errors and | 247 // TODO(rdevlin.cronin): Continue removing std::string errors and |
248 // replacing with string16 | 248 // replacing with string16 |
249 const string16* error = content::Details<const string16>(details).ptr(); | 249 const string16* error = content::Details<const string16>(details).ptr(); |
250 const std::string utf8_error = UTF16ToUTF8(*error); | 250 const std::string utf8_error = UTF16ToUTF8(*error); |
251 if (download_url_ == crx_installer->original_download_url()) | 251 if (download_url_ == crx_installer->original_download_url()) |
252 ReportFailure(utf8_error); | 252 ReportFailure(utf8_error, false); |
253 break; | 253 break; |
254 } | 254 } |
255 | 255 |
256 default: | 256 default: |
257 NOTREACHED(); | 257 NOTREACHED(); |
258 } | 258 } |
259 } | 259 } |
260 | 260 |
261 void WebstoreInstaller::SetDownloadDirectoryForTests(FilePath* directory) { | 261 void WebstoreInstaller::SetDownloadDirectoryForTests(FilePath* directory) { |
262 g_download_directory_for_tests = directory; | 262 g_download_directory_for_tests = directory; |
263 } | 263 } |
264 | 264 |
265 WebstoreInstaller::~WebstoreInstaller() { | 265 WebstoreInstaller::~WebstoreInstaller() { |
266 if (download_item_) { | 266 if (download_item_) { |
267 download_item_->RemoveObserver(this); | 267 download_item_->RemoveObserver(this); |
268 download_item_ = NULL; | 268 download_item_ = NULL; |
269 } | 269 } |
270 } | 270 } |
271 | 271 |
272 void WebstoreInstaller::OnDownloadStarted(DownloadId id, net::Error error) { | 272 void WebstoreInstaller::OnDownloadStarted(DownloadId id, net::Error error) { |
273 if (error != net::OK) { | 273 if (error != net::OK) { |
274 ReportFailure(net::ErrorToString(error)); | 274 ReportFailure(net::ErrorToString(error), false); |
275 return; | 275 return; |
276 } | 276 } |
277 | 277 |
278 CHECK(id.IsValid()); | 278 CHECK(id.IsValid()); |
279 | 279 |
280 DownloadManager* download_manager = | 280 DownloadManager* download_manager = |
281 BrowserContext::GetDownloadManager(profile_); | 281 BrowserContext::GetDownloadManager(profile_); |
282 if (!download_manager) | 282 if (!download_manager) |
283 return; | 283 return; |
284 download_item_ = download_manager->GetDownload(id.local()); | 284 download_item_ = download_manager->GetDownload(id.local()); |
285 if (download_item_) { | 285 if (download_item_) { |
286 download_item_->AddObserver(this); | 286 download_item_->AddObserver(this); |
287 if (approval_.get()) | 287 if (approval_.get()) |
288 download_item_->SetUserData(kApprovalKey, approval_.release()); | 288 download_item_->SetUserData(kApprovalKey, approval_.release()); |
289 if (delegate_) | |
290 delegate_->OnExtensionDownloadStarted(id_, download_item_); | |
289 } | 291 } |
290 } | 292 } |
291 | 293 |
292 void WebstoreInstaller::OnDownloadUpdated(DownloadItem* download) { | 294 void WebstoreInstaller::OnDownloadUpdated(DownloadItem* download) { |
293 CHECK_EQ(download_item_, download); | 295 CHECK_EQ(download_item_, download); |
294 | 296 |
295 switch (download->GetState()) { | 297 switch (download->GetState()) { |
296 case DownloadItem::CANCELLED: | 298 case DownloadItem::CANCELLED: |
297 ReportFailure(kDownloadCanceledError); | 299 ReportFailure(kDownloadCanceledError, true); |
298 break; | 300 break; |
299 case DownloadItem::INTERRUPTED: | 301 case DownloadItem::INTERRUPTED: |
300 ReportFailure(kDownloadInterruptedError); | 302 ReportFailure(kDownloadInterruptedError, false); |
301 break; | 303 break; |
302 case DownloadItem::COMPLETE: | 304 case DownloadItem::COMPLETE: |
303 // Wait for other notifications if the download is really an extension. | 305 // Wait for other notifications if the download is really an extension. |
304 if (!download_crx_util::IsExtensionDownload(*download)) | 306 if (!download_crx_util::IsExtensionDownload(*download)) |
305 ReportFailure(kInvalidDownloadError); | 307 ReportFailure(kInvalidDownloadError, false); |
308 else if (delegate_) | |
309 delegate_->OnExtensionDownloadProgress(id_, download); | |
310 break; | |
311 case DownloadItem::IN_PROGRESS: | |
312 if (delegate_) | |
313 delegate_->OnExtensionDownloadProgress(id_, download); | |
306 break; | 314 break; |
307 default: | 315 default: |
308 // Continue listening if the download is not in one of the above states. | 316 // Continue listening if the download is not in one of the above states. |
309 break; | 317 break; |
310 } | 318 } |
311 } | 319 } |
312 | 320 |
313 void WebstoreInstaller::OnDownloadDestroyed(DownloadItem* download) { | 321 void WebstoreInstaller::OnDownloadDestroyed(DownloadItem* download) { |
314 CHECK_EQ(download_item_, download); | 322 CHECK_EQ(download_item_, download); |
315 download_item_->RemoveObserver(this); | 323 download_item_->RemoveObserver(this); |
316 download_item_ = NULL; | 324 download_item_ = NULL; |
317 } | 325 } |
318 | 326 |
319 void WebstoreInstaller::StartDownload(const FilePath& file) { | 327 void WebstoreInstaller::StartDownload(const FilePath& file) { |
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
321 | 329 |
322 DownloadManager* download_manager = | 330 DownloadManager* download_manager = |
323 BrowserContext::GetDownloadManager(profile_); | 331 BrowserContext::GetDownloadManager(profile_); |
324 if (file.empty() || | 332 if (file.empty() || |
325 !download_manager || | 333 !download_manager || |
326 !controller_->GetWebContents() || | 334 !controller_->GetWebContents() || |
327 !controller_->GetWebContents()->GetRenderProcessHost() || | 335 !controller_->GetWebContents()->GetRenderProcessHost() || |
328 !controller_->GetWebContents()->GetRenderViewHost() || | 336 !controller_->GetWebContents()->GetRenderViewHost() || |
329 !controller_->GetWebContents()->GetBrowserContext() || | 337 !controller_->GetWebContents()->GetBrowserContext() || |
330 !controller_->GetWebContents()->GetBrowserContext() | 338 !controller_->GetWebContents()->GetBrowserContext() |
331 ->GetResourceContext()) { | 339 ->GetResourceContext()) { |
332 ReportFailure(kDownloadDirectoryError); | 340 ReportFailure(kDownloadDirectoryError, false); |
333 return; | 341 return; |
334 } | 342 } |
335 | 343 |
336 content::DownloadSaveInfo save_info; | 344 content::DownloadSaveInfo save_info; |
337 save_info.file_path = file; | 345 save_info.file_path = file; |
338 | 346 |
339 // The download url for the given extension is contained in |download_url_|. | 347 // The download url for the given extension is contained in |download_url_|. |
340 // We will navigate the current tab to this url to start the download. The | 348 // We will navigate the current tab to this url to start the download. The |
341 // download system will then pass the crx to the CrxInstaller. | 349 // download system will then pass the crx to the CrxInstaller. |
342 download_util::RecordDownloadSource( | 350 download_util::RecordDownloadSource( |
343 download_util::INITIATED_BY_WEBSTORE_INSTALLER); | 351 download_util::INITIATED_BY_WEBSTORE_INSTALLER); |
344 scoped_ptr<DownloadUrlParameters> params( | 352 scoped_ptr<DownloadUrlParameters> params( |
345 DownloadUrlParameters::FromWebContents( | 353 DownloadUrlParameters::FromWebContents( |
346 controller_->GetWebContents(), download_url_, save_info)); | 354 controller_->GetWebContents(), download_url_, save_info)); |
347 if (controller_->GetActiveEntry()) | 355 if (controller_->GetActiveEntry()) |
348 params->set_referrer( | 356 params->set_referrer( |
349 content::Referrer(controller_->GetActiveEntry()->GetURL(), | 357 content::Referrer(controller_->GetActiveEntry()->GetURL(), |
350 WebKit::WebReferrerPolicyDefault)); | 358 WebKit::WebReferrerPolicyDefault)); |
351 params->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted, this)); | 359 params->set_callback(base::Bind(&WebstoreInstaller::OnDownloadStarted, this)); |
352 download_manager->DownloadUrl(params.Pass()); | 360 download_manager->DownloadUrl(params.Pass()); |
353 } | 361 } |
354 | 362 |
355 void WebstoreInstaller::ReportFailure(const std::string& error) { | 363 void WebstoreInstaller::ReportFailure(const std::string& error, |
364 bool cancelled) { | |
356 if (delegate_) { | 365 if (delegate_) { |
357 delegate_->OnExtensionInstallFailure(id_, error); | 366 delegate_->OnExtensionInstallFailure(id_, error, cancelled); |
Nico
2012/09/25 03:07:18
nit: It looks like cancelled is true iff error ==
sail
2012/10/02 18:57:01
I changed this to take an enum.
error is a string
| |
358 delegate_ = NULL; | 367 delegate_ = NULL; |
359 } | 368 } |
360 | 369 |
361 Release(); // Balanced in Start(). | 370 Release(); // Balanced in Start(). |
362 } | 371 } |
363 | 372 |
364 void WebstoreInstaller::ReportSuccess() { | 373 void WebstoreInstaller::ReportSuccess() { |
365 if (delegate_) { | 374 if (delegate_) { |
366 delegate_->OnExtensionInstallSuccess(id_); | 375 delegate_->OnExtensionInstallSuccess(id_); |
367 delegate_ = NULL; | 376 delegate_ = NULL; |
368 } | 377 } |
369 | 378 |
370 Release(); // Balanced in Start(). | 379 Release(); // Balanced in Start(). |
371 } | 380 } |
372 | 381 |
373 } // namespace extensions | 382 } // namespace extensions |
OLD | NEW |