Roll src/third_party/skia e66fec2:2bd5d02
[chromium-blink-merge.git] / ppapi / shared_impl / flash_clipboard_format_registry.cc
blobfa43478a33954364579ce75d626768ef25c1aae1
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.
5 #include "ppapi/shared_impl/flash_clipboard_format_registry.h"
7 #include <cctype>
9 #include "base/numerics/safe_conversions.h"
11 namespace ppapi {
13 namespace {
15 // These values are chosen arbitrarily. Flash will never exceed these but if
16 // the interface becomes public, we can reconsider these.
17 const size_t kMaxNumFormats = 10;
18 const size_t kMaxFormatNameLength = 50;
20 // All formats in PP_Flash_Clipboard_Format should be added here.
21 const PP_Flash_Clipboard_Format kPredefinedFormats[] = {
22 PP_FLASH_CLIPBOARD_FORMAT_INVALID, PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT,
23 PP_FLASH_CLIPBOARD_FORMAT_HTML, PP_FLASH_CLIPBOARD_FORMAT_RTF};
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);
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;
36 } // namespace
38 FlashClipboardFormatRegistry::FlashClipboardFormatRegistry() {}
40 FlashClipboardFormatRegistry::~FlashClipboardFormatRegistry() {}
42 uint32_t FlashClipboardFormatRegistry::RegisterFormat(
43 const std::string& format_name) {
44 if (!IsValidFormatName(format_name) ||
45 custom_formats_.size() > kMaxNumFormats) {
46 return PP_FLASH_CLIPBOARD_FORMAT_INVALID;
48 uint32_t key = kFirstCustomFormat +
49 base::checked_cast<uint32_t>(custom_formats_.size());
50 custom_formats_[key] = format_name;
51 return key;
54 void FlashClipboardFormatRegistry::SetRegisteredFormat(
55 const std::string& format_name,
56 uint32_t format) {
57 custom_formats_[format] = format_name;
60 bool FlashClipboardFormatRegistry::IsFormatRegistered(uint32_t format) const {
61 return custom_formats_.find(format) != custom_formats_.end();
64 std::string FlashClipboardFormatRegistry::GetFormatName(uint32_t format) const {
65 FormatMap::const_iterator it = custom_formats_.find(format);
66 if (it == custom_formats_.end())
67 return std::string();
68 return it->second;
71 uint32_t FlashClipboardFormatRegistry::GetFormatID(
72 const std::string& format_name) const {
73 for (FormatMap::const_iterator it = custom_formats_.begin();
74 it != custom_formats_.end();
75 ++it) {
76 if (it->second == format_name)
77 return it->first;
79 return PP_FLASH_CLIPBOARD_FORMAT_INVALID;
82 // static
83 bool FlashClipboardFormatRegistry::IsValidPredefinedFormat(uint32_t format) {
84 if (format == PP_FLASH_CLIPBOARD_FORMAT_INVALID)
85 return false;
86 return format < kFirstCustomFormat;
89 } // namespace ppapi