Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: content/browser/hyphenation/hyphenation_impl.cc

Issue 2539383002: Replace base::File wrapping with typemapping. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/metrics/histogram.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/timer/elapsed_timer.h" 16 #include "base/timer/elapsed_timer.h"
17 #include "mojo/public/cpp/bindings/strong_binding.h" 17 #include "mojo/public/cpp/bindings/strong_binding.h"
18 #include "mojo/public/cpp/system/platform_handle.h"
19 18
20 namespace { 19 namespace {
21 20
22 using DictionaryFileMap = std::unordered_map<std::string, base::File>; 21 using DictionaryFileMap = std::unordered_map<std::string, base::File>;
23 22
24 static bool IsValidLocale(const std::string& locale) { 23 bool IsValidLocale(const std::string& locale) {
25 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) { 24 return std::all_of(locale.cbegin(), locale.cend(), [](const char ch) {
26 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-'; 25 return base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch) || ch == '-';
27 }); 26 });
28 } 27 }
29 28
30 static base::File& GetDictionaryFile(const std::string& locale) { 29 base::File GetDictionaryFile(const std::string& locale) {
31 // Keep Files open in the cache for subsequent calls. 30 // Keep Files open in the cache for subsequent calls.
32 CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ()); 31 CR_DEFINE_STATIC_LOCAL(DictionaryFileMap, cache, ());
33 32
34 const auto& it = cache.find(locale); 33 const auto& it = cache.find(locale);
35 if (it != cache.end()) 34 if (it != cache.end())
36 return it->second; 35 return it->second.Duplicate();
37 const auto& inserted = cache.insert(std::make_pair(locale, base::File())); 36 const auto& inserted = cache.insert(std::make_pair(locale, base::File()));
38 base::File& file = inserted.first->second; 37 base::File& file = inserted.first->second;
39 DCHECK(!file.IsValid()); 38 DCHECK(!file.IsValid());
40 39
41 #if defined(OS_ANDROID) 40 #if defined(OS_ANDROID)
42 base::FilePath dir("/system/usr/hyphen-data"); 41 base::FilePath dir("/system/usr/hyphen-data");
43 #else 42 #else
44 #error "This configuration is not supported." 43 #error "This configuration is not supported."
45 #endif 44 #endif
46 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str()); 45 std::string filename = base::StringPrintf("hyph-%s.hyb", locale.c_str());
47 base::FilePath path = dir.AppendASCII(filename); 46 base::FilePath path = dir.AppendASCII(filename);
48 base::ElapsedTimer timer; 47 base::ElapsedTimer timer;
49 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ); 48 file.Initialize(path, base::File::FLAG_OPEN | base::File::FLAG_READ);
50 UMA_HISTOGRAM_TIMES("Hyphenation.Open.File", timer.Elapsed()); 49 UMA_HISTOGRAM_TIMES("Hyphenation.Open.File", timer.Elapsed());
51 return file; 50 return file.Duplicate();
52 } 51 }
53 52
54 } // namespace 53 } // namespace
55 54
56 namespace hyphenation { 55 namespace hyphenation {
57 56
58 HyphenationImpl::HyphenationImpl() {} 57 HyphenationImpl::HyphenationImpl() {}
59 58
60 HyphenationImpl::~HyphenationImpl() {} 59 HyphenationImpl::~HyphenationImpl() {}
61 60
62 // static 61 // static
63 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) { 62 void HyphenationImpl::Create(blink::mojom::HyphenationRequest request) {
64 mojo::MakeStrongBinding(base::MakeUnique<HyphenationImpl>(), 63 mojo::MakeStrongBinding(base::MakeUnique<HyphenationImpl>(),
65 std::move(request)); 64 std::move(request));
66 } 65 }
67 66
68 void HyphenationImpl::OpenDictionary(const std::string& locale, 67 void HyphenationImpl::OpenDictionary(const std::string& locale,
69 const OpenDictionaryCallback& callback) { 68 const OpenDictionaryCallback& callback) {
70 mojo::ScopedHandle handle; 69 if (IsValidLocale(locale))
71 if (IsValidLocale(locale)) { 70 callback.Run(GetDictionaryFile(locale));
72 base::File& file = GetDictionaryFile(locale); 71 else
73 if (file.IsValid()) 72 callback.Run(base::File());
74 handle = mojo::WrapPlatformFile(file.Duplicate().TakePlatformFile());
75 }
76 callback.Run(std::move(handle));
77 } 73 }
78 74
79 } // namespace hyphenation 75 } // namespace hyphenation
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698