| 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.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 1235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1257 SendResultFromBitmap(*screen_capture); | 1258 SendResultFromBitmap(*screen_capture); |
| 1258 } | 1259 } |
| 1259 | 1260 |
| 1260 Release(); // Balanced in CaptureVisibleTabFunction::RunImpl(). | 1261 Release(); // Balanced in CaptureVisibleTabFunction::RunImpl(). |
| 1261 } | 1262 } |
| 1262 | 1263 |
| 1263 // Turn a bitmap of the screen into an image, set that image as the result, | 1264 // Turn a bitmap of the screen into an image, set that image as the result, |
| 1264 // and call SendResponse(). | 1265 // and call SendResponse(). |
| 1265 void CaptureVisibleTabFunction::SendResultFromBitmap( | 1266 void CaptureVisibleTabFunction::SendResultFromBitmap( |
| 1266 const SkBitmap& screen_capture) { | 1267 const SkBitmap& screen_capture) { |
| 1267 scoped_refptr<RefCountedBytes> image_data(new RefCountedBytes); | 1268 std::vector<unsigned char> data; |
| 1268 SkAutoLockPixels screen_capture_lock(screen_capture); | 1269 SkAutoLockPixels screen_capture_lock(screen_capture); |
| 1269 bool encoded = false; | 1270 bool encoded = false; |
| 1270 std::string mime_type; | 1271 std::string mime_type; |
| 1271 switch (image_format_) { | 1272 switch (image_format_) { |
| 1272 case FORMAT_JPEG: | 1273 case FORMAT_JPEG: |
| 1273 encoded = gfx::JPEGCodec::Encode( | 1274 encoded = gfx::JPEGCodec::Encode( |
| 1274 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), | 1275 reinterpret_cast<unsigned char*>(screen_capture.getAddr32(0, 0)), |
| 1275 gfx::JPEGCodec::FORMAT_SkBitmap, | 1276 gfx::JPEGCodec::FORMAT_SkBitmap, |
| 1276 screen_capture.width(), | 1277 screen_capture.width(), |
| 1277 screen_capture.height(), | 1278 screen_capture.height(), |
| 1278 static_cast<int>(screen_capture.rowBytes()), | 1279 static_cast<int>(screen_capture.rowBytes()), |
| 1279 image_quality_, | 1280 image_quality_, |
| 1280 &image_data->data); | 1281 &data); |
| 1281 mime_type = keys::kMimeTypeJpeg; | 1282 mime_type = keys::kMimeTypeJpeg; |
| 1282 break; | 1283 break; |
| 1283 case FORMAT_PNG: | 1284 case FORMAT_PNG: |
| 1284 encoded = gfx::PNGCodec::EncodeBGRASkBitmap( | 1285 encoded = gfx::PNGCodec::EncodeBGRASkBitmap( |
| 1285 screen_capture, | 1286 screen_capture, |
| 1286 true, // Discard transparency. | 1287 true, // Discard transparency. |
| 1287 &image_data->data); | 1288 &data); |
| 1288 mime_type = keys::kMimeTypePng; | 1289 mime_type = keys::kMimeTypePng; |
| 1289 break; | 1290 break; |
| 1290 default: | 1291 default: |
| 1291 NOTREACHED() << "Invalid image format."; | 1292 NOTREACHED() << "Invalid image format."; |
| 1292 } | 1293 } |
| 1293 | 1294 |
| 1294 if (!encoded) { | 1295 if (!encoded) { |
| 1295 error_ = keys::kInternalVisibleTabCaptureError; | 1296 error_ = keys::kInternalVisibleTabCaptureError; |
| 1296 SendResponse(false); | 1297 SendResponse(false); |
| 1297 return; | 1298 return; |
| 1298 } | 1299 } |
| 1299 | 1300 |
| 1300 std::string base64_result; | 1301 std::string base64_result; |
| 1301 std::string stream_as_string; | 1302 base::StringPiece stream_as_string( |
| 1302 stream_as_string.resize(image_data->data.size()); | 1303 reinterpret_cast<const char*>(vector_as_array(&data)), data.size()); |
| 1303 memcpy(&stream_as_string[0], | |
| 1304 reinterpret_cast<const char*>(&image_data->data[0]), | |
| 1305 image_data->data.size()); | |
| 1306 | 1304 |
| 1307 base::Base64Encode(stream_as_string, &base64_result); | 1305 base::Base64Encode(stream_as_string, &base64_result); |
| 1308 base64_result.insert(0, base::StringPrintf("data:%s;base64,", | 1306 base64_result.insert(0, base::StringPrintf("data:%s;base64,", |
| 1309 mime_type.c_str())); | 1307 mime_type.c_str())); |
| 1310 result_.reset(new StringValue(base64_result)); | 1308 result_.reset(new StringValue(base64_result)); |
| 1311 SendResponse(true); | 1309 SendResponse(true); |
| 1312 } | 1310 } |
| 1313 | 1311 |
| 1314 bool DetectTabLanguageFunction::RunImpl() { | 1312 bool DetectTabLanguageFunction::RunImpl() { |
| 1315 int tab_id = 0; | 1313 int tab_id = 0; |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1376 // called for every API call the extension made. | 1374 // called for every API call the extension made. |
| 1377 GotLanguage(language); | 1375 GotLanguage(language); |
| 1378 } | 1376 } |
| 1379 | 1377 |
| 1380 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { | 1378 void DetectTabLanguageFunction::GotLanguage(const std::string& language) { |
| 1381 result_.reset(Value::CreateStringValue(language.c_str())); | 1379 result_.reset(Value::CreateStringValue(language.c_str())); |
| 1382 SendResponse(true); | 1380 SendResponse(true); |
| 1383 | 1381 |
| 1384 Release(); // Balanced in Run() | 1382 Release(); // Balanced in Run() |
| 1385 } | 1383 } |
| OLD | NEW |