| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "chrome/browser/extensions/clipboard_extension_helper_chromeos.h" | 5 #include "chrome/browser/extensions/clipboard_extension_helper_chromeos.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/synchronization/cancellation_flag.h" | 12 #include "base/synchronization/cancellation_flag.h" |
| 11 #include "chrome/browser/image_decoder.h" | 13 #include "chrome/browser/image_decoder.h" |
| 12 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 13 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 15 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 14 | 16 |
| 15 using content::BrowserThread; | 17 using content::BrowserThread; |
| 16 | 18 |
| 17 namespace extensions { | 19 namespace extensions { |
| 18 | 20 |
| 19 namespace clipboard = api::clipboard; | 21 namespace clipboard = api::clipboard; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 76 |
| 75 ClipboardExtensionHelper::ClipboardExtensionHelper() { | 77 ClipboardExtensionHelper::ClipboardExtensionHelper() { |
| 76 clipboard_image_data_decoder_.reset(new ClipboardImageDataDecoder(this)); | 78 clipboard_image_data_decoder_.reset(new ClipboardImageDataDecoder(this)); |
| 77 } | 79 } |
| 78 | 80 |
| 79 ClipboardExtensionHelper::~ClipboardExtensionHelper() {} | 81 ClipboardExtensionHelper::~ClipboardExtensionHelper() {} |
| 80 | 82 |
| 81 void ClipboardExtensionHelper::DecodeAndSaveImageData( | 83 void ClipboardExtensionHelper::DecodeAndSaveImageData( |
| 82 const std::vector<char>& data, | 84 const std::vector<char>& data, |
| 83 clipboard::ImageType type, | 85 clipboard::ImageType type, |
| 86 AdditionalDataItemList additional_items, |
| 84 const base::Closure& success_callback, | 87 const base::Closure& success_callback, |
| 85 const base::Callback<void(const std::string&)>& error_callback) { | 88 const base::Callback<void(const std::string&)>& error_callback) { |
| 86 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 89 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 87 | 90 |
| 88 // If there is a previous image decoding request still running, cancel it | 91 // If there is a previous image decoding request still running, cancel it |
| 89 // first. We only need the most recent image save request be completed, since | 92 // first. We only need the most recent image save request be completed, since |
| 90 // the clipboard will only store data set by the most recent request, which | 93 // the clipboard will only store data set by the most recent request, which |
| 91 // is consistent with the clipboard "paste" behavior. | 94 // is consistent with the clipboard "paste" behavior. |
| 92 if (clipboard_image_data_decoder_->has_request_pending()) | 95 if (clipboard_image_data_decoder_->has_request_pending()) |
| 93 clipboard_image_data_decoder_->Cancel(); | 96 clipboard_image_data_decoder_->Cancel(); |
| 94 | 97 |
| 98 // Cache additonal items. |
| 99 additonal_items_ = std::move(additional_items); |
| 100 |
| 95 image_save_success_callback_ = success_callback; | 101 image_save_success_callback_ = success_callback; |
| 96 image_save_error_callback_ = error_callback; | 102 image_save_error_callback_ = error_callback; |
| 97 clipboard_image_data_decoder_->Start(data, type); | 103 clipboard_image_data_decoder_->Start(data, type); |
| 98 } | 104 } |
| 99 | 105 |
| 100 void ClipboardExtensionHelper::OnImageDecodeFailure() { | 106 void ClipboardExtensionHelper::OnImageDecodeFailure() { |
| 101 base::ResetAndReturn(&image_save_error_callback_) | 107 base::ResetAndReturn(&image_save_error_callback_) |
| 102 .Run("Image data decoding failed"); | 108 .Run("Image data decoding failed."); |
| 103 } | 109 } |
| 104 | 110 |
| 105 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { | 111 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { |
| 106 { | 112 { |
| 107 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); | 113 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 108 // Write the decoded image data to clipboard. | 114 // Write the decoded image data to clipboard. |
| 109 if (!bitmap.empty() && !bitmap.isNull()) | 115 if (!bitmap.empty() && !bitmap.isNull()) |
| 110 scw.WriteImage(bitmap); | 116 scw.WriteImage(bitmap); |
| 117 |
| 118 for (const clipboard::AdditionalDataItem& item : additonal_items_) { |
| 119 if (item.type == clipboard::DATA_ITEM_TYPE_TEXTPLAIN) |
| 120 scw.WriteText(base::UTF8ToUTF16(item.data)); |
| 121 else if (item.type == clipboard::DATA_ITEM_TYPE_TEXTHTML) |
| 122 scw.WriteHTML(base::UTF8ToUTF16(item.data), std::string()); |
| 123 } |
| 111 } | 124 } |
| 112 base::ResetAndReturn(&image_save_success_callback_).Run(); | 125 base::ResetAndReturn(&image_save_success_callback_).Run(); |
| 113 } | 126 } |
| 114 | 127 |
| 115 void ClipboardExtensionHelper::OnImageDecodeCancel() { | 128 void ClipboardExtensionHelper::OnImageDecodeCancel() { |
| 116 base::ResetAndReturn(&image_save_error_callback_).Run("Request canceled"); | 129 base::ResetAndReturn(&image_save_error_callback_).Run("Request canceled."); |
| 117 } | 130 } |
| 118 | 131 |
| 119 } // namespace extensions | 132 } // namespace extensions |
| OLD | NEW |