Chromium Code Reviews| 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/spellcheck.h" | 5 #include "chrome/renderer/spellchecker/spellcheck.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/time.h" | 9 #include "base/time.h" |
| 10 #include "chrome/common/render_messages.h" | 10 #include "chrome/common/render_messages.h" |
| 11 #include "chrome/common/spellcheck_common.h" | 11 #include "chrome/common/spellcheck_common.h" |
| 12 #include "chrome/common/spellcheck_messages.h" | 12 #include "chrome/common/spellcheck_messages.h" |
| 13 #include "chrome/common/spellcheck_result.h" | 13 #include "chrome/common/spellcheck_result.h" |
| 14 #include "chrome/renderer/spellchecker/hunspell_engine.h" | 14 #include "chrome/renderer/spellchecker/hunspell_engine.h" |
| 15 #include "chrome/renderer/spellchecker/spellcheck_provider.h" | |
| 15 #include "chrome/renderer/spellchecker/spelling_engine.h" | 16 #include "chrome/renderer/spellchecker/spelling_engine.h" |
| 17 #include "content/public/renderer/render_view.h" | |
| 18 #include "content/public/renderer/render_view_visitor.h" | |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple tion.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingComple tion.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult .h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTextCheckingResult .h" |
| 18 | 21 |
| 19 using base::TimeTicks; | 22 using base::TimeTicks; |
| 20 using WebKit::WebVector; | 23 using WebKit::WebVector; |
| 21 using WebKit::WebTextCheckingResult; | 24 using WebKit::WebTextCheckingResult; |
| 22 using WebKit::WebTextCheckingType; | 25 using WebKit::WebTextCheckingType; |
| 23 | 26 |
| 27 class UpdateSpellcheckEnabled : public content::RenderViewVisitor { | |
|
brettw
2012/12/11 18:38:58
IS it possible to put this in an anon. namespace?
groby-ooo-7-16
2012/12/11 20:19:55
Done.
| |
| 28 public: | |
| 29 explicit UpdateSpellcheckEnabled(bool enabled) : enabled_(enabled) {} | |
| 30 virtual bool Visit(content::RenderView* render_view) OVERRIDE; | |
| 31 | |
| 32 private: | |
| 33 bool enabled_; // New spellcheck-enabled state. | |
| 34 DISALLOW_COPY_AND_ASSIGN(UpdateSpellcheckEnabled); | |
| 35 }; | |
| 36 | |
| 37 bool UpdateSpellcheckEnabled::Visit(content::RenderView* render_view) { | |
| 38 SpellCheckProvider* provider = SpellCheckProvider::Get(render_view); | |
| 39 DCHECK(provider); | |
| 40 provider->EnableSpellCheck(enabled_); | |
| 41 return true; | |
| 42 } | |
| 43 | |
| 44 | |
| 24 class SpellCheck::SpellcheckRequest { | 45 class SpellCheck::SpellcheckRequest { |
| 25 public: | 46 public: |
| 26 SpellcheckRequest(const string16& text, | 47 SpellcheckRequest(const string16& text, |
| 27 int offset, | 48 int offset, |
| 28 WebKit::WebTextCheckingCompletion* completion) | 49 WebKit::WebTextCheckingCompletion* completion) |
| 29 : text_(text), offset_(offset), completion_(completion) { | 50 : text_(text), offset_(offset), completion_(completion) { |
| 30 DCHECK(completion); | 51 DCHECK(completion); |
| 31 } | 52 } |
| 32 ~SpellcheckRequest() {} | 53 ~SpellcheckRequest() {} |
| 33 | 54 |
| 34 string16 text() { return text_; } | 55 string16 text() { return text_; } |
| 35 int offset() { return offset_; } | 56 int offset() { return offset_; } |
| 36 WebKit::WebTextCheckingCompletion* completion() { return completion_; } | 57 WebKit::WebTextCheckingCompletion* completion() { return completion_; } |
| 37 | 58 |
| 38 private: | 59 private: |
| 39 string16 text_; // Text to be checked in this task. | 60 string16 text_; // Text to be checked in this task. |
| 40 int offset_; // The text offset from the beginning. | 61 int offset_; // The text offset from the beginning. |
| 41 | 62 |
| 42 // The interface to send the misspelled ranges to WebKit. | 63 // The interface to send the misspelled ranges to WebKit. |
| 43 WebKit::WebTextCheckingCompletion* completion_; | 64 WebKit::WebTextCheckingCompletion* completion_; |
| 44 | 65 |
| 45 DISALLOW_COPY_AND_ASSIGN(SpellcheckRequest); | 66 DISALLOW_COPY_AND_ASSIGN(SpellcheckRequest); |
| 46 }; | 67 }; |
| 47 | 68 |
| 48 SpellCheck::SpellCheck() : auto_spell_correct_turned_on_(false) { | 69 |
| 70 // Initializes SpellCheck object. | |
| 71 // spellcheck_enabled_ currently MUST be set to true, due to peculiarities of | |
| 72 // the initialization sequence. | |
| 73 // Since it defaults to true, newly created SpellCheckProviders will enable | |
| 74 // spellchecking. After the first word is typed, the provider requests a check, | |
| 75 // which in turn triggers the delayed initialization sequence in SpellCheck. | |
| 76 // This does send a message to the browser side, which triggers the creation | |
| 77 // of the SpellcheckService. That does create the observer for the preference | |
| 78 // responsible for enabling/disabling checking, which allows subsequent changes | |
| 79 // to that preference to be sent to all SpellCheckProviders. | |
| 80 // Setting |spellcheck_enabled_| to false by default prevents that mechanism, | |
| 81 // and as such the SpellCheckProviders will never be notified of different | |
| 82 // values. | |
| 83 // TODO(groby): Simplify this. | |
| 84 SpellCheck::SpellCheck() | |
| 85 : auto_spell_correct_turned_on_(false), | |
| 86 spellcheck_enabled_(true) { | |
| 49 platform_spelling_engine_.reset(CreateNativeSpellingEngine()); | 87 platform_spelling_engine_.reset(CreateNativeSpellingEngine()); |
| 50 } | 88 } |
| 51 | 89 |
| 52 SpellCheck::~SpellCheck() { | 90 SpellCheck::~SpellCheck() { |
| 53 } | 91 } |
| 54 | 92 |
| 55 bool SpellCheck::OnControlMessageReceived(const IPC::Message& message) { | 93 bool SpellCheck::OnControlMessageReceived(const IPC::Message& message) { |
| 56 bool handled = true; | 94 bool handled = true; |
| 57 IPC_BEGIN_MESSAGE_MAP(SpellCheck, message) | 95 IPC_BEGIN_MESSAGE_MAP(SpellCheck, message) |
| 58 IPC_MESSAGE_HANDLER(SpellCheckMsg_Init, OnInit) | 96 IPC_MESSAGE_HANDLER(SpellCheckMsg_Init, OnInit) |
| 59 IPC_MESSAGE_HANDLER(SpellCheckMsg_WordAdded, OnWordAdded) | 97 IPC_MESSAGE_HANDLER(SpellCheckMsg_WordAdded, OnWordAdded) |
| 60 IPC_MESSAGE_HANDLER(SpellCheckMsg_WordRemoved, OnWordRemoved) | 98 IPC_MESSAGE_HANDLER(SpellCheckMsg_WordRemoved, OnWordRemoved) |
| 61 IPC_MESSAGE_HANDLER(SpellCheckMsg_EnableAutoSpellCorrect, | 99 IPC_MESSAGE_HANDLER(SpellCheckMsg_EnableAutoSpellCorrect, |
| 62 OnEnableAutoSpellCorrect) | 100 OnEnableAutoSpellCorrect) |
| 101 IPC_MESSAGE_HANDLER(SpellCheckMsg_EnableSpellCheck, OnEnableSpellCheck) | |
| 63 IPC_MESSAGE_UNHANDLED(handled = false) | 102 IPC_MESSAGE_UNHANDLED(handled = false) |
| 64 IPC_END_MESSAGE_MAP() | 103 IPC_END_MESSAGE_MAP() |
| 65 | 104 |
| 66 return handled; | 105 return handled; |
| 67 } | 106 } |
| 68 | 107 |
| 69 void SpellCheck::OnInit(IPC::PlatformFileForTransit bdict_file, | 108 void SpellCheck::OnInit(IPC::PlatformFileForTransit bdict_file, |
| 70 const std::vector<std::string>& custom_words, | 109 const std::vector<std::string>& custom_words, |
| 71 const std::string& language, | 110 const std::string& language, |
| 72 bool auto_spell_correct) { | 111 bool auto_spell_correct) { |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 85 | 124 |
| 86 void SpellCheck::OnWordRemoved(const std::string& word) { | 125 void SpellCheck::OnWordRemoved(const std::string& word) { |
| 87 if (platform_spelling_engine_.get()) | 126 if (platform_spelling_engine_.get()) |
| 88 platform_spelling_engine_->OnWordRemoved(word); | 127 platform_spelling_engine_->OnWordRemoved(word); |
| 89 } | 128 } |
| 90 | 129 |
| 91 void SpellCheck::OnEnableAutoSpellCorrect(bool enable) { | 130 void SpellCheck::OnEnableAutoSpellCorrect(bool enable) { |
| 92 auto_spell_correct_turned_on_ = enable; | 131 auto_spell_correct_turned_on_ = enable; |
| 93 } | 132 } |
| 94 | 133 |
| 134 void SpellCheck::OnEnableSpellCheck(bool enable) { | |
| 135 spellcheck_enabled_ = enable; | |
| 136 UpdateSpellcheckEnabled updater(enable); | |
| 137 content::RenderView::ForEach(&updater); | |
| 138 } | |
| 139 | |
| 95 // TODO(groby): Make sure we always have a spelling engine, even before Init() | 140 // TODO(groby): Make sure we always have a spelling engine, even before Init() |
| 96 // is called. | 141 // is called. |
| 97 void SpellCheck::Init(base::PlatformFile file, | 142 void SpellCheck::Init(base::PlatformFile file, |
| 98 const std::vector<std::string>& custom_words, | 143 const std::vector<std::string>& custom_words, |
| 99 const std::string& language) { | 144 const std::string& language) { |
| 100 platform_spelling_engine_->Init(file, custom_words); | 145 platform_spelling_engine_->Init(file, custom_words); |
| 101 | 146 |
| 102 character_attributes_.SetDefaultLanguage(language); | 147 character_attributes_.SetDefaultLanguage(language); |
| 103 text_iterator_.Reset(); | 148 text_iterator_.Reset(); |
| 104 contraction_iterator_.Reset(); | 149 contraction_iterator_.Reset(); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 type = WebKit::WebTextCheckingTypeGrammar; | 420 type = WebKit::WebTextCheckingTypeGrammar; |
| 376 } | 421 } |
| 377 } | 422 } |
| 378 list[i] = WebKit::WebTextCheckingResult(type, | 423 list[i] = WebKit::WebTextCheckingResult(type, |
| 379 word_location + line_offset, | 424 word_location + line_offset, |
| 380 word_length, | 425 word_length, |
| 381 spellcheck_results[i].replacement); | 426 spellcheck_results[i].replacement); |
| 382 } | 427 } |
| 383 textcheck_results->swap(list); | 428 textcheck_results->swap(list); |
| 384 } | 429 } |
| OLD | NEW |