| 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 "platform/text/Hyphenation.h" | 5 #include "platform/text/Hyphenation.h" |
| 6 | 6 |
| 7 #include "base/files/file.h" | 7 #include "base/files/file.h" |
| 8 #include "base/files/memory_mapped_file.h" | 8 #include "base/files/memory_mapped_file.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/timer/elapsed_timer.h" | 10 #include "base/timer/elapsed_timer.h" |
| 11 #include "mojo/public/cpp/system/platform_handle.h" | |
| 12 #include "platform/LayoutLocale.h" | 11 #include "platform/LayoutLocale.h" |
| 13 #include "platform/text/hyphenation/HyphenatorAOSP.h" | 12 #include "platform/text/hyphenation/HyphenatorAOSP.h" |
| 14 #include "public/platform/InterfaceProvider.h" | 13 #include "public/platform/InterfaceProvider.h" |
| 15 #include "public/platform/Platform.h" | 14 #include "public/platform/Platform.h" |
| 16 #include "public/platform/modules/hyphenation/hyphenation.mojom-blink.h" | 15 #include "public/platform/modules/hyphenation/hyphenation.mojom-blink.h" |
| 17 | 16 |
| 18 namespace blink { | 17 namespace blink { |
| 19 | 18 |
| 20 using Hyphenator = android::Hyphenator; | 19 using Hyphenator = android::Hyphenator; |
| 21 | 20 |
| 22 class HyphenationMinikin : public Hyphenation { | 21 class HyphenationMinikin : public Hyphenation { |
| 23 public: | 22 public: |
| 24 bool openDictionary(const AtomicString& locale); | 23 bool openDictionary(const AtomicString& locale); |
| 25 | 24 |
| 26 size_t lastHyphenLocation(const StringView& text, | 25 size_t lastHyphenLocation(const StringView& text, |
| 27 size_t beforeIndex) const override; | 26 size_t beforeIndex) const override; |
| 28 Vector<size_t, 8> hyphenLocations(const StringView&) const override; | 27 Vector<size_t, 8> hyphenLocations(const StringView&) const override; |
| 29 | 28 |
| 30 private: | 29 private: |
| 31 static base::PlatformFile openDictionaryFile(const AtomicString& locale); | 30 static base::File openDictionaryFile(const AtomicString& locale); |
| 32 | 31 |
| 33 std::vector<uint8_t> hyphenate(const StringView&) const; | 32 std::vector<uint8_t> hyphenate(const StringView&) const; |
| 34 | 33 |
| 35 base::MemoryMappedFile m_file; | 34 base::MemoryMappedFile m_file; |
| 36 std::unique_ptr<Hyphenator> m_hyphenator; | 35 std::unique_ptr<Hyphenator> m_hyphenator; |
| 37 }; | 36 }; |
| 38 | 37 |
| 39 static mojom::blink::HyphenationPtr connectToRemoteService() { | 38 static mojom::blink::HyphenationPtr connectToRemoteService() { |
| 40 mojom::blink::HyphenationPtr service; | 39 mojom::blink::HyphenationPtr service; |
| 41 Platform::current()->interfaceProvider()->getInterface( | 40 Platform::current()->interfaceProvider()->getInterface( |
| 42 mojo::GetProxy(&service)); | 41 mojo::GetProxy(&service)); |
| 43 return service; | 42 return service; |
| 44 } | 43 } |
| 45 | 44 |
| 46 static const mojom::blink::HyphenationPtr& getService() { | 45 static const mojom::blink::HyphenationPtr& getService() { |
| 47 DEFINE_STATIC_LOCAL(mojom::blink::HyphenationPtr, service, | 46 DEFINE_STATIC_LOCAL(mojom::blink::HyphenationPtr, service, |
| 48 (connectToRemoteService())); | 47 (connectToRemoteService())); |
| 49 return service; | 48 return service; |
| 50 } | 49 } |
| 51 | 50 |
| 52 base::PlatformFile HyphenationMinikin::openDictionaryFile( | 51 base::File HyphenationMinikin::openDictionaryFile(const AtomicString& locale) { |
| 53 const AtomicString& locale) { | |
| 54 const mojom::blink::HyphenationPtr& service = getService(); | 52 const mojom::blink::HyphenationPtr& service = getService(); |
| 55 mojo::ScopedHandle handle; | 53 base::File file; |
| 56 base::ElapsedTimer timer; | 54 base::ElapsedTimer timer; |
| 57 service->OpenDictionary(locale, &handle); | 55 service->OpenDictionary(locale, &file); |
| 58 UMA_HISTOGRAM_TIMES("Hyphenation.Open", timer.Elapsed()); | 56 UMA_HISTOGRAM_TIMES("Hyphenation.Open", timer.Elapsed()); |
| 59 if (!handle.is_valid()) | |
| 60 return base::kInvalidPlatformFile; | |
| 61 | |
| 62 base::PlatformFile file; | |
| 63 MojoResult result = mojo::UnwrapPlatformFile(std::move(handle), &file); | |
| 64 if (result != MOJO_RESULT_OK) { | |
| 65 DLOG(ERROR) << "UnwrapPlatformFile failed"; | |
| 66 return base::kInvalidPlatformFile; | |
| 67 } | |
| 68 return file; | 57 return file; |
| 69 } | 58 } |
| 70 | 59 |
| 71 bool HyphenationMinikin::openDictionary(const AtomicString& locale) { | 60 bool HyphenationMinikin::openDictionary(const AtomicString& locale) { |
| 72 base::PlatformFile file = openDictionaryFile(locale); | 61 base::File file = openDictionaryFile(locale); |
| 73 if (file == base::kInvalidPlatformFile) | 62 if (!file.IsValid()) |
| 74 return false; | 63 return false; |
| 75 if (!m_file.Initialize(base::File(file))) { | 64 if (!m_file.Initialize(std::move(file))) { |
| 76 DLOG(ERROR) << "mmap failed"; | 65 DLOG(ERROR) << "mmap failed"; |
| 77 return false; | 66 return false; |
| 78 } | 67 } |
| 79 | 68 |
| 80 m_hyphenator = wrapUnique(Hyphenator::loadBinary(m_file.data())); | 69 m_hyphenator = wrapUnique(Hyphenator::loadBinary(m_file.data())); |
| 81 | 70 |
| 82 return true; | 71 return true; |
| 83 } | 72 } |
| 84 | 73 |
| 85 std::vector<uint8_t> HyphenationMinikin::hyphenate( | 74 std::vector<uint8_t> HyphenationMinikin::hyphenate( |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 | 164 |
| 176 DEFINE_STATIC_LOCAL(LocaleMap, localeFallback, (createLocaleFallbackMap())); | 165 DEFINE_STATIC_LOCAL(LocaleMap, localeFallback, (createLocaleFallbackMap())); |
| 177 const auto& it = localeFallback.find(locale); | 166 const auto& it = localeFallback.find(locale); |
| 178 if (it != localeFallback.end()) | 167 if (it != localeFallback.end()) |
| 179 return LayoutLocale::get(it->value)->getHyphenation(); | 168 return LayoutLocale::get(it->value)->getHyphenation(); |
| 180 | 169 |
| 181 return nullptr; | 170 return nullptr; |
| 182 } | 171 } |
| 183 | 172 |
| 184 } // namespace blink | 173 } // namespace blink |
| OLD | NEW |