OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/spellchecker/spellcheck_host_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "chrome/browser/spellchecker/spellcheck_custom_dictionary.h" |
| 11 #include "chrome/browser/spellchecker/spellcheck_factory.h" |
| 12 #include "chrome/browser/spellchecker/spellcheck_service.h" |
| 13 #include "components/spellcheck/browser/spellcheck_host_metrics.h" |
| 14 #include "components/spellcheck/browser/spelling_service_client.h" |
| 15 #include "components/spellcheck/common/spellcheck_result.h" |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 19 #include "services/service_manager/public/cpp/bind_source_info.h" |
| 20 |
| 21 SpellCheckHostImpl::SpellCheckHostImpl(int render_process_id) |
| 22 : render_process_id_(render_process_id), |
| 23 client_(new SpellingServiceClient) {} |
| 24 |
| 25 SpellCheckHostImpl::~SpellCheckHostImpl() = default; |
| 26 |
| 27 // static |
| 28 void SpellCheckHostImpl::Create( |
| 29 int render_process_id, |
| 30 const service_manager::BindSourceInfo& source_info, |
| 31 spellcheck::mojom::SpellCheckHostRequest request) { |
| 32 mojo::MakeStrongBinding( |
| 33 base::MakeUnique<SpellCheckHostImpl>(render_process_id), |
| 34 std::move(request)); |
| 35 } |
| 36 |
| 37 void SpellCheckHostImpl::RequestDictionary() { |
| 38 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 39 |
| 40 // The renderer has requested that we initialize its spellchecker. This |
| 41 // generally should only be called once per session, as after the first |
| 42 // call, future renderers will be passed the initialization information |
| 43 // on startup (or when the dictionary changes in some way). |
| 44 SpellcheckService* spellcheck = GetSpellcheckService(); |
| 45 if (!spellcheck) |
| 46 return; // Teardown. |
| 47 |
| 48 // The spellchecker initialization already started and finished; just |
| 49 // send it to the renderer. |
| 50 spellcheck->InitForRenderer( |
| 51 content::RenderProcessHost::FromID(render_process_id_)); |
| 52 |
| 53 // TODO(rlp): Ensure that we do not initialize the hunspell dictionary |
| 54 // more than once if we get requests from different renderers. |
| 55 } |
| 56 |
| 57 void SpellCheckHostImpl::NotifyChecked(const base::string16& word, |
| 58 bool misspelled) { |
| 59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 60 |
| 61 SpellcheckService* spellcheck = GetSpellcheckService(); |
| 62 if (!spellcheck) |
| 63 return; // Teardown. |
| 64 if (spellcheck->GetMetrics()) |
| 65 spellcheck->GetMetrics()->RecordCheckedWordStats(word, misspelled); |
| 66 } |
| 67 |
| 68 void SpellCheckHostImpl::CallSpellingService( |
| 69 const base::string16& text, |
| 70 const CallSpellingServiceCallback& callback) { |
| 71 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 72 DCHECK(!text.empty()); |
| 73 |
| 74 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 75 content::RenderProcessHost* host = |
| 76 content::RenderProcessHost::FromID(render_process_id_); |
| 77 |
| 78 // Checks the user profile and sends a JSON-RPC request to the Spelling |
| 79 // service if a user enables the "Ask Google for suggestions" option. When |
| 80 // a response is received (including an error) from the remote Spelling |
| 81 // service, calls CallSpellingServiceDone. |
| 82 client_->RequestTextCheck( |
| 83 host ? host->GetBrowserContext() : nullptr, |
| 84 SpellingServiceClient::SPELLCHECK, text, |
| 85 base::Bind(&SpellCheckHostImpl::CallSpellingServiceDone, |
| 86 base::Unretained(this), callback)); |
| 87 #else |
| 88 callback.Run(false, std::vector<SpellCheckResult>()); |
| 89 #endif |
| 90 } |
| 91 |
| 92 #if !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 93 void SpellCheckHostImpl::CallSpellingServiceDone( |
| 94 const CallSpellingServiceCallback& callback, |
| 95 bool success, |
| 96 const base::string16& text, |
| 97 const std::vector<SpellCheckResult>& service_results) { |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 99 |
| 100 SpellcheckService* spellcheck = GetSpellcheckService(); |
| 101 if (!spellcheck) { // Teardown. |
| 102 callback.Run(false, std::vector<SpellCheckResult>()); |
| 103 return; |
| 104 } |
| 105 |
| 106 std::vector<SpellCheckResult> filter_results = FilterCustomWordResults( |
| 107 base::UTF16ToUTF8(text), *spellcheck->GetCustomDictionary(), |
| 108 service_results); |
| 109 callback.Run(success, filter_results); |
| 110 } |
| 111 |
| 112 // static |
| 113 std::vector<SpellCheckResult> SpellCheckHostImpl::FilterCustomWordResults( |
| 114 const std::string& text, |
| 115 const SpellcheckCustomDictionary& custom_dictionary, |
| 116 const std::vector<SpellCheckResult>& service_results) { |
| 117 std::vector<SpellCheckResult> results; |
| 118 for (const auto& result : service_results) { |
| 119 const std::string word = text.substr(result.location, result.length); |
| 120 if (!custom_dictionary.HasWord(word)) |
| 121 results.push_back(result); |
| 122 } |
| 123 |
| 124 return results; |
| 125 } |
| 126 #endif // !BUILDFLAG(USE_BROWSER_SPELLCHECKER) |
| 127 |
| 128 SpellcheckService* SpellCheckHostImpl::GetSpellcheckService() const { |
| 129 return SpellcheckServiceFactory::GetForRenderProcessId(render_process_id_); |
| 130 } |
OLD | NEW |