Chromium Code Reviews| 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 const 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_.clear(); | |
| 100 for (const clipboard::AdditionalDataItem& item : additional_items) { | |
| 101 AdditionalDataItem data_item; | |
| 102 data_item.type = base::ToLowerASCII(item.type); | |
| 103 data_item.data = item.data; | |
| 104 additonal_items_.push_back(data_item); | |
| 105 } | |
| 106 | |
| 95 image_save_success_callback_ = success_callback; | 107 image_save_success_callback_ = success_callback; |
| 96 image_save_error_callback_ = error_callback; | 108 image_save_error_callback_ = error_callback; |
| 97 clipboard_image_data_decoder_->Start(data, type); | 109 clipboard_image_data_decoder_->Start(data, type); |
| 98 } | 110 } |
| 99 | 111 |
| 100 void ClipboardExtensionHelper::OnImageDecodeFailure() { | 112 void ClipboardExtensionHelper::OnImageDecodeFailure() { |
| 101 base::ResetAndReturn(&image_save_error_callback_) | 113 base::ResetAndReturn(&image_save_error_callback_) |
| 102 .Run("Image data decoding failed"); | 114 .Run("Image data decoding failed"); |
| 103 } | 115 } |
| 104 | 116 |
| 105 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { | 117 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { |
| 106 { | 118 { |
| 107 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); | 119 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 108 // Write the decoded image data to clipboard. | 120 // Write the decoded image data to clipboard. |
| 109 if (!bitmap.empty() && !bitmap.isNull()) | 121 if (!bitmap.empty() && !bitmap.isNull()) |
| 110 scw.WriteImage(bitmap); | 122 scw.WriteImage(bitmap); |
| 123 for (const AdditionalDataItem& item : additonal_items_) { | |
| 124 if (item.type == "text/plain") | |
| 125 scw.WriteText(base::UTF8ToUTF16(item.data)); | |
| 126 else if (item.type == "text/html") | |
| 127 scw.WriteHTML(base::UTF8ToUTF16(item.data), std::string("")); | |
|
Devlin
2017/04/26 18:20:15
So far this text (item.data) is totally unvalidate
dcheng
2017/04/27 15:42:09
It mirrors what we do for a renderer that calls Da
jennyz
2017/05/16 18:22:03
See dcheng@'s comment.
jennyz
2017/05/16 18:22:03
Done.
| |
| 128 } | |
| 111 } | 129 } |
| 112 base::ResetAndReturn(&image_save_success_callback_).Run(); | 130 base::ResetAndReturn(&image_save_success_callback_).Run(); |
| 113 } | 131 } |
| 114 | 132 |
| 115 void ClipboardExtensionHelper::OnImageDecodeCancel() { | 133 void ClipboardExtensionHelper::OnImageDecodeCancel() { |
| 116 base::ResetAndReturn(&image_save_error_callback_).Run("Request canceled"); | 134 base::ResetAndReturn(&image_save_error_callback_).Run("Request canceled"); |
| 117 } | 135 } |
| 118 | 136 |
| 119 } // namespace extensions | 137 } // namespace extensions |
| OLD | NEW |