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