OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/tab_contents/tab_contents_file_select_helper.h" | 5 #include "chrome/browser/tab_contents/tab_contents_file_select_helper.h" |
6 | 6 |
| 7 #include "app/l10n_util.h" |
7 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" |
| 11 #include "net/base/mime_util.h" |
8 #include "chrome/browser/profile.h" | 12 #include "chrome/browser/profile.h" |
9 #include "chrome/browser/renderer_host/render_view_host.h" | 13 #include "chrome/browser/renderer_host/render_view_host.h" |
10 #include "chrome/browser/shell_dialogs.h" | 14 #include "chrome/browser/shell_dialogs.h" |
11 #include "chrome/browser/tab_contents/tab_contents.h" | 15 #include "chrome/browser/tab_contents/tab_contents.h" |
12 #include "chrome/browser/tab_contents/tab_contents_view.h" | 16 #include "chrome/browser/tab_contents/tab_contents_view.h" |
13 #include "chrome/common/render_messages_params.h" | 17 #include "chrome/common/render_messages_params.h" |
| 18 #include "grit/generated_resources.h" |
14 | 19 |
15 TabContentsFileSelectHelper::TabContentsFileSelectHelper( | 20 TabContentsFileSelectHelper::TabContentsFileSelectHelper( |
16 TabContents* tab_contents) | 21 TabContents* tab_contents) |
17 : tab_contents_(tab_contents), | 22 : tab_contents_(tab_contents), |
18 select_file_dialog_(), | 23 select_file_dialog_(), |
19 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { | 24 dialog_type_(SelectFileDialog::SELECT_OPEN_FILE) { |
20 } | 25 } |
21 | 26 |
22 TabContentsFileSelectHelper::~TabContentsFileSelectHelper() { | 27 TabContentsFileSelectHelper::~TabContentsFileSelectHelper() { |
23 // There may be pending file dialogs, we need to tell them that we've gone | 28 // There may be pending file dialogs, we need to tell them that we've gone |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 GetRenderViewHost()->FilesSelectedInChooser(directory_lister_results_); | 86 GetRenderViewHost()->FilesSelectedInChooser(directory_lister_results_); |
82 directory_lister_ = NULL; | 87 directory_lister_ = NULL; |
83 } | 88 } |
84 | 89 |
85 void TabContentsFileSelectHelper::FileSelectionCanceled(void* params) { | 90 void TabContentsFileSelectHelper::FileSelectionCanceled(void* params) { |
86 // If the user cancels choosing a file to upload we pass back an | 91 // If the user cancels choosing a file to upload we pass back an |
87 // empty vector. | 92 // empty vector. |
88 GetRenderViewHost()->FilesSelectedInChooser(std::vector<FilePath>()); | 93 GetRenderViewHost()->FilesSelectedInChooser(std::vector<FilePath>()); |
89 } | 94 } |
90 | 95 |
| 96 SelectFileDialog::FileTypeInfo* |
| 97 TabContentsFileSelectHelper::GetFileTypesFromAcceptType( |
| 98 const string16& accept_types) { |
| 99 if (accept_types.empty()) |
| 100 return NULL; |
| 101 |
| 102 // Split the accept-type string on commas. |
| 103 std::vector<string16> mime_types; |
| 104 base::SplitStringUsingSubstr(accept_types, L",", &mime_types); |
| 105 if (mime_types.empty()) |
| 106 return NULL; |
| 107 |
| 108 // Create FileTypeInfo and pre-allocate for the first extension list. |
| 109 scoped_ptr<SelectFileDialog::FileTypeInfo> file_type( |
| 110 new SelectFileDialog::FileTypeInfo()); |
| 111 file_type->include_all_files = true; |
| 112 file_type->extensions.resize(1); |
| 113 std::vector<FilePath::StringType>* extensions = &file_type->extensions.back(); |
| 114 |
| 115 // Find the correspondinge extensions. |
| 116 int valid_type_count = 0; |
| 117 int description_id = 0; |
| 118 for (size_t i = 0; i < mime_types.size(); ++i) { |
| 119 string16 mime_type = mime_types[i]; |
| 120 std::string ascii_mime_type = StringToLowerASCII(UTF16ToASCII(mime_type)); |
| 121 |
| 122 TrimWhitespace(ascii_mime_type, TRIM_ALL, &ascii_mime_type); |
| 123 if (ascii_mime_type.empty()) |
| 124 continue; |
| 125 |
| 126 size_t old_extension_size = extensions->size(); |
| 127 if (ascii_mime_type == "image/*") { |
| 128 description_id = IDS_IMAGE_FILES; |
| 129 net::GetImageExtensions(extensions); |
| 130 } else if (ascii_mime_type == "audio/*") { |
| 131 description_id = IDS_AUDIO_FILES; |
| 132 net::GetAudioExtensions(extensions); |
| 133 } else if (ascii_mime_type == "video/*") { |
| 134 description_id = IDS_VIDEO_FILES; |
| 135 net::GetVideoExtensions(extensions); |
| 136 } else { |
| 137 net::GetExtensionsForMimeType(ascii_mime_type, extensions); |
| 138 } |
| 139 |
| 140 if (extensions->size() > old_extension_size) |
| 141 valid_type_count++; |
| 142 } |
| 143 |
| 144 // Use a generic description "Custom Files" if either of the following is |
| 145 // true: |
| 146 // 1) There're multiple types specified, like "audio/*,video/*" |
| 147 // 2) There're multiple extensions for a MIME type without parameter, like |
| 148 // "ehtml,shtml,htm,html" for "text/html". On Windows, the select file |
| 149 // dialog uses the first extension in the list to form the description, |
| 150 // like "EHTML Files". This is not what we want. |
| 151 if (valid_type_count > 1 || |
| 152 (valid_type_count == 1 && description_id == 0 && extensions->size() > 1)) |
| 153 description_id = IDS_CUSTOM_FILES; |
| 154 |
| 155 if (description_id) { |
| 156 file_type->extension_description_overrides.push_back( |
| 157 l10n_util::GetStringUTF16(description_id)); |
| 158 } |
| 159 |
| 160 return file_type.release(); |
| 161 } |
| 162 |
91 void TabContentsFileSelectHelper::RunFileChooser( | 163 void TabContentsFileSelectHelper::RunFileChooser( |
92 const ViewHostMsg_RunFileChooser_Params ¶ms) { | 164 const ViewHostMsg_RunFileChooser_Params ¶ms) { |
93 if (!select_file_dialog_.get()) | 165 if (!select_file_dialog_.get()) |
94 select_file_dialog_ = SelectFileDialog::Create(this); | 166 select_file_dialog_ = SelectFileDialog::Create(this); |
95 | 167 |
96 switch (params.mode) { | 168 switch (params.mode) { |
97 case ViewHostMsg_RunFileChooser_Params::Open: | 169 case ViewHostMsg_RunFileChooser_Params::Open: |
98 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; | 170 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; |
99 break; | 171 break; |
100 case ViewHostMsg_RunFileChooser_Params::OpenMultiple: | 172 case ViewHostMsg_RunFileChooser_Params::OpenMultiple: |
101 dialog_type_ = SelectFileDialog::SELECT_OPEN_MULTI_FILE; | 173 dialog_type_ = SelectFileDialog::SELECT_OPEN_MULTI_FILE; |
102 break; | 174 break; |
103 case ViewHostMsg_RunFileChooser_Params::OpenFolder: | 175 case ViewHostMsg_RunFileChooser_Params::OpenFolder: |
104 dialog_type_ = SelectFileDialog::SELECT_FOLDER; | 176 dialog_type_ = SelectFileDialog::SELECT_FOLDER; |
105 break; | 177 break; |
106 case ViewHostMsg_RunFileChooser_Params::Save: | 178 case ViewHostMsg_RunFileChooser_Params::Save: |
107 dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; | 179 dialog_type_ = SelectFileDialog::SELECT_SAVEAS_FILE; |
108 break; | 180 break; |
109 default: | 181 default: |
110 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning. | 182 dialog_type_ = SelectFileDialog::SELECT_OPEN_FILE; // Prevent warning. |
111 NOTREACHED(); | 183 NOTREACHED(); |
112 } | 184 } |
| 185 scoped_ptr<SelectFileDialog::FileTypeInfo> file_types( |
| 186 GetFileTypesFromAcceptType(params.accept_types)); |
113 FilePath default_file_name = params.default_file_name; | 187 FilePath default_file_name = params.default_file_name; |
114 if (default_file_name.empty()) | 188 if (default_file_name.empty()) |
115 default_file_name = tab_contents_->profile()->last_selected_directory(); | 189 default_file_name = tab_contents_->profile()->last_selected_directory(); |
116 select_file_dialog_->SelectFile( | 190 select_file_dialog_->SelectFile( |
117 dialog_type_, | 191 dialog_type_, |
118 params.title, | 192 params.title, |
119 default_file_name, | 193 default_file_name, |
120 NULL, | 194 file_types.get(), |
121 0, | 195 0, |
122 FILE_PATH_LITERAL(""), | 196 FILE_PATH_LITERAL(""), |
123 tab_contents_->view()->GetTopLevelNativeWindow(), | 197 tab_contents_->view()->GetTopLevelNativeWindow(), |
124 NULL); | 198 NULL); |
125 } | 199 } |
126 | 200 |
127 RenderViewHost* TabContentsFileSelectHelper::GetRenderViewHost() { | 201 RenderViewHost* TabContentsFileSelectHelper::GetRenderViewHost() { |
128 return tab_contents_->render_view_host(); | 202 return tab_contents_->render_view_host(); |
129 } | 203 } |
OLD | NEW |