Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(726)

Side by Side Diff: components/spellcheck/browser/spelling_service_client.cc

Issue 2244083002: Componentize spellcheck [4]: spellcheck/browser and android java-side. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/browser/spellchecker/spelling_service_client.h" 5 #include "components/spellcheck/browser/spelling_service_client.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/json/json_reader.h" 11 #include "base/json/json_reader.h"
12 #include "base/json/string_escape.h" 12 #include "base/json/string_escape.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/stl_util.h" 14 #include "base/stl_util.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 17 #include "base/strings/utf_string_conversions.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "chrome/common/pref_names.h"
20 #include "components/data_use_measurement/core/data_use_user_data.h" 19 #include "components/data_use_measurement/core/data_use_user_data.h"
21 #include "components/prefs/pref_service.h" 20 #include "components/prefs/pref_service.h"
21 #include "components/spellcheck/browser/pref_names.h"
22 #include "components/spellcheck/common/spellcheck_common.h" 22 #include "components/spellcheck/common/spellcheck_common.h"
23 #include "components/spellcheck/common/spellcheck_result.h" 23 #include "components/spellcheck/common/spellcheck_result.h"
24 #include "components/user_prefs/user_prefs.h" 24 #include "components/user_prefs/user_prefs.h"
25 #include "content/public/browser/browser_context.h" 25 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/storage_partition.h" 26 #include "content/public/browser/storage_partition.h"
27 #include "google_apis/google_api_keys.h" 27 #include "google_apis/google_api_keys.h"
28 #include "net/base/load_flags.h" 28 #include "net/base/load_flags.h"
29 #include "net/url_request/url_fetcher.h" 29 #include "net/url_request/url_fetcher.h"
30 #include "url/gurl.h" 30 #include "url/gurl.h"
31 31
32 namespace { 32 namespace {
33 33
34 // The URL for requesting spell checking and sending user feedback. 34 // The URL for requesting spell checking and sending user feedback.
35 const char kSpellingServiceURL[] = "https://www.googleapis.com/rpc"; 35 const char kSpellingServiceURL[] = "https://www.googleapis.com/rpc";
36 36
37 // The location of spellcheck suggestions in JSON response from spelling 37 // The location of spellcheck suggestions in JSON response from spelling
38 // service. 38 // service.
39 const char kMisspellingsPath[] = "result.spellingCheckResponse.misspellings"; 39 const char kMisspellingsPath[] = "result.spellingCheckResponse.misspellings";
40 40
41 // The location of error messages in JSON response from spelling service. 41 // The location of error messages in JSON response from spelling service.
42 const char kErrorPath[] = "error"; 42 const char kErrorPath[] = "error";
43 43
44 // Languages currently supported by SPELLCHECK. 44 // Languages currently supported by SPELLCHECK.
45 const char* const kValidLanguages[] = {"en", "es", "fi", "da"}; 45 const char* const kValidLanguages[] = {"en", "es", "fi", "da"};
46 46
47 } // namespace 47 } // namespace
48 48
49 SpellingServiceClient::SpellingServiceClient() { 49 SpellingServiceClient::SpellingServiceClient() {}
50 }
51 50
52 SpellingServiceClient::~SpellingServiceClient() { 51 SpellingServiceClient::~SpellingServiceClient() {
53 base::STLDeleteContainerPairPointers(spellcheck_fetchers_.begin(), 52 base::STLDeleteContainerPairPointers(spellcheck_fetchers_.begin(),
54 spellcheck_fetchers_.end()); 53 spellcheck_fetchers_.end());
55 } 54 }
56 55
57 bool SpellingServiceClient::RequestTextCheck( 56 bool SpellingServiceClient::RequestTextCheck(
58 content::BrowserContext* context, 57 content::BrowserContext* context,
59 ServiceType type, 58 ServiceType type,
60 const base::string16& text, 59 const base::string16& text,
61 const TextCheckCompleteCallback& callback) { 60 const TextCheckCompleteCallback& callback) {
62 DCHECK(type == SUGGEST || type == SPELLCHECK); 61 DCHECK(type == SUGGEST || type == SPELLCHECK);
63 if (!context || !IsAvailable(context, type)) { 62 if (!context || !IsAvailable(context, type)) {
64 callback.Run(false, text, std::vector<SpellCheckResult>()); 63 callback.Run(false, text, std::vector<SpellCheckResult>());
65 return false; 64 return false;
66 } 65 }
67 66
68 const PrefService* pref = user_prefs::UserPrefs::Get(context); 67 const PrefService* pref = user_prefs::UserPrefs::Get(context);
69 DCHECK(pref); 68 DCHECK(pref);
70 69
71 std::string dictionary; 70 std::string dictionary;
72 pref->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &dictionary); 71 pref->GetList(spellcheck::prefs::kSpellCheckDictionaries)
72 ->GetString(0, &dictionary);
73 73
74 std::string language_code; 74 std::string language_code;
75 std::string country_code; 75 std::string country_code;
76 spellcheck::GetISOLanguageCountryCodeFromLocale(dictionary, &language_code, 76 spellcheck::GetISOLanguageCountryCodeFromLocale(dictionary, &language_code,
77 &country_code); 77 &country_code);
78 78
79 // Replace typographical apostrophes with typewriter apostrophes, so that 79 // Replace typographical apostrophes with typewriter apostrophes, so that
80 // server word breaker behaves correctly. 80 // server word breaker behaves correctly.
81 const base::char16 kApostrophe = 0x27; 81 const base::char16 kApostrophe = 0x27;
82 const base::char16 kRightSingleQuotationMark = 0x2019; 82 const base::char16 kRightSingleQuotationMark = 0x2019;
(...skipping 10 matching lines...) Expand all
93 "\"apiVersion\":\"v%d\"," 93 "\"apiVersion\":\"v%d\","
94 "\"params\":{" 94 "\"params\":{"
95 "\"text\":%s," 95 "\"text\":%s,"
96 "\"language\":\"%s\"," 96 "\"language\":\"%s\","
97 "\"originCountry\":\"%s\"," 97 "\"originCountry\":\"%s\","
98 "\"key\":%s" 98 "\"key\":%s"
99 "}" 99 "}"
100 "}"; 100 "}";
101 std::string api_key = base::GetQuotedJSONString(google_apis::GetAPIKey()); 101 std::string api_key = base::GetQuotedJSONString(google_apis::GetAPIKey());
102 std::string request = base::StringPrintf( 102 std::string request = base::StringPrintf(
103 kSpellingRequest, 103 kSpellingRequest, type, encoded_text.c_str(), language_code.c_str(),
104 type, 104 country_code.c_str(), api_key.c_str());
105 encoded_text.c_str(),
106 language_code.c_str(),
107 country_code.c_str(),
108 api_key.c_str());
109 105
110 GURL url = GURL(kSpellingServiceURL); 106 GURL url = GURL(kSpellingServiceURL);
111 net::URLFetcher* fetcher = CreateURLFetcher(url).release(); 107 net::URLFetcher* fetcher = CreateURLFetcher(url).release();
112 data_use_measurement::DataUseUserData::AttachToFetcher( 108 data_use_measurement::DataUseUserData::AttachToFetcher(
113 fetcher, data_use_measurement::DataUseUserData::SPELL_CHECKER); 109 fetcher, data_use_measurement::DataUseUserData::SPELL_CHECKER);
114 fetcher->SetRequestContext( 110 fetcher->SetRequestContext(
115 content::BrowserContext::GetDefaultStoragePartition(context)-> 111 content::BrowserContext::GetDefaultStoragePartition(context)
116 GetURLRequestContext()); 112 ->GetURLRequestContext());
117 fetcher->SetUploadData("application/json", request); 113 fetcher->SetUploadData("application/json", request);
118 fetcher->SetLoadFlags( 114 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
119 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); 115 net::LOAD_DO_NOT_SAVE_COOKIES);
120 spellcheck_fetchers_[fetcher] = new TextCheckCallbackData(callback, text); 116 spellcheck_fetchers_[fetcher] = new TextCheckCallbackData(callback, text);
121 fetcher->Start(); 117 fetcher->Start();
122 return true; 118 return true;
123 } 119 }
124 120
125 bool SpellingServiceClient::IsAvailable( 121 bool SpellingServiceClient::IsAvailable(content::BrowserContext* context,
126 content::BrowserContext* context, 122 ServiceType type) {
127 ServiceType type) {
128 const PrefService* pref = user_prefs::UserPrefs::Get(context); 123 const PrefService* pref = user_prefs::UserPrefs::Get(context);
129 DCHECK(pref); 124 DCHECK(pref);
130 // If prefs don't allow spellchecking, if the context is off the record, or if 125 // If prefs don't allow spellchecking, if the context is off the record, or if
131 // multilingual spellchecking is enabled the spelling service should be 126 // multilingual spellchecking is enabled the spelling service should be
132 // unavailable. 127 // unavailable.
133 if (!pref->GetBoolean(prefs::kEnableContinuousSpellcheck) || 128 if (!pref->GetBoolean(spellcheck::prefs::kEnableContinuousSpellcheck) ||
134 !pref->GetBoolean(prefs::kSpellCheckUseSpellingService) || 129 !pref->GetBoolean(spellcheck::prefs::kSpellCheckUseSpellingService) ||
135 context->IsOffTheRecord()) 130 context->IsOffTheRecord())
136 return false; 131 return false;
137 132
138 // If the locale for spelling has not been set, the user has not decided to 133 // If the locale for spelling has not been set, the user has not decided to
139 // use spellcheck so we don't do anything remote (suggest or spelling). 134 // use spellcheck so we don't do anything remote (suggest or spelling).
140 std::string locale; 135 std::string locale;
141 pref->GetList(prefs::kSpellCheckDictionaries)->GetString(0, &locale); 136 pref->GetList(spellcheck::prefs::kSpellCheckDictionaries)
137 ->GetString(0, &locale);
142 if (locale.empty()) 138 if (locale.empty())
143 return false; 139 return false;
144 140
145 // Finally, if all options are available, we only enable only SUGGEST 141 // Finally, if all options are available, we only enable only SUGGEST
146 // if SPELLCHECK is not available for our language because SPELLCHECK results 142 // if SPELLCHECK is not available for our language because SPELLCHECK results
147 // are a superset of SUGGEST results. 143 // are a superset of SUGGEST results.
148 for (const char* language : kValidLanguages) { 144 for (const char* language : kValidLanguages) {
149 if (!locale.compare(0, 2, language)) 145 if (!locale.compare(0, 2, language))
150 return type == SPELLCHECK; 146 return type == SPELLCHECK;
151 } 147 }
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 !misspelling->GetList("suggestions", &suggestions)) { 228 !misspelling->GetList("suggestions", &suggestions)) {
233 return false; 229 return false;
234 } 230 }
235 231
236 base::DictionaryValue* suggestion = NULL; 232 base::DictionaryValue* suggestion = NULL;
237 base::string16 replacement; 233 base::string16 replacement;
238 if (!suggestions->GetDictionary(0, &suggestion) || 234 if (!suggestions->GetDictionary(0, &suggestion) ||
239 !suggestion->GetString("suggestion", &replacement)) { 235 !suggestion->GetString("suggestion", &replacement)) {
240 return false; 236 return false;
241 } 237 }
242 SpellCheckResult result( 238 SpellCheckResult result(SpellCheckResult::SPELLING, start, length,
243 SpellCheckResult::SPELLING, start, length, replacement); 239 replacement);
244 results->push_back(result); 240 results->push_back(result);
245 } 241 }
246 return true; 242 return true;
247 } 243 }
248 244
249 SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData( 245 SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData(
250 TextCheckCompleteCallback callback, 246 TextCheckCompleteCallback callback,
251 base::string16 text) 247 base::string16 text)
252 : callback(callback), 248 : callback(callback), text(text) {}
253 text(text) {
254 }
255 249
256 SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() { 250 SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() {}
257 }
258 251
259 void SpellingServiceClient::OnURLFetchComplete( 252 void SpellingServiceClient::OnURLFetchComplete(const net::URLFetcher* source) {
260 const net::URLFetcher* source) {
261 DCHECK(spellcheck_fetchers_[source]); 253 DCHECK(spellcheck_fetchers_[source]);
262 std::unique_ptr<const net::URLFetcher> fetcher(source); 254 std::unique_ptr<const net::URLFetcher> fetcher(source);
263 std::unique_ptr<TextCheckCallbackData> callback_data( 255 std::unique_ptr<TextCheckCallbackData> callback_data(
264 spellcheck_fetchers_[fetcher.get()]); 256 spellcheck_fetchers_[fetcher.get()]);
265 bool success = false; 257 bool success = false;
266 std::vector<SpellCheckResult> results; 258 std::vector<SpellCheckResult> results;
267 if (fetcher->GetResponseCode() / 100 == 2) { 259 if (fetcher->GetResponseCode() / 100 == 2) {
268 std::string data; 260 std::string data;
269 fetcher->GetResponseAsString(&data); 261 fetcher->GetResponseAsString(&data);
270 success = ParseResponse(data, &results); 262 success = ParseResponse(data, &results);
271 } 263 }
272 spellcheck_fetchers_.erase(fetcher.get()); 264 spellcheck_fetchers_.erase(fetcher.get());
273 265
274 // The callback may release the last (transitive) dependency on |this|. It 266 // The callback may release the last (transitive) dependency on |this|. It
275 // MUST be the last function called. 267 // MUST be the last function called.
276 callback_data->callback.Run(success, callback_data->text, results); 268 callback_data->callback.Run(success, callback_data->text, results);
277 } 269 }
278 270
279 std::unique_ptr<net::URLFetcher> SpellingServiceClient::CreateURLFetcher( 271 std::unique_ptr<net::URLFetcher> SpellingServiceClient::CreateURLFetcher(
280 const GURL& url) { 272 const GURL& url) {
281 return net::URLFetcher::Create(url, net::URLFetcher::POST, this); 273 return net::URLFetcher::Create(url, net::URLFetcher::POST, this);
282 } 274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698