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 mojom::blink::HyphenationPtr connectToRemoteService() |
| 31 { |
| 32 mojom::blink::HyphenationPtr service; |
| 33 Platform::current()->serviceRegistry()->connectToRemoteService( |
| 34 mojo::GetProxy(&service)); |
| 35 return service; |
| 36 } |
| 37 |
| 38 static const mojom::blink::HyphenationPtr& getService() |
| 39 { |
| 40 DEFINE_STATIC_LOCAL(mojom::blink::HyphenationPtr, service, |
| 41 (connectToRemoteService())); |
| 42 return service; |
| 43 } |
| 44 |
| 45 base::PlatformFile HyphenationMinikin::openDictionaryFile(const AtomicString& lo
cale) |
| 46 { |
| 47 const mojom::blink::HyphenationPtr& service = getService(); |
| 48 mojo::ScopedHandle handle; |
| 49 base::ElapsedTimer timer; |
| 50 service->OpenDictionary(locale, &handle); |
| 51 UMA_HISTOGRAM_TIMES("Hyphenation.Open", timer.Elapsed()); |
| 52 if (!handle.is_valid()) |
| 53 return base::kInvalidPlatformFile; |
| 54 |
| 55 base::PlatformFile file; |
| 56 MojoResult result = mojo::UnwrapPlatformFile(std::move(handle), &file); |
| 57 if (result != MOJO_RESULT_OK) { |
| 58 DLOG(ERROR) << "UnwrapPlatformFile failed"; |
| 59 return base::kInvalidPlatformFile; |
| 60 } |
| 61 return file; |
| 62 } |
| 63 |
| 64 bool HyphenationMinikin::openDictionary(const AtomicString& locale) |
| 65 { |
| 66 base::PlatformFile file = openDictionaryFile(locale); |
| 67 if (file == base::kInvalidPlatformFile) |
| 68 return false; |
| 69 if (!m_file.Initialize(base::File(file))) { |
| 70 DLOG(ERROR) << "mmap failed"; |
| 71 return false; |
| 72 } |
| 73 |
| 74 // TODO(kojii): Create dictionary from m_file when Minikin is ready. |
| 75 |
| 76 return true; |
| 77 } |
| 78 |
| 79 size_t HyphenationMinikin::lastHyphenLocation(const StringView& text, size_t bef
oreIndex) const |
| 80 { |
| 81 // TODO(kojii): Call minikin using the dictionary when Minikin is ready. |
| 82 return 0; |
| 83 } |
| 84 |
| 85 PassRefPtr<Hyphenation> Hyphenation::platformGetHyphenation(const AtomicString&
locale) |
| 86 { |
| 87 RefPtr<HyphenationMinikin> hyphenation(adoptRef(new HyphenationMinikin)); |
| 88 if (!hyphenation->openDictionary(locale)) |
| 89 return nullptr; |
| 90 return hyphenation.release(); |
| 91 } |
| 92 |
| 93 } // namespace blink |
OLD | NEW |