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/download_file_picker.h" | 5 #include "chrome/browser/download/download_file_picker.h" |
| 6 | 6 |
| 7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "chrome/browser/download/download_prefs.h" | 8 #include "chrome/browser/download/download_prefs.h" |
| 9 #include "chrome/browser/platform_util.h" | 9 #include "chrome/browser/platform_util.h" |
| 10 #include "chrome/browser/ui/chrome_select_file_policy.h" | 10 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 11 #include "content/public/browser/browser_context.h" | |
| 11 #include "content/public/browser/download_item.h" | 12 #include "content/public/browser/download_item.h" |
| 12 #include "content/public/browser/download_manager.h" | 13 #include "content/public/browser/download_manager.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_contents_view.h" | 15 #include "content/public/browser/web_contents_view.h" |
| 15 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 17 | 18 |
| 18 using content::DownloadItem; | 19 using content::DownloadItem; |
| 19 using content::DownloadManager; | 20 using content::DownloadManager; |
| 20 using content::WebContents; | 21 using content::WebContents; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 40 return; | 41 return; |
| 41 if (prefs->PromptForDownload()) | 42 if (prefs->PromptForDownload()) |
| 42 return; | 43 return; |
| 43 UMA_HISTOGRAM_ENUMERATION("Download.FilePickerResult", | 44 UMA_HISTOGRAM_ENUMERATION("Download.FilePickerResult", |
| 44 result, | 45 result, |
| 45 FILE_PICKER_MAX); | 46 FILE_PICKER_MAX); |
| 46 } | 47 } |
| 47 | 48 |
| 48 FilePickerResult ComparePaths(const base::FilePath& suggested_path, | 49 FilePickerResult ComparePaths(const base::FilePath& suggested_path, |
| 49 const base::FilePath& actual_path) { | 50 const base::FilePath& actual_path) { |
| 51 if (actual_path.empty()) | |
| 52 return FILE_PICKER_CANCEL; | |
| 50 if (suggested_path == actual_path) | 53 if (suggested_path == actual_path) |
| 51 return FILE_PICKER_SAME; | 54 return FILE_PICKER_SAME; |
| 52 if (suggested_path.DirName() != actual_path.DirName()) | 55 if (suggested_path.DirName() != actual_path.DirName()) |
| 53 return FILE_PICKER_DIFFERENT_DIR; | 56 return FILE_PICKER_DIFFERENT_DIR; |
| 54 return FILE_PICKER_DIFFERENT_NAME; | 57 return FILE_PICKER_DIFFERENT_NAME; |
| 55 } | 58 } |
| 56 | 59 |
| 57 } // namespace | 60 } // namespace |
| 58 | 61 |
| 59 DownloadFilePicker::DownloadFilePicker() : download_id_(0) { | 62 DownloadFilePicker::DownloadFilePicker( |
| 60 } | |
| 61 | |
| 62 void DownloadFilePicker::Init( | |
| 63 DownloadManager* download_manager, | |
| 64 DownloadItem* item, | 63 DownloadItem* item, |
| 65 const base::FilePath& suggested_path, | 64 const base::FilePath& suggested_path, |
| 66 const ChromeDownloadManagerDelegate::FileSelectedCallback& callback) { | 65 const FileSelectedCallback& callback) |
| 67 download_manager_ = download_manager; | 66 : suggested_path_(suggested_path), |
| 68 download_id_ = item->GetId(); | 67 file_selected_callback_(callback), |
| 69 file_selected_callback_ = callback; | 68 download_id_(item->GetId()) { |
|
benjhayden
2013/04/26 15:07:37
Is this used anywhere?
asanka
2013/04/26 17:02:21
Apparently not. Removed.
| |
| 70 InitSuggestedPath(item, suggested_path); | 69 download_manager_ = |
| 71 | 70 content::BrowserContext::GetDownloadManager(item->GetBrowserContext()); |
| 72 DCHECK(download_manager_); | 71 DCHECK(download_manager_); |
| 73 WebContents* web_contents = item->GetWebContents(); | 72 WebContents* web_contents = item->GetWebContents(); |
| 74 select_file_dialog_ = ui::SelectFileDialog::Create( | 73 select_file_dialog_ = ui::SelectFileDialog::Create( |
| 75 this, new ChromeSelectFilePolicy(web_contents)); | 74 this, new ChromeSelectFilePolicy(web_contents)); |
| 76 ui::SelectFileDialog::FileTypeInfo file_type_info; | 75 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 77 base::FilePath::StringType extension = suggested_path_.Extension(); | 76 base::FilePath::StringType extension = suggested_path_.Extension(); |
| 78 if (!extension.empty()) { | 77 if (!extension.empty()) { |
| 79 extension.erase(extension.begin()); // drop the . | 78 extension.erase(extension.begin()); // drop the . |
| 80 file_type_info.extensions.resize(1); | 79 file_type_info.extensions.resize(1); |
| 81 file_type_info.extensions[0].push_back(extension); | 80 file_type_info.extensions[0].push_back(extension); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 92 &file_type_info, | 91 &file_type_info, |
| 93 0, | 92 0, |
| 94 base::FilePath::StringType(), | 93 base::FilePath::StringType(), |
| 95 owning_window, | 94 owning_window, |
| 96 NULL); | 95 NULL); |
| 97 } | 96 } |
| 98 | 97 |
| 99 DownloadFilePicker::~DownloadFilePicker() { | 98 DownloadFilePicker::~DownloadFilePicker() { |
| 100 } | 99 } |
| 101 | 100 |
| 102 void DownloadFilePicker::InitSuggestedPath( | |
| 103 DownloadItem* item, | |
| 104 const base::FilePath& suggested_path) { | |
| 105 set_suggested_path(suggested_path); | |
| 106 } | |
| 107 | |
| 108 void DownloadFilePicker::OnFileSelected(const base::FilePath& path) { | 101 void DownloadFilePicker::OnFileSelected(const base::FilePath& path) { |
| 102 FilePickerResult result = ComparePaths(suggested_path_, path); | |
| 103 RecordFilePickerResult(download_manager_, result); | |
| 109 file_selected_callback_.Run(path); | 104 file_selected_callback_.Run(path); |
| 110 delete this; | 105 delete this; |
| 111 } | 106 } |
| 112 | 107 |
| 113 void DownloadFilePicker::RecordFileSelected(const base::FilePath& path) { | |
| 114 FilePickerResult result = ComparePaths(suggested_path_, path); | |
| 115 RecordFilePickerResult(download_manager_, result); | |
| 116 } | |
| 117 | |
| 118 void DownloadFilePicker::FileSelected(const base::FilePath& path, | 108 void DownloadFilePicker::FileSelected(const base::FilePath& path, |
| 119 int index, | 109 int index, |
| 120 void* params) { | 110 void* params) { |
| 121 RecordFileSelected(path); | |
| 122 OnFileSelected(path); | 111 OnFileSelected(path); |
| 123 // Deletes |this| | 112 // Deletes |this| |
| 124 } | 113 } |
| 125 | 114 |
| 126 void DownloadFilePicker::FileSelectionCanceled(void* params) { | 115 void DownloadFilePicker::FileSelectionCanceled(void* params) { |
| 127 RecordFilePickerResult(download_manager_, FILE_PICKER_CANCEL); | |
| 128 OnFileSelected(base::FilePath()); | 116 OnFileSelected(base::FilePath()); |
| 129 // Deletes |this| | 117 // Deletes |this| |
| 130 } | 118 } |
| 119 | |
| 120 // static | |
| 121 void DownloadFilePicker::ShowFilePicker(DownloadItem* item, | |
| 122 const base::FilePath& suggested_path, | |
| 123 const FileSelectedCallback& callback) { | |
| 124 new DownloadFilePicker(item, suggested_path, callback); | |
| 125 // DownloadFilePicker deletes itself. | |
| 126 } | |
| OLD | NEW |