OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // The |FeedbackSender| object stores the user feedback to spellcheck | 5 // The |FeedbackSender| object stores the user feedback to spellcheck |
6 // suggestions in a |Feedback| object. | 6 // suggestions in a |Feedback| object. |
7 // | 7 // |
8 // When spelling service returns spellcheck results, these results first arrive | 8 // When spelling service returns spellcheck results, these results first arrive |
9 // in |FeedbackSender| to assign hash identifiers for each | 9 // in |FeedbackSender| to assign hash identifiers for each |
10 // misspelling-suggestion pair. If the spelling service identifies the same | 10 // misspelling-suggestion pair. If the spelling service identifies the same |
11 // misspelling as already displayed to the user, then |FeedbackSender| reuses | 11 // misspelling as already displayed to the user, then |FeedbackSender| reuses |
12 // the same hash identifiers to avoid duplication. It detects the duplicates by | 12 // the same hash identifiers to avoid duplication. It detects the duplicates by |
13 // comparing misspelling offsets in text. Spelling service can return duplicates | 13 // comparing misspelling offsets in text. Spelling service can return duplicates |
14 // because we request spellcheck for whole paragraphs, as context around a | 14 // because we request spellcheck for whole paragraphs, as context around a |
15 // misspelled word is important to the spellcheck algorithm. | 15 // misspelled word is important to the spellcheck algorithm. |
16 // | 16 // |
17 // All feedback is initially pending. When a user acts upon a misspelling such | 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), | 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 | 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 | 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 | 21 // around for |kSessionHours| hours and then finalized even if user did not act |
22 // on the misspellings. | 22 // on the misspellings. |
23 // | 23 // |
24 // |FeedbackSender| periodically requests a list of hashes of all remaining | 24 // |FeedbackSender| periodically requests a list of hashes of all remaining |
25 // misspellings in renderers. When a renderer responds with a list of hashes, | 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 | 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 | 27 // displayed to the user and sends the current state of user feedback to the |
28 // spelling service. | 28 // spelling service. |
29 | 29 |
30 #include "chrome/browser/spellchecker/feedback_sender.h" | 30 #include "components/spellcheck/browser/feedback_sender.h" |
31 | 31 |
32 #include <algorithm> | 32 #include <algorithm> |
33 #include <iterator> | 33 #include <iterator> |
34 #include <utility> | 34 #include <utility> |
35 | 35 |
36 #include "base/command_line.h" | 36 #include "base/command_line.h" |
37 #include "base/hash.h" | 37 #include "base/hash.h" |
38 #include "base/json/json_writer.h" | 38 #include "base/json/json_writer.h" |
39 #include "base/location.h" | 39 #include "base/location.h" |
40 #include "base/metrics/field_trial.h" | 40 #include "base/metrics/field_trial.h" |
41 #include "base/single_thread_task_runner.h" | 41 #include "base/single_thread_task_runner.h" |
42 #include "base/stl_util.h" | 42 #include "base/stl_util.h" |
43 #include "base/strings/string_number_conversions.h" | 43 #include "base/strings/string_number_conversions.h" |
44 #include "base/strings/stringprintf.h" | 44 #include "base/strings/stringprintf.h" |
45 #include "base/threading/thread_task_runner_handle.h" | 45 #include "base/threading/thread_task_runner_handle.h" |
46 #include "base/values.h" | 46 #include "base/values.h" |
47 #include "chrome/browser/spellchecker/word_trimmer.h" | 47 #include "components/spellcheck/browser/word_trimmer.h" |
48 #include "chrome/common/chrome_switches.h" | 48 #include "content/public/common/content_switches.h" |
49 #include "chrome/common/spellcheck_common.h" | 49 #include "components/spellcheck/common/spellcheck_common.h" |
50 #include "chrome/common/spellcheck_marker.h" | 50 #include "components/spellcheck/common/spellcheck_marker.h" |
51 #include "chrome/common/spellcheck_messages.h" | 51 #include "components/spellcheck/common/spellcheck_messages.h" |
52 #include "components/data_use_measurement/core/data_use_user_data.h" | 52 #include "components/data_use_measurement/core/data_use_user_data.h" |
53 #include "content/public/browser/render_process_host.h" | 53 #include "content/public/browser/render_process_host.h" |
54 #include "crypto/random.h" | 54 #include "crypto/random.h" |
55 #include "crypto/secure_hash.h" | 55 #include "crypto/secure_hash.h" |
56 #include "crypto/sha2.h" | 56 #include "crypto/sha2.h" |
57 #include "google_apis/google_api_keys.h" | 57 #include "google_apis/google_api_keys.h" |
58 #include "net/base/load_flags.h" | 58 #include "net/base/load_flags.h" |
59 #include "net/url_request/url_fetcher.h" | 59 #include "net/url_request/url_fetcher.h" |
60 #include "net/url_request/url_request_context_getter.h" | 60 #include "net/url_request/url_request_context_getter.h" |
61 | 61 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 95 } |
96 | 96 |
97 // Returns a pending feedback data structure for the spellcheck |result| and | 97 // Returns a pending feedback data structure for the spellcheck |result| and |
98 // |text|. | 98 // |text|. |
99 Misspelling BuildFeedback(const SpellCheckResult& result, | 99 Misspelling BuildFeedback(const SpellCheckResult& result, |
100 const base::string16& text) { | 100 const base::string16& text) { |
101 size_t start = result.location; | 101 size_t start = result.location; |
102 base::string16 context = TrimWords(&start, | 102 base::string16 context = TrimWords(&start, |
103 start + result.length, | 103 start + result.length, |
104 text, | 104 text, |
105 chrome::spellcheck_common::kContextWordCount); | 105 spellcheck_common::kContextWordCount); |
106 return Misspelling(context, | 106 return Misspelling(context, |
107 start, | 107 start, |
108 result.length, | 108 result.length, |
109 std::vector<base::string16>(1, result.replacement), | 109 std::vector<base::string16>(1, result.replacement), |
110 result.hash); | 110 result.hash); |
111 } | 111 } |
112 | 112 |
113 // Builds suggestion info from |suggestions|. | 113 // Builds suggestion info from |suggestions|. |
114 std::unique_ptr<base::ListValue> BuildSuggestionInfo( | 114 std::unique_ptr<base::ListValue> BuildSuggestionInfo( |
115 const std::vector<Misspelling>& misspellings, | 115 const std::vector<Misspelling>& misspellings, |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 return; | 274 return; |
275 misspelling->action.set_type(SpellcheckAction::TYPE_MANUALLY_CORRECTED); | 275 misspelling->action.set_type(SpellcheckAction::TYPE_MANUALLY_CORRECTED); |
276 misspelling->action.set_value(correction); | 276 misspelling->action.set_value(correction); |
277 misspelling->timestamp = base::Time::Now(); | 277 misspelling->timestamp = base::Time::Now(); |
278 } | 278 } |
279 | 279 |
280 void FeedbackSender::OnReceiveDocumentMarkers( | 280 void FeedbackSender::OnReceiveDocumentMarkers( |
281 int renderer_process_id, | 281 int renderer_process_id, |
282 const std::vector<uint32_t>& markers) { | 282 const std::vector<uint32_t>& markers) { |
283 if ((base::Time::Now() - session_start_).InHours() >= | 283 if ((base::Time::Now() - session_start_).InHours() >= |
284 chrome::spellcheck_common::kSessionHours) { | 284 spellcheck_common::kSessionHours) { |
285 FlushFeedback(); | 285 FlushFeedback(); |
286 return; | 286 return; |
287 } | 287 } |
288 | 288 |
289 if (!feedback_.RendererHasMisspellings(renderer_process_id)) | 289 if (!feedback_.RendererHasMisspellings(renderer_process_id)) |
290 return; | 290 return; |
291 | 291 |
292 feedback_.FinalizeRemovedMisspellings(renderer_process_id, markers); | 292 feedback_.FinalizeRemovedMisspellings(renderer_process_id, markers); |
293 SendFeedback(feedback_.GetMisspellingsInRenderer(renderer_process_id), | 293 SendFeedback(feedback_.GetMisspellingsInRenderer(renderer_process_id), |
294 !renderers_sent_feedback_.count(renderer_process_id)); | 294 !renderers_sent_feedback_.count(renderer_process_id)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 const std::string& country) { | 336 const std::string& country) { |
337 FlushFeedback(); | 337 FlushFeedback(); |
338 language_ = language; | 338 language_ = language; |
339 country_ = country; | 339 country_ = country; |
340 } | 340 } |
341 | 341 |
342 void FeedbackSender::StartFeedbackCollection() { | 342 void FeedbackSender::StartFeedbackCollection() { |
343 if (timer_.IsRunning()) | 343 if (timer_.IsRunning()) |
344 return; | 344 return; |
345 | 345 |
346 int interval_seconds = chrome::spellcheck_common::kFeedbackIntervalSeconds; | 346 int interval_seconds = spellcheck_common::kFeedbackIntervalSeconds; |
347 // This command-line switch is for testing and temporary. | 347 // This command-line switch is for testing and temporary. |
348 // TODO(rouslan): Remove the command-line switch when testing is complete. | 348 // TODO(rouslan): Remove the command-line switch when testing is complete. |
349 // http://crbug.com/247726 | 349 // http://crbug.com/247726 |
350 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 350 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
351 switches::kSpellingServiceFeedbackIntervalSeconds)) { | 351 switches::kSpellingServiceFeedbackIntervalSeconds)) { |
352 base::StringToInt( | 352 base::StringToInt( |
353 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 353 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
354 switches::kSpellingServiceFeedbackIntervalSeconds), | 354 switches::kSpellingServiceFeedbackIntervalSeconds), |
355 &interval_seconds); | 355 &interval_seconds); |
356 if (interval_seconds < kMinIntervalSeconds) | 356 if (interval_seconds < kMinIntervalSeconds) |
357 interval_seconds = kMinIntervalSeconds; | 357 interval_seconds = kMinIntervalSeconds; |
358 static const int kSessionSeconds = | 358 static const int kSessionSeconds = |
359 chrome::spellcheck_common::kSessionHours * 60 * 60; | 359 spellcheck_common::kSessionHours * 60 * 60; |
360 if (interval_seconds > kSessionSeconds) | 360 if (interval_seconds > kSessionSeconds) |
361 interval_seconds = kSessionSeconds; | 361 interval_seconds = kSessionSeconds; |
362 } | 362 } |
363 timer_.Start(FROM_HERE, | 363 timer_.Start(FROM_HERE, |
364 base::TimeDelta::FromSeconds(interval_seconds), | 364 base::TimeDelta::FromSeconds(interval_seconds), |
365 this, | 365 this, |
366 &FeedbackSender::RequestDocumentMarkers); | 366 &FeedbackSender::RequestDocumentMarkers); |
367 } | 367 } |
368 | 368 |
369 void FeedbackSender::StopFeedbackCollection() { | 369 void FeedbackSender::StopFeedbackCollection() { |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 senders_.push_back(sender); | 455 senders_.push_back(sender); |
456 | 456 |
457 // Request context is nullptr in testing. | 457 // Request context is nullptr in testing. |
458 if (request_context_.get()) { | 458 if (request_context_.get()) { |
459 sender->SetRequestContext(request_context_.get()); | 459 sender->SetRequestContext(request_context_.get()); |
460 sender->Start(); | 460 sender->Start(); |
461 } | 461 } |
462 } | 462 } |
463 | 463 |
464 } // namespace spellcheck | 464 } // namespace spellcheck |
OLD | NEW |