diff options
-rw-r--r-- | webkit/plugins/ppapi/ppb_file_chooser_impl.cc | 26 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_file_chooser_impl.h | 10 | ||||
-rw-r--r-- | webkit/plugins/ppapi/ppb_file_chooser_impl_unittest.cc | 39 | ||||
-rw-r--r-- | webkit/tools/test_shell/test_shell.gypi | 1 |
4 files changed, 75 insertions, 1 deletions
diff --git a/webkit/plugins/ppapi/ppb_file_chooser_impl.cc b/webkit/plugins/ppapi/ppb_file_chooser_impl.cc index 179cf04..ce5dc4a 100644 --- a/webkit/plugins/ppapi/ppb_file_chooser_impl.cc +++ b/webkit/plugins/ppapi/ppb_file_chooser_impl.cc @@ -8,6 +8,8 @@ #include <vector> #include "base/logging.h" +#include "base/string_split.h" +#include "base/string_util.h" #include "base/sys_string_conversions.h" #include "ppapi/c/pp_completion_callback.h" #include "ppapi/c/pp_errors.h" @@ -169,7 +171,7 @@ int32_t PPB_FileChooser_Impl::ShowWithoutUserGesture( } else { params.multiSelect = (mode_ == PP_FILECHOOSERMODE_OPENMULTIPLE); } - params.acceptTypes = WebString::fromUTF8(accept_mime_types_); + params.acceptMIMETypes = ParseAcceptValue(accept_mime_types_); params.directory = false; PluginDelegate* plugin_delegate = ResourceHelper::GetPluginDelegate(this); @@ -191,5 +193,27 @@ PP_Resource PPB_FileChooser_Impl::GetNextChosenFile() { return chosen_files_[next_chosen_file_index_++]->GetReference(); } +std::vector<WebString> PPB_FileChooser_Impl::ParseAcceptValue( + const std::string& accept_mime_types) { + if (accept_mime_types.empty()) + return std::vector<WebString>(); + std::vector<std::string> mime_type_list; + base::SplitString(accept_mime_types, ',', &mime_type_list); + std::vector<WebString> normalized_mime_type_list; + normalized_mime_type_list.reserve(mime_type_list.size()); + for (size_t i = 0; i < mime_type_list.size(); ++i) { + std::string mime_type = mime_type_list[i]; + TrimWhitespaceASCII(mime_type, TRIM_ALL, &mime_type); + if (mime_type.empty()) + continue; + if (mime_type.find_first_of('/') == std::string::npos) + continue; + StringToLowerASCII(&mime_type); + normalized_mime_type_list.push_back(WebString::fromUTF8(mime_type.data(), + mime_type.size())); + } + return normalized_mime_type_list; +} + } // namespace ppapi } // namespace webkit diff --git a/webkit/plugins/ppapi/ppb_file_chooser_impl.h b/webkit/plugins/ppapi/ppb_file_chooser_impl.h index 9157208..74c05d1 100644 --- a/webkit/plugins/ppapi/ppb_file_chooser_impl.h +++ b/webkit/plugins/ppapi/ppb_file_chooser_impl.h @@ -16,6 +16,10 @@ struct PP_CompletionCallback; +namespace WebKit { +class WebString; +} + namespace webkit { namespace ppapi { @@ -63,6 +67,12 @@ class PPB_FileChooser_Impl : public ::ppapi::Resource, const char* suggested_file_name, const PP_CompletionCallback& callback) OVERRIDE; + // Splits a comma-separated MIME type list |accept_mime_types|, trims the + // resultant split types, makes them lowercase, and returns them. + // Though this should be private, this is public for testing. + static std::vector<WebKit::WebString> ParseAcceptValue( + const std::string& accept_mime_types); + private: PP_FileChooserMode_Dev mode_; std::string accept_mime_types_; diff --git a/webkit/plugins/ppapi/ppb_file_chooser_impl_unittest.cc b/webkit/plugins/ppapi/ppb_file_chooser_impl_unittest.cc new file mode 100644 index 0000000..b780743 --- /dev/null +++ b/webkit/plugins/ppapi/ppb_file_chooser_impl_unittest.cc @@ -0,0 +1,39 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" +#include "webkit/plugins/ppapi/ppb_file_chooser_impl.h" + +using WebKit::WebString; + +namespace webkit { +namespace ppapi { + +TEST(PPB_FileChooser_ImplTest, ParseAcceptValue) { + std::vector<WebString> parsed; + + parsed = PPB_FileChooser_Impl::ParseAcceptValue(""); + EXPECT_EQ(0U, parsed.size()); + + parsed = PPB_FileChooser_Impl::ParseAcceptValue(" "); + EXPECT_EQ(0U, parsed.size()); + + parsed = PPB_FileChooser_Impl::ParseAcceptValue("a"); + EXPECT_EQ(0U, parsed.size()); + + parsed = PPB_FileChooser_Impl::ParseAcceptValue(",, "); + EXPECT_EQ(0U, parsed.size()); + + parsed = PPB_FileChooser_Impl::ParseAcceptValue( + "IMAGE/*,,!!,,text/plain, audio /(*) "); + EXPECT_EQ(3U, parsed.size()); + EXPECT_EQ("image/*", parsed[0]); + EXPECT_EQ("text/plain", parsed[1]); + // We don't need to reject invalid MIME tokens strictly. + EXPECT_EQ("audio /(*)", parsed[2]); +} + +} // namespace ppapi +} // namespace webkit diff --git a/webkit/tools/test_shell/test_shell.gypi b/webkit/tools/test_shell/test_shell.gypi index 694b260..b4b18a3 100644 --- a/webkit/tools/test_shell/test_shell.gypi +++ b/webkit/tools/test_shell/test_shell.gypi @@ -443,6 +443,7 @@ '../../plugins/ppapi/mock_resource.h', '../../plugins/ppapi/ppapi_unittest.cc', '../../plugins/ppapi/ppapi_unittest.h', + '../../plugins/ppapi/ppb_file_chooser_impl_unittest.cc', '../../plugins/ppapi/quota_file_io_unittest.cc', '../../plugins/ppapi/time_conversion_unittest.cc', '../../plugins/ppapi/url_request_info_unittest.cc', |