OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // This file implements the ScopedClipboardWriter class. Documentation on its | 5 // This file implements the ScopedClipboardWriter class. Documentation on its |
6 // purpose can be found in base/scoped_clipboard_writer.h. Documentation on the | 6 // purpose can be found in base/scoped_clipboard_writer.h. Documentation on the |
7 // format of the parameters for each clipboard target can be found in | 7 // format of the parameters for each clipboard target can be found in |
8 // base/clipboard.h. | 8 // base/clipboard.h. |
9 #include "base/scoped_clipboard_writer.h" | 9 #include "base/scoped_clipboard_writer.h" |
10 | 10 |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 | 12 |
13 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard) | 13 ScopedClipboardWriter::ScopedClipboardWriter(Clipboard* clipboard) |
14 : clipboard_(clipboard) { | 14 : clipboard_(clipboard) { |
15 } | 15 } |
16 | 16 |
17 ScopedClipboardWriter::~ScopedClipboardWriter() { | 17 ScopedClipboardWriter::~ScopedClipboardWriter() { |
18 if (!objects_.empty() && clipboard_) | 18 if (!objects_.empty() && clipboard_) |
19 clipboard_->WriteObjects(objects_); | 19 clipboard_->WriteObjects(objects_); |
20 } | 20 } |
21 | 21 |
22 void ScopedClipboardWriter::WriteText(const std::wstring& text) { | 22 void ScopedClipboardWriter::WriteText(const string16& text) { |
23 if (text.empty()) | 23 if (text.empty()) |
24 return; | 24 return; |
25 | 25 |
26 std::string utf8_text = WideToUTF8(text); | 26 std::string utf8_text = UTF16ToUTF8(text); |
27 | 27 |
28 Clipboard::ObjectMapParams parameters; | 28 Clipboard::ObjectMapParams parameters; |
29 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), | 29 parameters.push_back(Clipboard::ObjectMapParam(utf8_text.begin(), |
30 utf8_text.end())); | 30 utf8_text.end())); |
31 objects_[Clipboard::CBF_TEXT] = parameters; | 31 objects_[Clipboard::CBF_TEXT] = parameters; |
32 } | 32 } |
33 | 33 |
34 void ScopedClipboardWriter::WriteHTML(const std::wstring& markup, | 34 void ScopedClipboardWriter::WriteHTML(const string16& markup, |
35 const std::string& source_url) { | 35 const std::string& source_url) { |
36 if (markup.empty()) | 36 if (markup.empty()) |
37 return; | 37 return; |
38 | 38 |
39 std::string utf8_markup = WideToUTF8(markup); | 39 std::string utf8_markup = UTF16ToUTF8(markup); |
40 | 40 |
41 Clipboard::ObjectMapParams parameters; | 41 Clipboard::ObjectMapParams parameters; |
42 parameters.push_back( | 42 parameters.push_back( |
43 Clipboard::ObjectMapParam(utf8_markup.begin(), | 43 Clipboard::ObjectMapParam(utf8_markup.begin(), |
44 utf8_markup.end())); | 44 utf8_markup.end())); |
45 if (!source_url.empty()) { | 45 if (!source_url.empty()) { |
46 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(), | 46 parameters.push_back(Clipboard::ObjectMapParam(source_url.begin(), |
47 source_url.end())); | 47 source_url.end())); |
48 } | 48 } |
49 | 49 |
50 objects_[Clipboard::CBF_HTML] = parameters; | 50 objects_[Clipboard::CBF_HTML] = parameters; |
51 } | 51 } |
52 | 52 |
53 void ScopedClipboardWriter::WriteBookmark(const std::wstring& bookmark_title, | 53 void ScopedClipboardWriter::WriteBookmark(const string16& bookmark_title, |
54 const std::string& url) { | 54 const std::string& url) { |
55 if (bookmark_title.empty() || url.empty()) | 55 if (bookmark_title.empty() || url.empty()) |
56 return; | 56 return; |
57 | 57 |
58 std::string utf8_markup = WideToUTF8(bookmark_title); | 58 std::string utf8_markup = UTF16ToUTF8(bookmark_title); |
59 | 59 |
60 Clipboard::ObjectMapParams parameters; | 60 Clipboard::ObjectMapParams parameters; |
61 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), | 61 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), |
62 utf8_markup.end())); | 62 utf8_markup.end())); |
63 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); | 63 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); |
64 objects_[Clipboard::CBF_BOOKMARK] = parameters; | 64 objects_[Clipboard::CBF_BOOKMARK] = parameters; |
65 } | 65 } |
66 | 66 |
67 void ScopedClipboardWriter::WriteHyperlink(const std::wstring& link_text, | 67 void ScopedClipboardWriter::WriteHyperlink(const string16& link_text, |
68 const std::string& url) { | 68 const std::string& url) { |
69 if (link_text.empty() || url.empty()) | 69 if (link_text.empty() || url.empty()) |
70 return; | 70 return; |
71 | 71 |
72 std::string utf8_markup = WideToUTF8(link_text); | 72 std::string utf8_markup = UTF16ToUTF8(link_text); |
73 | 73 |
74 Clipboard::ObjectMapParams parameters; | 74 Clipboard::ObjectMapParams parameters; |
75 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), | 75 parameters.push_back(Clipboard::ObjectMapParam(utf8_markup.begin(), |
76 utf8_markup.end())); | 76 utf8_markup.end())); |
77 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); | 77 parameters.push_back(Clipboard::ObjectMapParam(url.begin(), url.end())); |
78 objects_[Clipboard::CBF_LINK] = parameters; | 78 objects_[Clipboard::CBF_LINK] = parameters; |
79 } | 79 } |
80 | 80 |
81 void ScopedClipboardWriter::WriteFile(const std::wstring& file) { | 81 void ScopedClipboardWriter::WriteFile(const FilePath& file) { |
82 WriteFiles(std::vector<std::wstring>(1, file)); | 82 WriteFiles(std::vector<FilePath>(1, file)); |
83 } | 83 } |
84 | 84 |
85 // Save the filenames as a string separated by nulls and terminated with an | 85 // Save the filenames as a string separated by nulls and terminated with an |
86 // extra null. | 86 // extra null. |
87 void ScopedClipboardWriter::WriteFiles(const std::vector<std::wstring>& files) { | 87 void ScopedClipboardWriter::WriteFiles(const std::vector<FilePath>& files) { |
88 if (files.empty()) | 88 if (files.empty()) |
89 return; | 89 return; |
90 | 90 |
91 Clipboard::ObjectMapParam parameter; | 91 Clipboard::ObjectMapParam parameter; |
92 | 92 |
93 for (std::vector<std::wstring>::const_iterator iter = files.begin(); | 93 for (std::vector<FilePath>::const_iterator iter = files.begin(); |
94 iter != files.end(); ++iter) { | 94 iter != files.end(); ++iter) { |
95 std::string filename = WideToUTF8(*iter); | 95 FilePath filepath = *iter; |
96 for (std::string::const_iterator filename_iter = filename.begin(); | 96 FilePath::StringType filename = filepath.value(); |
97 filename_iter != filename.end(); ++filename_iter) { | 97 |
98 parameter.push_back(*filename_iter); | 98 size_t data_length = filename.length() * sizeof(FilePath::CharType); |
99 } | 99 const char *data = reinterpret_cast<const char *>(filename.data()); |
100 parameter.push_back('\0'); | 100 const char *data_end = data + data_length; |
| 101 |
| 102 for (const char *ch = data; ch < data_end; ++ch) |
| 103 parameter.push_back(*ch); |
| 104 |
| 105 // NUL-terminate the string. |
| 106 for (size_t i = 0; i < sizeof(FilePath::CharType); ++i) |
| 107 parameter.push_back('\0'); |
101 } | 108 } |
102 | 109 |
103 parameter.push_back('\0'); | 110 // NUL-terminate the string list. |
| 111 for (size_t i = 0; i < sizeof(FilePath::CharType); ++i) |
| 112 parameter.push_back('\0'); |
104 | 113 |
105 Clipboard::ObjectMapParams parameters; | 114 Clipboard::ObjectMapParams parameters; |
106 parameters.push_back(parameter); | 115 parameters.push_back(parameter); |
107 objects_[Clipboard::CBF_FILES] = parameters; | 116 objects_[Clipboard::CBF_FILES] = parameters; |
108 } | 117 } |
109 | 118 |
110 void ScopedClipboardWriter::WriteWebSmartPaste() { | 119 void ScopedClipboardWriter::WriteWebSmartPaste() { |
111 objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams(); | 120 objects_[Clipboard::CBF_WEBKIT] = Clipboard::ObjectMapParams(); |
112 } | 121 } |
113 | 122 |
(...skipping 11 matching lines...) Expand all Loading... |
125 for (size_t i = 0; i < size_length; i++) | 134 for (size_t i = 0; i < size_length; i++) |
126 size_parameter.push_back(size_data[i]); | 135 size_parameter.push_back(size_data[i]); |
127 | 136 |
128 Clipboard::ObjectMapParams parameters; | 137 Clipboard::ObjectMapParams parameters; |
129 parameters.push_back(pixels_parameter); | 138 parameters.push_back(pixels_parameter); |
130 parameters.push_back(size_parameter); | 139 parameters.push_back(size_parameter); |
131 objects_[Clipboard::CBF_BITMAP] = parameters; | 140 objects_[Clipboard::CBF_BITMAP] = parameters; |
132 } | 141 } |
133 #endif // defined(OS_WIN) | 142 #endif // defined(OS_WIN) |
134 | 143 |
OLD | NEW |