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 "platform/text/Hyphenation.h" | |
6 | |
7 #include "base/files/file.h" | |
8 #include "base/files/memory_mapped_file.h" | |
9 #include "base/metrics/histogram.h" | |
10 #include "base/timer/elapsed_timer.h" | |
11 #include "mojo/public/cpp/system/platform_handle.h" | |
12 #include "public/platform/Platform.h" | |
13 #include "public/platform/ServiceRegistry.h" | |
14 #include "public/platform/modules/hyphenation/hyphenation.mojom-blink.h" | |
15 | |
16 namespace blink { | |
17 | |
18 class HyphenationMinikin : public Hyphenation { | |
19 public: | |
20 bool openDictionary(const AtomicString& locale); | |
21 | |
22 size_t lastHyphenLocation(const StringView& text, size_t beforeIndex) const override; | |
23 | |
24 private: | |
25 static base::PlatformFile openDictionaryFile(const AtomicString& locale); | |
26 | |
27 base::MemoryMappedFile m_file; | |
28 }; | |
29 | |
30 static const mojom::blink::HyphenationPtr& getService() | |
31 { | |
32 DEFINE_STATIC_LOCAL(mojom::blink::HyphenationPtr, service, ()); | |
33 if (service.is_bound()) | |
dcheng
2016/07/13 02:56:29
I have a question about this pattern: https://grou
kojii
2016/07/13 05:02:29
Thanks for asking in the group. I tried a differen
dcheng
2016/07/13 12:08:58
Nod, either way is fine with me.
| |
34 return service; | |
35 | |
36 Platform::current()->serviceRegistry()->connectToRemoteService( | |
37 mojo::GetProxy(&service)); | |
38 return service; | |
39 } | |
40 | |
41 base::PlatformFile HyphenationMinikin::openDictionaryFile(const AtomicString& lo cale) | |
42 { | |
43 const mojom::blink::HyphenationPtr& service = getService(); | |
44 mojo::ScopedHandle handle; | |
45 base::ElapsedTimer timer; | |
46 service->OpenDictionary(locale, &handle); | |
47 UMA_HISTOGRAM_TIMES("Hyphenation.Open", timer.Elapsed()); | |
48 if (!handle.is_valid()) | |
49 return base::kInvalidPlatformFile; | |
50 | |
51 base::PlatformFile file; | |
52 MojoResult result = mojo::UnwrapPlatformFile(std::move(handle), &file); | |
53 if (result != MOJO_RESULT_OK) { | |
54 DLOG(ERROR) << "UnwrapPlatformFile failed"; | |
55 return base::kInvalidPlatformFile; | |
56 } | |
57 return file; | |
58 } | |
59 | |
60 bool HyphenationMinikin::openDictionary(const AtomicString& locale) | |
61 { | |
62 base::PlatformFile file = openDictionaryFile(locale); | |
63 if (file == base::kInvalidPlatformFile) | |
64 return false; | |
65 if (!m_file.Initialize(base::File(file))) { | |
66 DLOG(ERROR) << "mmap failed"; | |
67 return false; | |
68 } | |
69 | |
70 // TODO(kojii): Create dictionary from m_file when Minikin is ready. | |
71 | |
72 return true; | |
73 } | |
74 | |
75 size_t HyphenationMinikin::lastHyphenLocation(const StringView& text, size_t bef oreIndex) const | |
76 { | |
77 // TODO(kojii): Call minikin using the dictionary when Minikin is ready. | |
78 return 0; | |
79 } | |
80 | |
81 PassRefPtr<Hyphenation> Hyphenation::platformGetHyphenation(const AtomicString& locale) | |
82 { | |
83 RefPtr<HyphenationMinikin> hyphenation(adoptRef(new HyphenationMinikin)); | |
84 if (!hyphenation->openDictionary(locale)) | |
85 return nullptr; | |
86 return hyphenation.release(); | |
87 } | |
88 | |
89 } // namespace blink | |
OLD | NEW |