Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(206)

Side by Side Diff: content/browser/hyphenation/hyphenation_impl.cc

Issue 2113933003: Add mojo interface to load hyphenation dictionaries on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved to content/browser Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #if defined(HYPHENATION_DIR_HOME_FOR_TEST)
17 #include "base/base_paths.h"
18 #include "base/path_service.h"
19 #endif
20
21 namespace {
22
23 using DictionaryFileMap = std::unordered_map<std::string, base::File>;
24
25 static bool IsValidLocale(const std::string& locale) {
26 return std::all_of(locale.cbegin(), locale.cend(),
27 [](const char ch) { return isalpha(ch) || ch == '-'; });
28 }
29
30 static base::File& GetDictionaryFile(const std::string& locale) {
31 // Keep Files open in the cache for subsequent calls.
32 CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ());
33
34 // TODO(kojii): Replace find+insert with emplace()+std::piecewise_construct
35 // when std::map::emplace() is fully supported on all configurations.
36 const auto& it = cache.find(locale);
37 if (it != cache.end())
38 return it->second;
39 base::File file_on_stack;
40 const auto& inserted = cache.insert(
41 std::make_pair(locale, std::move(file_on_stack)));
42 base::File& file = inserted.first->second;
43 DCHECK(!file.IsValid());
44
45 // Only ISO lang code is accepted.
jam 2016/07/12 14:51:12 don't we want to check this and early exit before
46 if (!IsValidLocale(locale))
47 return file;
48
49 #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
50 base::FilePath dir;
51 if (!base::PathService::Get(base::DIR_HOME, &dir))
52 return file;
jam 2016/07/12 14:51:12 here as well, don't we only want to put valid path
53 #elif defined(OS_ANDROID)
54 base::FilePath dir("/system/usr/hyphen-data");
55 #else
56 #error "This configuration is not supported."
57 #endif
58 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str());
59 base::FilePath path = dir.AppendASCII(filename);
60 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
61 return file;
62 }
63
64 } // namespace
65
66 namespace hyphenation {
67
68 HyphenationImpl::HyphenationImpl(blink::mojom::HyphenationRequest request)
69 : binding_(this, std::move(request)) {}
70
71 HyphenationImpl::~HyphenationImpl() {}
72
73 // static
74 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) {
75 new HyphenationImpl(std::move(request));
76 }
77
78 void HyphenationImpl::OpenDictionary(const mojo::String& locale,
79 const OpenDictionaryCallback& callback) {
80 mojo::ScopedHandle handle;
81 base::File& file = GetDictionaryFile(locale);
82 if (file.IsValid())
83 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile());
84 callback.Run(std::move(handle));
85 }
86
87 } // namespace hyphenation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698