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