Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include "ppapi/proxy/flash_clipboard_format_registry.h" | |
| 2 | |
| 3 #include <cctype> | |
| 4 | |
| 5 namespace ppapi { | |
| 6 namespace proxy { | |
| 7 | |
| 8 namespace { | |
| 9 // These values are chosen arbitrarily. Flash will never exceed these but if | |
| 10 // the interface becomes public, we can reconsider these. | |
| 11 const size_t kMaxNumFormats = 10; | |
| 12 const size_t kMaxFormatNameLength = 50; | |
| 13 | |
| 14 // All formats in PP_Flash_Clipboard_Format should be added here. | |
| 15 const PP_Flash_Clipboard_Format kPredefinedFormats[] = { | |
|
yzshen1
2012/10/29 16:55:46
Is it better to define something like PP_FLASH_CLI
raymes
2012/10/29 18:44:58
As discussed, this could have problems if more val
| |
| 16 PP_FLASH_CLIPBOARD_FORMAT_INVALID, | |
| 17 PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | |
| 18 PP_FLASH_CLIPBOARD_FORMAT_HTML, | |
| 19 PP_FLASH_CLIPBOARD_FORMAT_RTF | |
| 20 }; | |
| 21 | |
| 22 // The first custom format ID will be the ID after that max value in | |
| 23 // PP_Flash_Clipboard_Format. | |
| 24 const size_t kFirstCustomFormat = | |
| 25 sizeof(kPredefinedFormats) / sizeof(kPredefinedFormats[0]); | |
| 26 | |
| 27 // Checks the validity of the given format name. | |
| 28 bool IsValidFormatName(const std::string& format_name) { | |
| 29 if (format_name.length() == 0 || format_name.length() > kMaxFormatNameLength) | |
| 30 return false; | |
| 31 return true; | |
| 32 } | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 FlashClipboardFormatRegistry::FlashClipboardFormatRegistry() { | |
| 37 custom_formats_.resize(kFirstCustomFormat); | |
| 38 } | |
| 39 | |
| 40 FlashClipboardFormatRegistry::~FlashClipboardFormatRegistry() { | |
| 41 } | |
| 42 | |
| 43 uint32_t FlashClipboardFormatRegistry::RegisterFormat( | |
| 44 const std::string& format_name) { | |
| 45 if (!IsValidFormatName(format_name) || | |
| 46 custom_formats_.size() > kMaxNumFormats) { | |
| 47 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; | |
| 48 } | |
| 49 custom_formats_.push_back(format_name); | |
| 50 return custom_formats_.size() - 1; | |
| 51 } | |
| 52 | |
| 53 void FlashClipboardFormatRegistry::SetRegisteredFormat( | |
| 54 const std::string& format_name, | |
| 55 uint32_t format) { | |
| 56 if (format >= custom_formats_.size()) | |
| 57 custom_formats_.resize(format + 1); | |
| 58 custom_formats_[format] = format_name; | |
| 59 } | |
| 60 | |
| 61 bool FlashClipboardFormatRegistry::IsFormatRegistered(uint32_t format) { | |
| 62 return format >= kFirstCustomFormat && format < custom_formats_.size(); | |
| 63 } | |
| 64 | |
| 65 std::string FlashClipboardFormatRegistry::GetFormatName(uint32_t format) { | |
| 66 if (format >= kFirstCustomFormat && format < custom_formats_.size()) | |
| 67 return custom_formats_[format]; | |
| 68 return std::string(); | |
| 69 } | |
| 70 | |
| 71 uint32_t FlashClipboardFormatRegistry::GetFormatID( | |
| 72 const std::string& format_name) { | |
| 73 for (size_t i = kFirstCustomFormat; i < custom_formats_.size(); ++i) { | |
| 74 if (custom_formats_[i] == format_name) | |
| 75 return i; | |
| 76 } | |
| 77 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; | |
| 78 } | |
| 79 | |
| 80 // static | |
| 81 bool FlashClipboardFormatRegistry::IsValidClipboardType( | |
| 82 PP_Flash_Clipboard_Type type) { | |
| 83 return type == PP_FLASH_CLIPBOARD_TYPE_STANDARD || | |
| 84 type == PP_FLASH_CLIPBOARD_TYPE_SELECTION; | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 bool FlashClipboardFormatRegistry::IsValidPredefinedFormat(uint32_t format) { | |
| 89 if (format == PP_FLASH_CLIPBOARD_FORMAT_INVALID) | |
| 90 return false; | |
| 91 return format < kFirstCustomFormat; | |
| 92 } | |
| 93 | |
| 94 } // proxy | |
| 95 } // ppapi | |
| OLD | NEW |