| 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 #ifndef EXTENSIONS_BROWSER_API_CAPTURE_WEB_CONTENTS_FUNCTION_IMPL_H_ | |
| 6 #define EXTENSIONS_BROWSER_API_CAPTURE_WEB_CONTENTS_FUNCTION_IMPL_H_ | |
| 7 | |
| 8 #include "extensions/browser/api/capture_web_contents_function.h" | 5 #include "extensions/browser/api/capture_web_contents_function.h" |
| 9 | 6 |
| 10 #include "base/base64.h" | 7 #include "base/base64.h" |
| 11 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 12 #include "content/public/browser/render_view_host.h" | 9 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/render_widget_host_view.h" | 10 #include "content/public/browser/render_widget_host_view.h" |
| 14 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
| 15 #include "extensions/browser/extension_function.h" | 12 #include "extensions/browser/extension_function.h" |
| 16 #include "extensions/common/constants.h" | 13 #include "extensions/common/constants.h" |
| 17 #include "ui/gfx/codec/jpeg_codec.h" | 14 #include "ui/gfx/codec/jpeg_codec.h" |
| 18 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
| 19 | 16 |
| 20 using content::RenderViewHost; | 17 using content::RenderViewHost; |
| 21 using content::RenderWidgetHost; | 18 using content::RenderWidgetHost; |
| 22 using content::RenderWidgetHostView; | 19 using content::RenderWidgetHostView; |
| 23 using content::WebContents; | 20 using content::WebContents; |
| 24 | 21 |
| 25 namespace extensions { | 22 namespace extensions { |
| 26 | 23 |
| 27 template <typename T> | 24 bool CaptureWebContentsFunction::HasPermission() { |
| 28 bool CaptureWebContentsFunction<T>::HasPermission() { | |
| 29 return true; | 25 return true; |
| 30 } | 26 } |
| 31 | 27 |
| 32 template <typename T> | 28 bool CaptureWebContentsFunction::RunAsync() { |
| 33 bool CaptureWebContentsFunction<T>::RunAsync() { | 29 EXTENSION_FUNCTION_VALIDATE(args_); |
| 34 EXTENSION_FUNCTION_VALIDATE(T::args_); | |
| 35 | 30 |
| 36 context_id_ = extension_misc::kCurrentWindowId; | 31 context_id_ = extension_misc::kCurrentWindowId; |
| 37 T::args_->GetInteger(0, &context_id_); | 32 args_->GetInteger(0, &context_id_); |
| 38 | 33 |
| 39 scoped_ptr<ImageDetails> image_details; | 34 scoped_ptr<ImageDetails> image_details; |
| 40 if (T::args_->GetSize() > 1) { | 35 if (args_->GetSize() > 1) { |
| 41 base::Value* spec = NULL; | 36 base::Value* spec = NULL; |
| 42 EXTENSION_FUNCTION_VALIDATE(T::args_->Get(1, &spec) && spec); | 37 EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec); |
| 43 image_details = ImageDetails::FromValue(*spec); | 38 image_details = ImageDetails::FromValue(*spec); |
| 44 } | 39 } |
| 45 | 40 |
| 46 if (!IsScreenshotEnabled()) | 41 if (!IsScreenshotEnabled()) |
| 47 return false; | 42 return false; |
| 48 | 43 |
| 49 WebContents* contents = GetWebContentsForID(context_id_); | 44 WebContents* contents = GetWebContentsForID(context_id_); |
| 50 if (!contents) | 45 if (!contents) |
| 51 return false; | 46 return false; |
| 52 | 47 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 72 } | 67 } |
| 73 render_view_host->CopyFromBackingStore( | 68 render_view_host->CopyFromBackingStore( |
| 74 gfx::Rect(), | 69 gfx::Rect(), |
| 75 view->GetViewBounds().size(), | 70 view->GetViewBounds().size(), |
| 76 base::Bind(&CaptureWebContentsFunction::CopyFromBackingStoreComplete, | 71 base::Bind(&CaptureWebContentsFunction::CopyFromBackingStoreComplete, |
| 77 this), | 72 this), |
| 78 kN32_SkColorType); | 73 kN32_SkColorType); |
| 79 return true; | 74 return true; |
| 80 } | 75 } |
| 81 | 76 |
| 82 template <typename T> | 77 void CaptureWebContentsFunction::CopyFromBackingStoreComplete( |
| 83 void CaptureWebContentsFunction<T>::CopyFromBackingStoreComplete( | |
| 84 bool succeeded, | 78 bool succeeded, |
| 85 const SkBitmap& bitmap) { | 79 const SkBitmap& bitmap) { |
| 86 if (succeeded) { | 80 if (succeeded) { |
| 87 OnCaptureSuccess(bitmap); | 81 OnCaptureSuccess(bitmap); |
| 88 return; | 82 return; |
| 89 } | 83 } |
| 90 OnCaptureFailure(FAILURE_REASON_UNKNOWN); | 84 OnCaptureFailure(FAILURE_REASON_UNKNOWN); |
| 91 } | 85 } |
| 92 | 86 |
| 93 template <typename T> | 87 void CaptureWebContentsFunction::OnCaptureSuccess(const SkBitmap& bitmap) { |
| 94 void CaptureWebContentsFunction<T>::OnCaptureSuccess(const SkBitmap& bitmap) { | |
| 95 std::vector<unsigned char> data; | 88 std::vector<unsigned char> data; |
| 96 SkAutoLockPixels screen_capture_lock(bitmap); | 89 SkAutoLockPixels screen_capture_lock(bitmap); |
| 97 bool encoded = false; | 90 bool encoded = false; |
| 98 std::string mime_type; | 91 std::string mime_type; |
| 99 switch (image_format_) { | 92 switch (image_format_) { |
| 100 case ImageDetails::FORMAT_JPEG: | 93 case ImageDetails::FORMAT_JPEG: |
| 101 encoded = gfx::JPEGCodec::Encode( | 94 encoded = gfx::JPEGCodec::Encode( |
| 102 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), | 95 reinterpret_cast<unsigned char*>(bitmap.getAddr32(0, 0)), |
| 103 gfx::JPEGCodec::FORMAT_SkBitmap, | 96 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 104 bitmap.width(), | 97 bitmap.width(), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 124 return; | 117 return; |
| 125 } | 118 } |
| 126 | 119 |
| 127 std::string base64_result; | 120 std::string base64_result; |
| 128 base::StringPiece stream_as_string( | 121 base::StringPiece stream_as_string( |
| 129 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); | 122 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
| 130 | 123 |
| 131 base::Base64Encode(stream_as_string, &base64_result); | 124 base::Base64Encode(stream_as_string, &base64_result); |
| 132 base64_result.insert( | 125 base64_result.insert( |
| 133 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); | 126 0, base::StringPrintf("data:%s;base64,", mime_type.c_str())); |
| 134 T::SetResult(new base::StringValue(base64_result)); | 127 SetResult(new base::StringValue(base64_result)); |
| 135 T::SendResponse(true); | 128 SendResponse(true); |
| 136 } | |
| 137 | |
| 138 template <typename T> | |
| 139 bool CaptureWebContentsFunction<T>::ValidationFailure( | |
| 140 CaptureWebContentsFunction<T>* function) { | |
| 141 return T::ValidationFailure(function); | |
| 142 } | 129 } |
| 143 | 130 |
| 144 } // namespace extensions | 131 } // namespace extensions |
| 145 | |
| 146 #endif // EXTENSIONS_BROWSER_API_CAPTURE_WEB_CONTENTS_FUNCTION_IMPL_H_ | |
| OLD | NEW |