OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extension_tabs_module.h" | 5 #include "chrome/browser/extensions/extension_tabs_module.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/stl_util-inl.h" |
12 #include "base/string_number_conversions.h" | 13 #include "base/string_number_conversions.h" |
13 #include "base/string_util.h" | 14 #include "base/string_util.h" |
14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
15 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
16 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 17 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
17 #include "chrome/browser/extensions/extension_host.h" | 18 #include "chrome/browser/extensions/extension_host.h" |
18 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
19 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 20 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
20 #include "chrome/browser/net/url_fixer_upper.h" | 21 #include "chrome/browser/net/url_fixer_upper.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
(...skipping 1234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1256 SendResultFromBitmap(*screen_capture); | 1257 SendResultFromBitmap(*screen_capture); |
1257 } | 1258 } |
1258 | 1259 |
1259 Release(); // Balanced in CaptureVisibleTabFunction::RunImpl(). | 1260 Release(); // Balanced in CaptureVisibleTabFunction::RunImpl(). |
1260 } | 1261 } |
1261 | 1262 |
1262 // Turn a bitmap of the screen into an image, set that image as the result, | 1263 // Turn a bitmap of the screen into an image, set that image as the result, |
1263 // and call SendResponse(). | 1264 // and call SendResponse(). |
1264 void CaptureVisibleTabFunction::SendResultFromBitmap( | 1265 void CaptureVisibleTabFunction::SendResultFromBitmap( |
1265 const SkBitmap& screen_capture) { | 1266 const SkBitmap& screen_capture) { |
1266 scoped_refptr<RefCountedBytes> image_data(new RefCountedBytes); | 1267 std::vector<unsigned char> data; |
1267 SkAutoLockPixels screen_capture_lock(screen_capture); | 1268 SkAutoLockPixels screen_capture_lock(screen_capture); |
1268 bool encoded = false; | 1269 bool encoded = false; |
1269 std::string mime_type; | 1270 std::string mime_type; |
1270 switch (image_format_) { | 1271 switch (image_format_) { |
1271 case FORMAT_JPEG: | 1272 case FORMAT_JPEG: |
1272 encoded = gfx::JPEGCodec::Encode( | 1273 encoded = gfx::JPEGCodec::Encode( |
1273 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), | 1274 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), |
1274 gfx::JPEGCodec::FORMAT_SkBitmap, | 1275 gfx::JPEGCodec::FORMAT_SkBitmap, |
1275 screen_capture.width(), | 1276 screen_capture.width(), |
1276 screen_capture.height(), | 1277 screen_capture.height(), |
1277 static_cast<int>(screen_capture.rowBytes()), | 1278 static_cast<int>(screen_capture.rowBytes()), |
1278 image_quality_, | 1279 image_quality_, |
1279 &image_data->data); | 1280 &data); |
1280 mime_type = keys::kMimeTypeJpeg; | 1281 mime_type = keys::kMimeTypeJpeg; |
1281 break; | 1282 break; |
1282 case FORMAT_PNG: | 1283 case FORMAT_PNG: |
1283 encoded = gfx::PNGCodec::EncodeBGRASkBitmap( | 1284 encoded = gfx::PNGCodec::EncodeBGRASkBitmap( |
1284 screen_capture, | 1285 screen_capture, |
1285 true, // Discard transparency. | 1286 true, // Discard transparency. |
1286 &image_data->data); | 1287 &data); |
1287 mime_type = keys::kMimeTypePng; | 1288 mime_type = keys::kMimeTypePng; |
1288 break; | 1289 break; |
1289 default: | 1290 default: |
1290 NOTREACHED() << "Invalid image format."; | 1291 NOTREACHED() << "Invalid image format."; |
1291 } | 1292 } |
1292 | 1293 |
1293 if (!encoded) { | 1294 if (!encoded) { |
1294 error_ = keys::kInternalVisibleTabCaptureError; | 1295 error_ = keys::kInternalVisibleTabCaptureError; |
1295 SendResponse(false); | 1296 SendResponse(false); |
1296 return; | 1297 return; |
1297 } | 1298 } |
1298 | 1299 |
1299 std::string base64_result; | 1300 std::string base64_result; |
1300 std::string stream_as_string; | 1301 base::StringPiece stream_as_string( |
1301 stream_as_string.resize(image_data->data.size()); | 1302 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
1302 memcpy(&stream_as_string[0], | |
1303 reinterpret_cast<const char*>(&image_data->data[0]), | |
1304 image_data->data.size()); | |
1305 | 1303 |
1306 base::Base64Encode(stream_as_string, &base64_result); | 1304 base::Base64Encode(stream_as_string, &base64_result); |
1307 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | 1305 base64_result.insert(0, base::StringPrintf("data:%s;base64,", |
1308 mime_type.c_str())); | 1306 mime_type.c_str())); |
1309 result_.reset(new StringValue(base64_result)); | 1307 result_.reset(new StringValue(base64_result)); |
1310 SendResponse(true); | 1308 SendResponse(true); |
1311 } | 1309 } |
1312 | 1310 |
1313 bool DetectTabLanguageFunction::RunImpl() { | 1311 bool DetectTabLanguageFunction::RunImpl() { |
1314 int tab_id = 0; | 1312 int tab_id = 0; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1375 // called for every API call the extension made. | 1373 // called for every API call the extension made. |
1376 GotLanguage(language); | 1374 GotLanguage(language); |
1377 } | 1375 } |
1378 | 1376 |
1379 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1377 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
1380 result_.reset(Value::CreateStringValue(language.c_str())); | 1378 result_.reset(Value::CreateStringValue(language.c_str())); |
1381 SendResponse(true); | 1379 SendResponse(true); |
1382 | 1380 |
1383 Release(); // Balanced in Run() | 1381 Release(); // Balanced in Run() |
1384 } | 1382 } |
OLD | NEW |