Chromium Code Reviews| Index: ppapi/shared_impl/flash_clipboard_format_registry.cc |
| diff --git a/ppapi/shared_impl/flash_clipboard_format_registry.cc b/ppapi/shared_impl/flash_clipboard_format_registry.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9d03fca96a369b9f485bdb0192d8b094d5bd6dc7 |
| --- /dev/null |
| +++ b/ppapi/shared_impl/flash_clipboard_format_registry.cc |
| @@ -0,0 +1,85 @@ |
| +#include "ppapi/shared_impl/flash_clipboard_format_registry.h" |
| + |
| +#include <cctype> |
| + |
| +namespace ppapi { |
| + |
| +namespace { |
| +// These values are chosen arbitrarily. Flash will never exceed these but if |
| +// the interface becomes public, we can reconsider these. |
| +const size_t kMaxNumFormats = 10; |
| +const size_t kMaxFormatNameLength = 50; |
| + |
| +// All formats in PP_Flash_Clipboard_Format should be added here. |
| +const PP_Flash_Clipboard_Format kPredefinedFormats[] = { |
| + PP_FLASH_CLIPBOARD_FORMAT_INVALID, |
| + PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, |
| + PP_FLASH_CLIPBOARD_FORMAT_HTML, |
| + PP_FLASH_CLIPBOARD_FORMAT_RTF |
| +}; |
| + |
| +// The first custom format ID will be the ID after that max value in |
| +// PP_Flash_Clipboard_Format. |
| +const size_t kFirstCustomFormat = arraysize(kPredefinedFormats); |
| + |
| +// Checks the validity of the given format name. |
| +bool IsValidFormatName(const std::string& format_name) { |
| + if (format_name.empty() || format_name.length() > kMaxFormatNameLength) |
| + return false; |
| + return true; |
| +} |
| + |
| +} // namespace |
| + |
| +FlashClipboardFormatRegistry::FlashClipboardFormatRegistry() { |
| +} |
| + |
| +FlashClipboardFormatRegistry::~FlashClipboardFormatRegistry() { |
| +} |
| + |
| +uint32_t FlashClipboardFormatRegistry::RegisterFormat( |
| + const std::string& format_name) { |
| + if (!IsValidFormatName(format_name) || |
| + custom_formats_.size() > kMaxNumFormats) { |
| + return PP_FLASH_CLIPBOARD_FORMAT_INVALID; |
| + } |
| + uint32_t key = kFirstCustomFormat + custom_formats_.size(); |
| + custom_formats_[key] = format_name; |
| + return key; |
| +} |
| + |
| +void FlashClipboardFormatRegistry::SetRegisteredFormat( |
| + const std::string& format_name, |
| + uint32_t format) { |
| + custom_formats_[format] = format_name; |
| +} |
| + |
| +bool FlashClipboardFormatRegistry::IsFormatRegistered(uint32_t format) { |
|
dcheng
2012/10/30 02:21:14
const
raymes
2012/10/30 02:53:03
Done.
|
| + return custom_formats_.count(format) == 1; |
|
dcheng
2012/10/30 02:21:14
custom_formats_.find(format) != custom_formats.end
raymes
2012/10/30 02:53:03
Done.
|
| +} |
| + |
| +std::string FlashClipboardFormatRegistry::GetFormatName(uint32_t format) { |
|
dcheng
2012/10/30 02:21:14
const
raymes
2012/10/30 02:53:03
Done.
|
| + FormatMap::iterator it = custom_formats_.find(format); |
|
dcheng
2012/10/30 02:21:14
const_iterator
raymes
2012/10/30 02:53:03
Done.
|
| + if (it == custom_formats_.end()) |
| + return std::string(); |
| + return it->second; |
| +} |
| + |
| +uint32_t FlashClipboardFormatRegistry::GetFormatID( |
| + const std::string& format_name) { |
|
dcheng
2012/10/30 02:21:14
This method can be const.
raymes
2012/10/30 02:53:03
Done.
|
| + for (FormatMap::iterator it = custom_formats_.begin(); |
|
dcheng
2012/10/30 02:21:14
const_iterator
raymes
2012/10/30 02:53:03
Done.
|
| + it != custom_formats_.end(); ++it) { |
| + if (it->second == format_name) |
| + return it->first; |
| + } |
| + return PP_FLASH_CLIPBOARD_FORMAT_INVALID; |
| +} |
| + |
| +// static |
| +bool FlashClipboardFormatRegistry::IsValidPredefinedFormat(uint32_t format) { |
| + if (format == PP_FLASH_CLIPBOARD_FORMAT_INVALID) |
| + return false; |
| + return format < kFirstCustomFormat; |
| +} |
| + |
| +} // ppapi |