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