Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/hyphenation/hyphenation_impl.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <map> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/files/file.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/strings/stringprintf.h" | |
| 14 #include "mojo/public/cpp/system/platform_handle.h" | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 using DictionaryFileMap = std::unordered_map<std::string, base::File>; | |
| 19 | |
| 20 static bool IsValidLocale(const std::string& locale) { | |
| 21 return std::all_of(locale.cbegin(), locale.cend(), | |
| 22 [](const char ch) { return isalpha(ch) || ch == '-'; }); | |
| 23 } | |
| 24 | |
| 25 static base::File& GetDictionaryFile(const std::string& locale) { | |
| 26 // Keep Files open in the cache for subsequent calls. | |
| 27 CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ()); | |
| 28 | |
| 29 // TODO(kojii): Replace find+insert with emplace()+std::piecewise_construct | |
| 30 // when std::map::emplace() is fully supported on all configurations. | |
|
dcheng
2016/07/13 02:56:28
FWIW, the C++ style owners don't really like piece
kojii
2016/07/13 05:02:28
Oh, thanks for the info. Removed the TODO. I origi
| |
| 31 const auto& it = cache.find(locale); | |
| 32 if (it != cache.end()) | |
| 33 return it->second; | |
| 34 base::File file_on_stack; | |
| 35 const auto& inserted = cache.insert( | |
| 36 std::make_pair(locale, std::move(file_on_stack))); | |
|
dcheng
2016/07/13 02:56:29
Slightly shorter to write:
const auto& inserted =
kojii
2016/07/13 05:02:29
Thought some compiler complained, but it looks lik
| |
| 37 base::File& file = inserted.first->second; | |
| 38 DCHECK(!file.IsValid()); | |
| 39 | |
| 40 #if defined(OS_ANDROID) | |
| 41 base::FilePath dir("/system/usr/hyphen-data"); | |
| 42 #else | |
| 43 #error "This configuration is not supported." | |
| 44 #endif | |
| 45 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); | |
| 46 base::FilePath path = dir.AppendASCII(filename); | |
| 47 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | |
| 48 return file; | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 namespace hyphenation { | |
| 54 | |
| 55 HyphenationImpl::HyphenationImpl(blink::mojom::HyphenationRequest request) | |
| 56 : binding_(this, std::move(request)) {} | |
| 57 | |
| 58 HyphenationImpl::~HyphenationImpl() {} | |
| 59 | |
| 60 // static | |
| 61 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { | |
| 62 new HyphenationImpl(std::move(request)); | |
| 63 } | |
| 64 | |
| 65 void HyphenationImpl::OpenDictionary(const mojo::String& locale, | |
| 66 const OpenDictionaryCallback& callback) { | |
| 67 mojo::ScopedHandle handle; | |
| 68 if (IsValidLocale(locale)) { | |
| 69 base::File& file = GetDictionaryFile(locale); | |
| 70 if (file.IsValid()) | |
| 71 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); | |
| 72 } | |
| 73 callback.Run(std::move(handle)); | |
| 74 } | |
| 75 | |
| 76 } // namespace hyphenation | |
| OLD | NEW |