OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/hyphenation/hyphenation_impl.h" | 5 #include "content/browser/hyphenation/hyphenation_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/strings/string_util.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 #include "mojo/public/cpp/system/platform_handle.h" | 15 #include "mojo/public/cpp/system/platform_handle.h" |
15 | 16 |
16 namespace { | 17 namespace { |
17 | 18 |
18 using DictionaryFileMap = std::unordered_map<std::string, base::File>; | 19 using DictionaryFileMap = std::unordered_map<std::string, base::File>; |
19 | 20 |
20 static bool IsValidLocale(const std::string& locale) { | 21 static bool IsValidLocale(const std::string& locale) { |
21 return std::all_of(locale.cbegin(), locale.cend(), | 22 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) { |
22 [](const char ch) { return isalpha(ch) || ch == '-'; }); | 23 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-'; |
| 24 }); |
23 } | 25 } |
24 | 26 |
25 static base::File& GetDictionaryFile(const std::string& locale) { | 27 static base::File& GetDictionaryFile(const std::string& locale) { |
26 // Keep Files open in the cache for subsequent calls. | 28 // Keep Files open in the cache for subsequent calls. |
27 CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ()); | 29 CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ()); |
28 | 30 |
29 const auto& it = cache.find(locale); | 31 const auto& it = cache.find(locale); |
30 if (it != cache.end()) | 32 if (it != cache.end()) |
31 return it->second; | 33 return it->second; |
32 const auto& inserted = cache.insert(std::make_pair(locale, base::File())); | 34 const auto& inserted = cache.insert(std::make_pair(locale, base::File())); |
(...skipping 30 matching lines...) Expand all Loading... |
63 mojo::ScopedHandle handle; | 65 mojo::ScopedHandle handle; |
64 if (IsValidLocale(locale)) { | 66 if (IsValidLocale(locale)) { |
65 base::File& file = GetDictionaryFile(locale); | 67 base::File& file = GetDictionaryFile(locale); |
66 if (file.IsValid()) | 68 if (file.IsValid()) |
67 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); | 69 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); |
68 } | 70 } |
69 callback.Run(std::move(handle)); | 71 callback.Run(std::move(handle)); |
70 } | 72 } |
71 | 73 |
72 } // namespace hyphenation | 74 } // namespace hyphenation |
OLD | NEW |