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 "webkit/plugins/ppapi/ppb_file_chooser_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_file_chooser_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 | 74 |
75 private: | 75 private: |
76 scoped_refptr<PPB_FileChooser_Impl> file_chooser_; | 76 scoped_refptr<PPB_FileChooser_Impl> file_chooser_; |
77 }; | 77 }; |
78 | 78 |
79 } // namespace | 79 } // namespace |
80 | 80 |
81 PPB_FileChooser_Impl::PPB_FileChooser_Impl( | 81 PPB_FileChooser_Impl::PPB_FileChooser_Impl( |
82 PP_Instance instance, | 82 PP_Instance instance, |
83 PP_FileChooserMode_Dev mode, | 83 PP_FileChooserMode_Dev mode, |
84 const char* accept_mime_types) | 84 const char* accept_types) |
85 : Resource(::ppapi::OBJECT_IS_IMPL, instance), | 85 : Resource(::ppapi::OBJECT_IS_IMPL, instance), |
86 mode_(mode), | 86 mode_(mode), |
87 next_chosen_file_index_(0) { | 87 next_chosen_file_index_(0) { |
88 if (accept_mime_types) | 88 if (accept_types) |
89 accept_mime_types_ = std::string(accept_mime_types); | 89 accept_types_ = std::string(accept_types); |
90 } | 90 } |
91 | 91 |
92 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { | 92 PPB_FileChooser_Impl::~PPB_FileChooser_Impl() { |
93 } | 93 } |
94 | 94 |
95 // static | 95 // static |
96 PP_Resource PPB_FileChooser_Impl::Create( | 96 PP_Resource PPB_FileChooser_Impl::Create( |
97 PP_Instance instance, | 97 PP_Instance instance, |
98 PP_FileChooserMode_Dev mode, | 98 PP_FileChooserMode_Dev mode, |
99 const char* accept_mime_types) { | 99 const char* accept_types) { |
100 if (mode != PP_FILECHOOSERMODE_OPEN && | 100 if (mode != PP_FILECHOOSERMODE_OPEN && |
101 mode != PP_FILECHOOSERMODE_OPENMULTIPLE) | 101 mode != PP_FILECHOOSERMODE_OPENMULTIPLE) |
102 return 0; | 102 return 0; |
103 return (new PPB_FileChooser_Impl(instance, mode, | 103 return (new PPB_FileChooser_Impl(instance, mode, |
104 accept_mime_types))->GetReference(); | 104 accept_types))->GetReference(); |
105 } | 105 } |
106 | 106 |
107 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { | 107 PPB_FileChooser_Impl* PPB_FileChooser_Impl::AsPPB_FileChooser_Impl() { |
108 return this; | 108 return this; |
109 } | 109 } |
110 | 110 |
111 PPB_FileChooser_API* PPB_FileChooser_Impl::AsPPB_FileChooser_API() { | 111 PPB_FileChooser_API* PPB_FileChooser_Impl::AsPPB_FileChooser_API() { |
112 return this; | 112 return this; |
113 } | 113 } |
114 | 114 |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 | 218 |
219 WebFileChooserParams params; | 219 WebFileChooserParams params; |
220 if (save_as) { | 220 if (save_as) { |
221 params.saveAs = true; | 221 params.saveAs = true; |
222 StringVar* str = StringVar::FromPPVar(suggested_file_name); | 222 StringVar* str = StringVar::FromPPVar(suggested_file_name); |
223 if (str) | 223 if (str) |
224 params.initialValue = WebString::fromUTF8(str->value().c_str()); | 224 params.initialValue = WebString::fromUTF8(str->value().c_str()); |
225 } else { | 225 } else { |
226 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); | 226 params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); |
227 } | 227 } |
228 params.acceptMIMETypes = ParseAcceptValue(accept_mime_types_); | 228 params.acceptTypes = ParseAcceptValue(accept_types_); |
229 params.directory = false; | 229 params.directory = false; |
230 | 230 |
231 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); | 231 PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); |
232 if (!plugin_delegate) | 232 if (!plugin_delegate) |
233 return PP_ERROR_FAILED; | 233 return PP_ERROR_FAILED; |
234 | 234 |
235 if (!plugin_delegate->RunFileChooser(params, | 235 if (!plugin_delegate->RunFileChooser(params, |
236 new FileChooserCompletionImpl(this))) | 236 new FileChooserCompletionImpl(this))) |
237 return PP_ERROR_FAILED; | 237 return PP_ERROR_FAILED; |
238 | 238 |
239 RegisterCallback(callback); | 239 RegisterCallback(callback); |
240 return PP_OK_COMPLETIONPENDING; | 240 return PP_OK_COMPLETIONPENDING; |
241 } | 241 } |
242 | 242 |
243 PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { | 243 PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { |
244 if (next_chosen_file_index_ >= chosen_files_.size()) | 244 if (next_chosen_file_index_ >= chosen_files_.size()) |
245 return 0; | 245 return 0; |
246 | 246 |
247 return chosen_files_[next_chosen_file_index_++]->GetReference(); | 247 return chosen_files_[next_chosen_file_index_++]->GetReference(); |
248 } | 248 } |
249 | 249 |
250 std::vector<WebString> PPB_FileChooser_Impl::ParseAcceptValue( | 250 std::vector<WebString> PPB_FileChooser_Impl::ParseAcceptValue( |
251 const std::string& accept_mime_types) { | 251 const std::string& accept_types) { |
252 if (accept_mime_types.empty()) | 252 if (accept_types.empty()) |
253 return std::vector<WebString>(); | 253 return std::vector<WebString>(); |
254 std::vector<std::string> mime_type_list; | 254 std::vector<std::string> type_list; |
255 base::SplitString(accept_mime_types, ',', &mime_type_list); | 255 base::SplitString(accept_types, ',', &type_list); |
256 std::vector<WebString> normalized_mime_type_list; | 256 std::vector<WebString> normalized_type_list; |
257 normalized_mime_type_list.reserve(mime_type_list.size()); | 257 normalized_type_list.reserve(type_list.size()); |
258 for (size_t i = 0; i < mime_type_list.size(); ++i) { | 258 for (size_t i = 0; i < type_list.size(); ++i) { |
259 std::string mime_type = mime_type_list[i]; | 259 std::string type = type_list[i]; |
260 TrimWhitespaceASCII(mime_type, TRIM_ALL, &mime_type); | 260 TrimWhitespaceASCII(type, TRIM_ALL, &type); |
261 if (mime_type.empty()) | 261 |
| 262 // If the type is a single character, it definitely cannot be valid. In the |
| 263 // case of a file extension it would be a single ".". In the case of a MIME |
| 264 // type it would just be a "/". |
| 265 if (type.length() < 2) |
262 continue; | 266 continue; |
263 if (mime_type.find_first_of('/') == std::string::npos) | 267 if (type.find_first_of('/') == std::string::npos && type[0] != '.') |
264 continue; | 268 continue; |
265 StringToLowerASCII(&mime_type); | 269 StringToLowerASCII(&type); |
266 normalized_mime_type_list.push_back(WebString::fromUTF8(mime_type.data(), | 270 normalized_type_list.push_back(WebString::fromUTF8(type.data(), |
267 mime_type.size())); | 271 type.size())); |
268 } | 272 } |
269 return normalized_mime_type_list; | 273 return normalized_type_list; |
270 } | 274 } |
271 | 275 |
272 } // namespace ppapi | 276 } // namespace ppapi |
273 } // namespace webkit | 277 } // namespace webkit |
OLD | NEW |