OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ |
6 #define COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ | 6 #define COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
15 #include "components/translate/content/renderer/renderer_cld_data_provider.h" | |
16 #include "components/translate/core/common/translate_errors.h" | 15 #include "components/translate/core/common/translate_errors.h" |
17 #include "content/public/renderer/render_frame_observer.h" | 16 #include "content/public/renderer/render_frame_observer.h" |
18 #include "url/gurl.h" | 17 #include "url/gurl.h" |
19 | 18 |
20 namespace blink { | 19 namespace blink { |
21 class WebDocument; | 20 class WebDocument; |
22 class WebLocalFrame; | 21 class WebLocalFrame; |
23 } | 22 } |
24 | 23 |
25 namespace content { | |
26 class RendererCldDataProvider; | |
27 } | |
28 | |
29 namespace translate { | 24 namespace translate { |
30 | 25 |
31 // This class deals with page translation. | 26 // This class deals with page translation. |
32 // There is one TranslateHelper per RenderView. | 27 // There is one TranslateHelper per RenderView. |
33 // | |
34 // This class provides metrics that allow tracking the user experience impact | |
35 // of non-static CldDataProvider implementations. For background on the data | |
36 // providers, please refer to the following documentation: | |
37 // http://www.chromium.org/developers/how-tos/compact-language-detector-cld-data
-source-configuration | |
38 // | |
39 // Available metrics (from the LanguageDetectionTiming enum): | |
40 // 1. ON_TIME | |
41 // Recorded if PageCaptured(...) is invoked after CLD is available. This is | |
42 // the ideal case, indicating that CLD is available before it is needed. | |
43 // 2. DEFERRED | |
44 // Recorded if PageCaptured(...) is invoked before CLD is available. | |
45 // Sub-optimal case indicating that CLD wasn't available when it was needed, | |
46 // so the request for detection has been deferred until CLD is available or | |
47 // until the user navigates to a different page. | |
48 // 3. RESUMED | |
49 // Recorded if CLD becomes available after a language detection request was | |
50 // deferred, but before the user navigated to a different page. Language | |
51 // detection is ultimately completed, it just didn't happen on time. | |
52 // | |
53 // Note that there is NOT a metric that records the number of times that | |
54 // language detection had to be aborted because CLD never became available in | |
55 // time. This is because there is no reasonable way to cover all the cases | |
56 // under which this could occur, particularly the destruction of the renderer | |
57 // for which this object was created. However, this value can be synthetically | |
58 // derived, using the logic below. | |
59 // | |
60 // Every page load that triggers language detection will result in the | |
61 // recording of exactly one of the first two events: ON_TIME or DEFERRED. If | |
62 // CLD is available in time to satisfy the request, the third event (RESUMED) | |
63 // will be recorded; thus, the number of times when language detection | |
64 // ultimately fails because CLD isn't ever available is implied as the number of | |
65 // times that detection is deferred minus the number of times that language | |
66 // detection is late: | |
67 // | |
68 // count(FAILED) ~= count(DEFERRED) - count(RESUMED) | |
69 // | |
70 // Note that this is not 100% accurate: some renderer process are so short-lived | |
71 // that language detection wouldn't have been relevant anyway, and so a failure | |
72 // to detect the language in a timely manner might be completely innocuous. The | |
73 // overall problem with language detection is that it isn't possible to know | |
74 // whether it was required or not until after it has been performed! | |
75 // | |
76 // We use histograms for recording these metrics. On Android, the renderer can | |
77 // be killed without the chance to clean up or transmit these histograms, | |
78 // leading to dropped metrics. To work around this, this method forces an IPC | |
79 // message to be sent to the browser process immediately. | |
80 class TranslateHelper : public content::RenderFrameObserver { | 28 class TranslateHelper : public content::RenderFrameObserver { |
81 public: | 29 public: |
82 explicit TranslateHelper(content::RenderFrame* render_frame, | 30 explicit TranslateHelper(content::RenderFrame* render_frame, |
83 int world_id, | 31 int world_id, |
84 int extension_group, | 32 int extension_group, |
85 const std::string& extension_scheme); | 33 const std::string& extension_scheme); |
86 ~TranslateHelper() override; | 34 ~TranslateHelper() override; |
87 | 35 |
88 // Informs us that the page's text has been extracted. | 36 // Informs us that the page's text has been extracted. |
89 void PageCaptured(const base::string16& contents); | 37 void PageCaptured(const base::string16& contents); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // run successfully. Otherwise, returns empty string. | 92 // run successfully. Otherwise, returns empty string. |
145 virtual std::string ExecuteScriptAndGetStringResult( | 93 virtual std::string ExecuteScriptAndGetStringResult( |
146 const std::string& script); | 94 const std::string& script); |
147 | 95 |
148 // Executes the JavaScript code in |script| in the main frame of RenderView. | 96 // Executes the JavaScript code in |script| in the main frame of RenderView. |
149 // and returns the number returned by the script evaluation if the script was | 97 // and returns the number returned by the script evaluation if the script was |
150 // run successfully. Otherwise, returns 0.0. | 98 // run successfully. Otherwise, returns 0.0. |
151 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); | 99 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); |
152 | 100 |
153 private: | 101 private: |
154 enum LanguageDetectionTiming { | |
155 ON_TIME, // Language detection was performed as soon as it was requested | |
156 DEFERRED, // Language detection couldn't be performed when it was requested | |
157 RESUMED, // A deferred language detection attempt was completed later | |
158 LANGUAGE_DETECTION_TIMING_MAX_VALUE // The bounding value for this enum | |
159 }; | |
160 | |
161 // Converts language code to the one used in server supporting list. | 102 // Converts language code to the one used in server supporting list. |
162 static void ConvertLanguageCodeSynonym(std::string* code); | 103 static void ConvertLanguageCodeSynonym(std::string* code); |
163 | 104 |
164 // RenderFrameObserver implementation. | 105 // RenderFrameObserver implementation. |
165 bool OnMessageReceived(const IPC::Message& message) override; | 106 bool OnMessageReceived(const IPC::Message& message) override; |
166 void OnDestruct() override; | 107 void OnDestruct() override; |
167 | 108 |
168 // Informs us that the page's text has been extracted. | 109 // Informs us that the page's text has been extracted. |
169 void PageCapturedImpl(int page_seq_no, const base::string16& contents); | 110 void PageCapturedImpl(int page_seq_no, const base::string16& contents); |
170 | 111 |
(...skipping 11 matching lines...) Expand all Loading... |
182 void TranslatePageImpl(int page_seq_no, int count); | 123 void TranslatePageImpl(int page_seq_no, int count); |
183 | 124 |
184 // Sends a message to the browser to notify it that the translation failed | 125 // Sends a message to the browser to notify it that the translation failed |
185 // with |error|. | 126 // with |error|. |
186 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); | 127 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); |
187 | 128 |
188 // Convenience method to access the main frame. Can return NULL, typically | 129 // Convenience method to access the main frame. Can return NULL, typically |
189 // if the page is being closed. | 130 // if the page is being closed. |
190 blink::WebLocalFrame* GetMainFrame(); | 131 blink::WebLocalFrame* GetMainFrame(); |
191 | 132 |
192 // Do not ask for CLD data any more. | |
193 void CancelCldDataPolling(); | |
194 | |
195 // Start polling for CLD data. | |
196 // Polling will automatically halt as soon as the renderer obtains a | |
197 // reference to the data file. | |
198 void SendCldDataRequest(const int delay_millis, const int next_delay_millis); | |
199 | |
200 // Callback triggered when CLD data becomes available. | |
201 void OnCldDataAvailable(); | |
202 | |
203 // Record the timing of language detection, immediately sending an IPC-based | |
204 // histogram delta update to the browser process in case the hosting renderer | |
205 // process terminates before the metrics would otherwise be transferred. | |
206 void RecordLanguageDetectionTiming(LanguageDetectionTiming timing); | |
207 | |
208 // An ever-increasing sequence number of the current page, used to match up | 133 // An ever-increasing sequence number of the current page, used to match up |
209 // translation requests with responses. | 134 // translation requests with responses. |
210 int page_seq_no_; | 135 int page_seq_no_; |
211 | 136 |
212 // The states associated with the current translation. | 137 // The states associated with the current translation. |
213 bool translation_pending_; | 138 bool translation_pending_; |
214 std::string source_lang_; | 139 std::string source_lang_; |
215 std::string target_lang_; | 140 std::string target_lang_; |
216 | 141 |
217 // Time when a page langauge is determined. This is used to know a duration | 142 // Time when a page langauge is determined. This is used to know a duration |
218 // time from showing infobar to requesting translation. | 143 // time from showing infobar to requesting translation. |
219 base::TimeTicks language_determined_time_; | 144 base::TimeTicks language_determined_time_; |
220 | 145 |
221 // Provides CLD data for this process. | |
222 std::unique_ptr<RendererCldDataProvider> cld_data_provider_; | |
223 | |
224 // Whether or not polling for CLD2 data has started. | |
225 bool cld_data_polling_started_; | |
226 | |
227 // Whether or not CancelCldDataPolling has been called. | |
228 bool cld_data_polling_canceled_; | |
229 | |
230 // Whether or not a PageCaptured event arrived prior to CLD data becoming | 146 // Whether or not a PageCaptured event arrived prior to CLD data becoming |
231 // available. If true, deferred_contents_ contains the most recent contents. | 147 // available. If true, deferred_contents_ contains the most recent contents. |
232 bool deferred_page_capture_; | 148 bool deferred_page_capture_; |
233 | 149 |
234 // The ID of the page most recently reported to PageCaptured if | 150 // The ID of the page most recently reported to PageCaptured if |
235 // deferred_page_capture_ is true. | 151 // deferred_page_capture_ is true. |
236 int deferred_page_seq_no_; | 152 int deferred_page_seq_no_; |
237 | 153 |
238 // The world ID to use for script execution. | 154 // The world ID to use for script execution. |
239 int world_id_; | 155 int world_id_; |
(...skipping 10 matching lines...) Expand all Loading... |
250 | 166 |
251 // Method factory used to make calls to TranslatePageImpl. | 167 // Method factory used to make calls to TranslatePageImpl. |
252 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 168 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
253 | 169 |
254 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 170 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
255 }; | 171 }; |
256 | 172 |
257 } // namespace translate | 173 } // namespace translate |
258 | 174 |
259 #endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ | 175 #endif // COMPONENTS_TRANSLATE_CONTENT_RENDERER_TRANSLATE_HELPER_H_ |
OLD | NEW |