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/metrics/histogram.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/timer/elapsed_timer.h" |
15 #include "mojo/public/cpp/bindings/strong_binding.h" | 17 #include "mojo/public/cpp/bindings/strong_binding.h" |
16 #include "mojo/public/cpp/system/platform_handle.h" | 18 #include "mojo/public/cpp/system/platform_handle.h" |
17 | 19 |
18 namespace { | 20 namespace { |
19 | 21 |
20 using DictionaryFileMap = std::unordered_map<std::string, base::File>; | 22 using DictionaryFileMap = std::unordered_map<std::string, base::File>; |
21 | 23 |
22 static bool IsValidLocale(const std::string& locale) { | 24 static bool IsValidLocale(const std::string& locale) { |
23 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) { | 25 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) { |
24 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-'; | 26 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-'; |
(...skipping 11 matching lines...) Expand all Loading... |
36 base::File& file = inserted.first->second; | 38 base::File& file = inserted.first->second; |
37 DCHECK(!file.IsValid()); | 39 DCHECK(!file.IsValid()); |
38 | 40 |
39 #if defined(OS_ANDROID) | 41 #if defined(OS_ANDROID) |
40 base::FilePath dir("/system/usr/hyphen-data"); | 42 base::FilePath dir("/system/usr/hyphen-data"); |
41 #else | 43 #else |
42 #error "This configuration is not supported." | 44 #error "This configuration is not supported." |
43 #endif | 45 #endif |
44 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); | 46 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); |
45 base::FilePath path = dir.AppendASCII(filename); | 47 base::FilePath path = dir.AppendASCII(filename); |
| 48 base::ElapsedTimer timer; |
46 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 49 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 50 UMA_HISTOGRAM_TIMES("Hyphenation.Open.File", timer.Elapsed()); |
47 return file; | 51 return file; |
48 } | 52 } |
49 | 53 |
50 } // namespace | 54 } // namespace |
51 | 55 |
52 namespace hyphenation { | 56 namespace hyphenation { |
53 | 57 |
54 HyphenationImpl::HyphenationImpl() {} | 58 HyphenationImpl::HyphenationImpl() {} |
55 | 59 |
56 HyphenationImpl::~HyphenationImpl() {} | 60 HyphenationImpl::~HyphenationImpl() {} |
57 | 61 |
58 // static | 62 // static |
59 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { | 63 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { |
60 mojo::MakeStrongBinding(base::MakeUnique<HyphenationImpl>(), | 64 mojo::MakeStrongBinding(base::MakeUnique<HyphenationImpl>(), |
61 std::move(request)); | 65 std::move(request)); |
62 } | 66 } |
63 | 67 |
64 void HyphenationImpl::OpenDictionary(const mojo::String& locale, | 68 void HyphenationImpl::OpenDictionary(const mojo::String& locale, |
65 const OpenDictionaryCallback& callback) { | 69 const OpenDictionaryCallback& callback) { |
66 mojo::ScopedHandle handle; | 70 mojo::ScopedHandle handle; |
67 if (IsValidLocale(locale)) { | 71 if (IsValidLocale(locale)) { |
68 base::File& file = GetDictionaryFile(locale); | 72 base::File& file = GetDictionaryFile(locale); |
69 if (file.IsValid()) | 73 if (file.IsValid()) |
70 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); | 74 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); |
71 } | 75 } |
72 callback.Run(std::move(handle)); | 76 callback.Run(std::move(handle)); |
73 } | 77 } |
74 | 78 |
75 } // namespace hyphenation | 79 } // namespace hyphenation |
OLD | NEW |