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" |
11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
12 #include "extensions/browser/extension_function.h" | 12 #include "extensions/browser/extension_function.h" |
13 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
14 #include "ui/gfx/codec/jpeg_codec.h" | 14 #include "ui/gfx/codec/jpeg_codec.h" |
15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
16 #include "ui/gfx/display.h" | 16 #include "ui/gfx/display.h" |
17 #include "ui/gfx/geometry/size_conversions.h" | 17 #include "ui/gfx/geometry/size_conversions.h" |
18 #include "ui/gfx/screen.h" | 18 #include "ui/gfx/screen.h" |
19 | 19 |
20 using content::RenderWidgetHost; | 20 using content::RenderWidgetHost; |
21 using content::RenderWidgetHostView; | 21 using content::RenderWidgetHostView; |
22 using content::WebContents; | 22 using content::WebContents; |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
| 26 using core_api::extension_types::ImageDetails; |
| 27 |
26 bool CaptureWebContentsFunction::HasPermission() { | 28 bool CaptureWebContentsFunction::HasPermission() { |
27 return true; | 29 return true; |
28 } | 30 } |
29 | 31 |
30 bool CaptureWebContentsFunction::RunAsync() { | 32 bool CaptureWebContentsFunction::RunAsync() { |
31 EXTENSION_FUNCTION_VALIDATE(args_); | 33 EXTENSION_FUNCTION_VALIDATE(args_); |
32 | 34 |
33 context_id_ = extension_misc::kCurrentWindowId; | 35 context_id_ = extension_misc::kCurrentWindowId; |
34 args_->GetInteger(0, &context_id_); | 36 args_->GetInteger(0, &context_id_); |
35 | 37 |
36 scoped_ptr<ImageDetails> image_details; | 38 scoped_ptr<ImageDetails> image_details; |
37 if (args_->GetSize() > 1) { | 39 if (args_->GetSize() > 1) { |
38 base::Value* spec = NULL; | 40 base::Value* spec = NULL; |
39 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec); | 41 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec); |
40 image_details = ImageDetails::FromValue(*spec); | 42 image_details = ImageDetails::FromValue(*spec); |
41 } | 43 } |
42 | 44 |
43 if (!IsScreenshotEnabled()) | 45 if (!IsScreenshotEnabled()) |
44 return false; | 46 return false; |
45 | 47 |
46 WebContents* contents = GetWebContentsForID(context_id_); | 48 WebContents* contents = GetWebContentsForID(context_id_); |
47 if (!contents) | 49 if (!contents) |
48 return false; | 50 return false; |
49 | 51 |
50 // The default format and quality setting used when encoding jpegs. | 52 // The default format and quality setting used when encoding jpegs. |
51 const ImageDetails::Format kDefaultFormat = ImageDetails::FORMAT_JPEG; | 53 const core_api::extension_types::ImageFormat kDefaultFormat = |
| 54 core_api::extension_types::IMAGE_FORMAT_JPEG; |
52 const int kDefaultQuality = 90; | 55 const int kDefaultQuality = 90; |
53 | 56 |
54 image_format_ = kDefaultFormat; | 57 image_format_ = kDefaultFormat; |
55 image_quality_ = kDefaultQuality; | 58 image_quality_ = kDefaultQuality; |
56 | 59 |
57 if (image_details) { | 60 if (image_details) { |
58 if (image_details->format != ImageDetails::FORMAT_NONE) | 61 if (image_details->format != |
| 62 core_api::extension_types::IMAGE_FORMAT_NONE) |
59 image_format_ = image_details->format; | 63 image_format_ = image_details->format; |
60 if (image_details->quality.get()) | 64 if (image_details->quality.get()) |
61 image_quality_ = *image_details->quality; | 65 image_quality_ = *image_details->quality; |
62 } | 66 } |
63 | 67 |
64 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 | 68 // TODO(miu): Account for fullscreen render widget? http://crbug.com/419878 |
65 RenderWidgetHostView* const view = contents->GetRenderWidgetHostView(); | 69 RenderWidgetHostView* const view = contents->GetRenderWidgetHostView(); |
66 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; | 70 RenderWidgetHost* const host = view ? view->GetRenderWidgetHost() : nullptr; |
67 if (!view || !host) { | 71 if (!view || !host) { |
68 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); | 72 OnCaptureFailure(FAILURE_REASON_VIEW_INVISIBLE); |
(...skipping 30 matching lines...) Expand all Loading... |
99 } | 103 } |
100 OnCaptureFailure(FAILURE_REASON_UNKNOWN); | 104 OnCaptureFailure(FAILURE_REASON_UNKNOWN); |
101 } | 105 } |
102 | 106 |
103 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { | 107 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { |
104 std::vector<unsigned char> data; | 108 std::vector<unsigned char> data; |
105 SkAutoLockPixels screen_capture_lock(bitmap); | 109 SkAutoLockPixels screen_capture_lock(bitmap); |
106 bool encoded = false; | 110 bool encoded = false; |
107 std::string mime_type; | 111 std::string mime_type; |
108 switch (image_format_) { | 112 switch (image_format_) { |
109 case ImageDetails::FORMAT_JPEG: | 113 case core_api::extension_types::IMAGE_FORMAT_JPEG: |
110 encoded = gfx::JPEGCodec::Encode( | 114 encoded = gfx::JPEGCodec::Encode( |
111 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 115 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
112 gfx::JPEGCodec::FORMAT_SkBitmap, | 116 gfx::JPEGCodec::FORMAT_SkBitmap, |
113 bitmap.width(), | 117 bitmap.width(), |
114 bitmap.height(), | 118 bitmap.height(), |
115 static_cast<int>(bitmap.rowBytes()), | 119 static_cast<int>(bitmap.rowBytes()), |
116 image_quality_, | 120 image_quality_, |
117 &data); | 121 &data); |
118 mime_type = kMimeTypeJpeg; | 122 mime_type = kMimeTypeJpeg; |
119 break; | 123 break; |
120 case ImageDetails::FORMAT_PNG: | 124 case core_api::extension_types::IMAGE_FORMAT_PNG: |
121 encoded = | 125 encoded = |
122 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, | 126 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, |
123 true, // Discard transparency. | 127 true, // Discard transparency. |
124 &data); | 128 &data); |
125 mime_type = kMimeTypePng; | 129 mime_type = kMimeTypePng; |
126 break; | 130 break; |
127 default: | 131 default: |
128 NOTREACHED() << "Invalid image format."; | 132 NOTREACHED() << "Invalid image format."; |
129 } | 133 } |
130 | 134 |
131 if (!encoded) { | 135 if (!encoded) { |
132 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED); | 136 OnCaptureFailure(FAILURE_REASON_ENCODING_FAILED); |
133 return; | 137 return; |
134 } | 138 } |
135 | 139 |
136 std::string base64_result; | 140 std::string base64_result; |
137 base::StringPiece stream_as_string( | 141 base::StringPiece stream_as_string( |
138 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | 142 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
139 | 143 |
140 base::Base64Encode(stream_as_string, &base64_result); | 144 base::Base64Encode(stream_as_string, &base64_result); |
141 base64_result.insert( | 145 base64_result.insert( |
142 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 146 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
143 SetResult(new base::StringValue(base64_result)); | 147 SetResult(new base::StringValue(base64_result)); |
144 SendResponse(true); | 148 SendResponse(true); |
145 } | 149 } |
146 | 150 |
147 } // namespace extensions | 151 } // namespace extensions |
OLD | NEW |