Index: chrome/browser/extensions/extension_tabs_module.cc =================================================================== --- chrome/browser/extensions/extension_tabs_module.cc (revision 29096) +++ chrome/browser/extensions/extension_tabs_module.cc (working copy) @@ -5,6 +5,7 @@ #include "chrome/browser/extensions/extension_tabs_module.h" #include "app/gfx/codec/jpeg_codec.h" +#include "app/gfx/codec/png_codec.h" #include "base/string_util.h" #include "chrome/browser/browser.h" #include "chrome/browser/browser_list.h" @@ -716,19 +717,50 @@ Browser* browser; // windowId defaults to "current" window. int window_id = -1; + int quality = 90; + ImageFormat image_format = IMAGE_JPEG; - if (!args_->IsType(Value::TYPE_NULL)) { - EXTENSION_FUNCTION_VALIDATE(args_->GetAsInteger(&window_id)); + EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); + const ListValue* args = static_cast(args_); + + Value* val; + args->Get(0, &val); + if (!val->IsType(Value::TYPE_NULL)) { + EXTENSION_FUNCTION_VALIDATE(val->GetAsInteger(&window_id)); browser = GetBrowserInProfileWithId(profile(), window_id, &error_); } else { browser = dispatcher()->GetBrowser(); } - if (!browser) { error_ = keys::kNoCurrentWindowError; return false; } + args->Get(1, &val); + if (!val->IsType(Value::TYPE_NULL)) { + EXTENSION_FUNCTION_VALIDATE(val->IsType(Value::TYPE_DICTIONARY)); + const DictionaryValue* params = static_cast(val); + if (params->HasKey(keys::kQualityKey)) { + EXTENSION_FUNCTION_VALIDATE(params->GetInteger( + keys::kQualityKey, + &quality)); + } + if (params->HasKey(keys::kFormatKey)) { + std::string format; + EXTENSION_FUNCTION_VALIDATE(params->GetString( + keys::kFormatKey, + &format)); + if (!format.compare(keys::kFormatValueJpeg) ) { + image_format = IMAGE_JPEG; + } else if (!format.compare(keys::kFormatValuePng) ) { + image_format = IMAGE_PNG; + } else { + error_ = keys::kNotSuuportedFormatError; + return false; + } + } + } + SkBitmap screen_capture; TabContents* tab_contents = browser->GetSelectedTabContents(); if (!tab_contents) { @@ -783,14 +815,26 @@ error_ = keys::kNotImplementedError; return false; #endif - scoped_refptr jpeg_data(new RefCountedBytes); + scoped_refptr bitmapdata(new RefCountedBytes); SkAutoLockPixels screen_capture_lock(screen_capture); - bool encoded = gfx::JPEGCodec::Encode( - reinterpret_cast(screen_capture.getAddr32(0, 0)), - gfx::JPEGCodec::FORMAT_BGRA, screen_capture.width(), - screen_capture.height(), - static_cast(screen_capture.rowBytes()), 90, - &jpeg_data->data); + bool encoded = false; + if (IMAGE_JPEG == image_format) { + encoded = gfx::JPEGCodec::Encode( + reinterpret_cast(screen_capture.getAddr32(0, 0)), + gfx::JPEGCodec::FORMAT_BGRA, screen_capture.width(), + screen_capture.height(), + static_cast(screen_capture.rowBytes()), quality, + &bitmapdata->data); + } else if (IMAGE_PNG == image_format) { + encoded = gfx::PNGCodec::Encode( + reinterpret_cast(screen_capture.getAddr32(0, 0)), + gfx::PNGCodec::FORMAT_BGRA, screen_capture.width(), + screen_capture.height(), + static_cast(screen_capture.rowBytes()), false, + &bitmapdata->data); + } else { + NOTREACHED(); + } if (!encoded) { error_ = ExtensionErrorUtils::FormatErrorMessage( keys::kInternalVisibleTabCaptureError, ""); @@ -799,13 +843,16 @@ std::string base64_result; std::string stream_as_string; - stream_as_string.resize(jpeg_data->data.size()); + stream_as_string.resize(bitmapdata->data.size()); memcpy(&stream_as_string[0], - reinterpret_cast(&jpeg_data->data[0]), - jpeg_data->data.size()); - + reinterpret_cast(&bitmapdata->data[0]), + bitmapdata->data.size()); + net::Base64Encode(stream_as_string, &base64_result); - base64_result.insert(0, "data:image/jpg;base64,"); + const char* mime_subtypes[] = {keys::kFormatValueJpeg, keys::kFormatValuePng}; + std::string mime = "data:image/"; + mime = mime + mime_subtypes[image_format] + ";base64,"; + base64_result.insert(0, mime.c_str()); result_.reset(new StringValue(base64_result)); return true; } Index: chrome/browser/extensions/extension_tabs_module.h =================================================================== --- chrome/browser/extensions/extension_tabs_module.h (revision 29096) +++ chrome/browser/extensions/extension_tabs_module.h (working copy) @@ -120,6 +120,10 @@ DECLARE_EXTENSION_FUNCTION_NAME("tabs.detectLanguage") }; class CaptureVisibleTabFunction : public SyncExtensionFunction { + enum ImageFormat { + IMAGE_JPEG, + IMAGE_PNG + }; virtual bool RunImpl(); DECLARE_EXTENSION_FUNCTION_NAME("tabs.captureVisibleTab") }; Index: chrome/browser/extensions/extension_tabs_module_constants.cc =================================================================== --- chrome/browser/extensions/extension_tabs_module_constants.cc (revision 29096) +++ chrome/browser/extensions/extension_tabs_module_constants.cc (working copy) @@ -8,6 +8,7 @@ const wchar_t kFavIconUrlKey[] = L"favIconUrl"; const wchar_t kFocusedKey[] = L"focused"; +const wchar_t kFormatKey[] = L"format"; const wchar_t kFromIndexKey[] = L"fromIndex"; const wchar_t kHeightKey[] = L"height"; const wchar_t kIdKey[] = L"id"; @@ -18,6 +19,7 @@ const wchar_t kOldPositionKey[] = L"oldPosition"; const wchar_t kOldWindowIdKey[] = L"oldWindowId"; const wchar_t kPopulateKey[] = L"populate"; +const wchar_t kQualityKey[] = L"quality"; const wchar_t kSelectedKey[] = L"selected"; const wchar_t kStatusKey[] = L"status"; const wchar_t kTabIdKey[] = L"tabId"; @@ -30,6 +32,9 @@ const wchar_t kWidthKey[] = L"width"; const wchar_t kWindowIdKey[] = L"windowId"; +const char kFormatValueJpeg[] = "jpeg"; +const char kFormatValuePng[] = "png"; + const char kStatusValueComplete[] = "complete"; const char kStatusValueLoading[] = "loading"; @@ -50,5 +55,6 @@ const char kMoreThanOneValuesError[] = "There should be only one value (either" "code or file) in the second argument."; const char kLoadFileError[] = "Failed to load file: \"*\". "; +const char kNotSuuportedFormatError[] = "Not supported image format"; } // namespace extension_tabs_module_constants Index: chrome/browser/extensions/extension_tabs_module_constants.h =================================================================== --- chrome/browser/extensions/extension_tabs_module_constants.h (revision 29096) +++ chrome/browser/extensions/extension_tabs_module_constants.h (working copy) @@ -12,6 +12,7 @@ // Keys used in serializing tab data & events. extern const wchar_t kFavIconUrlKey[]; extern const wchar_t kFocusedKey[]; +extern const wchar_t kFormatKey[]; extern const wchar_t kFromIndexKey[]; extern const wchar_t kHeightKey[]; extern const wchar_t kIdKey[]; @@ -22,6 +23,7 @@ extern const wchar_t kOldPositionKey[]; extern const wchar_t kOldWindowIdKey[]; extern const wchar_t kPopulateKey[]; +extern const wchar_t kQualityKey[]; extern const wchar_t kSelectedKey[]; extern const wchar_t kStatusKey[]; extern const wchar_t kTabIdKey[]; @@ -35,6 +37,9 @@ extern const wchar_t kWindowIdKey[]; // Value consts. +extern const char kFormatValueJpeg[]; +extern const char kFormatValuePng[]; + extern const char kStatusValueComplete[]; extern const char kStatusValueLoading[]; @@ -53,6 +58,7 @@ extern const char kNoCodeOrFileToExecuteError[]; extern const char kMoreThanOneValuesError[]; extern const char kLoadFileError[]; +extern const char kNotSuuportedFormatError[]; }; // namespace extension_tabs_module_constants Index: chrome/common/extensions/api/extension_api.json =================================================================== --- chrome/common/extensions/api/extension_api.json (revision 29096) +++ chrome/common/extensions/api/extension_api.json (working copy) @@ -589,6 +589,17 @@ "description": "The target window. Defaults to the current window." }, { + "type": "object", + "name": "params", + "optional": true, + "properties": { + "windowId": {"type": "integer", "optional": true, "minimum": 0}, + "format": {"type": "string", "optional": true, "description": "capture image format."}, + "quality": {"type": "integer", "optional": true, "maximum": 100, "description": "jpeg quality."} + }, + "description": "" + }, + { "type": "function", "name": "callback", "parameters": [ {"type": "string", "name": "dataUrl", "description": "A data URL of a JPEG encoding of the visible area of the captured tab. May be assigned to the 'src' property of an HTML Image element for display."} ]