Chromium Code Reviews| Index: chrome/browser/extensions/clipboard_extension_helper.cc |
| diff --git a/chrome/browser/extensions/clipboard_extension_helper.cc b/chrome/browser/extensions/clipboard_extension_helper.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d0b6d0aec2514842f0df2251b82b929c6727c424 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/clipboard_extension_helper.cc |
| @@ -0,0 +1,117 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/clipboard_extension_helper.h" |
| + |
| +#include "base/macros.h" |
| +#include "base/metrics/histogram_macros.h" |
| +#include "base/synchronization/cancellation_flag.h" |
| +#include "chrome/browser/image_decoder.h" |
| +#include "extensions/browser/api/clipboard/clipboard_api.h" |
| +#include "ui/base/clipboard/scoped_clipboard_writer.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace extensions { |
| + |
| +class ClipboardExtensionHelper::ClipboardImageDataDecoder |
| + : public ImageDecoder::ImageRequest { |
| + public: |
| + explicit ClipboardImageDataDecoder(ClipboardExtensionHelper* owner) |
| + : owner_(owner) {} |
| + |
| + void Start(const std::vector<char>& image_data, clipboard::ImageType type) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + std::string image_data_str(image_data.begin(), image_data.end()); |
| + |
| + ImageDecoder::ImageCodec codec = ImageDecoder::DEFAULT_CODEC; |
| +#if defined(OS_CHROMEOS) |
| + if (type == clipboard::IMAGE_TYPE_PNG) { |
| + codec = ImageDecoder::ROBUST_PNG_CODEC; |
| + } else if (type == clipboard::IMAGE_TYPE_JPEG) { |
| + codec = ImageDecoder::ROBUST_JPEG_CODEC; |
| + } else { |
| + OnDecodeImageFailed(); |
| + return; |
| + } |
| +#endif |
| + |
| + ImageDecoder::StartWithOptions(this, image_data_str, codec, true); |
| + } |
| + |
| + void Cancel() { |
| + cancel_flag_.Set(); |
| + owner_->OnImageDecodeCancel(); |
| + } |
| + |
| + void OnImageDecoded(const SkBitmap& decoded_image) override { |
| + if (!cancel_flag_.IsSet()) |
| + owner_->OnImageDecoded(decoded_image); |
| + delete this; |
| + } |
| + |
| + void OnDecodeImageFailed() override { |
| + if (!cancel_flag_.IsSet()) |
| + owner_->OnImageDecodeFailure(); |
| + delete this; |
| + } |
| + |
| + private: |
|
Devlin
2016/11/16 02:34:33
private dtor?
jennyz
2016/12/07 01:21:47
Done.
|
| + ClipboardExtensionHelper* owner_; |
| + base::CancellationFlag cancel_flag_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder); |
| +}; |
| + |
| +ClipboardExtensionHelper::ClipboardImageDataDecoder* |
| + ClipboardExtensionHelper::clipboard_image_data_decoder_; |
| + |
| +ClipboardExtensionHelper::ClipboardExtensionHelper() |
| + : image_save_success_callback_(nullptr), |
|
Devlin
2016/11/16 02:34:33
are these initializations needed?
jennyz
2016/12/07 01:21:47
removed.
|
| + image_save_error_callback_(nullptr), |
| + clipboard_writer_( |
| + new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)) {} |
| + |
| +ClipboardExtensionHelper::~ClipboardExtensionHelper() {} |
|
Devlin
2016/11/16 02:34:33
What if the clipboard_image_data_decoder_ outlives
jennyz
2016/12/07 01:21:47
Good catch. Make ClipboardExtensionHelper a WeakPt
|
| + |
| +void ClipboardExtensionHelper::DecodeAndSaveImageData( |
| + const std::vector<char>& data, |
| + clipboard::ImageType type, |
| + const base::Closure& success_callback, |
| + const base::Closure& error_callback) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| + |
| + // If there is a previous image decoding request still running, cancel it |
| + // first. We only need the most recent image save request be completed. |
| + if (clipboard_image_data_decoder_) |
| + clipboard_image_data_decoder_->Cancel(); |
| + |
| + clipboard_image_data_decoder_ = new ClipboardImageDataDecoder(this); |
|
Devlin
2016/11/16 02:34:33
Add a note that this manages its own lifetime.
dcheng
2016/11/17 01:13:32
Maybe it'd be better to just have the lifetime man
jennyz
2016/12/07 01:21:47
Done.
jennyz
2016/12/07 01:21:47
Changed to pass WeakPtr into clipboard_image_data_
jennyz
2016/12/14 01:15:35
Yes, changed to make it td::unique_ptr. Thanks for
|
| + image_save_success_callback_ = success_callback; |
| + image_save_error_callback_ = error_callback; |
| + clipboard_image_data_decoder_->Start(data, type); |
| +} |
| + |
| +void ClipboardExtensionHelper::OnImageDecodeFailure() { |
| + clipboard_image_data_decoder_ = NULL; |
|
Devlin
2016/11/16 02:34:33
nullptr in new code
jennyz
2016/12/07 01:21:47
Done.
|
| + image_save_error_callback_.Run(); |
| +} |
| + |
| +void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { |
| + clipboard_image_data_decoder_ = NULL; |
| + |
| + // Write the decoded image data to clipboard. |
| + if (!bitmap.empty() && !bitmap.isNull()) |
| + clipboard_writer_->WriteImage(bitmap); |
| + clipboard_writer_.reset( |
| + new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)); |
| + |
| + image_save_success_callback_.Run(); |
| +} |
| + |
| +void ClipboardExtensionHelper::OnImageDecodeCancel() { |
| + image_save_error_callback_.Run(); |
| +} |
| + |
| +} // namespace extensions |