Chromium Code Reviews| Index: extensions/browser/api/clipboard/clipboard_api.cc |
| diff --git a/extensions/browser/api/clipboard/clipboard_api.cc b/extensions/browser/api/clipboard/clipboard_api.cc |
| index ef7373b821cc50d80cd1cfa1222657623846b78b..7f323d6851f869ca8d8d66f5038558bf066bc258 100644 |
| --- a/extensions/browser/api/clipboard/clipboard_api.cc |
| +++ b/extensions/browser/api/clipboard/clipboard_api.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/lazy_instance.h" |
| #include "base/memory/ptr_util.h" |
| +#include "base/strings/string_util.h" |
| #include "base/values.h" |
| #include "extensions/browser/api/extensions_api_client.h" |
| #include "extensions/browser/event_router.h" |
| @@ -17,6 +18,9 @@ namespace extensions { |
| namespace clipboard = api::clipboard; |
| +const char kTextPlain[] = "text/plain"; |
| +const char kTextHtml[] = "text/html"; |
| + |
| static base::LazyInstance< |
| BrowserContextKeyedAPIFactory<ClipboardAPI>>::DestructorAtExit g_factory = |
| LAZY_INSTANCE_INITIALIZER; |
| @@ -54,8 +58,13 @@ ExtensionFunction::ResponseAction ClipboardSetImageDataFunction::Run() { |
| std::unique_ptr<clipboard::SetImageData::Params> params( |
| clipboard::SetImageData::Params::Create(*args_)); |
| EXTENSION_FUNCTION_VALIDATE(params); |
| + |
| + if (!IsAdditionalItemsParamValid(params->additional_items)) { |
| + return RespondNow(Error("Unsupported additional_items parameter data.")); |
| + } |
| + |
| ExtensionsAPIClient::Get()->SaveImageDataToClipboard( |
| - params->image_data, params->type, |
| + params->image_data, params->type, params->additional_items, |
| base::Bind(&ClipboardSetImageDataFunction::OnSaveImageDataSuccess, this), |
| base::Bind(&ClipboardSetImageDataFunction::OnSaveImageDataError, this)); |
| return RespondLater(); |
| @@ -70,4 +79,39 @@ void ClipboardSetImageDataFunction::OnSaveImageDataError( |
| Respond(Error(error)); |
| } |
| +bool ClipboardSetImageDataFunction::IsAdditionalItemsParamValid( |
| + const AdditionalDataItemList& items) { |
| + // It is possible for user to embed the base64 encoded image data in |
| + // the addtional data item. We allow maxumum 4kx4k pixels for the orignal |
| + // image size. Each pixel costs 4 byes, and base64 encoding ouput bytes |
| + // to input bytes ration is 4:3. We also add an extra 2k for other data |
| + // such as url, etc. |
| + const size_t max_item_data_bytes = |
| + std::ceil(4 * 1024 * 4 * 1024 * 4 / 3) * 4 + 2048; |
|
dcheng
2017/04/27 15:42:09
This is currently a runtime calculation, but it sh
jennyz
2017/05/16 18:22:03
Done.
|
| + |
| + if (items.size() > 2) |
| + return false; |
|
dcheng
2017/04/27 15:42:09
I would just omit this, this will get caught by th
jennyz
2017/05/16 18:22:03
Done.
|
| + bool has_text_plain = false; |
| + bool has_text_html = false; |
| + for (const clipboard::AdditionalDataItem& item : items) { |
| + std::string type = base::ToLowerASCII(item.type); |
| + if (type != kTextPlain && type != kTextHtml) |
| + return false; |
| + if (type == kTextPlain) { |
| + if (has_text_plain) |
| + return false; |
| + has_text_plain = true; |
| + } |
| + if (type == kTextHtml) { |
| + if (has_text_html) |
| + return false; |
| + has_text_html = true; |
| + } |
| + // Check maximum length of the string data. |
| + if (item.data.length() > max_item_data_bytes) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| } // namespace extensions |