| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "ui/base/clipboard/clipboard.h" | 5 #include "ui/base/clipboard/clipboard.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 9 #include "third_party/skia/include/core/SkBitmap.h" | 10 #include "third_party/skia/include/core/SkBitmap.h" |
| 10 #include "ui/gfx/linux_util.h" | 11 #include "ui/gfx/linux_util.h" |
| 11 #include "ui/gfx/size.h" | 12 #include "ui/gfx/size.h" |
| 12 | 13 |
| 13 namespace ui { | 14 namespace ui { |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 const char kMimeTypeBitmap[] = "image/bmp"; | 17 const char kMimeTypeBitmap[] = "image/bmp"; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 | 129 |
| 129 } // namespace | 130 } // namespace |
| 130 | 131 |
| 131 // TODO(varunjain): Complete implementation: | 132 // TODO(varunjain): Complete implementation: |
| 132 // 1. Handle different types of BUFFERs. | 133 // 1. Handle different types of BUFFERs. |
| 133 // 2. Do we need to care about concurrency here? Can there be multiple instances | 134 // 2. Do we need to care about concurrency here? Can there be multiple instances |
| 134 // of ui::Clipboard? Ask oshima. | 135 // of ui::Clipboard? Ask oshima. |
| 135 // 3. Implement File types. | 136 // 3. Implement File types. |
| 136 // 4. Handle conversion between types. | 137 // 4. Handle conversion between types. |
| 137 | 138 |
| 139 Clipboard::FormatType::FormatType() { |
| 140 } |
| 141 |
| 142 Clipboard::FormatType::FormatType(const std::string& native_format) |
| 143 : data_(native_format) { |
| 144 } |
| 145 |
| 146 Clipboard::FormatType::~FormatType() { |
| 147 } |
| 148 |
| 149 std::string Clipboard::FormatType::Serialize() const { |
| 150 return data_; |
| 151 } |
| 152 |
| 153 // static |
| 154 Clipboard::FormatType Clipboard::FormatType::Deserialize( |
| 155 const std::string& serialization) { |
| 156 return FormatType(serialization); |
| 157 } |
| 158 |
| 159 bool Clipboard::FormatType::Equals(const FormatType& other) const { |
| 160 return data_ == other.data_; |
| 161 } |
| 162 |
| 138 Clipboard::Clipboard() { | 163 Clipboard::Clipboard() { |
| 139 // Make sure clipboard is created. | 164 // Make sure clipboard is created. |
| 140 GetClipboardData(); | 165 GetClipboardData(); |
| 141 } | 166 } |
| 142 | 167 |
| 143 Clipboard::~Clipboard() { | 168 Clipboard::~Clipboard() { |
| 144 } | 169 } |
| 145 | 170 |
| 146 void Clipboard::WriteObjects(const ObjectMap& objects) { | 171 void Clipboard::WriteObjects(const ObjectMap& objects) { |
| 147 // We need to overwrite previous data. Probably best to just delete | 172 // We need to overwrite previous data. Probably best to just delete |
| 148 // everything and start fresh. | 173 // everything and start fresh. |
| 149 DeleteClipboardData(); | 174 DeleteClipboardData(); |
| 150 for (ObjectMap::const_iterator iter = objects.begin(); | 175 for (ObjectMap::const_iterator iter = objects.begin(); |
| 151 iter != objects.end(); ++iter) { | 176 iter != objects.end(); ++iter) { |
| 152 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); | 177 DispatchObject(static_cast<ObjectType>(iter->first), iter->second); |
| 153 } | 178 } |
| 154 } | 179 } |
| 155 | 180 |
| 156 bool Clipboard::IsFormatAvailable(const FormatType& format, | 181 bool Clipboard::IsFormatAvailable(const FormatType& format, |
| 157 Buffer buffer) const { | 182 Buffer buffer) const { |
| 158 ClipboardData* data = GetClipboardData(); | 183 ClipboardData* data = GetClipboardData(); |
| 159 if (GetPlainTextFormatType() == format) | 184 if (GetPlainTextFormatType().Equals(format)) |
| 160 return !data->text().empty(); | 185 return !data->text().empty(); |
| 161 else if (GetHtmlFormatType() == format) | 186 else if (GetHtmlFormatType().Equals(format)) |
| 162 return !data->markup_data().empty() || !data->url().empty(); | 187 return !data->markup_data().empty() || !data->url().empty(); |
| 163 else if (GetBitmapFormatType() == format) | 188 else if (GetBitmapFormatType().Equals(format)) |
| 164 return !!data->bitmap_data(); | 189 return !!data->bitmap_data(); |
| 165 else if (GetWebKitSmartPasteFormatType() == format) | 190 else if (GetWebKitSmartPasteFormatType().Equals(format)) |
| 166 return data->web_smart_paste(); | 191 return data->web_smart_paste(); |
| 167 else if (data->custom_data_format() == format) | 192 else if (data->custom_data_format() == format.ToString()) |
| 168 return true; | 193 return true; |
| 169 return false; | 194 return false; |
| 170 } | 195 } |
| 171 | 196 |
| 172 bool Clipboard::IsFormatAvailableByString(const std::string& format, | |
| 173 Buffer buffer) const { | |
| 174 return IsFormatAvailable(format, buffer); | |
| 175 } | |
| 176 | |
| 177 void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, | 197 void Clipboard::ReadAvailableTypes(Buffer buffer, std::vector<string16>* types, |
| 178 bool* contains_filenames) const { | 198 bool* contains_filenames) const { |
| 179 if (!types || !contains_filenames) { | 199 if (!types || !contains_filenames) { |
| 180 NOTREACHED(); | 200 NOTREACHED(); |
| 181 return; | 201 return; |
| 182 } | 202 } |
| 183 | 203 |
| 184 types->clear(); | 204 types->clear(); |
| 185 if (IsFormatAvailable(GetPlainTextFormatType(), buffer)) | 205 if (IsFormatAvailable(GetPlainTextFormatType(), buffer)) |
| 186 types->push_back(UTF8ToUTF16(GetPlainTextFormatType())); | 206 types->push_back(UTF8ToUTF16(GetPlainTextFormatType().ToString())); |
| 187 if (IsFormatAvailable(GetHtmlFormatType(), buffer)) | 207 if (IsFormatAvailable(GetHtmlFormatType(), buffer)) |
| 188 types->push_back(UTF8ToUTF16(GetHtmlFormatType())); | 208 types->push_back(UTF8ToUTF16(GetHtmlFormatType().ToString())); |
| 189 if (IsFormatAvailable(GetBitmapFormatType(), buffer)) | 209 if (IsFormatAvailable(GetBitmapFormatType(), buffer)) |
| 190 types->push_back(UTF8ToUTF16(GetBitmapFormatType())); | 210 types->push_back(UTF8ToUTF16(GetBitmapFormatType().ToString())); |
| 191 if (IsFormatAvailable(GetWebKitSmartPasteFormatType(), buffer)) | |
| 192 types->push_back(UTF8ToUTF16(GetWebKitSmartPasteFormatType())); | |
| 193 *contains_filenames = false; | 211 *contains_filenames = false; |
| 194 } | 212 } |
| 195 | 213 |
| 196 void Clipboard::ReadText(Buffer buffer, string16* result) const { | 214 void Clipboard::ReadText(Buffer buffer, string16* result) const { |
| 197 *result = UTF8ToUTF16(GetClipboardData()->text()); | 215 *result = UTF8ToUTF16(GetClipboardData()->text()); |
| 198 } | 216 } |
| 199 | 217 |
| 200 void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const { | 218 void Clipboard::ReadAsciiText(Buffer buffer, std::string* result) const { |
| 201 *result = GetClipboardData()->text(); | 219 *result = GetClipboardData()->text(); |
| 202 } | 220 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 } | 273 } |
| 256 | 274 |
| 257 void Clipboard::ReadFile(FilePath* file) const { | 275 void Clipboard::ReadFile(FilePath* file) const { |
| 258 NOTIMPLEMENTED(); | 276 NOTIMPLEMENTED(); |
| 259 } | 277 } |
| 260 | 278 |
| 261 void Clipboard::ReadFiles(std::vector<FilePath>* files) const { | 279 void Clipboard::ReadFiles(std::vector<FilePath>* files) const { |
| 262 NOTIMPLEMENTED(); | 280 NOTIMPLEMENTED(); |
| 263 } | 281 } |
| 264 | 282 |
| 265 void Clipboard::ReadData(const std::string& format, std::string* result) const { | 283 void Clipboard::ReadData(const FormatType& format, std::string* result) const { |
| 266 result->clear(); | 284 result->clear(); |
| 267 ClipboardData* data = GetClipboardData(); | 285 ClipboardData* data = GetClipboardData(); |
| 268 if (data->custom_data_format() == format) | 286 if (data->custom_data_format() == format.ToString()) |
| 269 *result = std::string(data->custom_data_data(), data->custom_data_len()); | 287 *result = std::string(data->custom_data_data(), data->custom_data_len()); |
| 270 } | 288 } |
| 271 | 289 |
| 272 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { | 290 uint64 Clipboard::GetSequenceNumber(Buffer buffer) { |
| 273 NOTIMPLEMENTED(); | 291 NOTIMPLEMENTED(); |
| 274 return 0; | 292 return 0; |
| 275 } | 293 } |
| 276 | 294 |
| 277 void Clipboard::WriteText(const char* text_data, size_t text_len) { | 295 void Clipboard::WriteText(const char* text_data, size_t text_len) { |
| 278 GetClipboardData()->set_text(std::string(text_data, text_len)); | 296 GetClipboardData()->set_text(std::string(text_data, text_len)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 295 } | 313 } |
| 296 | 314 |
| 297 void Clipboard::WriteWebSmartPaste() { | 315 void Clipboard::WriteWebSmartPaste() { |
| 298 GetClipboardData()->set_web_smart_paste(true); | 316 GetClipboardData()->set_web_smart_paste(true); |
| 299 } | 317 } |
| 300 | 318 |
| 301 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { | 319 void Clipboard::WriteBitmap(const char* pixel_data, const char* size_data) { |
| 302 GetClipboardData()->SetBitmapData(pixel_data, size_data); | 320 GetClipboardData()->SetBitmapData(pixel_data, size_data); |
| 303 } | 321 } |
| 304 | 322 |
| 305 void Clipboard::WriteData(const char* format_name, size_t format_len, | 323 void Clipboard::WriteData(const FormatType& format, |
| 306 const char* data_data, size_t data_len) { | 324 const char* data_data, |
| 307 GetClipboardData()->SetCustomData(std::string(format_name, format_len), | 325 size_t data_len) { |
| 308 data_data, data_len); | 326 GetClipboardData()->SetCustomData(format.ToString(), data_data, data_len); |
| 309 } | 327 } |
| 310 | 328 |
| 311 // static | 329 // static |
| 312 Clipboard::FormatType Clipboard::GetPlainTextFormatType() { | 330 Clipboard::FormatType Clipboard::GetFormatType( |
| 313 return std::string(kMimeTypeText); | 331 const std::string& format_string) { |
| 332 return FormatType::Deserialize(format_string); |
| 314 } | 333 } |
| 315 | 334 |
| 316 // static | 335 // static |
| 317 Clipboard::FormatType Clipboard::GetPlainTextWFormatType() { | 336 const Clipboard::FormatType& Clipboard::GetPlainTextFormatType() { |
| 337 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeText)); |
| 338 return type; |
| 339 } |
| 340 |
| 341 // static |
| 342 const Clipboard::FormatType& Clipboard::GetPlainTextWFormatType() { |
| 318 return GetPlainTextFormatType(); | 343 return GetPlainTextFormatType(); |
| 319 } | 344 } |
| 320 | 345 |
| 321 // static | 346 // static |
| 322 Clipboard::FormatType Clipboard::GetHtmlFormatType() { | 347 const Clipboard::FormatType& Clipboard::GetHtmlFormatType() { |
| 323 return std::string(kMimeTypeHTML); | 348 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeHTML)); |
| 349 return type; |
| 324 } | 350 } |
| 325 | 351 |
| 326 // static | 352 // static |
| 327 Clipboard::FormatType Clipboard::GetBitmapFormatType() { | 353 const Clipboard::FormatType& Clipboard::GetBitmapFormatType() { |
| 328 return std::string(kMimeTypeBitmap); | 354 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeBitmap)); |
| 355 return type; |
| 329 } | 356 } |
| 330 | 357 |
| 331 // static | 358 // static |
| 332 Clipboard::FormatType Clipboard::GetWebKitSmartPasteFormatType() { | 359 const Clipboard::FormatType& Clipboard::GetWebKitSmartPasteFormatType() { |
| 333 return std::string(kMimeTypeWebkitSmartPaste); | 360 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebkitSmartPaste)); |
| 361 return type; |
| 334 } | 362 } |
| 335 | 363 |
| 336 // static | 364 // static |
| 337 Clipboard::FormatType Clipboard::GetWebCustomDataFormatType() { | 365 const Clipboard::FormatType& Clipboard::GetWebCustomDataFormatType() { |
| 338 return std::string(kMimeTypeWebCustomData); | 366 CR_DEFINE_STATIC_LOCAL(FormatType, type, (kMimeTypeWebCustomData)); |
| 367 return type; |
| 339 } | 368 } |
| 340 | 369 |
| 341 } // namespace ui | 370 } // namespace ui |
| OLD | NEW |