Chromium Code Reviews| Index: content/browser/hyphenation/hyphenation_impl.cc |
| diff --git a/content/browser/hyphenation/hyphenation_impl.cc b/content/browser/hyphenation/hyphenation_impl.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a1ed2956ba44dadca355aeb899020bf36d802da6 |
| --- /dev/null |
| +++ b/content/browser/hyphenation/hyphenation_impl.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/browser/hyphenation/hyphenation_impl.h" |
| + |
| +#include <algorithm> |
| +#include <map> |
| +#include <utility> |
| + |
| +#include "base/files/file.h" |
| +#include "base/files/file_path.h" |
| +#include "base/strings/stringprintf.h" |
| +#include "mojo/public/cpp/system/platform_handle.h" |
| + |
| +#if defined(HYPHENATION_DIR_HOME_FOR_TEST) |
| +#include "base/base_paths.h" |
| +#include "base/path_service.h" |
| +#endif |
| + |
| +namespace { |
| + |
| +using DictionaryFileMap = std::unordered_map<std::string, base::File>; |
| + |
| +static bool IsValidLocale(const std::string& locale) { |
| + return std::all_of(locale.cbegin(), locale.cend(), |
| + [](const char ch) { return isalpha(ch) || ch == '-'; }); |
| +} |
| + |
| +static base::File& GetDictionaryFile(const std::string& locale) { |
| + // Keep Files open in the cache for subsequent calls. |
| + CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ()); |
| + |
| + // TODO(kojii): Replace find+insert with emplace()+std::piecewise_construct |
| + // when std::map::emplace() is fully supported on all configurations. |
| + const auto& it = cache.find(locale); |
| + if (it != cache.end()) |
| + return it->second; |
| + base::File file_on_stack; |
| + const auto& inserted = cache.insert( |
| + std::make_pair(locale, std::move(file_on_stack))); |
| + base::File& file = inserted.first->second; |
| + DCHECK(!file.IsValid()); |
| + |
| + // Only ISO lang code is accepted. |
|
jam
2016/07/12 14:51:12
don't we want to check this and early exit before
|
| + if (!IsValidLocale(locale)) |
| + return file; |
| + |
| +#if defined(HYPHENATION_DIR_HOME_FOR_TEST) |
|
jam
2016/07/12 14:51:12
normally this is done by adding a command line fla
kojii
2016/07/12 17:32:21
This is really for dev/testing on other platforms
|
| + base::FilePath dir; |
| + if (!base::PathService::Get(base::DIR_HOME, &dir)) |
| + return file; |
|
jam
2016/07/12 14:51:12
here as well, don't we only want to put valid path
|
| +#elif defined(OS_ANDROID) |
| + base::FilePath dir("/system/usr/hyphen-data"); |
| +#else |
| +#error "This configuration is not supported." |
| +#endif |
| + std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); |
| + base::FilePath path = dir.AppendASCII(filename); |
| + file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| + return file; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace hyphenation { |
| + |
| +HyphenationImpl::HyphenationImpl(blink::mojom::HyphenationRequest request) |
| + : binding_(this, std::move(request)) {} |
| + |
| +HyphenationImpl::~HyphenationImpl() {} |
| + |
| +// static |
| +void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { |
| + new HyphenationImpl(std::move(request)); |
| +} |
| + |
| +void HyphenationImpl::OpenDictionary(const mojo::String& locale, |
| + const OpenDictionaryCallback& callback) { |
| + mojo::ScopedHandle handle; |
| + base::File& file = GetDictionaryFile(locale); |
| + if (file.IsValid()) |
| + handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); |
| + callback.Run(std::move(handle)); |
| +} |
| + |
| +} // namespace hyphenation |