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 CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 5 #ifndef CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
6 #define CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 6 #define CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/memory/scoped_ptr.h" |
11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/translate/content/renderer/renderer_cld_data_provider.h" |
14 #include "components/translate/core/common/translate_errors.h" | 16 #include "components/translate/core/common/translate_errors.h" |
15 #include "content/public/renderer/render_view_observer.h" | 17 #include "content/public/renderer/render_view_observer.h" |
16 | |
17 #if defined(CLD2_DYNAMIC_MODE) | |
18 #include "base/files/file.h" | |
19 #include "base/files/memory_mapped_file.h" | |
20 #include "base/lazy_instance.h" | |
21 #include "ipc/ipc_platform_file.h" | |
22 #include "url/gurl.h" | 18 #include "url/gurl.h" |
23 #endif | |
24 | 19 |
25 namespace blink { | 20 namespace blink { |
26 class WebDocument; | 21 class WebDocument; |
27 class WebFrame; | 22 class WebFrame; |
28 } | 23 } |
29 | 24 |
30 // This class deals with page translation. | 25 // This class deals with page translation. |
31 // There is one TranslateHelper per RenderView. | 26 // There is one TranslateHelper per RenderView. |
32 | 27 |
33 class TranslateHelper : public content::RenderViewObserver { | 28 class TranslateHelper : public content::RenderViewObserver { |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 std::string source_lang_; | 150 std::string source_lang_; |
156 std::string target_lang_; | 151 std::string target_lang_; |
157 | 152 |
158 // Time when a page langauge is determined. This is used to know a duration | 153 // Time when a page langauge is determined. This is used to know a duration |
159 // time from showing infobar to requesting translation. | 154 // time from showing infobar to requesting translation. |
160 base::TimeTicks language_determined_time_; | 155 base::TimeTicks language_determined_time_; |
161 | 156 |
162 // Method factory used to make calls to TranslatePageImpl. | 157 // Method factory used to make calls to TranslatePageImpl. |
163 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 158 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
164 | 159 |
165 #if defined(CLD2_DYNAMIC_MODE) | |
166 // Do not ask for CLD data any more. | 160 // Do not ask for CLD data any more. |
167 void CancelCLD2DataFilePolling(); | 161 void CancelCldDataPolling(); |
168 | 162 |
169 // Invoked when PageCaptured is called prior to obtaining CLD data. This | 163 // Invoked when PageCaptured is called prior to obtaining CLD data. This |
170 // method stores the page ID into deferred_page_id_ and COPIES the contents | 164 // method stores the page ID into deferred_page_id_ and COPIES the contents |
171 // of the page, then sets deferred_page_capture_ to true. When CLD data is | 165 // of the page, then sets deferred_page_capture_ to true. When CLD data is |
172 // eventually received (in OnCLDDataAvailable), any deferred request will be | 166 // eventually received (in OnCldDataAvailable), any deferred request will be |
173 // "resurrected" and allowed to proceed automatically, assuming that the | 167 // "resurrected" and allowed to proceed automatically, assuming that the |
174 // page ID has not changed. | 168 // page ID has not changed. |
175 void DeferPageCaptured(const int page_id, const base::string16& contents); | 169 void DeferPageCaptured(const int page_id, const base::string16& contents); |
176 | 170 |
177 // Immediately send an IPC request to the browser process to get the CLD | 171 // Start polling for CLD data. |
178 // data file. In most cases, the file will already exist and we will only | |
179 // poll once; but since the file might need to be downloaded first, poll | |
180 // indefinitely until a ChromeViewMsg_CLDDataAvailable message is received | |
181 // from the browser process. | |
182 // Polling will automatically halt as soon as the renderer obtains a | 172 // Polling will automatically halt as soon as the renderer obtains a |
183 // reference to the data file. | 173 // reference to the data file. |
184 void SendCLD2DataFileRequest(const int delay_millis, | 174 void SendCldDataRequest(const int delay_millis, |
185 const int next_delay_millis); | 175 const int next_delay_millis); |
186 | 176 |
187 // Invoked when a ChromeViewMsg_CLDDataAvailable message is received from | 177 // Callback triggered when CLD data becomes available. |
188 // the browser process, providing a file handle for the CLD data file. If a | 178 void OnCldDataAvailable(); |
189 // PageCaptured request was previously deferred with DeferPageCaptured and | |
190 // the page ID has not yet changed, the PageCaptured is reinvoked to | |
191 // "resurrect" the language detection pathway. | |
192 void OnCLDDataAvailable(const IPC::PlatformFileForTransit ipc_file_handle, | |
193 const uint64 data_offset, | |
194 const uint64 data_length); | |
195 | 179 |
196 // After receiving data in OnCLDDataAvailable, loads the data into CLD2. | 180 // Provides CLD data for this process. |
197 void LoadCLDDData(base::File file, | 181 scoped_ptr<content::RendererCldDataProvider> cld_data_provider_; |
198 const uint64 data_offset, | |
199 const uint64 data_length); | |
200 | |
201 // A struct that contains the pointer to the CLD mmap. Used so that we can | |
202 // leverage LazyInstance:Leaky to properly scope the lifetime of the mmap. | |
203 struct CLDMmapWrapper { | |
204 CLDMmapWrapper() { | |
205 value = NULL; | |
206 } | |
207 base::MemoryMappedFile* value; | |
208 }; | |
209 static base::LazyInstance<CLDMmapWrapper>::Leaky s_cld_mmap_; | |
210 | 182 |
211 // Whether or not polling for CLD2 data has started. | 183 // Whether or not polling for CLD2 data has started. |
212 bool cld2_data_file_polling_started_; | 184 bool cld_data_polling_started_; |
213 | 185 |
214 // Whether or not CancelCLD2DataFilePolling has been called. | 186 // Whether or not CancelCldDataPolling has been called. |
215 bool cld2_data_file_polling_canceled_; | 187 bool cld_data_polling_canceled_; |
216 | 188 |
217 // Whether or not a PageCaptured event arrived prior to CLD data becoming | 189 // Whether or not a PageCaptured event arrived prior to CLD data becoming |
218 // available. If true, deferred_page_id_ contains the most recent page ID | 190 // available. If true, deferred_page_id_ contains the most recent page ID |
219 // and deferred_contents_ contains the most recent contents. | 191 // and deferred_contents_ contains the most recent contents. |
220 bool deferred_page_capture_; | 192 bool deferred_page_capture_; |
221 | 193 |
222 // The ID of the page most recently reported to PageCaptured if | 194 // The ID of the page most recently reported to PageCaptured if |
223 // deferred_page_capture_ is true. | 195 // deferred_page_capture_ is true. |
224 int deferred_page_id_; | 196 int deferred_page_id_; |
225 | 197 |
226 // The contents of the page most recently reported to PageCaptured if | 198 // The contents of the page most recently reported to PageCaptured if |
227 // deferred_page_capture_ is true. | 199 // deferred_page_capture_ is true. |
228 base::string16 deferred_contents_; | 200 base::string16 deferred_contents_; |
229 | 201 |
230 #endif | |
231 | |
232 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 202 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
233 }; | 203 }; |
234 | 204 |
235 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 205 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
OLD | NEW |