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/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
15 #include "mojo/public/cpp/system/platform_handle.h" | 16 #include "mojo/public/cpp/system/platform_handle.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 using DictionaryFileMap = std::unordered_map<std::string, base::File>; | 20 using DictionaryFileMap = std::unordered_map<std::string, base::File>; |
20 | 21 |
21 static bool IsValidLocale(const std::string& locale) { | 22 static bool IsValidLocale(const std::string& locale) { |
22 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) { | 23 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) { |
23 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-'; | 24 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-'; |
24 }); | 25 }); |
(...skipping 18 matching lines...) Expand all Loading... |
43 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); | 44 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); |
44 base::FilePath path = dir.AppendASCII(filename); | 45 base::FilePath path = dir.AppendASCII(filename); |
45 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); | 46 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
46 return file; | 47 return file; |
47 } | 48 } |
48 | 49 |
49 } // namespace | 50 } // namespace |
50 | 51 |
51 namespace hyphenation { | 52 namespace hyphenation { |
52 | 53 |
53 HyphenationImpl::HyphenationImpl(blink::mojom::HyphenationRequest request) | 54 HyphenationImpl::HyphenationImpl() {} |
54 : binding_(this, std::move(request)) {} | |
55 | 55 |
56 HyphenationImpl::~HyphenationImpl() {} | 56 HyphenationImpl::~HyphenationImpl() {} |
57 | 57 |
58 // static | 58 // static |
59 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { | 59 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { |
60 new HyphenationImpl(std::move(request)); | 60 mojo::MakeStrongBinding(base::MakeUnique<HyphenationImpl>(), |
| 61 std::move(request)); |
61 } | 62 } |
62 | 63 |
63 void HyphenationImpl::OpenDictionary(const mojo::String& locale, | 64 void HyphenationImpl::OpenDictionary(const mojo::String& locale, |
64 const OpenDictionaryCallback& callback) { | 65 const OpenDictionaryCallback& callback) { |
65 mojo::ScopedHandle handle; | 66 mojo::ScopedHandle handle; |
66 if (IsValidLocale(locale)) { | 67 if (IsValidLocale(locale)) { |
67 base::File& file = GetDictionaryFile(locale); | 68 base::File& file = GetDictionaryFile(locale); |
68 if (file.IsValid()) | 69 if (file.IsValid()) |
69 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); | 70 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile()); |
70 } | 71 } |
71 callback.Run(std::move(handle)); | 72 callback.Run(std::move(handle)); |
72 } | 73 } |
73 | 74 |
74 } // namespace hyphenation | 75 } // namespace hyphenation |
OLD | NEW |