Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6629)

Unified Diff: chrome/browser/extensions/clipboard_extension_helper_chromeos.cc

Issue 2379573008: Add SetImageData api to chrome.clipboard. (Closed)
Patch Set: Add a warning in clipboard.idl about the future deprecation plan. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/clipboard_extension_helper_chromeos.cc
diff --git a/chrome/browser/extensions/clipboard_extension_helper_chromeos.cc b/chrome/browser/extensions/clipboard_extension_helper_chromeos.cc
new file mode 100644
index 0000000000000000000000000000000000000000..52ee41e2609c6f9bfc3967672db55f8ac374f1de
--- /dev/null
+++ b/chrome/browser/extensions/clipboard_extension_helper_chromeos.cc
@@ -0,0 +1,118 @@
+// 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_chromeos.h"
+
+#include "base/callback_helpers.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"
Devlin 2016/12/16 02:26:16 needed?
jennyz 2016/12/19 07:01:36 Removed.
+#include "ui/base/clipboard/scoped_clipboard_writer.h"
+
+using content::BrowserThread;
+
+namespace extensions {
+
+namespace clipboard = api::clipboard;
+
+class ClipboardExtensionHelper::ClipboardImageDataDecoder
+ : public ImageDecoder::ImageRequest {
+ public:
+ explicit ClipboardImageDataDecoder(ClipboardExtensionHelper* owner)
+ : owner_(owner) {}
Devlin 2016/12/16 02:26:16 this seems like it needs to be git cl format'd.
jennyz 2016/12/19 07:01:36 Done.
+
+ ~ClipboardImageDataDecoder() override {
+ ImageDecoder::Cancel(this);
+ }
+
+ 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;
+ switch (type) {
+ case clipboard::IMAGE_TYPE_PNG:
+ codec = ImageDecoder::ROBUST_PNG_CODEC;
+ break;
+ case clipboard::IMAGE_TYPE_JPEG:
+ codec = ImageDecoder::ROBUST_JPEG_CODEC;
+ break;
+ default:
+ OnDecodeImageFailed();
+ break;
+ }
+
+ ImageDecoder::StartWithOptions(this, image_data_str, codec, true);
+ }
+
+ void Cancel() {
+ ImageDecoder::Cancel(this);
+ owner_->OnImageDecodeCancel();
+ }
+
+ void OnImageDecoded(const SkBitmap& decoded_image) override {
+ owner_->OnImageDecoded(decoded_image);
+ }
+
+ void OnDecodeImageFailed() override {
+ owner_->OnImageDecodeFailure();
+ }
+
+ private:
+ ClipboardExtensionHelper* owner_;
Devlin 2016/12/16 02:26:16 Document this (in particular, lifetime).
jennyz 2016/12/19 07:01:36 Done.
+
+ DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder);
+};
+
+ClipboardExtensionHelper::ClipboardExtensionHelper()
+ : clipboard_writer_(
+ new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)) {}
+
+ClipboardExtensionHelper::~ClipboardExtensionHelper() {}
+
+void ClipboardExtensionHelper::DecodeAndSaveImageData(
+ const std::vector<char>& data,
+ clipboard::ImageType type,
+ const base::Closure& success_callback,
+ const ExtensionsBrowserClient::ErrorCallback& 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_.reset(new ClipboardImageDataDecoder(this));
Devlin 2016/12/16 02:26:16 Is there a reason we need to reset this each time?
jennyz 2016/12/19 07:01:36 Done.
+ 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_.reset();
+ base::ResetAndReturn(&image_save_error_callback_).Run(
+ std::string("Image data decoding failed"));
Devlin 2016/12/16 02:26:16 const char[] can be implicitly converted to std::s
jennyz 2016/12/19 07:01:36 Done.
+}
+
+void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) {
+ clipboard_image_data_decoder_.reset();
+
+ // Write the decoded image data to clipboard.
+ if (!bitmap.empty() && !bitmap.isNull())
+ clipboard_writer_->WriteImage(bitmap);
+ clipboard_writer_.reset(
Devlin 2016/12/16 02:26:16 here, too, why do we need to reset?
jennyz 2016/12/19 07:01:36 Done.
+ new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE));
+
+ base::ResetAndReturn(&image_save_success_callback_).Run();
+}
+
+void ClipboardExtensionHelper::OnImageDecodeCancel() {
+ base::ResetAndReturn(&image_save_error_callback_).Run(
+ std::string("Request canceled"));
+}
+
+} // namespace extensions

Powered by Google App Engine
This is Rietveld 408576698