Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "extensions/browser/api/clipboard/clipboard_api.h" | 5 #include "extensions/browser/api/clipboard/clipboard_api.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_util.h" | |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "extensions/browser/api/extensions_api_client.h" | 13 #include "extensions/browser/api/extensions_api_client.h" |
| 13 #include "extensions/browser/event_router.h" | 14 #include "extensions/browser/event_router.h" |
| 14 #include "ui/base/clipboard/clipboard_monitor.h" | 15 #include "ui/base/clipboard/clipboard_monitor.h" |
| 15 | 16 |
| 16 namespace extensions { | 17 namespace extensions { |
| 17 | 18 |
| 18 namespace clipboard = api::clipboard; | 19 namespace clipboard = api::clipboard; |
| 19 | 20 |
| 21 const char kTextPlain[] = "text/plain"; | |
| 22 const char kTextHtml[] = "text/html"; | |
| 23 | |
| 20 static base::LazyInstance< | 24 static base::LazyInstance< |
| 21 BrowserContextKeyedAPIFactory<ClipboardAPI>>::DestructorAtExit g_factory = | 25 BrowserContextKeyedAPIFactory<ClipboardAPI>>::DestructorAtExit g_factory = |
| 22 LAZY_INSTANCE_INITIALIZER; | 26 LAZY_INSTANCE_INITIALIZER; |
| 23 | 27 |
| 24 // static | 28 // static |
| 25 BrowserContextKeyedAPIFactory<ClipboardAPI>* | 29 BrowserContextKeyedAPIFactory<ClipboardAPI>* |
| 26 ClipboardAPI::GetFactoryInstance() { | 30 ClipboardAPI::GetFactoryInstance() { |
| 27 return g_factory.Pointer(); | 31 return g_factory.Pointer(); |
| 28 } | 32 } |
| 29 | 33 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 47 router->BroadcastEvent(std::move(event)); | 51 router->BroadcastEvent(std::move(event)); |
| 48 } | 52 } |
| 49 } | 53 } |
| 50 | 54 |
| 51 ClipboardSetImageDataFunction::~ClipboardSetImageDataFunction() {} | 55 ClipboardSetImageDataFunction::~ClipboardSetImageDataFunction() {} |
| 52 | 56 |
| 53 ExtensionFunction::ResponseAction ClipboardSetImageDataFunction::Run() { | 57 ExtensionFunction::ResponseAction ClipboardSetImageDataFunction::Run() { |
| 54 std::unique_ptr<clipboard::SetImageData::Params> params( | 58 std::unique_ptr<clipboard::SetImageData::Params> params( |
| 55 clipboard::SetImageData::Params::Create(*args_)); | 59 clipboard::SetImageData::Params::Create(*args_)); |
| 56 EXTENSION_FUNCTION_VALIDATE(params); | 60 EXTENSION_FUNCTION_VALIDATE(params); |
| 61 | |
| 62 if (!IsAdditionalItemsParamValid(params->additional_items)) { | |
| 63 return RespondNow(Error("Unsupported additional_items parameter data.")); | |
| 64 } | |
| 65 | |
| 57 ExtensionsAPIClient::Get()->SaveImageDataToClipboard( | 66 ExtensionsAPIClient::Get()->SaveImageDataToClipboard( |
| 58 params->image_data, params->type, | 67 params->image_data, params->type, params->additional_items, |
| 59 base::Bind(&ClipboardSetImageDataFunction::OnSaveImageDataSuccess, this), | 68 base::Bind(&ClipboardSetImageDataFunction::OnSaveImageDataSuccess, this), |
| 60 base::Bind(&ClipboardSetImageDataFunction::OnSaveImageDataError, this)); | 69 base::Bind(&ClipboardSetImageDataFunction::OnSaveImageDataError, this)); |
| 61 return RespondLater(); | 70 return RespondLater(); |
| 62 } | 71 } |
| 63 | 72 |
| 64 void ClipboardSetImageDataFunction::OnSaveImageDataSuccess() { | 73 void ClipboardSetImageDataFunction::OnSaveImageDataSuccess() { |
| 65 Respond(NoArguments()); | 74 Respond(NoArguments()); |
| 66 } | 75 } |
| 67 | 76 |
| 68 void ClipboardSetImageDataFunction::OnSaveImageDataError( | 77 void ClipboardSetImageDataFunction::OnSaveImageDataError( |
| 69 const std::string& error) { | 78 const std::string& error) { |
| 70 Respond(Error(error)); | 79 Respond(Error(error)); |
| 71 } | 80 } |
| 72 | 81 |
| 82 bool ClipboardSetImageDataFunction::IsAdditionalItemsParamValid( | |
| 83 const AdditionalDataItemList& items) { | |
| 84 // It is possible for user to embed the base64 encoded image data in | |
| 85 // the addtional data item. We allow maxumum 4kx4k pixels for the orignal | |
| 86 // image size. Each pixel costs 4 byes, and base64 encoding ouput bytes | |
| 87 // to input bytes ration is 4:3. We also add an extra 2k for other data | |
| 88 // such as url, etc. | |
| 89 const size_t max_item_data_bytes = | |
| 90 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.
| |
| 91 | |
| 92 if (items.size() > 2) | |
| 93 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.
| |
| 94 bool has_text_plain = false; | |
| 95 bool has_text_html = false; | |
| 96 for (const clipboard::AdditionalDataItem& item : items) { | |
| 97 std::string type = base::ToLowerASCII(item.type); | |
| 98 if (type != kTextPlain && type != kTextHtml) | |
| 99 return false; | |
| 100 if (type == kTextPlain) { | |
| 101 if (has_text_plain) | |
| 102 return false; | |
| 103 has_text_plain = true; | |
| 104 } | |
| 105 if (type == kTextHtml) { | |
| 106 if (has_text_html) | |
| 107 return false; | |
| 108 has_text_html = true; | |
| 109 } | |
| 110 // Check maximum length of the string data. | |
| 111 if (item.data.length() > max_item_data_bytes) | |
| 112 return false; | |
| 113 } | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 73 } // namespace extensions | 117 } // namespace extensions |
| OLD | NEW |