OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/tab_contents/tab_contents_file_select_helper.h" |
| 6 |
| 7 #include "base/file_util.h" |
| 8 #include "chrome/browser/profile.h" |
| 9 #include "chrome/browser/renderer_host/render_view_host.h" |
| 10 #include "chrome/browser/shell_dialogs.h" |
| 11 #include "chrome/browser/tab_contents/tab_contents.h" |
| 12 #include "chrome/browser/tab_contents/tab_contents_view.h" |
| 13 #include "chrome/common/render_messages.h" |
| 14 |
| 15 TabContentsFileSelectHelper::TabContentsFileSelectHelper( |
| 16 TabContents* tab_contents) |
| 17 : tab_contents_(tab_contents), |
| 18 select_file_dialog_(), |
| 19 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { |
| 20 } |
| 21 |
| 22 TabContentsFileSelectHelper::~TabContentsFileSelectHelper() { |
| 23 // There may be pending file dialogs, we need to tell them that we've gone |
| 24 // away so they don't try and call back to us. |
| 25 if (select_file_dialog_.get()) |
| 26 select_file_dialog_->ListenerDestroyed(); |
| 27 |
| 28 // Stop any pending directory enumeration and prevent a callback. |
| 29 if (directory_lister_.get()) { |
| 30 directory_lister_->set_delegate(NULL); |
| 31 directory_lister_->Cancel(); |
| 32 } |
| 33 } |
| 34 |
| 35 void TabContentsFileSelectHelper::FileSelected(const FilePath& path, |
| 36 int index, void* params) { |
| 37 tab_contents_->profile()->set_last_selected_directory(path.DirName()); |
| 38 |
| 39 if (dialog_type_ == SelectFileDialog::SELECT_FOLDER) { |
| 40 DirectorySelected(path); |
| 41 return; |
| 42 } |
| 43 |
| 44 std::vector<FilePath> files; |
| 45 files.push_back(path); |
| 46 GetRenderViewHost()->FilesSelectedInChooser(files); |
| 47 } |
| 48 |
| 49 void TabContentsFileSelectHelper::MultiFilesSelected( |
| 50 const std::vector<FilePath>& files, void* params) { |
| 51 if (!files.empty()) |
| 52 tab_contents_->profile()->set_last_selected_directory(files[0].DirName()); |
| 53 GetRenderViewHost()->FilesSelectedInChooser(files); |
| 54 } |
| 55 |
| 56 void TabContentsFileSelectHelper::DirectorySelected(const FilePath& path) { |
| 57 directory_lister_ = new net::DirectoryLister(path, |
| 58 true, |
| 59 net::DirectoryLister::NO_SORT, |
| 60 this); |
| 61 if (!directory_lister_->Start()) |
| 62 FileSelectionCanceled(NULL); |
| 63 } |
| 64 |
| 65 void TabContentsFileSelectHelper::OnListFile( |
| 66 const net::DirectoryLister::DirectoryListerData& data) { |
| 67 // Directory upload only cares about files. This util call just checks |
| 68 // the flags in the structure; there's no file I/O going on. |
| 69 if (file_util::FileEnumerator::IsDirectory(data.info)) |
| 70 return; |
| 71 |
| 72 directory_lister_results_.push_back(data.path); |
| 73 } |
| 74 |
| 75 void TabContentsFileSelectHelper::OnListDone(int error) { |
| 76 if (error) { |
| 77 FileSelectionCanceled(NULL); |
| 78 return; |
| 79 } |
| 80 |
| 81 GetRenderViewHost()->FilesSelectedInChooser(directory_lister_results_); |
| 82 directory_lister_ = NULL; |
| 83 } |
| 84 |
| 85 void TabContentsFileSelectHelper::FileSelectionCanceled(void* params) { |
| 86 // If the user cancels choosing a file to upload we pass back an |
| 87 // empty vector. |
| 88 GetRenderViewHost()->FilesSelectedInChooser(std::vector<FilePath>()); |
| 89 } |
| 90 |
| 91 void TabContentsFileSelectHelper::RunFileChooser( |
| 92 const ViewHostMsg_RunFileChooser_Params ¶ms) { |
| 93 if (!select_file_dialog_.get()) |
| 94 select_file_dialog_ = SelectFileDialog::Create(this); |
| 95 |
| 96 switch (params.mode) { |
| 97 case ViewHostMsg_RunFileChooser_Params::Open: |
| 98 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; |
| 99 break; |
| 100 case ViewHostMsg_RunFileChooser_Params::OpenMultiple: |
| 101 dialog_type_ = SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
| 102 break; |
| 103 case ViewHostMsg_RunFileChooser_Params::OpenFolder: |
| 104 dialog_type_ = SelectFileDialog::SELECT_FOLDER; |
| 105 break; |
| 106 case ViewHostMsg_RunFileChooser_Params::Save: |
| 107 dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; |
| 108 break; |
| 109 default: |
| 110 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning. |
| 111 NOTREACHED(); |
| 112 } |
| 113 FilePath default_file_name = params.default_file_name; |
| 114 if (default_file_name.empty()) |
| 115 default_file_name = tab_contents_->profile()->last_selected_directory(); |
| 116 select_file_dialog_->SelectFile( |
| 117 dialog_type_, |
| 118 params.title, |
| 119 default_file_name, |
| 120 NULL, |
| 121 0, |
| 122 FILE_PATH_LITERAL(""), |
| 123 tab_contents_->view()->GetTopLevelNativeWindow(), |
| 124 NULL); |
| 125 } |
| 126 |
| 127 RenderViewHost* TabContentsFileSelectHelper::GetRenderViewHost() { |
| 128 return tab_contents_->render_view_host(); |
| 129 } |
OLD | NEW |