- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / spellchecker / feedback_sender.cc
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 // The |FeedbackSender| object stores the user feedback to spellcheck
6 // suggestions in a |Feedback| object.
7 //
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.
16 //
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.
23 //
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
28 // spelling service.
29
30 #include "chrome/browser/spellchecker/feedback_sender.h"
31
32 #include <algorithm>
33 #include <iterator>
34
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"
53
54 namespace spellcheck {
55
56 namespace {
57
58 // The default URL where feedback data is sent.
59 const char kFeedbackServiceURL[] = "https://www.googleapis.com/rpc";
60
61 // The minimum number of seconds between sending batches of feedback.
62 const int kMinIntervalSeconds = 5;
63
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) {
67   return base::Hash(
68       base::StringPrintf("%" PRId64 "%" PRId64 "%" PRIuS,
69                          session_start.ToInternalValue(),
70                          base::Time::Now().ToInternalValue(),
71                          suggestion_index));
72 }
73
74 // Returns a pending feedback data structure for the spellcheck |result| and
75 // |text|.
76 Misspelling BuildFeedback(const SpellCheckResult& result,
77                           const string16& text) {
78   size_t start = result.location;
79   string16 context = TrimWords(&start,
80                                result.length,
81                                text,
82                                chrome::spellcheck_common::kContextWordCount);
83   return Misspelling(context,
84                      start,
85                      result.length,
86                      std::vector<string16>(1, result.replacement),
87                      result.hash);
88 }
89
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 =
96            suggestions.begin();
97        suggestion_it != suggestions.end();
98        ++suggestion_it) {
99     base::DictionaryValue* suggestion = suggestion_it->Serialize();
100     suggestion->SetBoolean("isFirstInSession", is_first_feedback_batch);
101     suggestion->SetBoolean("isAutoCorrection", false);
102     list->Append(suggestion);
103   }
104   return list;
105 }
106
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");
118   return params;
119 }
120
121 // Builds feedback data from |params|. Takes ownership of |params|. The caller
122 // owns the result.
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");
128   return result;
129 }
130
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) <=
138              text_length;
139 }
140
141 }  // namespace
142
143 FeedbackSender::FeedbackSender(net::URLRequestContextGetter* request_context,
144                                const std::string& language,
145                                const std::string& country)
146     : request_context_(request_context),
147       language_(language),
148       country_(country),
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) {
158     return;
159   }
160
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));
169   }
170
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),
179                       &interval_seconds);
180     if (interval_seconds < kMinIntervalSeconds)
181       interval_seconds = kMinIntervalSeconds;
182   }
183
184   timer_.Start(FROM_HERE,
185                base::TimeDelta::FromSeconds(interval_seconds),
186                this,
187                &FeedbackSender::RequestDocumentMarkers);
188 }
189
190 FeedbackSender::~FeedbackSender() {
191 }
192
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.
197   if (!misspelling)
198     return;
199   misspelling->action.type = SpellcheckAction::TYPE_SELECT;
200   misspelling->action.index = suggestion_index;
201   misspelling->timestamp = base::Time::Now();
202 }
203
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.
208   if (!misspelling)
209     return;
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();
216        ++hash_it) {
217     Misspelling* duplicate_misspelling = feedback_.GetMisspelling(*hash_it);
218     if (!duplicate_misspelling || duplicate_misspelling->action.IsFinal())
219       continue;
220     duplicate_misspelling->action.type = SpellcheckAction::TYPE_ADD_TO_DICT;
221     duplicate_misspelling->timestamp = misspelling->timestamp;
222   }
223 }
224
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.
229   if (!misspelling)
230     return;
231   misspelling->action.type = SpellcheckAction::TYPE_IN_DICTIONARY;
232 }
233
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.
238   if (!misspelling)
239     return;
240   misspelling->action.type = SpellcheckAction::TYPE_PENDING_IGNORE;
241   misspelling->timestamp = base::Time::Now();
242 }
243
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.
249   if (!misspelling)
250     return;
251   misspelling->action.type = SpellcheckAction::TYPE_MANUALLY_CORRECTED;
252   misspelling->action.value = correction;
253   misspelling->timestamp = base::Time::Now();
254 }
255
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) {
261     FlushFeedback();
262     return;
263   }
264
265   if (!feedback_.RendererHasMisspellings(renderer_process_id))
266     return;
267
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);
273 }
274
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())
282     return;
283
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
286   // in text.
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;
291
292   for (std::vector<SpellCheckResult>::iterator result_it = results->begin();
293        result_it != results->end();
294        ++result_it) {
295     if (!IsInBounds(result_it->location, result_it->length, text.length()))
296       continue;
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;
303     } else {
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_);
307     }
308     // Save the feedback data for the spellcheck result.
309     feedback_.AddMisspelling(renderer_process_id,
310                              BuildFeedback(*result_it, text));
311   }
312 }
313
314 void FeedbackSender::OnLanguageCountryChange(const std::string& language,
315                                              const std::string& country) {
316   FlushFeedback();
317   language_ = language;
318   country_ = country;
319 }
320
321 void FeedbackSender::OnURLFetchComplete(const net::URLFetcher* source) {
322   for (ScopedVector<net::URLFetcher>::iterator sender_it = senders_.begin();
323        sender_it != senders_.end();
324        ++sender_it) {
325     if (*sender_it == source) {
326       senders_.erase(sender_it);
327       return;
328     }
329   }
330   delete source;
331 }
332
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());
338        !it.IsAtEnd();
339        it.Advance()) {
340     alive_renderers.insert(it.GetCurrentValue()->GetID());
341     it.GetCurrentValue()->Send(new SpellCheckMsg_RequestDocumentMarkers());
342   }
343
344   // Asynchronously send out the feedback for all the renderers that are no
345   // longer alive.
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,
350                                                 alive_renderers);
351   for (std::vector<int>::const_iterator it = dead_renderers.begin();
352        it != dead_renderers.end();
353        ++it) {
354     base::MessageLoop::current()->PostTask(
355         FROM_HERE,
356         base::Bind(&FeedbackSender::OnReceiveDocumentMarkers,
357                    AsWeakPtr(),
358                    *it,
359                    std::vector<uint32>()));
360   }
361 }
362
363 void FeedbackSender::FlushFeedback() {
364   if (feedback_.Empty())
365     return;
366   feedback_.FinalizeAllMisspellings();
367   SendFeedback(feedback_.GetAllMisspellings(),
368                renderers_sent_feedback_.empty());
369   feedback_.Clear();
370   renderers_sent_feedback_.clear();
371   session_start_ = base::Time::Now();
372   timer_.Reset();
373 }
374
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),
379                   language_,
380                   country_)));
381   std::string feedback;
382   base::JSONWriter::Write(feedback_value.get(), &feedback);
383
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);
392
393   // Request context is NULL in testing.
394   if (request_context_.get()) {
395     sender->SetRequestContext(request_context_.get());
396     sender->Start();
397   }
398 }
399
400 }  // namespace spellcheck