OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/renderer/hyphenator/hyphenator.h" | 5 #include "content/renderer/hyphenator/hyphenator.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 } | 177 } |
178 return !hyphen_offsets->empty(); | 178 return !hyphen_offsets->empty(); |
179 } | 179 } |
180 | 180 |
181 } // namespace | 181 } // namespace |
182 | 182 |
183 namespace content { | 183 namespace content { |
184 | 184 |
185 Hyphenator::Hyphenator(base::PlatformFile file) | 185 Hyphenator::Hyphenator(base::PlatformFile file) |
186 : dictionary_(NULL), | 186 : dictionary_(NULL), |
187 rule_file_(file), | 187 dictionary_file_(base::FdopenPlatformFile(file, "r")), |
188 result_(0) { | 188 result_(0) { |
189 } | 189 } |
190 | 190 |
191 Hyphenator::~Hyphenator() { | 191 Hyphenator::~Hyphenator() { |
192 if (dictionary_) | 192 if (dictionary_) |
193 hnj_hyphen_free(dictionary_); | 193 hnj_hyphen_free(dictionary_); |
194 if (rule_file_ != base::kInvalidPlatformFileValue) | 194 if (dictionary_file_) |
195 base::ClosePlatformFile(rule_file_); | 195 fclose(dictionary_file_); |
196 } | 196 } |
197 | 197 |
198 bool Hyphenator::Initialize() { | 198 bool Hyphenator::Initialize() { |
199 if (dictionary_) | 199 if (dictionary_) |
200 return true; | 200 return true; |
201 | 201 |
202 // Attach the dictionary file to the MemoryMappedFile object. When it | 202 dictionary_ = hnj_hyphen_load_file(dictionary_file_); |
203 // succeeds, this class does not have to close this file because it is closed | |
204 // by the MemoryMappedFile class. To prevent this class from closing this | |
205 // file, we reset its handle. | |
206 rule_map_.reset(new file_util::MemoryMappedFile); | |
207 if (!rule_map_->Initialize(rule_file_)) | |
208 return false; | |
209 rule_file_ = base::kInvalidPlatformFileValue; | |
210 | |
211 dictionary_ = hnj_hyphen_load(rule_map_->data(), rule_map_->length()); | |
212 return !!dictionary_; | 203 return !!dictionary_; |
213 } | 204 } |
214 | 205 |
215 bool Hyphenator::Attach(RenderThread* thread, const string16& locale) { | 206 bool Hyphenator::Attach(RenderThread* thread, const string16& locale) { |
216 if (!thread) | 207 if (!thread) |
217 return false; | 208 return false; |
218 locale_.assign(locale); | 209 locale_.assign(locale); |
219 thread->AddObserver(this); | 210 thread->AddObserver(this); |
220 return thread->Send(new HyphenatorHostMsg_OpenDictionary(locale)); | 211 return thread->Send(new HyphenatorHostMsg_OpenDictionary(locale)); |
221 } | 212 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 // Delete the current dictionary and save the given file to this object. We | 257 // Delete the current dictionary and save the given file to this object. We |
267 // initialize the hyphen library the first time when WebKit actually | 258 // initialize the hyphen library the first time when WebKit actually |
268 // hyphenates a word, i.e. when WebKit calls the ComputeLastHyphenLocation | 259 // hyphenates a word, i.e. when WebKit calls the ComputeLastHyphenLocation |
269 // function. (WebKit does not always hyphenate words even when it calls the | 260 // function. (WebKit does not always hyphenate words even when it calls the |
270 // CanHyphenate function, e.g. WebKit does not have to hyphenate words when it | 261 // CanHyphenate function, e.g. WebKit does not have to hyphenate words when it |
271 // does not have to break text into lines.) | 262 // does not have to break text into lines.) |
272 if (dictionary_) { | 263 if (dictionary_) { |
273 hnj_hyphen_free(dictionary_); | 264 hnj_hyphen_free(dictionary_); |
274 dictionary_ = NULL; | 265 dictionary_ = NULL; |
275 } | 266 } |
276 rule_map_.reset(); | 267 dictionary_file_ = base::FdopenPlatformFile(rule_file, "r"); |
277 rule_file_ = rule_file; | |
278 } | 268 } |
279 | 269 |
280 } // namespace content | 270 } // namespace content |
OLD | NEW |