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