OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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 // An object to record and send user feedback to spelling service. The spelling | |
6 // service uses the feedback to improve its suggestions. | |
7 // | |
8 // Assigns uint32_t hash identifiers to spelling suggestions from spelling | |
9 // service and stores these suggestions. Records user's actions on these | |
10 // suggestions. Periodically sends batches of user feedback to the spelling | |
11 // service. | |
12 | |
13 #ifndef CHROME_BROWSER_SPELLCHECKER_FEEDBACK_SENDER_H_ | |
14 #define CHROME_BROWSER_SPELLCHECKER_FEEDBACK_SENDER_H_ | |
15 | |
16 #include <stddef.h> | |
17 #include <stdint.h> | |
18 | |
19 #include <array> | |
20 #include <climits> | |
21 #include <map> | |
22 #include <set> | |
23 #include <vector> | |
24 | |
25 #include "base/macros.h" | |
26 #include "base/memory/scoped_vector.h" | |
27 #include "base/memory/weak_ptr.h" | |
28 #include "base/timer/timer.h" | |
29 #include "chrome/browser/spellchecker/feedback.h" | |
30 #include "chrome/browser/spellchecker/misspelling.h" | |
31 #include "net/url_request/url_fetcher_delegate.h" | |
32 #include "url/gurl.h" | |
33 | |
34 class SpellCheckMarker; | |
35 struct SpellCheckResult; | |
36 | |
37 namespace net { | |
38 class URLFetcher; | |
39 class URLRequestContextGetter; | |
40 } | |
41 | |
42 namespace spellcheck { | |
43 | |
44 namespace { | |
45 | |
46 // Constants for the feedback field trial. | |
47 const char kFeedbackFieldTrialName[] = "SpellingServiceFeedback"; | |
48 const char kFeedbackFieldTrialEnabledGroupName[] = "Enabled"; | |
49 | |
50 } // namespace | |
51 | |
52 // Stores and sends user feedback to the spelling service. Sample usage: | |
53 // FeedbackSender sender(profile.GetRequestContext(), language, country); | |
54 // sender.OnSpellcheckResults(spellcheck_results_from_spelling_service, | |
55 // renderer_process_id, | |
56 // spellchecked_text, | |
57 // existing_hashes); | |
58 // sender.SelectedSuggestion(hash, suggestion_index); | |
59 class FeedbackSender : public base::SupportsWeakPtr<FeedbackSender>, | |
60 public net::URLFetcherDelegate { | |
61 public: | |
62 // Constructs a feedback sender. Keeps |request_context| in a scoped_refptr, | |
63 // because URLRequestContextGetter implements RefcountedThreadSafe. | |
64 FeedbackSender(net::URLRequestContextGetter* request_context, | |
65 const std::string& language, | |
66 const std::string& country); | |
67 ~FeedbackSender() override; | |
68 | |
69 // Records that user selected suggestion |suggestion_index| for the | |
70 // misspelling identified by |hash|. | |
71 void SelectedSuggestion(uint32_t hash, int suggestion_index); | |
72 | |
73 // Records that user added the misspelling identified by |hash| to the | |
74 // dictionary. | |
75 void AddedToDictionary(uint32_t hash); | |
76 | |
77 // Records that user right-clicked on the misspelling identified by |hash|, | |
78 // but did not select any suggestion. | |
79 void IgnoredSuggestions(uint32_t hash); | |
80 | |
81 // Records that user did not choose any suggestion but manually corrected the | |
82 // misspelling identified by |hash| to string |correction|, which is not in | |
83 // the list of suggestions. | |
84 void ManuallyCorrected(uint32_t hash, const base::string16& correction); | |
85 | |
86 // Records that user has the misspelling in the custom dictionary. The user | |
87 // will never see the spellcheck suggestions for the misspelling. | |
88 void RecordInDictionary(uint32_t hash); | |
89 | |
90 // Receives document markers for renderer with process ID |render_process_id| | |
91 // when the renderer responds to a RequestDocumentMarkers() call. Finalizes | |
92 // feedback for the markers that are gone from the renderer. Sends feedback | |
93 // data for the renderer with process ID |renderer_process_id| to the spelling | |
94 // service. If the current session has expired, then refreshes the session | |
95 // start timestamp and sends out all of the feedback data. | |
96 void OnReceiveDocumentMarkers(int renderer_process_id, | |
97 const std::vector<uint32_t>& markers); | |
98 | |
99 // Generates feedback data based on spellcheck results. The new feedback data | |
100 // is pending. Sets hash identifiers for |results|. Called when spelling | |
101 // service client receives results from the spelling service. Does not take | |
102 // ownership of |results|. | |
103 void OnSpellcheckResults(int renderer_process_id, | |
104 const base::string16& text, | |
105 const std::vector<SpellCheckMarker>& markers, | |
106 std::vector<SpellCheckResult>* results); | |
107 | |
108 // Receives updated language and country code for feedback. Finalizes and | |
109 // sends out all of the feedback data. | |
110 void OnLanguageCountryChange(const std::string& language, | |
111 const std::string& country); | |
112 | |
113 // Starts collecting feedback, if it's not already being collected. | |
114 void StartFeedbackCollection(); | |
115 | |
116 // Sends out all previously collected data and stops collecting feedback, if | |
117 // it was being collected. | |
118 void StopFeedbackCollection(); | |
119 | |
120 // 256 bits | |
121 typedef std::array<char, 256 / CHAR_BIT> RandSalt; | |
122 | |
123 private: | |
124 friend class FeedbackSenderTest; | |
125 | |
126 // Allow unit tests to override RNG. | |
127 virtual void RandBytes(void* p, size_t len); | |
128 | |
129 // net::URLFetcherDelegate implementation. Takes ownership of |source|. | |
130 void OnURLFetchComplete(const net::URLFetcher* source) override; | |
131 | |
132 // Requests the document markers from all of the renderers to determine which | |
133 // feedback can be finalized. Finalizes feedback for renderers that are gone. | |
134 // Called periodically when |timer_| fires. | |
135 void RequestDocumentMarkers(); | |
136 | |
137 // Sends out all feedback data. Resets the session-start timestamp to now. | |
138 // Restarts the timer that requests markers from the renderers. | |
139 void FlushFeedback(); | |
140 | |
141 // Sends out the |feedback_data|. | |
142 void SendFeedback(const std::vector<Misspelling>& feedback_data, | |
143 bool is_first_feedback_batch); | |
144 | |
145 // URL request context for the feedback senders. | |
146 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
147 | |
148 // The feedback API version. | |
149 const std::string api_version_; | |
150 | |
151 // The language of text. The string is a BCP 47 language tag. | |
152 std::string language_; | |
153 | |
154 // The country of origin. The string is the ISO 3166-1 alpha-3 code. | |
155 std::string country_; | |
156 | |
157 // Misspelling counter used to generate unique hash identifier for each | |
158 // misspelling. | |
159 size_t misspelling_counter_; | |
160 | |
161 // Feedback data. | |
162 Feedback feedback_; | |
163 | |
164 // A set of renderer process IDs for renderers that have sent out feedback in | |
165 // this session. | |
166 std::set<int> renderers_sent_feedback_; | |
167 | |
168 // When the session started. | |
169 base::Time session_start_; | |
170 | |
171 // The last time that we updated |salt_|. | |
172 base::Time last_salt_update_; | |
173 | |
174 // A random number updated once a day. | |
175 RandSalt salt_; | |
176 | |
177 // The URL where the feedback data should be sent. | |
178 GURL feedback_service_url_; | |
179 | |
180 // A timer to periodically request a list of document spelling markers from | |
181 // all of the renderers. The timer starts in StartFeedbackCollection() and | |
182 // stops in StopFeedbackCollection(). The timer stops and abandons its tasks | |
183 // on destruction. | |
184 base::RepeatingTimer timer_; | |
185 | |
186 // Feedback senders that need to stay alive for the duration of sending data. | |
187 // If a sender is destroyed before it finishes, then sending feedback will be | |
188 // canceled. | |
189 ScopedVector<net::URLFetcher> senders_; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(FeedbackSender); | |
192 }; | |
193 | |
194 } // namespace spellcheck | |
195 | |
196 #endif // CHROME_BROWSER_SPELLCHECKER_FEEDBACK_SENDER_H_ | |
OLD | NEW |