Skip to content

Commit

Permalink
Fix handling of UTF8_STRING
Browse files Browse the repository at this point in the history
Currently we use a default MIME type of US-ASCII when the type string isn't a
MIME type with a charset attribute. This means that unicode characters sent
as type UTF8_STRING get mangled during conversion. Fix this by special casing
UTF8_STRING in mime_utils.cc

R=oshima@chromium.org

Bug: 994470
Change-Id: I292bc42bce528449ebb1b9e8f5b72c9ea0e3968d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1757261
Reviewed-by: Mitsuru Oshima <oshima@chromium.org>
Commit-Queue: Fergus Dall <sidereal@google.com>
Auto-Submit: Fergus Dall <sidereal@google.com>
Cr-Commit-Position: refs/heads/master@{#687946}
  • Loading branch information
fergus-dall authored and Commit Bot committed Aug 17, 2019
1 parent 8776fa0 commit 6c759ab
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
8 changes: 1 addition & 7 deletions components/exo/data_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ constexpr char kTextHTML[] = "text/html";
constexpr char kUtfPrefix[] = "UTF";
constexpr char kEncoding16[] = "16";
constexpr char kEncodingASCII[] = "ASCII";
constexpr char kEncodingUTF8Legacy[] = "UTF8_STRING";
constexpr char kEncodingUTF8Charset[] = "UTF-8";

constexpr char kUTF16Unspecified[] = "UTF-16";
constexpr char kUTF16LittleEndian[] = "UTF-16LE";
Expand Down Expand Up @@ -250,11 +248,7 @@ void DataSource::GetDataForPreferredMimeTypes(
continue;

std::string charset;
// We special case UTF8_STRING to provide minimal handling of X11 apps.
if (mime_type == kEncodingUTF8Legacy)
charset = kEncodingUTF8Charset;
else
charset = GetCharset(mime_type);
charset = GetCharset(mime_type);
int new_rank = GetCharsetRank(charset);
if (new_rank < text_rank) {
text_mime = mime_type;
Expand Down
5 changes: 5 additions & 0 deletions components/exo/mime_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ namespace {

constexpr char kCharset[] = ";charset=";
constexpr char kDefaultCharset[] = "US-ASCII";
constexpr char kEncodingUTF8Charset[] = "UTF-8";

} // namespace

namespace exo {

std::string GetCharset(const std::string& mime_type) {
// We special case UTF8_STRING to provide minimal handling of X11 apps.
if (mime_type == kEncodingUTF8Legacy)
return std::string(kEncodingUTF8Charset);

auto pos = mime_type.find(kCharset);
if (pos == std::string::npos)
return std::string(kDefaultCharset);
Expand Down
2 changes: 2 additions & 0 deletions components/exo/mime_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace exo {

constexpr char kEncodingUTF8Legacy[] = "UTF8_STRING";

// Takes a text/* mime type and returns the name of the character set specified
// in the type. If no character set is specified, defaults to US-ASCII.
std::string GetCharset(const std::string& mime_type);
Expand Down
6 changes: 6 additions & 0 deletions components/exo/mime_utils_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace exo {
namespace {
using MimeUtilsTest = testing::Test;

TEST_F(MimeUtilsTest, LegacyString) {
std::string mime_type("UTF8_STRING");
std::string expected("UTF-8");
EXPECT_EQ(GetCharset(mime_type), expected);
}

TEST_F(MimeUtilsTest, CharsetNotPresent) {
std::string mime_type("text/plain");
std::string expected("US-ASCII");
Expand Down
26 changes: 26 additions & 0 deletions components/exo/seat_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ TEST_F(SeatTest, SetSelectionTextUTF8) {
EXPECT_EQ(clipboard, converted_data);
}

TEST_F(SeatTest, SetSelectionTextUTF8Legacy) {
Seat seat;

// UTF8 encoded data
const uint8_t data[] = {
0xe2, 0x9d, 0x84, // SNOWFLAKE
0xf0, 0x9f, 0x94, 0xa5 // FIRE
};
base::string16 converted_data;
EXPECT_TRUE(base::UTF8ToUTF16(reinterpret_cast<const char*>(data),
sizeof(data), &converted_data));

TestDataSourceDelegate delegate;
DataSource source(&delegate);
source.Offer("UTF8_STRING");
delegate.SetData(std::vector<uint8_t>(data, data + sizeof(data)));
seat.SetSelection(&source);

RunReadingTask();

base::string16 clipboard;
ui::Clipboard::GetForCurrentThread()->ReadText(ui::ClipboardType::kCopyPaste,
&clipboard);
EXPECT_EQ(clipboard, converted_data);
}

TEST_F(SeatTest, SetSelectionTextUTF16LE) {
Seat seat;

Expand Down

0 comments on commit 6c759ab

Please sign in to comment.