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.
5 // The |FeedbackSender| object stores the user feedback to spellcheck
6 // suggestions in a |Feedback| object.
8 // When spelling service returns spellcheck results, these results first arrive
9 // in |FeedbackSender| to assign hash identifiers for each
10 // misspelling-suggestion pair. If the spelling service identifies the same
11 // misspelling as already displayed to the user, then |FeedbackSender| reuses
12 // the same hash identifiers to avoid duplication. It detects the duplicates by
13 // comparing misspelling offsets in text. Spelling service can return duplicates
14 // because we request spellcheck for whole paragraphs, as context around a
15 // misspelled word is important to the spellcheck algorithm.
17 // All feedback is initially pending. When a user acts upon a misspelling such
18 // that the misspelling is no longer displayed (red squiggly line goes away),
19 // then the feedback for this misspelling is finalized. All finalized feedback
20 // is erased after being sent to the spelling service. Pending feedback is kept
21 // around for |kSessionHours| hours and then finalized even if user did not act
22 // on the misspellings.
24 // |FeedbackSender| periodically requests a list of hashes of all remaining
25 // misspellings in renderers. When a renderer responds with a list of hashes,
26 // |FeedbackSender| uses the list to determine which misspellings are no longer
27 // displayed to the user and sends the current state of user feedback to the
30 #include "chrome/browser/spellchecker/feedback_sender.h"
35 #include "base/command_line.h"
36 #include "base/hash.h"
37 #include "base/json/json_writer.h"
38 #include "base/metrics/field_trial.h"
39 #include "base/stl_util.h"
40 #include "base/strings/string_number_conversions.h"
41 #include "base/strings/stringprintf.h"
42 #include "base/values.h"
43 #include "chrome/browser/spellchecker/word_trimmer.h"
44 #include "chrome/common/chrome_switches.h"
45 #include "chrome/common/spellcheck_common.h"
46 #include "chrome/common/spellcheck_marker.h"
47 #include "chrome/common/spellcheck_messages.h"
48 #include "content/public/browser/render_process_host.h"
49 #include "google_apis/google_api_keys.h"
50 #include "net/base/load_flags.h"
51 #include "net/url_request/url_fetcher.h"
52 #include "net/url_request/url_request_context_getter.h"
54 namespace spellcheck {
58 // The default URL where feedback data is sent.
59 const char kFeedbackServiceURL[] = "https://www.googleapis.com/rpc";
61 // The minimum number of seconds between sending batches of feedback.
62 const int kMinIntervalSeconds = 5;
64 // Returns a hash of |session_start|, the current timestamp, and
65 // |suggestion_index|.
66 uint32 BuildHash(const base::Time& session_start, size_t suggestion_index) {
68 base::StringPrintf("%" PRId64 "%" PRId64 "%" PRIuS,
69 session_start.ToInternalValue(),
70 base::Time::Now().ToInternalValue(),
74 // Returns a pending feedback data structure for the spellcheck |result| and
76 Misspelling BuildFeedback(const SpellCheckResult& result,
77 const string16& text) {
78 size_t start = result.location;
79 string16 context = TrimWords(&start,
82 chrome::spellcheck_common::kContextWordCount);
83 return Misspelling(context,
86 std::vector<string16>(1, result.replacement),
90 // Builds suggestion info from |suggestions|. The caller owns the result.
91 base::ListValue* BuildSuggestionInfo(
92 const std::vector<Misspelling>& suggestions,
93 bool is_first_feedback_batch) {
94 base::ListValue* list = new base::ListValue;
95 for (std::vector<Misspelling>::const_iterator suggestion_it =
97 suggestion_it != suggestions.end();
99 base::DictionaryValue* suggestion = suggestion_it->Serialize();
100 suggestion->SetBoolean("isFirstInSession", is_first_feedback_batch);
101 suggestion->SetBoolean("isAutoCorrection", false);
102 list->Append(suggestion);
107 // Builds feedback parameters from |suggestion_info|, |language|, and |country|.
108 // Takes ownership of |suggestion_list|. The caller owns the result.
109 base::DictionaryValue* BuildParams(base::ListValue* suggestion_info,
110 const std::string& language,
111 const std::string& country) {
112 base::DictionaryValue* params = new base::DictionaryValue;
113 params->Set("suggestionInfo", suggestion_info);
114 params->SetString("key", google_apis::GetAPIKey());
115 params->SetString("language", language);
116 params->SetString("originCountry", country);
117 params->SetString("clientName", "Chrome");
121 // Builds feedback data from |params|. Takes ownership of |params|. The caller
123 base::Value* BuildFeedbackValue(base::DictionaryValue* params) {
124 base::DictionaryValue* result = new base::DictionaryValue;
125 result->Set("params", params);
126 result->SetString("method", "spelling.feedback");
127 result->SetString("apiVersion", "v2");
131 // Returns true if the misspelling location is within text bounds.
132 bool IsInBounds(int misspelling_location,
133 int misspelling_length,
134 size_t text_length) {
135 return misspelling_location >= 0 && misspelling_length > 0 &&
136 static_cast<size_t>(misspelling_location) < text_length &&
137 static_cast<size_t>(misspelling_location + misspelling_length) <=
143 FeedbackSender::FeedbackSender(net::URLRequestContextGetter* request_context,
144 const std::string& language,
145 const std::string& country)
146 : request_context_(request_context),
149 misspelling_counter_(0),
150 session_start_(base::Time::Now()),
151 feedback_service_url_(kFeedbackServiceURL) {
152 // This guard is temporary.
153 // TODO(rouslan): Remove the guard. http://crbug.com/247726
154 if (!CommandLine::ForCurrentProcess()->HasSwitch(
155 switches::kEnableSpellingServiceFeedback) ||
156 base::FieldTrialList::FindFullName(kFeedbackFieldTrialName) !=
157 kFeedbackFieldTrialEnabledGroupName) {
161 // The command-line switch is for testing and temporary.
162 // TODO(rouslan): Remove the command-line switch when testing is complete.
163 // http://crbug.com/247726
164 if (CommandLine::ForCurrentProcess()->HasSwitch(
165 switches::kSpellingServiceFeedbackUrl)) {
166 feedback_service_url_ =
167 GURL(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
168 switches::kSpellingServiceFeedbackUrl));
171 int interval_seconds = chrome::spellcheck_common::kFeedbackIntervalSeconds;
172 // This command-line switch is for testing and temporary.
173 // TODO(rouslan): Remove the command-line switch when testing is complete.
174 // http://crbug.com/247726
175 if (CommandLine::ForCurrentProcess()->HasSwitch(
176 switches::kSpellingServiceFeedbackIntervalSeconds)) {
177 base::StringToInt(CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
178 switches::kSpellingServiceFeedbackIntervalSeconds),
180 if (interval_seconds < kMinIntervalSeconds)
181 interval_seconds = kMinIntervalSeconds;
184 timer_.Start(FROM_HERE,
185 base::TimeDelta::FromSeconds(interval_seconds),
187 &FeedbackSender::RequestDocumentMarkers);
190 FeedbackSender::~FeedbackSender() {
193 void FeedbackSender::SelectedSuggestion(uint32 hash, int suggestion_index) {
194 Misspelling* misspelling = feedback_.GetMisspelling(hash);
195 // GetMisspelling() returns null for flushed feedback. Feedback is flushed
196 // when the session expires every |kSessionHours| hours.
199 misspelling->action.type = SpellcheckAction::TYPE_SELECT;
200 misspelling->action.index = suggestion_index;
201 misspelling->timestamp = base::Time::Now();
204 void FeedbackSender::AddedToDictionary(uint32 hash) {
205 Misspelling* misspelling = feedback_.GetMisspelling(hash);
206 // GetMisspelling() returns null for flushed feedback. Feedback is flushed
207 // when the session expires every |kSessionHours| hours.
210 misspelling->action.type = SpellcheckAction::TYPE_ADD_TO_DICT;
211 misspelling->timestamp = base::Time::Now();
212 const std::set<uint32>& hashes =
213 feedback_.FindMisspellings(misspelling->GetMisspelledString());
214 for (std::set<uint32>::const_iterator hash_it = hashes.begin();
215 hash_it != hashes.end();
217 Misspelling* duplicate_misspelling = feedback_.GetMisspelling(*hash_it);
218 if (!duplicate_misspelling || duplicate_misspelling->action.IsFinal())
220 duplicate_misspelling->action.type = SpellcheckAction::TYPE_ADD_TO_DICT;
221 duplicate_misspelling->timestamp = misspelling->timestamp;
225 void FeedbackSender::RecordInDictionary(uint32 hash) {
226 Misspelling* misspelling = feedback_.GetMisspelling(hash);
227 // GetMisspelling() returns null for flushed feedback. Feedback is flushed
228 // when the session expires every |kSessionHours| hours.
231 misspelling->action.type = SpellcheckAction::TYPE_IN_DICTIONARY;
234 void FeedbackSender::IgnoredSuggestions(uint32 hash) {
235 Misspelling* misspelling = feedback_.GetMisspelling(hash);
236 // GetMisspelling() returns null for flushed feedback. Feedback is flushed
237 // when the session expires every |kSessionHours| hours.
240 misspelling->action.type = SpellcheckAction::TYPE_PENDING_IGNORE;
241 misspelling->timestamp = base::Time::Now();
244 void FeedbackSender::ManuallyCorrected(uint32 hash,
245 const string16& correction) {
246 Misspelling* misspelling = feedback_.GetMisspelling(hash);
247 // GetMisspelling() returns null for flushed feedback. Feedback is flushed
248 // when the session expires every |kSessionHours| hours.
251 misspelling->action.type = SpellcheckAction::TYPE_MANUALLY_CORRECTED;
252 misspelling->action.value = correction;
253 misspelling->timestamp = base::Time::Now();
256 void FeedbackSender::OnReceiveDocumentMarkers(
257 int renderer_process_id,
258 const std::vector<uint32>& markers) {
259 if ((base::Time::Now() - session_start_).InHours() >=
260 chrome::spellcheck_common::kSessionHours) {
265 if (!feedback_.RendererHasMisspellings(renderer_process_id))
268 feedback_.FinalizeRemovedMisspellings(renderer_process_id, markers);
269 SendFeedback(feedback_.GetMisspellingsInRenderer(renderer_process_id),
270 !renderers_sent_feedback_.count(renderer_process_id));
271 renderers_sent_feedback_.insert(renderer_process_id);
272 feedback_.EraseFinalizedMisspellings(renderer_process_id);
275 void FeedbackSender::OnSpellcheckResults(
276 int renderer_process_id,
277 const string16& text,
278 const std::vector<SpellCheckMarker>& markers,
279 std::vector<SpellCheckResult>* results) {
280 // Don't collect feedback if not going to send it.
281 if (!timer_.IsRunning())
284 // Generate a map of marker offsets to marker hashes. This map helps to
285 // efficiently lookup feedback data based on the position of the misspelling
287 typedef std::map<size_t, uint32> MarkerMap;
288 MarkerMap marker_map;
289 for (size_t i = 0; i < markers.size(); ++i)
290 marker_map[markers[i].offset] = markers[i].hash;
292 for (std::vector<SpellCheckResult>::iterator result_it = results->begin();
293 result_it != results->end();
295 if (!IsInBounds(result_it->location, result_it->length, text.length()))
297 MarkerMap::const_iterator marker_it = marker_map.find(result_it->location);
298 if (marker_it != marker_map.end() &&
299 feedback_.HasMisspelling(marker_it->second)) {
300 // If the renderer already has a marker for this spellcheck result, then
301 // set the hash of the spellcheck result to be the same as the marker.
302 result_it->hash = marker_it->second;
304 // If the renderer does not yet have a marker for this spellcheck result,
305 // then generate a new hash for the spellcheck result.
306 result_it->hash = BuildHash(session_start_, ++misspelling_counter_);
308 // Save the feedback data for the spellcheck result.
309 feedback_.AddMisspelling(renderer_process_id,
310 BuildFeedback(*result_it, text));
314 void FeedbackSender::OnLanguageCountryChange(const std::string& language,
315 const std::string& country) {
317 language_ = language;
321 void FeedbackSender::OnURLFetchComplete(const net::URLFetcher* source) {
322 for (ScopedVector<net::URLFetcher>::iterator sender_it = senders_.begin();
323 sender_it != senders_.end();
325 if (*sender_it == source) {
326 senders_.erase(sender_it);
333 void FeedbackSender::RequestDocumentMarkers() {
334 // Request document markers from all the renderers that are still alive.
335 std::set<int> alive_renderers;
336 for (content::RenderProcessHost::iterator it(
337 content::RenderProcessHost::AllHostsIterator());
340 alive_renderers.insert(it.GetCurrentValue()->GetID());
341 it.GetCurrentValue()->Send(new SpellCheckMsg_RequestDocumentMarkers());
344 // Asynchronously send out the feedback for all the renderers that are no
346 std::vector<int> known_renderers = feedback_.GetRendersWithMisspellings();
347 std::sort(known_renderers.begin(), known_renderers.end());
348 std::vector<int> dead_renderers =
349 base::STLSetDifference<std::vector<int> >(known_renderers,
351 for (std::vector<int>::const_iterator it = dead_renderers.begin();
352 it != dead_renderers.end();
354 base::MessageLoop::current()->PostTask(
356 base::Bind(&FeedbackSender::OnReceiveDocumentMarkers,
359 std::vector<uint32>()));
363 void FeedbackSender::FlushFeedback() {
364 if (feedback_.Empty())
366 feedback_.FinalizeAllMisspellings();
367 SendFeedback(feedback_.GetAllMisspellings(),
368 renderers_sent_feedback_.empty());
370 renderers_sent_feedback_.clear();
371 session_start_ = base::Time::Now();
375 void FeedbackSender::SendFeedback(const std::vector<Misspelling>& feedback_data,
376 bool is_first_feedback_batch) {
377 scoped_ptr<base::Value> feedback_value(BuildFeedbackValue(
378 BuildParams(BuildSuggestionInfo(feedback_data, is_first_feedback_batch),
381 std::string feedback;
382 base::JSONWriter::Write(feedback_value.get(), &feedback);
384 // The tests use this identifier to mock the URL fetcher.
385 static const int kUrlFetcherId = 0;
386 net::URLFetcher* sender = net::URLFetcher::Create(
387 kUrlFetcherId, feedback_service_url_, net::URLFetcher::POST, this);
388 sender->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
389 net::LOAD_DO_NOT_SAVE_COOKIES);
390 sender->SetUploadData("application/json", feedback);
391 senders_.push_back(sender);
393 // Request context is NULL in testing.
394 if (request_context_.get()) {
395 sender->SetRequestContext(request_context_.get());
400 } // namespace spellcheck