OLD | NEW |
---|---|
(Empty) | |
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 { | |
brettw
2012/10/31 18:05:53
Nit: blank line after this.
raymes
2012/11/01 19:57:54
Done.
| |
12 // These values are chosen arbitrarily. Flash will never exceed these but if | |
13 // the interface becomes public, we can reconsider these. | |
14 const size_t kMaxNumFormats = 10; | |
15 const size_t kMaxFormatNameLength = 50; | |
16 | |
17 // All formats in PP_Flash_Clipboard_Format should be added here. | |
18 const PP_Flash_Clipboard_Format kPredefinedFormats[] = { | |
19 PP_FLASH_CLIPBOARD_FORMAT_INVALID, | |
20 PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | |
21 PP_FLASH_CLIPBOARD_FORMAT_HTML, | |
22 PP_FLASH_CLIPBOARD_FORMAT_RTF | |
23 }; | |
24 | |
25 // The first custom format ID will be the ID after that max value in | |
26 // PP_Flash_Clipboard_Format. | |
27 const size_t kFirstCustomFormat = arraysize(kPredefinedFormats); | |
28 | |
29 // Checks the validity of the given format name. | |
30 bool IsValidFormatName(const std::string& format_name) { | |
31 if (format_name.empty() || format_name.length() > kMaxFormatNameLength) | |
32 return false; | |
33 return true; | |
34 } | |
35 | |
36 } // namespace | |
37 | |
38 FlashClipboardFormatRegistry::FlashClipboardFormatRegistry() { | |
39 } | |
40 | |
41 FlashClipboardFormatRegistry::~FlashClipboardFormatRegistry() { | |
42 } | |
43 | |
44 uint32_t FlashClipboardFormatRegistry::RegisterFormat( | |
45 const std::string& format_name) { | |
46 if (!IsValidFormatName(format_name) || | |
47 custom_formats_.size() > kMaxNumFormats) { | |
48 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; | |
49 } | |
50 uint32_t key = kFirstCustomFormat + custom_formats_.size(); | |
51 custom_formats_[key] = format_name; | |
52 return key; | |
53 } | |
54 | |
55 void FlashClipboardFormatRegistry::SetRegisteredFormat( | |
56 const std::string& format_name, | |
57 uint32_t format) { | |
58 custom_formats_[format] = format_name; | |
59 } | |
60 | |
61 bool FlashClipboardFormatRegistry::IsFormatRegistered(uint32_t format) const { | |
62 return custom_formats_.find(format) != custom_formats_.end(); | |
63 } | |
64 | |
65 std::string FlashClipboardFormatRegistry::GetFormatName( | |
66 uint32_t format) const { | |
67 FormatMap::const_iterator it = custom_formats_.find(format); | |
68 if (it == custom_formats_.end()) | |
69 return std::string(); | |
70 return it->second; | |
71 } | |
72 | |
73 uint32_t FlashClipboardFormatRegistry::GetFormatID( | |
74 const std::string& format_name) const { | |
75 for (FormatMap::const_iterator it = custom_formats_.begin(); | |
76 it != custom_formats_.end(); ++it) { | |
77 if (it->second == format_name) | |
78 return it->first; | |
79 } | |
80 return PP_FLASH_CLIPBOARD_FORMAT_INVALID; | |
81 } | |
82 | |
83 // static | |
84 bool FlashClipboardFormatRegistry::IsValidPredefinedFormat(uint32_t format) { | |
85 if (format == PP_FLASH_CLIPBOARD_FORMAT_INVALID) | |
86 return false; | |
87 return format < kFirstCustomFormat; | |
88 } | |
89 | |
90 } // ppapi | |
brettw
2012/10/31 18:05:53
Nit: need "namespace"
raymes
2012/11/01 19:57:54
Done.
| |
OLD | NEW |