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_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 "extensions/browser/api/clipboard/clipboard_api.h" | |
Devlin
2016/12/16 02:26:16
needed?
jennyz
2016/12/19 07:01:36
Removed.
| |
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) {} | |
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.
| |
26 | |
27 ~ClipboardImageDataDecoder() override { | |
28 ImageDecoder::Cancel(this); | |
29 } | |
30 | |
31 void Start(const std::vector<char>& image_data, | |
32 clipboard::ImageType type) { | |
33 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
34 std::string image_data_str(image_data.begin(), image_data.end()); | |
35 | |
36 ImageDecoder::ImageCodec codec = ImageDecoder::DEFAULT_CODEC; | |
37 switch (type) { | |
38 case clipboard::IMAGE_TYPE_PNG: | |
39 codec = ImageDecoder::ROBUST_PNG_CODEC; | |
40 break; | |
41 case clipboard::IMAGE_TYPE_JPEG: | |
42 codec = ImageDecoder::ROBUST_JPEG_CODEC; | |
43 break; | |
44 default: | |
45 OnDecodeImageFailed(); | |
46 break; | |
47 } | |
48 | |
49 ImageDecoder::StartWithOptions(this, image_data_str, codec, true); | |
50 } | |
51 | |
52 void Cancel() { | |
53 ImageDecoder::Cancel(this); | |
54 owner_->OnImageDecodeCancel(); | |
55 } | |
56 | |
57 void OnImageDecoded(const SkBitmap& decoded_image) override { | |
58 owner_->OnImageDecoded(decoded_image); | |
59 } | |
60 | |
61 void OnDecodeImageFailed() override { | |
62 owner_->OnImageDecodeFailure(); | |
63 } | |
64 | |
65 private: | |
66 ClipboardExtensionHelper* owner_; | |
Devlin
2016/12/16 02:26:16
Document this (in particular, lifetime).
jennyz
2016/12/19 07:01:36
Done.
| |
67 | |
68 DISALLOW_COPY_AND_ASSIGN(ClipboardImageDataDecoder); | |
69 }; | |
70 | |
71 ClipboardExtensionHelper::ClipboardExtensionHelper() | |
72 : clipboard_writer_( | |
73 new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)) {} | |
74 | |
75 ClipboardExtensionHelper::~ClipboardExtensionHelper() {} | |
76 | |
77 void ClipboardExtensionHelper::DecodeAndSaveImageData( | |
78 const std::vector<char>& data, | |
79 clipboard::ImageType type, | |
80 const base::Closure& success_callback, | |
81 const ExtensionsBrowserClient::ErrorCallback& error_callback) { | |
82 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
83 | |
84 // If there is a previous image decoding request still running, cancel it | |
85 // first. We only need the most recent image save request be completed. | |
86 if (clipboard_image_data_decoder_) | |
87 clipboard_image_data_decoder_->Cancel(); | |
88 | |
89 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.
| |
90 image_save_success_callback_ = success_callback; | |
91 image_save_error_callback_ = error_callback; | |
92 clipboard_image_data_decoder_->Start(data, type); | |
93 } | |
94 | |
95 void ClipboardExtensionHelper::OnImageDecodeFailure() { | |
96 clipboard_image_data_decoder_.reset(); | |
97 base::ResetAndReturn(&image_save_error_callback_).Run( | |
98 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.
| |
99 } | |
100 | |
101 void ClipboardExtensionHelper::OnImageDecoded(const SkBitmap& bitmap) { | |
102 clipboard_image_data_decoder_.reset(); | |
103 | |
104 // Write the decoded image data to clipboard. | |
105 if (!bitmap.empty() && !bitmap.isNull()) | |
106 clipboard_writer_->WriteImage(bitmap); | |
107 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.
| |
108 new ui::ScopedClipboardWriter(ui::CLIPBOARD_TYPE_COPY_PASTE)); | |
109 | |
110 base::ResetAndReturn(&image_save_success_callback_).Run(); | |
111 } | |
112 | |
113 void ClipboardExtensionHelper::OnImageDecodeCancel() { | |
114 base::ResetAndReturn(&image_save_error_callback_).Run( | |
115 std::string("Request canceled")); | |
116 } | |
117 | |
118 } // namespace extensions | |
OLD | NEW |