File: spelling_request.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 4,229 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SPELLCHECKER_SPELLING_REQUEST_H_
#define CHROME_BROWSER_SPELLCHECKER_SPELLING_REQUEST_H_

#include "base/containers/unique_ptr_adapters.h"
#include "base/gtest_prod_util.h"
#include "components/spellcheck/browser/platform_spell_checker.h"
#include "components/spellcheck/browser/spell_check_host_impl.h"
#include "components/spellcheck/browser/spelling_service_client.h"

class SpellingRequest;

struct SpellCheckResult;

// SpellingRequest is owned by SpellCheckHostChromeImpl.
class SpellingRequest {
 public:
  using RequestTextCheckCallback =
      spellcheck::mojom::SpellCheckHost::RequestTextCheckCallback;
  using DestructionCallback = base::OnceCallback<void(SpellingRequest*)>;

  // Creates the request, but does not start it. Gives tests finer control over
  // how the request is executed.
  static std::unique_ptr<SpellingRequest> CreateForTest(
      const std::u16string& text,
      RequestTextCheckCallback callback,
      DestructionCallback destruction_callback,
      base::RepeatingClosure completion_barrier);

  SpellingRequest(PlatformSpellChecker* platform_spell_checker,
                  SpellingServiceClient* client,
                  const std::u16string& text,
                  int render_process_id,
                  int document_tag,
                  RequestTextCheckCallback callback,
                  DestructionCallback destruction_callback);

  SpellingRequest(const SpellingRequest&) = delete;
  SpellingRequest& operator=(const SpellingRequest&) = delete;

  ~SpellingRequest();

  // Exposed to tests only.
  static void CombineResults(
      std::vector<SpellCheckResult>* remote_results,
      const std::vector<SpellCheckResult>& local_results);

 private:
  FRIEND_TEST_ALL_PREFIXES(SpellingRequestRemoteCheckUnitTest,
                           OnRemoteCheckCompleted);

  // Test-only constructor that does not kick off the remote and local checks.
  // Use the `CreateForTest` static helper.
  SpellingRequest(const std::u16string& text,
                  RequestTextCheckCallback callback,
                  DestructionCallback destruction_callback,
                  base::RepeatingClosure completion_barrier);

  // Request server-side checking for |text_|.
  void RequestRemoteCheck(SpellingServiceClient* client, int render_process_id);

  // Request a check for |text_| from local spell checker.
  void RequestLocalCheck(PlatformSpellChecker* platform_spell_checker,
                         int document_tag);

  // Check if all pending requests are done, send reply to render process if so.
  void OnCheckCompleted();

  // Called when server-side checking is complete. Must be called on UI thread.
  void OnRemoteCheckCompleted(bool success,
                              const std::u16string& text,
                              const std::vector<SpellCheckResult>& results);

  // Called when local checking is complete. Must be called on UI thread.
  void OnLocalCheckCompleted(const std::vector<SpellCheckResult>& results);

  // Forwards the results back to UI thread when local checking completes.
  static void OnLocalCheckCompletedOnAnyThread(
      base::WeakPtr<SpellingRequest> request,
      const std::vector<SpellCheckResult>& results);

  std::vector<SpellCheckResult> local_results_;
  std::vector<SpellCheckResult> remote_results_;

  // Barrier closure for completion of both remote and local check.
  base::RepeatingClosure completion_barrier_;
  bool remote_success_;

  // The string to be spell-checked.
  std::u16string text_;

  // Callback to send the results to renderer. Note that both RequestTextCheck
  // and RequestPartialTextCheck have the same callback signatures, so both
  // callback types can be assigned to this member (the generated
  // base::OnceCallback types are the same).
  RequestTextCheckCallback callback_;

  // Callback to delete |this|. Called on |this| after everything is done.
  DestructionCallback destruction_callback_;

  base::WeakPtrFactory<SpellingRequest> weak_factory_{this};
};

#endif  // CHROME_BROWSER_SPELLCHECKER_SPELLING_REQUEST_H_