summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-04 03:36:36 +0000
committerestade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-03-04 03:36:36 +0000
commit3a2a5d201631f31800a889164cb84b3c6784b8c2 (patch)
tree52f45c58258845ad6b0a9a9591e15b3cc225337c /base
parent0bb1a6204af17f50ad0577f811a2c044b2bf62ff (diff)
downloadchromium_src-3a2a5d201631f31800a889164cb84b3c6784b8c2.zip
chromium_src-3a2a5d201631f31800a889164cb84b3c6784b8c2.tar.gz
chromium_src-3a2a5d201631f31800a889164cb84b3c6784b8c2.tar.bz2
Update clipboard classes to use string16 and FilePath instead of wstring.
Update callers as well. This patch builds on a patch by mark.a.lindner@gmail.com which can be found at http://codereview.chromium.org/28294 Review URL: http://codereview.chromium.org/27370 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@10860 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/clipboard.h18
-rw-r--r--base/clipboard_linux.cc16
-rw-r--r--base/clipboard_mac.mm38
-rw-r--r--base/clipboard_unittest.cc54
-rw-r--r--base/clipboard_win.cc45
-rw-r--r--base/pickle.cc26
-rw-r--r--base/pickle.h3
-rw-r--r--base/scoped_clipboard_writer.cc47
-rw-r--r--base/scoped_clipboard_writer.h17
-rw-r--r--base/string_util.h1
10 files changed, 158 insertions, 107 deletions
diff --git a/base/clipboard.h b/base/clipboard.h
index 631a516..5abf4774 100644
--- a/base/clipboard.h
+++ b/base/clipboard.h
@@ -9,7 +9,9 @@
#include <string>
#include <vector>
+#include "base/file_path.h"
#include "base/process.h"
+#include "base/string16.h"
#include "base/gfx/size.h"
#if defined(OS_MACOSX)
@@ -66,6 +68,8 @@ class Clipboard {
// CBF_FILES files char array representing multiple files.
// Filenames are separated by null characters and
// the final filename is double null terminated.
+ // The filenames are encoded in platform-specific
+ // encoding.
// CBF_WEBKIT none empty vector
// CBF_BITMAP pixels byte array
// size gfx::Size struct
@@ -94,21 +98,21 @@ class Clipboard {
bool IsFormatAvailable(FormatType format) const;
// Reads UNICODE text from the clipboard, if available.
- void ReadText(std::wstring* result) const;
+ void ReadText(string16* result) const;
// Reads ASCII text from the clipboard, if available.
void ReadAsciiText(std::string* result) const;
// Reads HTML from the clipboard, if available.
- void ReadHTML(std::wstring* markup, std::string* src_url) const;
+ void ReadHTML(string16* markup, std::string* src_url) const;
// Reads a bookmark from the clipboard, if available.
- void ReadBookmark(std::wstring* title, std::string* url) const;
+ void ReadBookmark(string16* title, std::string* url) const;
// Reads a file or group of files from the clipboard, if available, into the
// out parameter.
- void ReadFile(std::wstring* file) const;
- void ReadFiles(std::vector<std::wstring>* files) const;
+ void ReadFile(FilePath* file) const;
+ void ReadFiles(std::vector<FilePath>* files) const;
// Get format Identifiers for various types.
static FormatType GetUrlFormatType();
@@ -171,8 +175,8 @@ class Clipboard {
// Safely write to system clipboard. Free |handle| on failure.
void WriteToClipboard(FormatType format, HANDLE handle);
- static void ParseBookmarkClipboardFormat(const std::wstring& bookmark,
- std::wstring* title,
+ static void ParseBookmarkClipboardFormat(const string16& bookmark,
+ string16* title,
std::string* url);
// Free a handle depending on its type (as intuited from format)
diff --git a/base/clipboard_linux.cc b/base/clipboard_linux.cc
index e535942..17ecf6c 100644
--- a/base/clipboard_linux.cc
+++ b/base/clipboard_linux.cc
@@ -200,7 +200,7 @@ bool Clipboard::IsFormatAvailable(Clipboard::FormatType format) const {
return retval;
}
-void Clipboard::ReadText(std::wstring* result) const {
+void Clipboard::ReadText(string16* result) const {
result->clear();
gchar* text = gtk_clipboard_wait_for_text(clipboard_);
@@ -208,7 +208,7 @@ void Clipboard::ReadText(std::wstring* result) const {
return;
// TODO(estade): do we want to handle the possible error here?
- UTF8ToWide(text, strlen(text), result);
+ UTF8ToUTF16(text, strlen(text), result);
g_free(text);
}
@@ -223,8 +223,12 @@ void Clipboard::ReadAsciiText(std::string* result) const {
g_free(text);
}
+void Clipboard::ReadFile(FilePath* file) const {
+ *file = FilePath();
+}
+
// TODO(estade): handle different charsets.
-void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
+void Clipboard::ReadHTML(string16* markup, std::string* src_url) const {
markup->clear();
GtkSelectionData* data = gtk_clipboard_wait_for_contents(clipboard_,
@@ -233,9 +237,9 @@ void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
if (!data)
return;
- UTF8ToWide(reinterpret_cast<char*>(data->data),
- strlen(reinterpret_cast<char*>(data->data)),
- markup);
+ UTF8ToUTF16(reinterpret_cast<char*>(data->data),
+ strlen(reinterpret_cast<char*>(data->data)),
+ markup);
gtk_selection_data_free(data);
}
diff --git a/base/clipboard_mac.mm b/base/clipboard_mac.mm
index 592b4c6..f2b6294 100644
--- a/base/clipboard_mac.mm
+++ b/base/clipboard_mac.mm
@@ -140,13 +140,13 @@ bool Clipboard::IsFormatAvailable(NSString* format) const {
return [types containsObject:format];
}
-void Clipboard::ReadText(std::wstring* result) const {
+void Clipboard::ReadText(string16* result) const {
NSPasteboard* pb = GetPasteboard();
NSString* contents = [pb stringForType:NSStringPboardType];
- UTF8ToWide([contents UTF8String],
- [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
- result);
+ UTF8ToUTF16([contents UTF8String],
+ [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
+ result);
}
void Clipboard::ReadAsciiText(std::string* result) const {
@@ -159,7 +159,7 @@ void Clipboard::ReadAsciiText(std::string* result) const {
result->assign([contents UTF8String]);
}
-void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
+void Clipboard::ReadHTML(string16* markup, std::string* src_url) const {
if (markup) {
NSPasteboard* pb = GetPasteboard();
NSArray *supportedTypes = [NSArray arrayWithObjects:NSHTMLPboardType,
@@ -167,9 +167,9 @@ void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
nil];
NSString *bestType = [pb availableTypeFromArray:supportedTypes];
NSString* contents = [pb stringForType:bestType];
- UTF8ToWide([contents UTF8String],
- [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
- markup);
+ UTF8ToUTF16([contents UTF8String],
+ [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
+ markup);
}
// TODO(avi): src_url?
@@ -177,14 +177,14 @@ void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
src_url->clear();
}
-void Clipboard::ReadBookmark(std::wstring* title, std::string* url) const {
+void Clipboard::ReadBookmark(string16* title, std::string* url) const {
NSPasteboard* pb = GetPasteboard();
if (title) {
NSString* contents = [pb stringForType:kUTTypeURLName];
- UTF8ToWide([contents UTF8String],
- [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
- title);
+ UTF8ToUTF16([contents UTF8String],
+ [contents lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
+ title);
}
if (url) {
@@ -196,22 +196,22 @@ void Clipboard::ReadBookmark(std::wstring* title, std::string* url) const {
}
}
-void Clipboard::ReadFile(std::wstring* file) const {
+void Clipboard::ReadFile(FilePath* file) const {
if (!file) {
NOTREACHED();
return;
}
- file->clear();
- std::vector<std::wstring> files;
+ *file = FilePath();
+ std::vector<FilePath> files;
ReadFiles(&files);
// Take the first file, if available.
if (!files.empty())
- file->assign(files[0]);
+ *file = files[0];
}
-void Clipboard::ReadFiles(std::vector<std::wstring>* files) const {
+void Clipboard::ReadFiles(std::vector<FilePath>* files) const {
if (!files) {
NOTREACHED();
return;
@@ -223,8 +223,8 @@ void Clipboard::ReadFiles(std::vector<std::wstring>* files) const {
NSArray* fileList = [pb propertyListForType:NSFilenamesPboardType];
for (unsigned int i = 0; i < [fileList count]; ++i) {
- std::wstring file = UTF8ToWide([[fileList objectAtIndex:i] UTF8String]);
- files->push_back(file);
+ std::string file = [[fileList objectAtIndex:i] UTF8String];
+ files->push_back(FilePath(file));
}
}
diff --git a/base/clipboard_unittest.cc b/base/clipboard_unittest.cc
index 1015753..6a0ca09 100644
--- a/base/clipboard_unittest.cc
+++ b/base/clipboard_unittest.cc
@@ -33,12 +33,12 @@ TEST_F(ClipboardTest, ClearTest) {
{
ScopedClipboardWriter clipboard_writer(&clipboard);
- clipboard_writer.WriteText(std::wstring(L"clear me"));
+ clipboard_writer.WriteText(ASCIIToUTF16("clear me"));
}
{
ScopedClipboardWriter clipboard_writer(&clipboard);
- clipboard_writer.WriteHTML(std::wstring(L"<b>broom</b>"), "");
+ clipboard_writer.WriteHTML(ASCIIToUTF16("<b>broom</b>"), "");
}
EXPECT_FALSE(clipboard.IsFormatAvailable(
@@ -50,7 +50,7 @@ TEST_F(ClipboardTest, ClearTest) {
TEST_F(ClipboardTest, TextTest) {
Clipboard clipboard;
- std::wstring text(L"This is a wstring!#$"), text_result;
+ string16 text(ASCIIToUTF16("This is a string16!#$")), text_result;
std::string ascii_text;
{
@@ -63,15 +63,16 @@ TEST_F(ClipboardTest, TextTest) {
EXPECT_TRUE(clipboard.IsFormatAvailable(
Clipboard::GetPlainTextFormatType()));
clipboard.ReadText(&text_result);
+
EXPECT_EQ(text, text_result);
clipboard.ReadAsciiText(&ascii_text);
- EXPECT_EQ(WideToUTF8(text), ascii_text);
+ EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
}
TEST_F(ClipboardTest, HTMLTest) {
Clipboard clipboard;
- std::wstring markup(L"<string>Hi!</string>"), markup_result;
+ string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result;
std::string url("http://www.example.com/"), url_result;
{
@@ -93,7 +94,8 @@ TEST_F(ClipboardTest, HTMLTest) {
TEST_F(ClipboardTest, TrickyHTMLTest) {
Clipboard clipboard;
- std::wstring markup(L"<em>Bye!<!--EndFragment --></em>"), markup_result;
+ string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")),
+ markup_result;
std::string url, url_result;
{
@@ -117,7 +119,7 @@ TEST_F(ClipboardTest, TrickyHTMLTest) {
TEST_F(ClipboardTest, BookmarkTest) {
Clipboard clipboard;
- std::wstring title(L"The Example Company"), title_result;
+ string16 title(ASCIIToUTF16("The Example Company")), title_result;
std::string url("http://www.example.com/"), url_result;
{
@@ -136,8 +138,8 @@ TEST_F(ClipboardTest, BookmarkTest) {
TEST_F(ClipboardTest, MultiFormatTest) {
Clipboard clipboard;
- std::wstring text(L"Hi!"), text_result;
- std::wstring markup(L"<strong>Hi!</string>"), markup_result;
+ string16 text(ASCIIToUTF16("Hi!")), text_result;
+ string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result;
std::string url("http://www.example.com/"), url_result;
std::string ascii_text;
@@ -163,7 +165,7 @@ TEST_F(ClipboardTest, MultiFormatTest) {
clipboard.ReadText(&text_result);
EXPECT_EQ(text, text_result);
clipboard.ReadAsciiText(&ascii_text);
- EXPECT_EQ(WideToUTF8(text), ascii_text);
+ EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
}
// TODO(estade): Port the following tests (decide what targets we use for files)
@@ -173,11 +175,11 @@ TEST_F(ClipboardTest, MultiFormatTest) {
TEST_F(ClipboardTest, FileTest) {
Clipboard clipboard;
#if defined(OS_WIN)
- std::wstring file = L"C:\\Downloads\\My Downloads\\A Special File.txt";
+ FilePath file(L"C:\\Downloads\\My Downloads\\A Special File.txt");
#elif defined(OS_MACOSX)
// OS X will print a warning message if we stick a non-existant file on the
// clipboard.
- std::wstring file = L"/usr/bin/make";
+ FilePath file("/usr/bin/make");
#endif // defined(OS_MACOSX)
{
@@ -185,26 +187,26 @@ TEST_F(ClipboardTest, FileTest) {
clipboard_writer.WriteFile(file);
}
- std::wstring out_file;
+ FilePath out_file;
clipboard.ReadFile(&out_file);
- EXPECT_EQ(file, out_file);
+ EXPECT_EQ(file.value(), out_file.value());
}
TEST_F(ClipboardTest, MultipleFilesTest) {
Clipboard clipboard;
#if defined(OS_WIN)
- std::wstring file1 = L"C:\\Downloads\\My Downloads\\File 1.exe";
- std::wstring file2 = L"C:\\Downloads\\My Downloads\\File 2.pdf";
- std::wstring file3 = L"C:\\Downloads\\My Downloads\\File 3.doc";
+ FilePath file1(L"C:\\Downloads\\My Downloads\\File 1.exe");
+ FilePath file2(L"C:\\Downloads\\My Downloads\\File 2.pdf");
+ FilePath file3(L"C:\\Downloads\\My Downloads\\File 3.doc");
#elif defined(OS_MACOSX)
// OS X will print a warning message if we stick a non-existant file on the
// clipboard.
- std::wstring file1 = L"/usr/bin/make";
- std::wstring file2 = L"/usr/bin/man";
- std::wstring file3 = L"/usr/bin/perl";
+ FilePath file1("/usr/bin/make");
+ FilePath file2("/usr/bin/man");
+ FilePath file3("/usr/bin/perl");
#endif // defined(OS_MACOSX)
- std::vector<std::wstring> files;
+ std::vector<FilePath> files;
files.push_back(file1);
files.push_back(file2);
files.push_back(file3);
@@ -214,12 +216,12 @@ TEST_F(ClipboardTest, MultipleFilesTest) {
clipboard_writer.WriteFiles(files);
}
- std::vector<std::wstring> out_files;
+ std::vector<FilePath> out_files;
clipboard.ReadFiles(&out_files);
EXPECT_EQ(files.size(), out_files.size());
for (size_t i = 0; i < out_files.size(); ++i)
- EXPECT_EQ(files[i], out_files[i]);
+ EXPECT_EQ(files[i].value(), out_files[i].value());
}
#endif // !defined(OS_LINUX)
@@ -227,10 +229,10 @@ TEST_F(ClipboardTest, MultipleFilesTest) {
TEST_F(ClipboardTest, HyperlinkTest) {
Clipboard clipboard;
- std::wstring title(L"The Example Company"), title_result;
+ string16 title(ASCIIToUTF16("The Example Company")), title_result;
std::string url("http://www.example.com/"), url_result;
- std::wstring html(L"<a href=\"http://www.example.com/\">"
- L"The Example Company</a>"), html_result;
+ string16 html(ASCIIToUTF16("<a href=\"http://www.example.com/\">"
+ "The Example Company</a>")), html_result;
{
ScopedClipboardWriter clipboard_writer(&clipboard);
diff --git a/base/clipboard_win.cc b/base/clipboard_win.cc
index 3efe7d36..b765f5f 100644
--- a/base/clipboard_win.cc
+++ b/base/clipboard_win.cc
@@ -11,6 +11,7 @@
#include <shellapi.h>
#include "base/clipboard_util.h"
+#include "base/lock.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/shared_memory.h"
@@ -169,8 +170,8 @@ void Clipboard::WriteObjects(const ObjectMap& objects,
}
void Clipboard::WriteText(const char* text_data, size_t text_len) {
- std::wstring text;
- UTF8ToWide(text_data, text_len, &text);
+ string16 text;
+ UTF8ToUTF16(text_data, text_len, &text);
HGLOBAL glob = CreateGlobalData(text);
WriteToClipboard(CF_UNICODETEXT, glob);
@@ -200,7 +201,7 @@ void Clipboard::WriteBookmark(const char* title_data,
bookmark.append(1, L'\n');
bookmark.append(url_data, url_len);
- std::wstring wide_bookmark = UTF8ToWide(bookmark);
+ string16 wide_bookmark = UTF8ToWide(bookmark);
HGLOBAL glob = CreateGlobalData(wide_bookmark);
WriteToClipboard(GetUrlWFormatType(), glob);
@@ -351,10 +352,9 @@ void Clipboard::WriteBitmapFromHandle(HBITMAP source_hbitmap,
// invokes a paste command (in a Windows explorer shell, for example), the files
// will be copied to the paste location.
void Clipboard::WriteFiles(const char* file_data, size_t file_len) {
- std::wstring filenames(UTF8ToWide(std::string(file_data, file_len)));
// Calculate the amount of space we'll need store the strings and
// a DROPFILES struct.
- size_t bytes = sizeof(DROPFILES) + filenames.length() * sizeof(wchar_t);
+ size_t bytes = sizeof(DROPFILES) + file_len;
HANDLE hdata = ::GlobalAlloc(GMEM_MOVEABLE, bytes);
if (!hdata)
@@ -365,8 +365,7 @@ void Clipboard::WriteFiles(const char* file_data, size_t file_len) {
drop_files->pFiles = sizeof(DROPFILES);
drop_files->fWide = TRUE;
- memcpy(data + sizeof DROPFILES, filenames.c_str(),
- filenames.length() * sizeof(wchar_t));
+ memcpy(data + sizeof(DROPFILES), file_data, file_len);
::GlobalUnlock(hdata);
WriteToClipboard(CF_HDROP, hdata);
@@ -384,7 +383,7 @@ bool Clipboard::IsFormatAvailable(unsigned int format) const {
return ::IsClipboardFormatAvailable(format) != FALSE;
}
-void Clipboard::ReadText(std::wstring* result) const {
+void Clipboard::ReadText(string16* result) const {
if (!result) {
NOTREACHED();
return;
@@ -401,7 +400,7 @@ void Clipboard::ReadText(std::wstring* result) const {
if (!data)
return;
- result->assign(static_cast<const wchar_t*>(::GlobalLock(data)));
+ result->assign(static_cast<const char16*>(::GlobalLock(data)));
::GlobalUnlock(data);
}
@@ -426,7 +425,7 @@ void Clipboard::ReadAsciiText(std::string* result) const {
::GlobalUnlock(data);
}
-void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
+void Clipboard::ReadHTML(string16* markup, std::string* src_url) const {
if (markup)
markup->clear();
@@ -450,7 +449,7 @@ void Clipboard::ReadHTML(std::wstring* markup, std::string* src_url) const {
markup->assign(UTF8ToWide(markup_utf8));
}
-void Clipboard::ReadBookmark(std::wstring* title, std::string* url) const {
+void Clipboard::ReadBookmark(string16* title, std::string* url) const {
if (title)
title->clear();
@@ -466,30 +465,30 @@ void Clipboard::ReadBookmark(std::wstring* title, std::string* url) const {
if (!data)
return;
- std::wstring bookmark(static_cast<const wchar_t*>(::GlobalLock(data)));
+ string16 bookmark(static_cast<const char16*>(::GlobalLock(data)));
::GlobalUnlock(data);
ParseBookmarkClipboardFormat(bookmark, title, url);
}
// Read a file in HDROP format from the clipboard.
-void Clipboard::ReadFile(std::wstring* file) const {
+void Clipboard::ReadFile(FilePath* file) const {
if (!file) {
NOTREACHED();
return;
}
- file->clear();
- std::vector<std::wstring> files;
+ *file = FilePath();
+ std::vector<FilePath> files;
ReadFiles(&files);
// Take the first file, if available.
if (!files.empty())
- file->assign(files[0]);
+ *file = files[0];
}
// Read a set of files in HDROP format from the clipboard.
-void Clipboard::ReadFiles(std::vector<std::wstring>* files) const {
+void Clipboard::ReadFiles(std::vector<FilePath>* files) const {
if (!files) {
NOTREACHED();
return;
@@ -513,16 +512,16 @@ void Clipboard::ReadFiles(std::vector<std::wstring>* files) const {
int size = ::DragQueryFile(drop, i, NULL, 0) + 1;
std::wstring file;
::DragQueryFile(drop, i, WriteInto(&file, size), size);
- files->push_back(file);
+ files->push_back(FilePath(file));
}
}
}
// static
-void Clipboard::ParseBookmarkClipboardFormat(const std::wstring& bookmark,
- std::wstring* title,
+void Clipboard::ParseBookmarkClipboardFormat(const string16& bookmark,
+ string16* title,
std::string* url) {
- const wchar_t* const kDelim = L"\r\n";
+ const string16 kDelim = ASCIIToUTF16("\r\n");
const size_t title_end = bookmark.find_first_of(kDelim);
if (title)
@@ -530,8 +529,8 @@ void Clipboard::ParseBookmarkClipboardFormat(const std::wstring& bookmark,
if (url) {
const size_t url_start = bookmark.find_first_not_of(kDelim, title_end);
- if (url_start != std::wstring::npos)
- *url = WideToUTF8(bookmark.substr(url_start, std::wstring::npos));
+ if (url_start != string16::npos)
+ *url = UTF16ToUTF8(bookmark.substr(url_start, string16::npos));
}
}
diff --git a/base/pickle.cc b/base/pickle.cc
index aac59df..c2b3341 100644
--- a/base/pickle.cc
+++ b/base/pickle.cc
@@ -218,6 +218,22 @@ bool Pickle::ReadWString(void** iter, std::wstring* result) const {
return true;
}
+bool Pickle::ReadString16(void** iter, string16* result) const {
+ DCHECK(iter);
+
+ int len;
+ if (!ReadLength(iter, &len))
+ return false;
+ if (!IteratorHasRoomFor(*iter, len))
+ return false;
+
+ char16* chars = reinterpret_cast<char16*>(*iter);
+ result->assign(chars, len);
+
+ UpdateIter(iter, len * sizeof(char16));
+ return true;
+}
+
bool Pickle::ReadBytes(void** iter, const char** data, int length) const {
DCHECK(iter);
DCHECK(data);
@@ -290,7 +306,15 @@ bool Pickle::WriteWString(const std::wstring& value) {
return false;
return WriteBytes(value.data(),
- static_cast<int>(value.size() * sizeof(value.data()[0])));
+ static_cast<int>(value.size() * sizeof(wchar_t)));
+}
+
+bool Pickle::WriteString16(const string16& value) {
+ if (!WriteInt(static_cast<int>(value.size())))
+ return false;
+
+ return WriteBytes(value.data(),
+ static_cast<int>(value.size()) * sizeof(char16));
}
bool Pickle::WriteData(const char* data, int length) {
diff --git a/base/pickle.h b/base/pickle.h
index 66340ca..9f03662 100644
--- a/base/pickle.h
+++ b/base/pickle.h
@@ -9,6 +9,7 @@
#include "base/basictypes.h"
#include "base/logging.h"
+#include "base/string16.h"
#include "testing/gtest/include/gtest/gtest_prod.h"
// This class provides facilities for basic binary value packing and unpacking.
@@ -72,6 +73,7 @@ class Pickle {
bool ReadIntPtr(void** iter, intptr_t* result) const;
bool ReadString(void** iter, std::string* result) const;
bool ReadWString(void** iter, std::wstring* result) const;
+ bool ReadString16(void** iter, string16* result) const;
bool ReadData(void** iter, const char** data, int* length) const;
bool ReadBytes(void** iter, const char** data, int length) const;
@@ -106,6 +108,7 @@ class Pickle {
}
bool WriteString(const std::string& value);
bool WriteWString(const std::wstring& value);
+ bool WriteString16(const string16& value);
bool WriteData(const char* data, int length);
bool WriteBytes(const void* data, int data_len);
diff --git a/base/scoped_clipboard_writer.cc b/base/scoped_clipboard_writer.cc
index f940462..5114a22 100644
--- a/base/scoped_clipboard_writer.cc
+++ b/base/scoped_clipboard_writer.cc
@@ -19,11 +19,11 @@ ScopedClipboardWriter::~ScopedClipboardWriter() {
clipboard_->WriteObjects(objects_);
}
-void ScopedClipboardWriter::WriteText(const std::wstring& text) {
+void ScopedClipboardWriter::WriteText(const string16& text) {
if (text.empty())
return;
- std::string utf8_text = WideToUTF8(text);
+ std::string utf8_text = UTF16ToUTF8(text);
Clipboard::ObjectMapParams parameters;
parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(),
@@ -31,12 +31,12 @@ void ScopedClipboardWriter::WriteText(const std::wstring& text) {
objects_[Clipboard::CBF_TEXT] = parameters;
}
-void ScopedClipboardWriter::WriteHTML(const std::wstring& markup,
+void ScopedClipboardWriter::WriteHTML(const string16& markup,
const std::string& source_url) {
if (markup.empty())
return;
- std::string utf8_markup = WideToUTF8(markup);
+ std::string utf8_markup = UTF16ToUTF8(markup);
Clipboard::ObjectMapParams parameters;
parameters.push_back(
@@ -50,12 +50,12 @@ void ScopedClipboardWriter::WriteHTML(const std::wstring& markup,
objects_[Clipboard::CBF_HTML] = parameters;
}
-void ScopedClipboardWriter::WriteBookmark(const std::wstring& bookmark_title,
+void ScopedClipboardWriter::WriteBookmark(const string16& bookmark_title,
const std::string& url) {
if (bookmark_title.empty() || url.empty())
return;
- std::string utf8_markup = WideToUTF8(bookmark_title);
+ std::string utf8_markup = UTF16ToUTF8(bookmark_title);
Clipboard::ObjectMapParams parameters;
parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(),
@@ -64,12 +64,12 @@ void ScopedClipboardWriter::WriteBookmark(const std::wstring& bookmark_title,
objects_[Clipboard::CBF_BOOKMARK] = parameters;
}
-void ScopedClipboardWriter::WriteHyperlink(const std::wstring& link_text,
+void ScopedClipboardWriter::WriteHyperlink(const string16& link_text,
const std::string& url) {
if (link_text.empty() || url.empty())
return;
- std::string utf8_markup = WideToUTF8(link_text);
+ std::string utf8_markup = UTF16ToUTF8(link_text);
Clipboard::ObjectMapParams parameters;
parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(),
@@ -78,29 +78,38 @@ void ScopedClipboardWriter::WriteHyperlink(const std::wstring& link_text,
objects_[Clipboard::CBF_LINK] = parameters;
}
-void ScopedClipboardWriter::WriteFile(const std::wstring& file) {
- WriteFiles(std::vector<std::wstring>(1, file));
+void ScopedClipboardWriter::WriteFile(const FilePath& file) {
+ WriteFiles(std::vector<FilePath>(1, file));
}
// Save the filenames as a string separated by nulls and terminated with an
// extra null.
-void ScopedClipboardWriter::WriteFiles(const std::vector<std::wstring>& files) {
+void ScopedClipboardWriter::WriteFiles(const std::vector<FilePath>& files) {
if (files.empty())
return;
Clipboard::ObjectMapParam parameter;
- for (std::vector<std::wstring>::const_iterator iter = files.begin();
+ for (std::vector<FilePath>::const_iterator iter = files.begin();
iter != files.end(); ++iter) {
- std::string filename = WideToUTF8(*iter);
- for (std::string::const_iterator filename_iter = filename.begin();
- filename_iter != filename.end(); ++filename_iter) {
- parameter.push_back(*filename_iter);
- }
- parameter.push_back('\0');
+ FilePath filepath = *iter;
+ FilePath::StringType filename = filepath.value();
+
+ size_t data_length = filename.length() * sizeof(FilePath::CharType);
+ const char* data = reinterpret_cast<const char*>(filename.data());
+ const char* data_end = data + data_length;
+
+ for (const char* ch = data; ch < data_end; ++ch)
+ parameter.push_back(*ch);
+
+ // NUL-terminate the string.
+ for (size_t i = 0; i < sizeof(FilePath::CharType); ++i)
+ parameter.push_back('\0');
}
- parameter.push_back('\0');
+ // NUL-terminate the string list.
+ for (size_t i = 0; i < sizeof(FilePath::CharType); ++i)
+ parameter.push_back('\0');
Clipboard::ObjectMapParams parameters;
parameters.push_back(parameter);
diff --git a/base/scoped_clipboard_writer.h b/base/scoped_clipboard_writer.h
index 7bfedf4..a62a94c 100644
--- a/base/scoped_clipboard_writer.h
+++ b/base/scoped_clipboard_writer.h
@@ -10,7 +10,12 @@
#ifndef BASE_SCOPED_CLIPBOARD_WRITER_H_
#define BASE_SCOPED_CLIPBOARD_WRITER_H_
+#include <string>
+#include <vector>
+
#include "base/clipboard.h"
+#include "base/file_path.h"
+#include "base/string16.h"
// This class is a wrapper for |Clipboard| that handles packing data
// into a Clipboard::ObjectMap.
@@ -24,24 +29,24 @@ class ScopedClipboardWriter {
~ScopedClipboardWriter();
// Converts |text| to UTF-8 and adds it to the clipboard.
- void WriteText(const std::wstring& text);
+ void WriteText(const string16& text);
// Adds HTML to the clipboard. The url parameter is optional, but especially
// useful if the HTML fragment contains relative links.
- void WriteHTML(const std::wstring& markup, const std::string& source_url);
+ void WriteHTML(const string16& markup, const std::string& source_url);
// Adds a bookmark to the clipboard.
- void WriteBookmark(const std::wstring& bookmark_title,
+ void WriteBookmark(const string16& bookmark_title,
const std::string& url);
// Adds both a bookmark and an HTML hyperlink to the clipboard. It is a
// convenience wrapper around WriteBookmark and WriteHTML. |link_text| is
// used as the bookmark title.
- void WriteHyperlink(const std::wstring& link_text, const std::string& url);
+ void WriteHyperlink(const string16& link_text, const std::string& url);
// Adds a file or group of files to the clipboard.
- void WriteFile(const std::wstring& file);
- void WriteFiles(const std::vector<std::wstring>& files);
+ void WriteFile(const FilePath& file);
+ void WriteFiles(const std::vector<FilePath>& files);
// Used by WebKit to determine whether WebKit wrote the clipboard last
void WriteWebSmartPaste();
diff --git a/base/string_util.h b/base/string_util.h
index 26c42e5..1247cd5 100644
--- a/base/string_util.h
+++ b/base/string_util.h
@@ -596,4 +596,5 @@ bool MatchPattern(const std::string& string, const std::string& pattern);
// std::numeric_limits<size_t>::max() / 2
std::string HexEncode(const void* bytes, size_t size);
+
#endif // BASE_STRING_UTIL_H_