OLD | NEW |
---|---|
(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.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/metrics/histogram_macros.h" | |
9 #include "base/synchronization/cancellation_flag.h" | |
10 #include "chrome/browser/image_decoder.h" | |
11 #include "extensions/browser/api/clipboard/clipboard_api.h" | |
12 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
13 | |
14 using content::BrowserThread; | |
15 | |
16 namespace extensions { | |
17 | |
18 class ClipboardExtensionHelper::ClipboardImageDataDecoder | |
19 : public ImageDecoder::ImageRequest { | |
20 public: | |
21 explicit ClipboardImageDataDecoder(ClipboardExtensionHelper* owner) | |
22 : owner_(owner) {} | |
23 | |
24 void Start(const std::vector<char>& image_data, const std::string& type) { | |
25 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
26 std::string image_data_str(image_data.begin(), image_data.end()); | |
27 | |
28 ImageDecoder::ImageCodec codec = ImageDecoder::DEFAULT_CODEC; | |
29 std::string encoding_type = base::ToLowerASCII(type); | |
30 #if defined(OS_CHROMEOS) | |
31 if (encoding_type == "png") { | |
32 codec = ImageDecoder::ROBUST_PNG_CODEC; | |
33 } else if (encoding_type == "jpeg") { | |
34 codec = ImageDecoder::ROBUST_JPEG_CODEC; | |
35 } else { | |
36 OnDecodeImageFailed(); | |
37 return; | |
38 } | |
39 #endif | |
40 | |
41 ImageDecoder::StartWithOptions(this, image_data_str, codec, true); | |
dcheng
2016/10/04 05:13:27
IIRC, ImageDecoder internally uses std::vector<uin
jennyz
2016/10/04 23:53:54
The automatically generated clipboard.h maps the A
| |
42 } | |
43 | |
44 void Cancel() { | |
45 cancel_flag_.Set(); | |
46 owner_->OnImageDecodeCancel(); | |
47 } | |
48 | |
49 void OnImageDecoded(const SkBitmap& decoded_image) override { | |
50 if (!cancel_flag_.IsSet()) | |
51 owner_->OnImageDecoded(decoded_image); | |
52 delete this; | |
53 } | |
54 | |
55 void OnDecodeImageFailed() override { | |
56 if (!cancel_flag_.IsSet()) | |
57 owner_->OnImageDecodeFailure(); | |
58 delete this; | |
59 } | |
60 | |
61 private: | |
62 ClipboardExtensionHelper* owner_; | |
63 base::CancellationFlag cancel_flag_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder); | |
66 }; | |
67 | |
68 ClipboardExtensionHelper::ClipboardImageDataDecoder* | |
69 ClipboardExtensionHelper::clipboard_image_data_decoder_; | |
70 | |
71 ClipboardExtensionHelper::ClipboardExtensionHelper() | |
72 : image_save_success_callback_(nullptr), | |
73 image_save_error_callback_(nullptr), | |
74 clipboard_writer_( | |
75 new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)) {} | |
76 | |
77 ClipboardExtensionHelper::~ClipboardExtensionHelper() {} | |
78 | |
79 void ClipboardExtensionHelper::DecodeAndSaveImageData( | |
80 const std::vector<char>& data, | |
81 const std::string& type, | |
82 const base::Closure& success_callback, | |
83 const base::Closure& error_callback) { | |
84 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
85 | |
86 // If there is a previous image decoding request still running, cancel it | |
87 // first. We only need the most recent image save request be completed. | |
88 if (clipboard_image_data_decoder_) | |
89 clipboard_image_data_decoder_->Cancel(); | |
90 | |
91 clipboard_image_data_decoder_ = new ClipboardImageDataDecoder(this); | |
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 clipboard_image_data_decoder_ = NULL; | |
99 image_save_error_callback_.Run(); | |
100 } | |
101 | |
102 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { | |
103 clipboard_image_data_decoder_ = NULL; | |
104 | |
105 // Write the decoded image data to clipboard. | |
106 if (!bitmap.empty() && !bitmap.isNull()) | |
107 clipboard_writer_->WriteImage(bitmap); | |
108 clipboard_writer_.reset( | |
109 new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)); | |
110 | |
111 image_save_success_callback_.Run(); | |
112 } | |
113 | |
114 void ClipboardExtensionHelper::OnImageDecodeCancel() { | |
115 image_save_error_callback_.Run(); | |
116 } | |
117 | |
118 } // namespace extensions | |
OLD | NEW |