1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "ppapi/shared_impl/flash_clipboard_format_registry.h" 6 7 #include <cctype> 8 9 namespace ppapi { 10 11 namespace { 12 13 // These values are chosen arbitrarily. Flash will never exceed these but if 14 // the interface becomes public, we can reconsider these. 15 const size_t kMaxNumFormats = 10; 16 const size_t kMaxFormatNameLength = 50; 17 18 // All formats in PP_Flash_Clipboard_Format should be added here. 19 const PP_Flash_Clipboard_Format kPredefinedFormats[] = { 20 PP_FLASH_CLIPBOARD_FORMAT_INVALID, PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, 21 PP_FLASH_CLIPBOARD_FORMAT_HTML, PP_FLASH_CLIPBOARD_FORMAT_RTF}; 22 23 // The first custom format ID will be the ID after that max value in 24 // PP_Flash_Clipboard_Format. 25 const size_t kFirstCustomFormat = arraysize(kPredefinedFormats); 26 27 // Checks the validity of the given format name. IsValidFormatName(const std::string & format_name)28bool IsValidFormatName(const std::string& format_name) { 29 if (format_name.empty() || format_name.length() > kMaxFormatNameLength) 30 return false; 31 return true; 32 } 33 34 } // namespace 35 FlashClipboardFormatRegistry()36FlashClipboardFormatRegistry::FlashClipboardFormatRegistry() {} 37 ~FlashClipboardFormatRegistry()38FlashClipboardFormatRegistry::~FlashClipboardFormatRegistry() {} 39 RegisterFormat(const std::string & format_name)40uint32_t FlashClipboardFormatRegistry::RegisterFormat( 41 const std::string& format_name) { 42 if (!IsValidFormatName(format_name) || 43 custom_formats_.size() > kMaxNumFormats) { 44 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; 45 } 46 uint32_t key = kFirstCustomFormat + custom_formats_.size(); 47 custom_formats_[key] = format_name; 48 return key; 49 } 50 SetRegisteredFormat(const std::string & format_name,uint32_t format)51void FlashClipboardFormatRegistry::SetRegisteredFormat( 52 const std::string& format_name, 53 uint32_t format) { 54 custom_formats_[format] = format_name; 55 } 56 IsFormatRegistered(uint32_t format) const57bool FlashClipboardFormatRegistry::IsFormatRegistered(uint32_t format) const { 58 return custom_formats_.find(format) != custom_formats_.end(); 59 } 60 GetFormatName(uint32_t format) const61std::string FlashClipboardFormatRegistry::GetFormatName(uint32_t format) const { 62 FormatMap::const_iterator it = custom_formats_.find(format); 63 if (it == custom_formats_.end()) 64 return std::string(); 65 return it->second; 66 } 67 GetFormatID(const std::string & format_name) const68uint32_t FlashClipboardFormatRegistry::GetFormatID( 69 const std::string& format_name) const { 70 for (FormatMap::const_iterator it = custom_formats_.begin(); 71 it != custom_formats_.end(); 72 ++it) { 73 if (it->second == format_name) 74 return it->first; 75 } 76 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; 77 } 78 79 // static IsValidPredefinedFormat(uint32_t format)80bool FlashClipboardFormatRegistry::IsValidPredefinedFormat(uint32_t format) { 81 if (format == PP_FLASH_CLIPBOARD_FORMAT_INVALID) 82 return false; 83 return format < kFirstCustomFormat; 84 } 85 86 } // namespace ppapi 87