Chromium Code Reviews| 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/download/save_package_file_picker_chromeos.h" | 5 #include "chrome/browser/download/save_package_file_picker_chromeos.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/i18n/file_util_icu.h" | 9 #include "base/i18n/file_util_icu.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | 10 #include "base/threading/sequenced_worker_pool.h" |
| 11 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | 11 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" |
| 12 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" | 12 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
| 13 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | 13 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
| 14 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 14 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 15 #include "chrome/browser/platform_util.h" | 15 #include "chrome/browser/platform_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile_manager.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/download_item.h" | |
| 18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
| 19 | 20 |
| 20 using content::BrowserThread; | 21 namespace { |
| 22 | |
| 23 // If false, we don't prompt the user as to where to save the file. This | |
| 24 // exists only for testing. | |
| 25 bool g_should_prompt_for_filename = true; | |
| 26 | |
| 27 // This method is passed as a callback to SavePackage, which calls it when the | |
| 28 // DownloadItem is created. SavePackage is in content, so it cannot access gdata | |
| 29 // concepts. SetGDataPath must be called after the DownloadItem is created. | |
| 30 void OnSavePackageDownloadCreated( | |
| 31 const FilePath& gdata_path, | |
| 32 content::DownloadItem* download) { | |
| 33 gdata::GDataDownloadObserver::SetGDataPath(download, gdata_path); | |
| 34 download->SetDisplayName(gdata_path.BaseName()); | |
| 35 download->SetIsTemporary(true); | |
| 36 } | |
| 37 | |
| 38 // Trampoline callback between GetGDataTempDownloadPath() and |callback|. | |
| 39 void ContinueSettingUpGDataDownload( | |
| 40 const content::SaveFilePathPickedCallback& callback, | |
| 41 FilePath* gdata_tmp_download_path, | |
| 42 const FilePath& gdata_path) { | |
| 43 callback.Run(*gdata_tmp_download_path, content::SAVE_PAGE_TYPE_AS_MHTML, | |
| 44 base::Bind(&OnSavePackageDownloadCreated, gdata_path)); | |
| 45 } | |
| 46 | |
| 47 } // anonymous namespace | |
| 21 | 48 |
| 22 SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( | 49 SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( |
| 23 content::WebContents* web_contents, | 50 content::WebContents* web_contents, |
| 24 const FilePath& suggested_path) | 51 const FilePath& suggested_path, |
| 25 : content::WebContentsObserver(web_contents) { | 52 const content::SaveFilePathPickedCallback& callback) |
| 26 select_file_dialog_ = SelectFileDialog::Create(this); | 53 : content::WebContentsObserver(web_contents), |
| 27 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 54 callback_(callback) { |
| 28 string16(), | 55 if (g_should_prompt_for_filename) { |
| 29 suggested_path.ReplaceExtension("mhtml"), | 56 select_file_dialog_ = SelectFileDialog::Create(this); |
| 30 NULL, | 57 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
| 31 0, | 58 string16(), |
| 32 "mhtml", | 59 suggested_path.ReplaceExtension("mhtml"), |
| 33 web_contents, | 60 NULL, |
| 34 platform_util::GetTopLevel( | 61 0, |
| 35 web_contents->GetNativeView()), | 62 "mhtml", |
| 36 NULL); | 63 web_contents, |
| 64 platform_util::GetTopLevel( | |
| 65 web_contents->GetNativeView()), | |
| 66 NULL); | |
| 67 } else { | |
| 68 FileSelected(suggested_path.ReplaceExtension("mhtml"), 0, NULL); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void SavePackageFilePickerChromeOS::SetShouldPromptUser(bool should_prompt) { | |
| 73 g_should_prompt_for_filename = should_prompt; | |
| 37 } | 74 } |
| 38 | 75 |
| 39 SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { | 76 SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { |
| 40 } | 77 } |
| 41 | 78 |
| 42 void SavePackageFilePickerChromeOS::FileSelected(const FilePath& selected_path, | 79 void SavePackageFilePickerChromeOS::FileSelected( |
| 43 int index, | 80 const FilePath& selected_path_const, |
| 44 void* params) { | 81 int unused_index, |
| 82 void* unused_params) { | |
| 45 if (!web_contents()) { | 83 if (!web_contents()) { |
| 46 delete this; | 84 delete this; |
| 47 return; | 85 return; |
| 48 } | 86 } |
| 49 | 87 FilePath selected_path = selected_path_const; |
| 50 FilePath path = selected_path; | 88 file_util::NormalizeFileNameEncoding(&selected_path); |
| 51 file_util::NormalizeFileNameEncoding(&path); | |
| 52 | |
| 53 gdata::GDataFileSystem* gdata_filesystem = GetGDataFileSystem(); | |
| 54 if (gdata_filesystem && gdata::util::IsUnderGDataMountPoint(path)) { | |
| 55 FilePath gdata_tmp_download_dir = | |
| 56 gdata_filesystem->GetCacheDirectoryPath( | |
| 57 gdata::GDataRootDirectory::CACHE_TYPE_TMP_DOWNLOADS); | |
| 58 | |
| 59 selected_path_ = path; | |
| 60 FilePath* gdata_tmp_download_path = new FilePath(); | |
| 61 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, | |
| 62 base::Bind(&gdata::GDataDownloadObserver::GetGDataTempDownloadPath, | |
| 63 gdata_tmp_download_dir, | |
| 64 gdata_tmp_download_path), | |
| 65 base::Bind(&SavePackageFilePickerChromeOS::GenerateMHTML, | |
| 66 base::Unretained(this), | |
| 67 base::Owned(gdata_tmp_download_path))); | |
| 68 } else { | |
| 69 DVLOG(1) << "SavePackageFilePickerChromeOS non-gdata file"; | |
| 70 GenerateMHTML(&path); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) { | |
| 75 delete this; | |
| 76 } | |
| 77 | |
| 78 void SavePackageFilePickerChromeOS::GenerateMHTML(const FilePath* mhtml_path) { | |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 80 if (!web_contents()) { | |
| 81 delete this; | |
| 82 return; | |
| 83 } | |
| 84 | |
| 85 DVLOG(1) << "GenerateMHTML " << mhtml_path->value(); | |
| 86 web_contents()->GenerateMHTML(*mhtml_path, | |
| 87 base::Bind(&SavePackageFilePickerChromeOS::OnMHTMLGenerated, | |
| 88 base::Unretained(this))); | |
| 89 } | |
| 90 | |
| 91 void SavePackageFilePickerChromeOS::OnMHTMLGenerated(const FilePath& src_path, | |
| 92 int64 file_size) { | |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 94 if (!web_contents()) { | |
| 95 delete this; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 gdata::GDataFileSystem* gdata_filesystem = GetGDataFileSystem(); | |
| 100 if (gdata_filesystem && !selected_path_.empty()) { | |
| 101 DVLOG(1) << "TransferFile from " << src_path.value() | |
| 102 << " to " << selected_path_.value(); | |
| 103 gdata_filesystem->TransferFile(src_path, | |
| 104 gdata::util::ExtractGDataPath(selected_path_), | |
| 105 base::Bind(&SavePackageFilePickerChromeOS::OnTransferFile, | |
| 106 base::Unretained(this))); | |
| 107 } else { | |
| 108 delete this; | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void SavePackageFilePickerChromeOS::OnTransferFile( | |
| 113 base::PlatformFileError error) { | |
| 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 115 DCHECK_EQ(error, base::PLATFORM_FILE_OK); | |
| 116 delete this; | |
| 117 } | |
| 118 | |
| 119 gdata::GDataFileSystem* | |
| 120 SavePackageFilePickerChromeOS::GetGDataFileSystem() { | |
| 121 DCHECK(web_contents()); | |
| 122 Profile* profile = Profile::FromBrowserContext( | 89 Profile* profile = Profile::FromBrowserContext( |
| 123 web_contents()->GetBrowserContext()); | 90 web_contents()->GetBrowserContext()); |
| 124 DCHECK(profile); | 91 DCHECK(profile); |
| 125 gdata::GDataSystemService* system_service = | 92 gdata::GDataSystemService* system_service = |
| 126 gdata::GDataSystemServiceFactory::GetForProfile(profile); | 93 gdata::GDataSystemServiceFactory::GetForProfile(profile); |
| 127 // system_service is NULL in incognito. | 94 // system_service is NULL in incognito. |
| 128 return system_service ? system_service->file_system() : NULL; | 95 if (system_service && gdata::util::IsUnderGDataMountPoint(selected_path)) { |
| 96 // Here's a map to the callback chain: | |
| 97 // GetGDataTempDownloadPath -> | |
| 98 // ContinueSettingUpGDataDownload -> | |
| 99 // callback_ = SavePackage::OnPathPicked -> | |
| 100 // download_created_callback = OnSavePackageDownloadCreated | |
| 101 FilePath gdata_tmp_download_dir = | |
| 102 system_service->file_system()->GetCacheDirectoryPath( | |
| 103 gdata::GDataRootDirectory::CACHE_TYPE_TMP_DOWNLOADS); | |
| 104 FilePath* gdata_tmp_download_path(new FilePath()); | |
| 105 content::BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, | |
| 106 base::Bind(&gdata::GDataDownloadObserver::GetGDataTempDownloadPath, | |
| 107 gdata_tmp_download_dir, | |
| 108 gdata_tmp_download_path), | |
| 109 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
| |
| 110 callback_, | |
| 111 base::Owned(gdata_tmp_download_path), | |
| 112 selected_path)); | |
| 113 } else { | |
| 114 callback_.Run(selected_path, content::SAVE_PAGE_TYPE_AS_MHTML, | |
| 115 content::SaveFileDownloadCreatedCallback()); | |
| 116 } | |
| 117 delete this; | |
| 129 } | 118 } |
| 119 | |
| 120 void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) { | |
| 121 delete this; | |
| 122 } | |
| OLD | NEW |