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..d2fb87422a796222af90707075b959e1c436db9e |
| --- /dev/null |
| +++ b/chrome/browser/extensions/clipboard_extension_helper.cc |
| @@ -0,0 +1,121 @@ |
| +// 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( |
| + const base::WeakPtr<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) |
|
Devlin
2016/12/09 15:23:43
Is this API only supported on CrOS? If so, can't
jennyz
2016/12/14 01:15:36
Yes, this API is Cros-only, I made a couple of cha
|
| + if (type == clipboard::IMAGE_TYPE_PNG) { |
|
Devlin
2016/12/09 15:23:43
prefer a switch
jennyz
2016/12/14 01:15:35
Done.
|
| + 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() { |
|
Devlin
2016/12/09 15:23:48
Shouldn't we also call ImageDecoder::Cancel()?
jennyz
2016/12/14 01:15:35
Yes, good suggestion. Now I changed to call ImageD
|
| + cancel_flag_.Set(); |
| + if (owner_) |
| + owner_->OnImageDecodeCancel(); |
| + } |
| + |
| + void OnImageDecoded(const SkBitmap& decoded_image) override { |
| + if (!cancel_flag_.IsSet() && owner_) |
|
Devlin
2016/12/09 15:23:45
If we *do* want this approach, instead of doing th
jennyz
2016/12/14 01:15:35
Changed to use unique_ptr to manage the life cycle
|
| + owner_->OnImageDecoded(decoded_image); |
| + delete this; |
| + } |
| + |
| + void OnDecodeImageFailed() override { |
| + if (!cancel_flag_.IsSet() && owner_) |
| + owner_->OnImageDecodeFailure(); |
| + delete this; |
| + } |
| + |
| + private: |
| + ~ClipboardImageDataDecoder() override {} |
| + base::WeakPtr<ClipboardExtensionHelper> owner_; |
|
Devlin
2016/12/09 15:23:48
What was wrong with dcheng's suggestion to have th
jennyz
2016/12/14 01:15:36
Yes, changed to use unique_ptr to manage the lifet
|
| + base::CancellationFlag cancel_flag_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder); |
| +}; |
| + |
| +ClipboardExtensionHelper::ClipboardImageDataDecoder* |
| + ClipboardExtensionHelper::clipboard_image_data_decoder_; |
| + |
| +ClipboardExtensionHelper::ClipboardExtensionHelper() |
| + : clipboard_writer_( |
| + new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)), |
| + weak_factory_(this) {} |
| + |
| +ClipboardExtensionHelper::~ClipboardExtensionHelper() {} |
| + |
| +void ClipboardExtensionHelper::DecodeAndSaveImageData( |
| + const std::vector<char>& data, |
| + api::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(); |
|
Devlin
2016/12/09 15:23:47
So only one item can be using the decode api at an
jennyz
2016/12/14 01:15:35
The clipboard can only save one image from the Set
|
| + |
| + // clipboard_image_data_decoder_ will manage its own lifetime. |
| + clipboard_image_data_decoder_ = new ClipboardImageDataDecoder( |
| + weak_factory_.GetWeakPtr()); |
| + 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_ = nullptr; |
| + image_save_error_callback_.Run(); |
|
Devlin
2016/12/09 15:23:46
prefer using base::ResetAndReturn or a OnceCallbac
jennyz
2016/12/14 01:15:35
Done.
|
| +} |
| + |
| +void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { |
| + clipboard_image_data_decoder_ = nullptr; |
| + |
| + // 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 |