OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/api/capture_web_contents_function.h" | 5 #include "extensions/browser/api/capture_web_contents_function.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "content/public/browser/render_widget_host.h" | 9 #include "content/public/browser/render_widget_host.h" |
10 #include "content/public/browser/render_widget_host_view.h" | 10 #include "content/public/browser/render_widget_host_view.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 } | 41 } |
42 | 42 |
43 if (!IsScreenshotEnabled()) | 43 if (!IsScreenshotEnabled()) |
44 return false; | 44 return false; |
45 | 45 |
46 WebContents* contents = GetWebContentsForID(context_id_); | 46 WebContents* contents = GetWebContentsForID(context_id_); |
47 if (!contents) | 47 if (!contents) |
48 return false; | 48 return false; |
49 | 49 |
50 // The default format and quality setting used when encoding jpegs. | 50 // The default format and quality setting used when encoding jpegs. |
51 const ImageDetails::Format kDefaultFormat = ImageDetails::FORMAT_JPEG; | 51 const core_api::extension_types::ImageDetailsFormat kDefaultFormat = |
| 52 core_api::extension_types::IMAGE_DETAILS_FORMAT_JPEG; |
52 const int kDefaultQuality = 90; | 53 const int kDefaultQuality = 90; |
53 | 54 |
54 image_format_ = kDefaultFormat; | 55 image_format_ = kDefaultFormat; |
55 image_quality_ = kDefaultQuality; | 56 image_quality_ = kDefaultQuality; |
56 | 57 |
57 if (image_details) { | 58 if (image_details) { |
58 if (image_details->format != ImageDetails::FORMAT_NONE) | 59 if (image_details->format != |
| 60 core_api::extension_types::IMAGE_DETAILS_FORMAT_NONE) |
59 image_format_ = image_details->format; | 61 image_format_ = image_details->format; |
60 if (image_details->quality.get()) | 62 if (image_details->quality.get()) |
61 image_quality_ = *image_details->quality; | 63 image_quality_ = *image_details->quality; |
62 } | 64 } |
63 | 65 |
64 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 | 66 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 |
65 RenderWidgetHostView* const view = contents->GetRenderWidgetHostView(); | 67 RenderWidgetHostView* const view = contents->GetRenderWidgetHostView(); |
66 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; | 68 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; |
67 if (!view || !host) { | 69 if (!view || !host) { |
68 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); | 70 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); |
(...skipping 30 matching lines...) Expand all Loading... |
99 } | 101 } |
100 OnCaptureFailure(FAILURE_REASON_UNKNOWN); | 102 OnCaptureFailure(FAILURE_REASON_UNKNOWN); |
101 } | 103 } |
102 | 104 |
103 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { | 105 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { |
104 std::vector<unsigned char> data; | 106 std::vector<unsigned char> data; |
105 SkAutoLockPixels screen_capture_lock(bitmap); | 107 SkAutoLockPixels screen_capture_lock(bitmap); |
106 bool encoded = false; | 108 bool encoded = false; |
107 std::string mime_type; | 109 std::string mime_type; |
108 switch (image_format_) { | 110 switch (image_format_) { |
109 case ImageDetails::FORMAT_JPEG: | 111 case core_api::extension_types::IMAGE_DETAILS_FORMAT_JPEG: |
110 encoded = gfx::JPEGCodec::Encode( | 112 encoded = gfx::JPEGCodec::Encode( |
111 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 113 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
112 gfx::JPEGCodec::FORMAT_SkBitmap, | 114 gfx::JPEGCodec::FORMAT_SkBitmap, |
113 bitmap.width(), | 115 bitmap.width(), |
114 bitmap.height(), | 116 bitmap.height(), |
115 static_cast<int>(bitmap.rowBytes()), | 117 static_cast<int>(bitmap.rowBytes()), |
116 image_quality_, | 118 image_quality_, |
117 &data); | 119 &data); |
118 mime_type = kMimeTypeJpeg; | 120 mime_type = kMimeTypeJpeg; |
119 break; | 121 break; |
120 case ImageDetails::FORMAT_PNG: | 122 case core_api::extension_types::IMAGE_DETAILS_FORMAT_PNG: |
121 encoded = | 123 encoded = |
122 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, | 124 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, |
123 true, // Discard transparency. | 125 true, // Discard transparency. |
124 &data); | 126 &data); |
125 mime_type = kMimeTypePng; | 127 mime_type = kMimeTypePng; |
126 break; | 128 break; |
127 default: | 129 default: |
128 NOTREACHED() << "Invalid image format."; | 130 NOTREACHED() << "Invalid image format."; |
129 } | 131 } |
130 | 132 |
131 if (!encoded) { | 133 if (!encoded) { |
132 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED); | 134 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED); |
133 return; | 135 return; |
134 } | 136 } |
135 | 137 |
136 std::string base64_result; | 138 std::string base64_result; |
137 base::StringPiece stream_as_string( | 139 base::StringPiece stream_as_string( |
138 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | 140 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
139 | 141 |
140 base::Base64Encode(stream_as_string, &base64_result); | 142 base::Base64Encode(stream_as_string, &base64_result); |
141 base64_result.insert( | 143 base64_result.insert( |
142 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 144 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
143 SetResult(new base::StringValue(base64_result)); | 145 SetResult(new base::StringValue(base64_result)); |
144 SendResponse(true); | 146 SendResponse(true); |
145 } | 147 } |
146 | 148 |
147 } // namespace extensions | 149 } // namespace extensions |
OLD | NEW |