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" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/i18n/file_util_icu.h" | 7 #include "base/i18n/file_util_icu.h" |
| 10 #include "base/threading/sequenced_worker_pool.h" | |
| 11 #include "chrome/browser/chromeos/gdata/gdata_download_observer.h" | |
| 12 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" | |
| 13 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | |
| 14 #include "chrome/browser/chromeos/gdata/gdata_util.h" | |
| 15 #include "chrome/browser/platform_util.h" | 8 #include "chrome/browser/platform_util.h" |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 19 | 10 |
| 20 using content::BrowserThread; | |
| 21 | |
| 22 SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( | 11 SavePackageFilePickerChromeOS::SavePackageFilePickerChromeOS( |
| 23 content::WebContents* web_contents, | 12 content::WebContents* web_contents, |
| 24 const FilePath& suggested_path) | 13 const FilePath& suggested_path, |
| 25 : content::WebContentsObserver(web_contents) { | 14 content::SaveFilePathPickedCallback callback) |
| 15 : content::WebContentsObserver(web_contents), | |
| 16 callback_(callback) { | |
| 26 select_file_dialog_ = SelectFileDialog::Create(this); | 17 select_file_dialog_ = SelectFileDialog::Create(this); |
| 27 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, | 18 select_file_dialog_->SelectFile(SelectFileDialog::SELECT_SAVEAS_FILE, |
| 28 string16(), | 19 string16(), |
| 29 suggested_path.ReplaceExtension("mhtml"), | 20 suggested_path.ReplaceExtension("mhtml"), |
| 30 NULL, | 21 NULL, |
| 31 0, | 22 0, |
| 32 "mhtml", | 23 "mhtml", |
| 33 web_contents, | 24 web_contents, |
| 34 platform_util::GetTopLevel( | 25 platform_util::GetTopLevel( |
| 35 web_contents->GetNativeView()), | 26 web_contents->GetNativeView()), |
| 36 NULL); | 27 NULL); |
| 37 } | 28 } |
| 38 | 29 |
| 39 SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { | 30 SavePackageFilePickerChromeOS::~SavePackageFilePickerChromeOS() { |
| 40 } | 31 } |
| 41 | 32 |
| 42 void SavePackageFilePickerChromeOS::FileSelected(const FilePath& selected_path, | 33 void SavePackageFilePickerChromeOS::FileSelected(const FilePath& selected_path, |
| 43 int index, | 34 int index, |
| 44 void* params) { | 35 void* params) { |
| 45 if (!web_contents()) { | 36 if (web_contents()) { |
| 46 delete this; | 37 FilePath path = selected_path; |
| 47 return; | 38 file_util::NormalizeFileNameEncoding(&path); |
| 39 callback_.Run(path, content::SAVE_PAGE_TYPE_AS_MHTML); | |
|
asanka
2012/04/18 18:34:59
How does the file get uploaded to GData if it was
benjhayden
2012/04/18 18:38:28
My understanding is that there's a DownloadManager
asanka
2012/04/18 18:50:24
It's not automatic. See DownloadFilePickerChromeOS
| |
| 48 } | 40 } |
| 49 | 41 delete this; |
| 50 FilePath path = 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->GetGDataTempDownloadFolderPath(); | |
| 57 | |
| 58 selected_path_ = path; | |
| 59 FilePath* gdata_tmp_download_path = new FilePath(); | |
| 60 BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, | |
| 61 base::Bind(&gdata::GDataDownloadObserver::GetGDataTempDownloadPath, | |
| 62 gdata_tmp_download_dir, | |
| 63 gdata_tmp_download_path), | |
| 64 base::Bind(&SavePackageFilePickerChromeOS::GenerateMHTML, | |
| 65 base::Unretained(this), | |
| 66 base::Owned(gdata_tmp_download_path))); | |
| 67 } else { | |
| 68 DVLOG(1) << "SavePackageFilePickerChromeOS non-gdata file"; | |
| 69 GenerateMHTML(&path); | |
| 70 } | |
| 71 } | 42 } |
| 72 | 43 |
| 73 void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) { | 44 void SavePackageFilePickerChromeOS::FileSelectionCanceled(void* params) { |
| 74 delete this; | 45 delete this; |
| 75 } | 46 } |
| 76 | |
| 77 void SavePackageFilePickerChromeOS::GenerateMHTML(const FilePath* mhtml_path) { | |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 79 if (!web_contents()) { | |
| 80 delete this; | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 DVLOG(1) << "GenerateMHTML " << mhtml_path->value(); | |
| 85 web_contents()->GenerateMHTML(*mhtml_path, | |
| 86 base::Bind(&SavePackageFilePickerChromeOS::OnMHTMLGenerated, | |
| 87 base::Unretained(this))); | |
| 88 } | |
| 89 | |
| 90 void SavePackageFilePickerChromeOS::OnMHTMLGenerated(const FilePath& src_path, | |
| 91 int64 file_size) { | |
| 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 93 if (!web_contents()) { | |
| 94 delete this; | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 gdata::GDataFileSystem* gdata_filesystem = GetGDataFileSystem(); | |
| 99 if (gdata_filesystem && !selected_path_.empty()) { | |
| 100 DVLOG(1) << "TransferFile from " << src_path.value() | |
| 101 << " to " << selected_path_.value(); | |
| 102 gdata_filesystem->TransferFile(src_path, | |
| 103 gdata::util::ExtractGDataPath(selected_path_), | |
| 104 base::Bind(&SavePackageFilePickerChromeOS::OnTransferFile, | |
| 105 base::Unretained(this))); | |
| 106 } else { | |
| 107 delete this; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void SavePackageFilePickerChromeOS::OnTransferFile( | |
| 112 base::PlatformFileError error) { | |
| 113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 114 DCHECK_EQ(error, base::PLATFORM_FILE_OK); | |
| 115 delete this; | |
| 116 } | |
| 117 | |
| 118 gdata::GDataFileSystem* | |
| 119 SavePackageFilePickerChromeOS::GetGDataFileSystem() { | |
| 120 DCHECK(web_contents()); | |
| 121 Profile* profile = Profile::FromBrowserContext( | |
| 122 web_contents()->GetBrowserContext()); | |
| 123 DCHECK(profile); | |
| 124 gdata::GDataSystemService* system_service = | |
| 125 gdata::GDataSystemServiceFactory::GetForProfile(profile); | |
| 126 // system_service is NULL in incognito. | |
| 127 return system_service ? system_service->file_system() : NULL; | |
| 128 } | |
| OLD | NEW |