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

Side by Side Diff: chrome/browser/extensions/clipboard_extension_helper_chromeos.cc

Issue 2379573008: Add SetImageData api to chrome.clipboard. (Closed)
Patch Set: Fix nits. Created 3 years, 11 months 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/extensions/clipboard_extension_helper_chromeos.h"
6
7 #include "base/callback_helpers.h"
8 #include "base/macros.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/synchronization/cancellation_flag.h"
11 #include "chrome/browser/image_decoder.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "ui/base/clipboard/scoped_clipboard_writer.h"
14
15 using content::BrowserThread;
16
17 namespace extensions {
18
19 namespace clipboard = api::clipboard;
20
21 class ClipboardExtensionHelper::ClipboardImageDataDecoder
22 : public ImageDecoder::ImageRequest {
23 public:
24 explicit ClipboardImageDataDecoder(ClipboardExtensionHelper* owner)
25 : owner_(owner) {}
26
27 ~ClipboardImageDataDecoder() override { ImageDecoder::Cancel(this); }
28
29 bool has_request_pending() const { return has_request_pending_; }
30
31 void Start(const std::vector<char>& image_data, clipboard::ImageType type) {
32 DCHECK_CURRENTLY_ON(BrowserThread::UI);
33 std::string image_data_str(image_data.begin(), image_data.end());
34
35 ImageDecoder::ImageCodec codec = ImageDecoder::DEFAULT_CODEC;
36 switch (type) {
37 case clipboard::IMAGE_TYPE_PNG:
38 codec = ImageDecoder::ROBUST_PNG_CODEC;
39 break;
40 case clipboard::IMAGE_TYPE_JPEG:
41 codec = ImageDecoder::ROBUST_JPEG_CODEC;
42 break;
43 case clipboard::IMAGE_TYPE_NONE:
44 NOTREACHED();
45 break;
46 }
47
48 has_request_pending_ = true;
49 ImageDecoder::StartWithOptions(this, image_data_str, codec, true);
50 }
51
52 void Cancel() {
53 has_request_pending_ = false;
54 ImageDecoder::Cancel(this);
55 owner_->OnImageDecodeCancel();
56 }
57
58 void OnImageDecoded(const SkBitmap& decoded_image) override {
59 has_request_pending_ = false;
60 owner_->OnImageDecoded(decoded_image);
61 }
62
63 void OnDecodeImageFailed() override {
64 has_request_pending_ = false;
65 owner_->OnImageDecodeFailure();
66 }
67
68 private:
69 ClipboardExtensionHelper* owner_; // Not owned.
70 bool has_request_pending_ = false;
71
72 DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder);
73 };
74
75 ClipboardExtensionHelper::ClipboardExtensionHelper() {
76 clipboard_image_data_decoder_.reset(new ClipboardImageDataDecoder(this));
77 }
78
79 ClipboardExtensionHelper::~ClipboardExtensionHelper() {}
80
81 void ClipboardExtensionHelper::DecodeAndSaveImageData(
82 const std::vector<char>& data,
83 clipboard::ImageType type,
84 const base::Closure& success_callback,
85 const base::Callback<void(const std::string&)>& error_callback) {
86 DCHECK_CURRENTLY_ON(BrowserThread::UI);
87
88 // 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
90 // the clipboard will only store data set by the most recent request, which
91 // is consistent with the clipboard "paste" behavior.
92 if (clipboard_image_data_decoder_->has_request_pending())
93 clipboard_image_data_decoder_->Cancel();
94
95 image_save_success_callback_ = success_callback;
96 image_save_error_callback_ = error_callback;
97 clipboard_image_data_decoder_->Start(data, type);
98 }
99
100 void ClipboardExtensionHelper::OnImageDecodeFailure() {
101 base::ResetAndReturn(&image_save_error_callback_)
102 .Run("Image data decoding failed");
103 }
104
105 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) {
106 {
107 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE);
108 // Write the decoded image data to clipboard.
109 if (!bitmap.empty() && !bitmap.isNull())
110 scw.WriteImage(bitmap);
111 }
112 base::ResetAndReturn(&image_save_success_callback_).Run();
113 }
114
115 void ClipboardExtensionHelper::OnImageDecodeCancel() {
116 base::ResetAndReturn(&image_save_error_callback_).Run("Request canceled");
117 }
118
119 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698