File: spelling_service_client.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (350 lines) | stat: -rw-r--r-- 13,400 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/spellcheck/browser/spelling_service_client.h"

#include <stddef.h>

#include <algorithm>
#include <memory>

#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/json/string_escape.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/prefs/pref_service.h"
#include "components/spellcheck/browser/pref_names.h"
#include "components/spellcheck/common/spellcheck_common.h"
#include "components/spellcheck/common/spellcheck_result.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"
#include "google_apis/google_api_keys.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/gurl.h"

namespace {

// The REST endpoint for requesting spell checking and sending user feedback.
const char kSpellingServiceRestURL[] =
    "https://www.googleapis.com/spelling/v%d/spelling/check?key=%s";

// The spellcheck suggestions object key in the JSON response from the spelling
// service.
const char kMisspellingsRestPath[] = "spellingCheckResponse.misspellings";

// The location of error messages in JSON response from spelling service.
const char kErrorPath[] = "error";

// Languages currently supported by SPELLCHECK.
const char* const kValidLanguages[] = {"en", "es", "fi", "da"};

}  // namespace

SpellingServiceClient::SpellingServiceClient() = default;

SpellingServiceClient::~SpellingServiceClient() = default;

bool SpellingServiceClient::RequestTextCheck(
    content::BrowserContext* context,
    ServiceType type,
    const std::u16string& text,
    TextCheckCompleteCallback callback) {
  DCHECK(type == SUGGEST || type == SPELLCHECK);
  if (!context || !IsAvailable(context, type)) {
    std::move(callback).Run(false, text, std::vector<SpellCheckResult>());
    return false;
  }
  const PrefService* pref = user_prefs::UserPrefs::Get(context);
  DCHECK(pref);

  std::string dictionary;
  const base::Value::List& dicts_list =
      pref->GetList(spellcheck::prefs::kSpellCheckDictionaries);
  if (0u < dicts_list.size() && dicts_list[0].is_string())
    dictionary = dicts_list[0].GetString();

  std::string language_code;
  std::string country_code;
  spellcheck::GetISOLanguageCountryCodeFromLocale(dictionary, &language_code,
                                                  &country_code);

  // Replace typographical apostrophes with typewriter apostrophes, so that
  // server word breaker behaves correctly.
  const char16_t kApostrophe = 0x27;
  const char16_t kRightSingleQuotationMark = 0x2019;
  std::u16string text_copy = text;
  std::replace(text_copy.begin(), text_copy.end(), kRightSingleQuotationMark,
               kApostrophe);

  std::string encoded_text = base::GetQuotedJSONString(text_copy);

  static const char kSpellingRequestRestBodyTemplate[] =
      "{"
      "\"text\":%s,"
      "\"language\":\"%s\","
      "\"originCountry\":\"%s\""
      "}";

  std::string request_body =
      base::StringPrintf(kSpellingRequestRestBodyTemplate, encoded_text.c_str(),
                         language_code.c_str(), country_code.c_str());

  // Create traffic annotation tag.
  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("spellcheck_lookup", R"(
        semantics {
          sender: "Online Spellcheck"
          description:
            "Chromium can provide smarter spell-checking, by sending the text "
            "that the users type into the browser, to Google's servers. This"
            "allows users to use the same spell-checking technology used by "
            "Google products, such as Docs. If the feature is enabled, "
            "Chromium will send the entire contents of text fields as user "
            "types them to Google, along with the browser’s default language. "
            "Google returns a list of suggested spellings, which will be "
            "displayed in the context menu."
          trigger: "User types text into a text field or asks to correct a "
                   "misspelled word."
          data: "Text a user has typed into a text field. No user identifier "
                "is sent along with the text."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "Users can enable or disable this feature via 'Enhanced spell "
            "check' in Chromium's settings under 'Sync and Google services'. "
            "The feature is disabled by default."
          chrome_policy {
            SpellCheckServiceEnabled {
                policy_options {mode: MANDATORY}
                SpellCheckServiceEnabled: false
            }
          }
        })");

  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = BuildEndpointUrl(type);
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;
  resource_request->method = "POST";

  std::unique_ptr<network::SimpleURLLoader> simple_url_loader =
      network::SimpleURLLoader::Create(std::move(resource_request),
                                       traffic_annotation);
  simple_url_loader->AttachStringForUpload(request_body, "application/json");

  auto it = spellcheck_loaders_.insert(
      spellcheck_loaders_.begin(),
      std::make_unique<TextCheckCallbackData>(std::move(simple_url_loader),
                                              std::move(callback), text));
  network::SimpleURLLoader* loader = it->get()->simple_url_loader.get();
  auto url_loader_factory = url_loader_factory_for_testing_
                                ? url_loader_factory_for_testing_
                                : context->GetDefaultStoragePartition()
                                      ->GetURLLoaderFactoryForBrowserProcess();
  loader->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      url_loader_factory.get(),
      base::BindOnce(&SpellingServiceClient::OnSimpleLoaderComplete,
                     base::Unretained(this), std::move(it),
                     base::TimeTicks::Now()));
  return true;
}

bool SpellingServiceClient::IsAvailable(content::BrowserContext* context,
                                        ServiceType type) {
  const PrefService* pref = user_prefs::UserPrefs::Get(context);
  DCHECK(pref);
  // If prefs don't allow spell checking, if enhanced spell check is disabled,
  // or if the context is off the record, the spelling service should be
  // unavailable.
  if (!pref->GetBoolean(spellcheck::prefs::kSpellCheckEnable) ||
      !pref->GetBoolean(spellcheck::prefs::kSpellCheckUseSpellingService) ||
      context->IsOffTheRecord())
    return false;

  // If the locale for spelling has not been set, the user has not decided to
  // use spellcheck so we don't do anything remote (suggest or spelling).
  std::string locale;
  const auto& dicts_list =
      pref->GetList(spellcheck::prefs::kSpellCheckDictionaries);
  if (!dicts_list.empty() && dicts_list[0].is_string()) {
    locale = dicts_list[0].GetString();
  }

  if (locale.empty())
    return false;

  // Finally, if all options are available, we only enable only SUGGEST
  // if SPELLCHECK is not available for our language because SPELLCHECK results
  // are a superset of SUGGEST results.
  for (const char* language : kValidLanguages) {
    if (!locale.compare(0, 2, language))
      return type == SPELLCHECK;
  }

  // Only SUGGEST is allowed.
  return type == SUGGEST;
}

void SpellingServiceClient::SetURLLoaderFactoryForTesting(
    scoped_refptr<network::SharedURLLoaderFactory>
        url_loader_factory_for_testing) {
  url_loader_factory_for_testing_ = std::move(url_loader_factory_for_testing);
}

GURL SpellingServiceClient::BuildEndpointUrl(int type) {
    return GURL(base::StringPrintf(kSpellingServiceRestURL, type,
                                   google_apis::GetAPIKey().c_str()));
}

bool SpellingServiceClient::ParseResponse(
    const std::string& data,
    std::vector<SpellCheckResult>* results) {
  // Data is in the following format:
  //  * spellingCheckResponse: A wrapper object containing the response
  //    * mispellings: (optional Array<object>) A list of mistakes for the
  //      requested text, with the following format:
  //      * charStart: (number) The zero-based start of the misspelled region
  //      * charLength: (number) The length of the misspelled region
  //      * suggestions: (Array<object>) The suggestions for the misspelled
  //        text, with the following format:
  //        * suggestion: (string) the suggestion for the correct text
  //      * canAutoCorrect (optional boolean) Whether we can use the first
  //        suggestion for auto-correction
  //
  // Example response for "duck goes quisk":
  //  {
  //    "spellingCheckResponse": {
  //      "misspellings": [{
  //        "charStart": 10,
  //        "charLength": 5,
  //        "suggestions": [{
  //          "suggestion": "quack"
  //        }],
  //        "canAutoCorrect": false
  //      }]
  //    }
  //  }
  //
  // If the service is not available, the Spelling service returns JSON with an
  // error:
  //  {
  //    "error": {
  //      "code": 400,
  //      "message": "Bad Request",
  //      "data": [...]
  //    }
  //  }

  std::optional<base::Value::Dict> value =
      base::JSONReader::ReadDict(data, base::JSON_ALLOW_TRAILING_COMMAS);
  if (!value) {
    return false;
  }

  // Check for errors from spelling service.
    const base::Value* error = value->Find(kErrorPath);
    if (error) {
    return false;
    }

  // Retrieve the array of Misspelling objects. When the input text does not
  // have misspelled words, it returns an empty JSON. (In this case, its HTTP
  // status is 200.) We just return true for this case.
    const base::Value::List* misspellings =
        value->FindListByDottedPath(kMisspellingsRestPath);

    if (!misspellings) {
    return true;
    }

    for (const base::Value& misspelling : *misspellings) {
    // Retrieve the i-th misspelling region and put it to the given vector. When
    // the Spelling service sends two or more suggestions, we read only the
    // first one because SpellCheckResult can store only one suggestion.
    auto* misspelling_dict = misspelling.GetIfDict();
    if (!misspelling_dict) {
      return false;
    }

    std::optional<int> start = misspelling_dict->FindInt("charStart");
    std::optional<int> length = misspelling_dict->FindInt("charLength");
    const base::Value::List* suggestions =
        misspelling_dict->FindList("suggestions");
    if (!start || !length || !suggestions) {
      return false;
    }

    const auto* suggestion = suggestions->front().GetIfDict();
    if (!suggestion) {
      return false;
    }

    const std::string* replacement = suggestion->FindString("suggestion");
    if (!replacement) {
      return false;
    }
    SpellCheckResult result(SpellCheckResult::SPELLING, *start, *length,
                            base::UTF8ToUTF16(*replacement));
    results->push_back(result);
    }
  return true;
}

SpellingServiceClient::TextCheckCallbackData::TextCheckCallbackData(
    std::unique_ptr<network::SimpleURLLoader> simple_url_loader,
    TextCheckCompleteCallback callback,
    std::u16string text)
    : simple_url_loader(std::move(simple_url_loader)),
      callback(std::move(callback)),
      text(text) {}

SpellingServiceClient::TextCheckCallbackData::~TextCheckCallbackData() =
    default;

void SpellingServiceClient::OnSimpleLoaderComplete(
    SpellCheckLoaderList::iterator it,
    base::TimeTicks request_start,
    std::unique_ptr<std::string> response_body) {
  UMA_HISTOGRAM_TIMES("SpellCheck.SpellingService.RequestDuration",
                      base::TimeTicks::Now() - request_start);

  TextCheckCompleteCallback callback = std::move(it->get()->callback);
  std::u16string text = it->get()->text;
  bool success = false;
  std::vector<SpellCheckResult> results;
  if (response_body)
    success = ParseResponse(*response_body, &results);

  int response_code = net::ERR_FAILED;
  auto* resp_info = it->get()->simple_url_loader->ResponseInfo();
  if (resp_info && resp_info->headers) {
    response_code = resp_info->headers->response_code();
  }

  ServiceRequestResultType result_type =
      ServiceRequestResultType::kRequestFailure;
  if (success) {
    result_type = results.empty()
                      ? ServiceRequestResultType::kSuccessEmpty
                      : ServiceRequestResultType::kSuccessWithSuggestions;
  }

  base::UmaHistogramSparse("SpellCheck.SpellingService.RequestHttpResponseCode",
                           response_code);
  UMA_HISTOGRAM_ENUMERATION("SpellCheck.SpellingService.RequestResultType",
                            result_type);

  spellcheck_loaders_.erase(it);
  std::move(callback).Run(success, text, results);
}