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

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

Issue 2379573008: Add SetImageData api to chrome.clipboard. (Closed)
Patch Set: Update idl for possible web alternative timeline. 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 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 "ui/base/clipboard/scoped_clipboard_writer.h"
13
14 using content::BrowserThread;
15
16 namespace extensions {
17
18 namespace clipboard = api::clipboard;
19
20 class ClipboardExtensionHelper::ClipboardImageDataDecoder
21 : public ImageDecoder::ImageRequest {
22 public:
23 explicit ClipboardImageDataDecoder(ClipboardExtensionHelper* owner)
24 : owner_(owner) {}
25
26 ~ClipboardImageDataDecoder() override { ImageDecoder::Cancel(this); }
27
28 bool has_request_pending() const { return has_request_pending_; }
29
30 void Start(const std::vector<char>& image_data, clipboard::ImageType type) {
31 DCHECK_CURRENTLY_ON(BrowserThread::UI);
32 std::string image_data_str(image_data.begin(), image_data.end());
33
34 ImageDecoder::ImageCodec codec = ImageDecoder::DEFAULT_CODEC;
35 switch (type) {
36 case clipboard::IMAGE_TYPE_PNG:
37 codec = ImageDecoder::ROBUST_PNG_CODEC;
38 break;
39 case clipboard::IMAGE_TYPE_JPEG:
40 codec = ImageDecoder::ROBUST_JPEG_CODEC;
41 break;
42 default:
43 OnDecodeImageFailed();
44 break;
45 }
46
47 has_request_pending_ = true;
48 ImageDecoder::StartWithOptions(this, image_data_str, codec, true);
49 }
50
51 void Cancel() {
52 has_request_pending_ = false;
53 ImageDecoder::Cancel(this);
54 owner_->OnImageDecodeCancel();
55 }
56
57 void OnImageDecoded(const SkBitmap& decoded_image) override {
58 has_request_pending_ = false;
59 owner_->OnImageDecoded(decoded_image);
60 }
61
62 void OnDecodeImageFailed() override {
63 has_request_pending_ = false;
64 owner_->OnImageDecodeFailure();
65 }
66
67 private:
68 ClipboardExtensionHelper* owner_; // Not owned.
69 bool has_request_pending_ = false;
70
71 DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder);
72 };
73
74 ClipboardExtensionHelper::ClipboardExtensionHelper() {
75 clipboard_image_data_decoder_.reset(new ClipboardImageDataDecoder(this));
76 }
77
78 ClipboardExtensionHelper::~ClipboardExtensionHelper() {}
79
80 void ClipboardExtensionHelper::DecodeAndSaveImageData(
81 const std::vector<char>& data,
82 clipboard::ImageType type,
83 const base::Closure& success_callback,
84 const base::Callback<void(const std::string&)>& error_callback) {
85 DCHECK_CURRENTLY_ON(BrowserThread::UI);
86
87 // If there is a previous image decoding request still running, cancel it
88 // first. We only need the most recent image save request be completed.
89 if (clipboard_image_data_decoder_->has_request_pending())
90 clipboard_image_data_decoder_->Cancel();
Devlin 2016/12/21 23:58:38 Have you thought about the case of multiple extens
jennyz 2016/12/22 22:45:21 In the case of multiple extensions trying to this
Devlin 2016/12/27 17:08:00 Should we send a different error, then? Rather th
jennyz 2016/12/29 05:16:03 We are sending a different error: "Request cancele
91
92 image_save_success_callback_ = success_callback;
93 image_save_error_callback_ = error_callback;
94 clipboard_image_data_decoder_->Start(data, type);
95 }
96
97 void ClipboardExtensionHelper::OnImageDecodeFailure() {
98 base::ResetAndReturn(&image_save_error_callback_)
99 .Run("Image data decoding failed");
100 }
101
102 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) {
103 {
104 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE);
105 // Write the decoded image data to clipboard.
106 if (!bitmap.empty() && !bitmap.isNull())
107 scw.WriteImage(bitmap);
108 }
109 base::ResetAndReturn(&image_save_success_callback_).Run();
110 }
111
112 void ClipboardExtensionHelper::OnImageDecodeCancel() {
113 base::ResetAndReturn(&image_save_error_callback_).Run("Request canceled");
114 }
115
116 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698