| 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 "chrome/renderer/spellchecker/hunspell_engine.h" | 5 #include "chrome/renderer/spellchecker/hunspell_engine.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <iterator> | 8 #include <iterator> |
| 9 | 9 |
| 10 #include "base/files/memory_mapped_file.h" | 10 #include "base/files/memory_mapped_file.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // Maximum length of words we actually check. | 22 // Maximum length of words we actually check. |
| 23 // 64 is the observed limits for OSX system checker. | 23 // 64 is the observed limits for OSX system checker. |
| 24 const size_t kMaxCheckedLen = 64; | 24 const size_t kMaxCheckedLen = 64; |
| 25 | 25 |
| 26 // Maximum length of words we provide suggestions for. | 26 // Maximum length of words we provide suggestions for. |
| 27 // 24 is the observed limits for OSX system checker. | 27 // 24 is the observed limits for OSX system checker. |
| 28 const size_t kMaxSuggestLen = 24; | 28 const size_t kMaxSuggestLen = 24; |
| 29 | 29 |
| 30 COMPILE_ASSERT(kMaxCheckedLen <= size_t(MAXWORDLEN), MaxCheckedLen_too_long); | 30 COMPILE_ASSERT(kMaxCheckedLen <= size_t(MAXWORDLEN), MaxCheckedLen_too_long); |
| 31 COMPILE_ASSERT(kMaxSuggestLen <= kMaxCheckedLen, MaxSuggestLen_too_long); | 31 COMPILE_ASSERT(kMaxSuggestLen <= kMaxCheckedLen, MaxSuggestLen_too_long); |
| 32 } | 32 } // namespace |
| 33 | 33 |
| 34 #if !defined(OS_MACOSX) | 34 #if !defined(OS_MACOSX) |
| 35 SpellingEngine* CreateNativeSpellingEngine() { | 35 SpellingEngine* CreateNativeSpellingEngine() { |
| 36 return new HunspellEngine(); | 36 return new HunspellEngine(); |
| 37 } | 37 } |
| 38 #endif | 38 #endif |
| 39 | 39 |
| 40 HunspellEngine::HunspellEngine() | 40 HunspellEngine::HunspellEngine() |
| 41 : file_(base::kInvalidPlatformFileValue), | 41 : hunspell_enabled_(false), |
| 42 initialized_(false), | 42 initialized_(false), |
| 43 dictionary_requested_(false) { | 43 dictionary_requested_(false) { |
| 44 // Wait till we check the first word before doing any initializing. | 44 // Wait till we check the first word before doing any initializing. |
| 45 } | 45 } |
| 46 | 46 |
| 47 HunspellEngine::~HunspellEngine() { | 47 HunspellEngine::~HunspellEngine() { |
| 48 } | 48 } |
| 49 | 49 |
| 50 void HunspellEngine::Init(base::PlatformFile file) { | 50 void HunspellEngine::Init(base::File file) { |
| 51 initialized_ = true; | 51 initialized_ = true; |
| 52 hunspell_.reset(); | 52 hunspell_.reset(); |
| 53 bdict_file_.reset(); | 53 bdict_file_.reset(); |
| 54 file_ = file; | 54 file_ = file.Pass(); |
| 55 hunspell_enabled_ = file_.IsValid(); |
| 55 // Delay the actual initialization of hunspell until it is needed. | 56 // Delay the actual initialization of hunspell until it is needed. |
| 56 } | 57 } |
| 57 | 58 |
| 58 void HunspellEngine::InitializeHunspell() { | 59 void HunspellEngine::InitializeHunspell() { |
| 59 if (hunspell_.get()) | 60 if (hunspell_.get()) |
| 60 return; | 61 return; |
| 61 | 62 |
| 62 bdict_file_.reset(new base::MemoryMappedFile); | 63 bdict_file_.reset(new base::MemoryMappedFile); |
| 63 | 64 |
| 64 // TODO(rvargas): This object should not keep file_ after passing it to | 65 if (bdict_file_->Initialize(file_.Pass())) { |
| 65 // bdict_file_. | |
| 66 if (bdict_file_->Initialize(base::File(file_))) { | |
| 67 TimeTicks debug_start_time = base::Histogram::DebugNow(); | 66 TimeTicks debug_start_time = base::Histogram::DebugNow(); |
| 68 | 67 |
| 69 hunspell_.reset( | 68 hunspell_.reset(new Hunspell(bdict_file_->data(), bdict_file_->length())); |
| 70 new Hunspell(bdict_file_->data(), bdict_file_->length())); | |
| 71 | 69 |
| 72 DHISTOGRAM_TIMES("Spellcheck.InitTime", | 70 DHISTOGRAM_TIMES("Spellcheck.InitTime", |
| 73 base::Histogram::DebugNow() - debug_start_time); | 71 base::Histogram::DebugNow() - debug_start_time); |
| 74 } else { | 72 } else { |
| 75 NOTREACHED() << "Could not mmap spellchecker dictionary."; | 73 NOTREACHED() << "Could not mmap spellchecker dictionary."; |
| 76 } | 74 } |
| 77 } | 75 } |
| 78 | 76 |
| 79 bool HunspellEngine::CheckSpelling(const base::string16& word_to_check, | 77 bool HunspellEngine::CheckSpelling(const base::string16& word_to_check, |
| 80 int tag) { | 78 int tag) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 bool HunspellEngine::InitializeIfNeeded() { | 125 bool HunspellEngine::InitializeIfNeeded() { |
| 128 if (!initialized_ && !dictionary_requested_) { | 126 if (!initialized_ && !dictionary_requested_) { |
| 129 // RenderThread will not exist in test. | 127 // RenderThread will not exist in test. |
| 130 if (RenderThread::Get()) | 128 if (RenderThread::Get()) |
| 131 RenderThread::Get()->Send(new SpellCheckHostMsg_RequestDictionary); | 129 RenderThread::Get()->Send(new SpellCheckHostMsg_RequestDictionary); |
| 132 dictionary_requested_ = true; | 130 dictionary_requested_ = true; |
| 133 return true; | 131 return true; |
| 134 } | 132 } |
| 135 | 133 |
| 136 // Don't initialize if hunspell is disabled. | 134 // Don't initialize if hunspell is disabled. |
| 137 if (file_ != base::kInvalidPlatformFileValue) | 135 if (file_.IsValid()) |
| 138 InitializeHunspell(); | 136 InitializeHunspell(); |
| 139 | 137 |
| 140 return !initialized_; | 138 return !initialized_; |
| 141 } | 139 } |
| 142 | 140 |
| 143 bool HunspellEngine::IsEnabled() { | 141 bool HunspellEngine::IsEnabled() { |
| 144 return file_ != base::kInvalidPlatformFileValue; | 142 return hunspell_enabled_; |
| 145 } | 143 } |
| OLD | NEW |