blob: 286008f240567178639f2414d65f91e170c43d72 [file] [log] [blame]
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01001// 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#ifndef CHROME_BROWSER_SPELLCHECKER_FEEDBACK_SENDER_H_
6#define CHROME_BROWSER_SPELLCHECKER_FEEDBACK_SENDER_H_
7
8#include <map>
9#include <set>
10#include <vector>
11
12#include "base/memory/scoped_vector.h"
13#include "base/memory/weak_ptr.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010014#include "base/timer/timer.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010015#include "chrome/browser/spellchecker/feedback.h"
16#include "chrome/browser/spellchecker/misspelling.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010017#include "net/url_request/url_fetcher_delegate.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010018#include "url/gurl.h"
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010019
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010020class SpellCheckMarker;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010021struct SpellCheckResult;
22
23namespace net {
24class URLFetcher;
25class URLRequestContextGetter;
26}
27
28namespace spellcheck {
29
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010030// Constants for the feedback field trial.
31static const char kFeedbackFieldTrialName[] = "SpellingServiceFeedback";
32static const char kFeedbackFieldTrialEnabledGroupName[] = "Enabled";
33
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010034// Manages sending feedback to the spelling service.
35class FeedbackSender : public base::SupportsWeakPtr<FeedbackSender>,
36 public net::URLFetcherDelegate {
37 public:
38 FeedbackSender(net::URLRequestContextGetter* request_context,
39 const std::string& language,
40 const std::string& country);
41 virtual ~FeedbackSender();
42
43 // Records that user selected suggestion |suggestion_index| for the
44 // misspelling identified by |hash|.
45 void SelectedSuggestion(uint32 hash, int suggestion_index);
46
47 // Records that user added the misspelling identified by |hash| to the
48 // dictionary.
49 void AddedToDictionary(uint32 hash);
50
51 // Records that user right-clicked on the misspelling identified by |hash|,
52 // but did not select any suggestion.
53 void IgnoredSuggestions(uint32 hash);
54
55 // Records that user did not choose any suggestion but manually corrected the
56 // misspelling identified by |hash| to string |correction|, which is not in
57 // the list of suggestions.
58 void ManuallyCorrected(uint32 hash, const string16& correction);
59
Ben Murdocheb525c52013-07-10 11:40:50 +010060 // Records that user has the misspelling in the custom dictionary. The user
61 // will never see the spellcheck suggestions for the misspelling.
62 void RecordInDictionary(uint32 hash);
63
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010064 // Receives document markers for renderer with process ID |render_process_id|
65 // when the renderer responds to a RequestDocumentMarkers() call. Finalizes
66 // feedback for the markers that are gone from the renderer. Sends feedback
67 // data for the renderer with process ID |renderer_process_id| to the spelling
68 // service. If the current session has expired, then refreshes the session
69 // start timestamp and sends out all of the feedback data.
70 void OnReceiveDocumentMarkers(int renderer_process_id,
71 const std::vector<uint32>& markers);
72
73 // Generates feedback data based on spellcheck results. The new feedback data
74 // is pending. Sets hash identifiers for |results|. Called when spelling
75 // service client receives results from the spelling service.
76 void OnSpellcheckResults(std::vector<SpellCheckResult>* results,
77 int renderer_process_id,
78 const string16& text,
79 const std::vector<SpellCheckMarker>& markers);
80
81 // Receives updated language and country code for feedback. Finalizes and
82 // sends out all of the feedback data.
83 void OnLanguageCountryChange(const std::string& language,
84 const std::string& country);
85
86 private:
87 friend class FeedbackSenderTest;
88
89 // net::URLFetcherDelegate implementation.
90 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
91
92 // Requests the document markers from all of the renderers to determine which
93 // feedback can be finalized. Finalizes feedback for renderers that are gone.
94 // Called periodically when |timer_| fires.
95 void RequestDocumentMarkers();
96
97 // Sends out all feedback data. Resets the session-start timestamp to now.
98 // Restarts the timer that requests markers from the renderers.
99 void FlushFeedback();
100
101 // Sends out the |feedback_data|.
102 void SendFeedback(const std::vector<Misspelling>& feedback_data,
103 bool is_first_feedback_batch);
104
105 // Request context for the feedback senders.
106 scoped_refptr<net::URLRequestContextGetter> request_context_;
107
108 // The language of text. The string is a BCP 47 language tag.
109 std::string language_;
110
111 // The country of origin. The string is the ISO 3166-1 alpha-3 code.
112 std::string country_;
113
114 // Misspelling counter used to generate unique hash identifier for each
115 // misspelling.
116 size_t misspelling_counter_;
117
118 // Feedback data.
119 Feedback feedback_;
120
121 // A set of renderer process IDs for renderers that have sent out feedback in
122 // this session.
123 std::set<int> renderers_sent_feedback_;
124
125 // When the session started.
126 base::Time session_start_;
127
128 // The URL where the feedback data should be sent.
129 GURL feedback_service_url_;
130
131 // A timer to periodically request a list of document spelling markers from
132 // all of the renderers. The timer runs while an instance of this class is
133 // alive.
134 base::RepeatingTimer<FeedbackSender> timer_;
135
136 // Feedback senders that need to stay alive for the duration of sending data.
137 // If a sender is destroyed before it finishes, then sending feedback will be
138 // canceled.
139 ScopedVector<net::URLFetcher> senders_;
140
141 DISALLOW_COPY_AND_ASSIGN(FeedbackSender);
142};
143
144} // namespace spellcheck
145
146#endif // CHROME_BROWSER_SPELLCHECKER_FEEDBACK_SENDER_H_