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, clipboard::ImageType 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 #if defined(OS_CHROMEOS) | |
30 if (type == clipboard::IMAGE_TYPE_PNG) { | |
31 codec = ImageDecoder::ROBUST_PNG_CODEC; | |
32 } else if (type == clipboard::IMAGE_TYPE_JPEG) { | |
33 codec = ImageDecoder::ROBUST_JPEG_CODEC; | |
34 } else { | |
35 OnDecodeImageFailed(); | |
36 return; | |
37 } | |
38 #endif | |
39 | |
40 ImageDecoder::StartWithOptions(this, image_data_str, codec, true); | |
41 } | |
42 | |
43 void Cancel() { | |
44 cancel_flag_.Set(); | |
45 owner_->OnImageDecodeCancel(); | |
46 } | |
47 | |
48 void OnImageDecoded(const SkBitmap& decoded_image) override { | |
49 if (!cancel_flag_.IsSet()) | |
50 owner_->OnImageDecoded(decoded_image); | |
51 delete this; | |
52 } | |
53 | |
54 void OnDecodeImageFailed() override { | |
55 if (!cancel_flag_.IsSet()) | |
56 owner_->OnImageDecodeFailure(); | |
57 delete this; | |
58 } | |
59 | |
60 private: | |
Devlin
2016/11/16 02:34:33
private dtor?
jennyz
2016/12/07 01:21:47
Done.
| |
61 ClipboardExtensionHelper* owner_; | |
62 base::CancellationFlag cancel_flag_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder); | |
65 }; | |
66 | |
67 ClipboardExtensionHelper::ClipboardImageDataDecoder* | |
68 ClipboardExtensionHelper::clipboard_image_data_decoder_; | |
69 | |
70 ClipboardExtensionHelper::ClipboardExtensionHelper() | |
71 : image_save_success_callback_(nullptr), | |
Devlin
2016/11/16 02:34:33
are these initializations needed?
jennyz
2016/12/07 01:21:47
removed.
| |
72 image_save_error_callback_(nullptr), | |
73 clipboard_writer_( | |
74 new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)) {} | |
75 | |
76 ClipboardExtensionHelper::~ClipboardExtensionHelper() {} | |
Devlin
2016/11/16 02:34:33
What if the clipboard_image_data_decoder_ outlives
jennyz
2016/12/07 01:21:47
Good catch. Make ClipboardExtensionHelper a WeakPt
| |
77 | |
78 void ClipboardExtensionHelper::DecodeAndSaveImageData( | |
79 const std::vector<char>& data, | |
80 clipboard::ImageType type, | |
81 const base::Closure& success_callback, | |
82 const base::Closure& error_callback) { | |
83 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
84 | |
85 // If there is a previous image decoding request still running, cancel it | |
86 // first. We only need the most recent image save request be completed. | |
87 if (clipboard_image_data_decoder_) | |
88 clipboard_image_data_decoder_->Cancel(); | |
89 | |
90 clipboard_image_data_decoder_ = new ClipboardImageDataDecoder(this); | |
Devlin
2016/11/16 02:34:33
Add a note that this manages its own lifetime.
dcheng
2016/11/17 01:13:32
Maybe it'd be better to just have the lifetime man
jennyz
2016/12/07 01:21:47
Done.
jennyz
2016/12/07 01:21:47
Changed to pass WeakPtr into clipboard_image_data_
jennyz
2016/12/14 01:15:35
Yes, changed to make it td::unique_ptr. Thanks for
| |
91 image_save_success_callback_ = success_callback; | |
92 image_save_error_callback_ = error_callback; | |
93 clipboard_image_data_decoder_->Start(data, type); | |
94 } | |
95 | |
96 void ClipboardExtensionHelper::OnImageDecodeFailure() { | |
97 clipboard_image_data_decoder_ = NULL; | |
Devlin
2016/11/16 02:34:33
nullptr in new code
jennyz
2016/12/07 01:21:47
Done.
| |
98 image_save_error_callback_.Run(); | |
99 } | |
100 | |
101 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { | |
102 clipboard_image_data_decoder_ = NULL; | |
103 | |
104 // Write the decoded image data to clipboard. | |
105 if (!bitmap.empty() && !bitmap.isNull()) | |
106 clipboard_writer_->WriteImage(bitmap); | |
107 clipboard_writer_.reset( | |
108 new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)); | |
109 | |
110 image_save_success_callback_.Run(); | |
111 } | |
112 | |
113 void ClipboardExtensionHelper::OnImageDecodeCancel() { | |
114 image_save_error_callback_.Run(); | |
115 } | |
116 | |
117 } // namespace extensions | |
OLD | NEW |