Chromium Code Reviews| Index: chrome/browser/download/save_package_file_picker_chromeos.cc |
| diff --git a/chrome/browser/download/save_package_file_picker_chromeos.cc b/chrome/browser/download/save_package_file_picker_chromeos.cc |
| index 5a5c2130c1154f430dbb79eeb86ad8023d859bb9..c7ad395feb7ec6872f130b14f7a8c202d2d6f49e 100644 |
| --- a/chrome/browser/download/save_package_file_picker_chromeos.cc |
| +++ b/chrome/browser/download/save_package_file_picker_chromeos.cc |
| @@ -13,117 +13,110 @@ |
| #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
| #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| #include "chrome/browser/platform_util.h" |
| -#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/download_item.h" |
| #include "content/public/browser/web_contents.h" |
| -using content::BrowserThread; |
| - |
| -SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( |
| - content::WebContents* web_contents, |
| - const FilePath& suggested_path) |
| - : content::WebContentsObserver(web_contents) { |
| - select_file_dialog_ = SelectFileDialog::Create(this); |
| - select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
| - string16(), |
| - suggested_path.ReplaceExtension("mhtml"), |
| - NULL, |
| - 0, |
| - "mhtml", |
| - web_contents, |
| - platform_util::GetTopLevel( |
| - web_contents->GetNativeView()), |
| - NULL); |
| +namespace { |
| + |
| +// If false, we don't prompt the user as to where to save the file. This |
| +// exists only for testing. |
| +bool g_should_prompt_for_filename = true; |
| + |
| +// This method is passed as a callback to SavePackage, which calls it when the |
| +// DownloadItem is created. SavePackage is in content, so it cannot access gdata |
| +// concepts. SetGDataPath must be called after the DownloadItem is created. |
| +void OnSavePackageDownloadCreated( |
| + const FilePath& gdata_path, |
| + content::DownloadItem* download) { |
| + gdata::GDataDownloadObserver::SetGDataPath(download, gdata_path); |
| + download->SetDisplayName(gdata_path.BaseName()); |
| + download->SetIsTemporary(true); |
| } |
| -SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { |
| +// Trampoline callback between GetGDataTempDownloadPath() and |callback|. |
| +void ContinueSettingUpGDataDownload( |
| + const content::SaveFilePathPickedCallback& callback, |
| + FilePath* gdata_tmp_download_path, |
| + const FilePath& gdata_path) { |
| + callback.Run(*gdata_tmp_download_path, content::SAVE_PAGE_TYPE_AS_MHTML, |
| + base::Bind(&OnSavePackageDownloadCreated, gdata_path)); |
| } |
| -void SavePackageFilePickerChromeOS::FileSelected(const FilePath& selected_path, |
| - int index, |
| - void* params) { |
| - if (!web_contents()) { |
| - delete this; |
| - return; |
| - } |
| - |
| - FilePath path = selected_path; |
| - file_util::NormalizeFileNameEncoding(&path); |
| +} // anonymous namespace |
| - gdata::GDataFileSystem* gdata_filesystem = GetGDataFileSystem(); |
| - if (gdata_filesystem && gdata::util::IsUnderGDataMountPoint(path)) { |
| - FilePath gdata_tmp_download_dir = |
| - gdata_filesystem->GetCacheDirectoryPath( |
| - gdata::GDataRootDirectory::CACHE_TYPE_TMP_DOWNLOADS); |
| - |
| - selected_path_ = path; |
| - FilePath* gdata_tmp_download_path = new FilePath(); |
| - BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
| - base::Bind(&gdata::GDataDownloadObserver::GetGDataTempDownloadPath, |
| - gdata_tmp_download_dir, |
| - gdata_tmp_download_path), |
| - base::Bind(&SavePackageFilePickerChromeOS::GenerateMHTML, |
| - base::Unretained(this), |
| - base::Owned(gdata_tmp_download_path))); |
| +SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( |
| + content::WebContents* web_contents, |
| + const FilePath& suggested_path, |
| + const content::SaveFilePathPickedCallback& callback) |
| + : content::WebContentsObserver(web_contents), |
| + callback_(callback) { |
| + if (g_should_prompt_for_filename) { |
| + select_file_dialog_ = SelectFileDialog::Create(this); |
| + select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
| + string16(), |
| + suggested_path.ReplaceExtension("mhtml"), |
| + NULL, |
| + 0, |
| + "mhtml", |
| + web_contents, |
| + platform_util::GetTopLevel( |
| + web_contents->GetNativeView()), |
| + NULL); |
| } else { |
| - DVLOG(1) << "SavePackageFilePickerChromeOS non-gdata file"; |
| - GenerateMHTML(&path); |
| + FileSelected(suggested_path.ReplaceExtension("mhtml"), 0, NULL); |
| } |
| } |
| -void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) { |
| - delete this; |
| +void SavePackageFilePickerChromeOS::SetShouldPromptUser(bool should_prompt) { |
| + g_should_prompt_for_filename = should_prompt; |
| } |
| -void SavePackageFilePickerChromeOS::GenerateMHTML(const FilePath* mhtml_path) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - if (!web_contents()) { |
| - delete this; |
| - return; |
| - } |
| - |
| - DVLOG(1) << "GenerateMHTML " << mhtml_path->value(); |
| - web_contents()->GenerateMHTML(*mhtml_path, |
| - base::Bind(&SavePackageFilePickerChromeOS::OnMHTMLGenerated, |
| - base::Unretained(this))); |
| +SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { |
| } |
| -void SavePackageFilePickerChromeOS::OnMHTMLGenerated(const FilePath& src_path, |
| - int64 file_size) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| +void SavePackageFilePickerChromeOS::FileSelected( |
| + const FilePath& selected_path_const, |
| + int unused_index, |
| + void* unused_params) { |
| if (!web_contents()) { |
| delete this; |
| return; |
| } |
| - |
| - gdata::GDataFileSystem* gdata_filesystem = GetGDataFileSystem(); |
| - if (gdata_filesystem && !selected_path_.empty()) { |
| - DVLOG(1) << "TransferFile from " << src_path.value() |
| - << " to " << selected_path_.value(); |
| - gdata_filesystem->TransferFile(src_path, |
| - gdata::util::ExtractGDataPath(selected_path_), |
| - base::Bind(&SavePackageFilePickerChromeOS::OnTransferFile, |
| - base::Unretained(this))); |
| - } else { |
| - delete this; |
| - } |
| -} |
| - |
| -void SavePackageFilePickerChromeOS::OnTransferFile( |
| - base::PlatformFileError error) { |
| - DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - DCHECK_EQ(error, base::PLATFORM_FILE_OK); |
| - delete this; |
| -} |
| - |
| -gdata::GDataFileSystem* |
| -SavePackageFilePickerChromeOS::GetGDataFileSystem() { |
| - DCHECK(web_contents()); |
| + FilePath selected_path = selected_path_const; |
| + file_util::NormalizeFileNameEncoding(&selected_path); |
| Profile* profile = Profile::FromBrowserContext( |
| web_contents()->GetBrowserContext()); |
| DCHECK(profile); |
| gdata::GDataSystemService* system_service = |
| gdata::GDataSystemServiceFactory::GetForProfile(profile); |
| // system_service is NULL in incognito. |
| - return system_service ? system_service->file_system() : NULL; |
| + if (system_service && gdata::util::IsUnderGDataMountPoint(selected_path)) { |
| + // Here's a map to the callback chain: |
| + // GetGDataTempDownloadPath -> |
| + // ContinueSettingUpGDataDownload -> |
| + // callback_ = SavePackage::OnPathPicked -> |
| + // download_created_callback = OnSavePackageDownloadCreated |
| + FilePath gdata_tmp_download_dir = |
| + system_service->file_system()->GetCacheDirectoryPath( |
| + gdata::GDataRootDirectory::CACHE_TYPE_TMP_DOWNLOADS); |
| + FilePath* gdata_tmp_download_path(new FilePath()); |
| + content::BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
| + base::Bind(&gdata::GDataDownloadObserver::GetGDataTempDownloadPath, |
| + gdata_tmp_download_dir, |
| + gdata_tmp_download_path), |
| + base::Bind(&ContinueSettingUpGDataDownload, |
|
Randy Smith (Not in Mondays)
2012/04/27 18:24:04
What do you get by doing the trampoline rather tha
benjhayden
2012/04/28 19:09:03
The clarity of purpose.
The flexibility to do some
|
| + callback_, |
| + base::Owned(gdata_tmp_download_path), |
| + selected_path)); |
| + } else { |
| + callback_.Run(selected_path, content::SAVE_PAGE_TYPE_AS_MHTML, |
| + content::SaveFileDownloadCreatedCallback()); |
| + } |
| + delete this; |
| +} |
| + |
| +void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) { |
| + delete this; |
| } |