| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/webui/chromeos/imageburner/imageburner_utils.h" | 5 #include "chrome/browser/ui/webui/chromeos/imageburner/imageburner_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/task.h" | 10 #include "base/task.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/chrome_paths.h" | 12 #include "chrome/common/chrome_paths.h" |
| 13 #include "content/browser/download/download_types.h" | 13 #include "content/browser/download/download_types.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 | 15 |
| 16 using content::BrowserThread; | 16 using content::BrowserThread; |
| 17 using content::DownloadItem; | 17 using content::DownloadItem; |
| 18 using content::DownloadManager; | 18 using content::DownloadManager; |
| 19 using content::WebContents; |
| 19 | 20 |
| 20 namespace imageburner { | 21 namespace imageburner { |
| 21 | 22 |
| 22 const char kName[] = "name"; | 23 const char kName[] = "name"; |
| 23 const char kHwid[] = "hwid"; | 24 const char kHwid[] = "hwid"; |
| 24 const char kFileName[] = "file"; | 25 const char kFileName[] = "file"; |
| 25 const char kUrl[] = "url"; | 26 const char kUrl[] = "url"; |
| 26 | 27 |
| 27 const char kConfigFileUrl[] = | 28 const char kConfigFileUrl[] = |
| 28 "https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf"; | 29 "https://dl.google.com/dl/edgedl/chromeos/recovery/recovery.conf"; |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 image_dir_ = image_dir_.Append(kTempImageFolderName); | 294 image_dir_ = image_dir_.Append(kTempImageFolderName); |
| 294 success = file_util::CreateDirectory(image_dir_); | 295 success = file_util::CreateDirectory(image_dir_); |
| 295 } | 296 } |
| 296 delegate->OnImageDirCreated(success); | 297 delegate->OnImageDirCreated(success); |
| 297 } | 298 } |
| 298 | 299 |
| 299 const FilePath& BurnManager::GetImageDir() { | 300 const FilePath& BurnManager::GetImageDir() { |
| 300 return image_dir_; | 301 return image_dir_; |
| 301 } | 302 } |
| 302 | 303 |
| 303 void BurnManager::FetchConfigFile(TabContents* tab_contents, | 304 void BurnManager::FetchConfigFile(WebContents* web_contents, |
| 304 Delegate* delegate) { | 305 Delegate* delegate) { |
| 305 if (config_file_fetched_) { | 306 if (config_file_fetched_) { |
| 306 delegate->OnConfigFileFetched(config_file_, true); | 307 delegate->OnConfigFileFetched(config_file_, true); |
| 307 return; | 308 return; |
| 308 } | 309 } |
| 309 downloaders_.push_back(delegate->AsWeakPtr()); | 310 downloaders_.push_back(delegate->AsWeakPtr()); |
| 310 | 311 |
| 311 if (config_file_requested_) | 312 if (config_file_requested_) |
| 312 return; | 313 return; |
| 313 config_file_requested_ = true; | 314 config_file_requested_ = true; |
| 314 | 315 |
| 315 config_file_path_ = GetImageDir().Append(kConfigFileName); | 316 config_file_path_ = GetImageDir().Append(kConfigFileName); |
| 316 download_manager_ = tab_contents->GetBrowserContext()->GetDownloadManager(); | 317 download_manager_ = web_contents->GetBrowserContext()->GetDownloadManager(); |
| 317 download_manager_->AddObserver(this); | 318 download_manager_->AddObserver(this); |
| 318 downloader()->AddListener(this, config_file_url_); | 319 downloader()->AddListener(this, config_file_url_); |
| 319 downloader()->DownloadFile(config_file_url_, config_file_path_, tab_contents); | 320 downloader()->DownloadFile(config_file_url_, config_file_path_, web_contents); |
| 320 } | 321 } |
| 321 | 322 |
| 322 void BurnManager::ConfigFileFetchedOnUIThread(bool fetched, | 323 void BurnManager::ConfigFileFetchedOnUIThread(bool fetched, |
| 323 const std::string& content) { | 324 const std::string& content) { |
| 324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 325 | 326 |
| 326 if (config_file_fetched_) | 327 if (config_file_fetched_) |
| 327 return; | 328 return; |
| 328 | 329 |
| 329 if (active_download_item_) { | 330 if (active_download_item_) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 356 // | 357 // |
| 357 //////////////////////////////////////////////////////////////////////////////// | 358 //////////////////////////////////////////////////////////////////////////////// |
| 358 | 359 |
| 359 class DownloaderTaskProxy | 360 class DownloaderTaskProxy |
| 360 : public base::RefCountedThreadSafe<DownloaderTaskProxy> { | 361 : public base::RefCountedThreadSafe<DownloaderTaskProxy> { |
| 361 public: | 362 public: |
| 362 explicit DownloaderTaskProxy() {} | 363 explicit DownloaderTaskProxy() {} |
| 363 | 364 |
| 364 void CreateFileStream(const GURL& url, | 365 void CreateFileStream(const GURL& url, |
| 365 const FilePath& target_path, | 366 const FilePath& target_path, |
| 366 TabContents* tab_contents) { | 367 WebContents* web_contents) { |
| 367 BurnManager::GetInstance()->downloader()-> | 368 BurnManager::GetInstance()->downloader()-> |
| 368 CreateFileStreamOnFileThread(url, target_path, tab_contents); | 369 CreateFileStreamOnFileThread(url, target_path, web_contents); |
| 369 } | 370 } |
| 370 | 371 |
| 371 void OnFileStreamCreated(const GURL& url, | 372 void OnFileStreamCreated(const GURL& url, |
| 372 const FilePath& file_path, | 373 const FilePath& file_path, |
| 373 TabContents* tab_contents, | 374 WebContents* web_contents, |
| 374 net::FileStream* created_file_stream) { | 375 net::FileStream* created_file_stream) { |
| 375 BurnManager::GetInstance()->downloader()-> | 376 BurnManager::GetInstance()->downloader()-> |
| 376 OnFileStreamCreatedOnUIThread(url, file_path, tab_contents, | 377 OnFileStreamCreatedOnUIThread(url, file_path, web_contents, |
| 377 created_file_stream); | 378 created_file_stream); |
| 378 } | 379 } |
| 379 | 380 |
| 380 private: | 381 private: |
| 381 ~DownloaderTaskProxy() {} | 382 ~DownloaderTaskProxy() {} |
| 382 | 383 |
| 383 friend class base::RefCountedThreadSafe<DownloaderTaskProxy>; | 384 friend class base::RefCountedThreadSafe<DownloaderTaskProxy>; |
| 384 | 385 |
| 385 DISALLOW_COPY_AND_ASSIGN(DownloaderTaskProxy); | 386 DISALLOW_COPY_AND_ASSIGN(DownloaderTaskProxy); |
| 386 }; | 387 }; |
| 387 | 388 |
| 388 //////////////////////////////////////////////////////////////////////////////// | 389 //////////////////////////////////////////////////////////////////////////////// |
| 389 // | 390 // |
| 390 // Downloader | 391 // Downloader |
| 391 // | 392 // |
| 392 //////////////////////////////////////////////////////////////////////////////// | 393 //////////////////////////////////////////////////////////////////////////////// |
| 393 | 394 |
| 394 Downloader::Downloader() {} | 395 Downloader::Downloader() {} |
| 395 | 396 |
| 396 Downloader::~Downloader() {} | 397 Downloader::~Downloader() {} |
| 397 | 398 |
| 398 void Downloader::DownloadFile(const GURL& url, | 399 void Downloader::DownloadFile(const GURL& url, |
| 399 const FilePath& file_path, TabContents* tab_contents) { | 400 const FilePath& file_path, WebContents* web_contents) { |
| 400 // First we have to create file stream we will download file to. | 401 // First we have to create file stream we will download file to. |
| 401 // That has to be done on File thread. | 402 // That has to be done on File thread. |
| 402 scoped_refptr<DownloaderTaskProxy> task = new DownloaderTaskProxy(); | 403 scoped_refptr<DownloaderTaskProxy> task = new DownloaderTaskProxy(); |
| 403 BrowserThread::PostTask( | 404 BrowserThread::PostTask( |
| 404 BrowserThread::FILE, FROM_HERE, | 405 BrowserThread::FILE, FROM_HERE, |
| 405 base::Bind(&DownloaderTaskProxy::CreateFileStream, task.get(), url, | 406 base::Bind(&DownloaderTaskProxy::CreateFileStream, task.get(), url, |
| 406 file_path, tab_contents)); | 407 file_path, web_contents)); |
| 407 } | 408 } |
| 408 | 409 |
| 409 void Downloader::CreateFileStreamOnFileThread( | 410 void Downloader::CreateFileStreamOnFileThread( |
| 410 const GURL& url, const FilePath& file_path, | 411 const GURL& url, const FilePath& file_path, |
| 411 TabContents* tab_contents) { | 412 WebContents* web_contents) { |
| 412 | 413 |
| 413 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 414 DCHECK(!file_path.empty()); | 415 DCHECK(!file_path.empty()); |
| 415 | 416 |
| 416 scoped_ptr<net::FileStream> file_stream(new net::FileStream); | 417 scoped_ptr<net::FileStream> file_stream(new net::FileStream); |
| 417 // TODO(tbarzic): Save temp image file to temp folder instead of Downloads | 418 // TODO(tbarzic): Save temp image file to temp folder instead of Downloads |
| 418 // once extracting image directly to removalbe device is implemented | 419 // once extracting image directly to removalbe device is implemented |
| 419 if (file_stream->Open(file_path, base::PLATFORM_FILE_OPEN_ALWAYS | | 420 if (file_stream->Open(file_path, base::PLATFORM_FILE_OPEN_ALWAYS | |
| 420 base::PLATFORM_FILE_WRITE)) | 421 base::PLATFORM_FILE_WRITE)) |
| 421 file_stream.reset(NULL); | 422 file_stream.reset(NULL); |
| 422 | 423 |
| 423 scoped_refptr<DownloaderTaskProxy> task = new DownloaderTaskProxy(); | 424 scoped_refptr<DownloaderTaskProxy> task = new DownloaderTaskProxy(); |
| 424 // Call callback method on UI thread. | 425 // Call callback method on UI thread. |
| 425 BrowserThread::PostTask( | 426 BrowserThread::PostTask( |
| 426 BrowserThread::UI, FROM_HERE, | 427 BrowserThread::UI, FROM_HERE, |
| 427 base::Bind(&DownloaderTaskProxy::OnFileStreamCreated, task.get(), | 428 base::Bind(&DownloaderTaskProxy::OnFileStreamCreated, task.get(), |
| 428 url, file_path, tab_contents, file_stream.release())); | 429 url, file_path, web_contents, file_stream.release())); |
| 429 } | 430 } |
| 430 | 431 |
| 431 void Downloader::OnFileStreamCreatedOnUIThread(const GURL& url, | 432 void Downloader::OnFileStreamCreatedOnUIThread(const GURL& url, |
| 432 const FilePath& file_path, TabContents* tab_contents, | 433 const FilePath& file_path, WebContents* web_contents, |
| 433 net::FileStream* created_file_stream) { | 434 net::FileStream* created_file_stream) { |
| 434 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 435 | 436 |
| 436 if (created_file_stream) { | 437 if (created_file_stream) { |
| 437 DownloadManager* download_manager = | 438 DownloadManager* download_manager = |
| 438 tab_contents->GetBrowserContext()->GetDownloadManager(); | 439 web_contents->GetBrowserContext()->GetDownloadManager(); |
| 439 DownloadSaveInfo save_info; | 440 DownloadSaveInfo save_info; |
| 440 save_info.file_path = file_path; | 441 save_info.file_path = file_path; |
| 441 save_info.file_stream = linked_ptr<net::FileStream>(created_file_stream); | 442 save_info.file_stream = linked_ptr<net::FileStream>(created_file_stream); |
| 442 DownloadStarted(true, url); | 443 DownloadStarted(true, url); |
| 443 download_manager->DownloadUrlToFile(url, | 444 download_manager->DownloadUrlToFile( |
| 444 tab_contents->GetURL(), | 445 url, |
| 445 tab_contents->GetEncoding(), | 446 web_contents->GetURL(), |
| 446 save_info, | 447 web_contents->GetEncoding(), |
| 447 tab_contents); | 448 save_info, |
| 449 static_cast<TabContents*>(web_contents)); |
| 448 } else { | 450 } else { |
| 449 DownloadStarted(false, url); | 451 DownloadStarted(false, url); |
| 450 } | 452 } |
| 451 } | 453 } |
| 452 | 454 |
| 453 void Downloader::AddListener(Listener* listener, const GURL& url) { | 455 void Downloader::AddListener(Listener* listener, const GURL& url) { |
| 454 listeners_.insert(std::make_pair(url, listener->AsWeakPtr())); | 456 listeners_.insert(std::make_pair(url, listener->AsWeakPtr())); |
| 455 } | 457 } |
| 456 | 458 |
| 457 void Downloader::DownloadStarted(bool success, const GURL& url) { | 459 void Downloader::DownloadStarted(bool success, const GURL& url) { |
| 458 std::pair<ListenerMap::iterator, ListenerMap::iterator> listener_range = | 460 std::pair<ListenerMap::iterator, ListenerMap::iterator> listener_range = |
| 459 listeners_.equal_range(url); | 461 listeners_.equal_range(url); |
| 460 for (ListenerMap::iterator current_listener = listener_range.first; | 462 for (ListenerMap::iterator current_listener = listener_range.first; |
| 461 current_listener != listener_range.second; | 463 current_listener != listener_range.second; |
| 462 ++current_listener) { | 464 ++current_listener) { |
| 463 if (current_listener->second) | 465 if (current_listener->second) |
| 464 current_listener->second->OnBurnDownloadStarted(success); | 466 current_listener->second->OnBurnDownloadStarted(success); |
| 465 } | 467 } |
| 466 listeners_.erase(listener_range.first, listener_range.second); | 468 listeners_.erase(listener_range.first, listener_range.second); |
| 467 } | 469 } |
| 468 | 470 |
| 469 } // namespace imageburner | 471 } // namespace imageburner |
| OLD | NEW |