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 |
25 namespace content { | |
26 class RendererCldDataProvider; | |
27 } | |
28 | |
30 // This class deals with page translation. | 29 // This class deals with page translation. |
31 // There is one TranslateHelper per RenderView. | 30 // There is one TranslateHelper per RenderView. |
32 | 31 |
33 class TranslateHelper : public content::RenderViewObserver { | 32 class TranslateHelper : public content::RenderViewObserver { |
34 public: | 33 public: |
35 explicit TranslateHelper(content::RenderView* render_view); | 34 explicit TranslateHelper(content::RenderView* render_view); |
36 virtual ~TranslateHelper(); | 35 virtual ~TranslateHelper(); |
37 | 36 |
38 // Informs us that the page's text has been extracted. | 37 // Informs us that the page's text has been extracted. |
39 void PageCaptured(int page_id, const base::string16& contents); | 38 void PageCaptured(int page_id, const base::string16& contents); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 std::string source_lang_; | 154 std::string source_lang_; |
156 std::string target_lang_; | 155 std::string target_lang_; |
157 | 156 |
158 // Time when a page langauge is determined. This is used to know a duration | 157 // Time when a page langauge is determined. This is used to know a duration |
159 // time from showing infobar to requesting translation. | 158 // time from showing infobar to requesting translation. |
160 base::TimeTicks language_determined_time_; | 159 base::TimeTicks language_determined_time_; |
161 | 160 |
162 // Method factory used to make calls to TranslatePageImpl. | 161 // Method factory used to make calls to TranslatePageImpl. |
163 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 162 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
164 | 163 |
165 #if defined(CLD2_DYNAMIC_MODE) | |
166 // Do not ask for CLD data any more. | 164 // Do not ask for CLD data any more. |
Takashi Toyoshima
2014/06/23 08:50:01
Now that following methods and members are free fr
Andrew Hayden (chromium.org)
2014/06/23 13:20:36
Yes, good catch. Done. Die, ifdefs, die!
| |
167 void CancelCLD2DataFilePolling(); | 165 void CancelCldDataPolling(); |
168 | 166 |
169 // Invoked when PageCaptured is called prior to obtaining CLD data. This | 167 // 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 | 168 // 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 | 169 // of the page, then sets deferred_page_capture_ to true. When CLD data is |
172 // eventually received (in OnCLDDataAvailable), any deferred request will be | 170 // eventually received (in OnCldDataAvailable), any deferred request will be |
173 // "resurrected" and allowed to proceed automatically, assuming that the | 171 // "resurrected" and allowed to proceed automatically, assuming that the |
174 // page ID has not changed. | 172 // page ID has not changed. |
175 void DeferPageCaptured(const int page_id, const base::string16& contents); | 173 void DeferPageCaptured(const int page_id, const base::string16& contents); |
176 | 174 |
177 // Immediately send an IPC request to the browser process to get the CLD | 175 // 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 | 176 // Polling will automatically halt as soon as the renderer obtains a |
183 // reference to the data file. | 177 // reference to the data file. |
184 void SendCLD2DataFileRequest(const int delay_millis, | 178 void SendCldDataRequest(const int delay_millis, |
185 const int next_delay_millis); | 179 const int next_delay_millis); |
186 | 180 |
187 // Invoked when a ChromeViewMsg_CLDDataAvailable message is received from | 181 // Callback triggered when CLD data becomes available. |
188 // the browser process, providing a file handle for the CLD data file. If a | 182 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 | 183 |
196 // After receiving data in OnCLDDataAvailable, loads the data into CLD2. | 184 // Provides CLD data for this process. |
197 void LoadCLDDData(base::File file, | 185 scoped_ptr<translate::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 | 186 |
211 // Whether or not polling for CLD2 data has started. | 187 // Whether or not polling for CLD2 data has started. |
212 bool cld2_data_file_polling_started_; | 188 bool cld_data_polling_started_; |
213 | 189 |
214 // Whether or not CancelCLD2DataFilePolling has been called. | 190 // Whether or not CancelCldDataPolling has been called. |
215 bool cld2_data_file_polling_canceled_; | 191 bool cld_data_polling_canceled_; |
216 | 192 |
217 // Whether or not a PageCaptured event arrived prior to CLD data becoming | 193 // 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 | 194 // available. If true, deferred_page_id_ contains the most recent page ID |
219 // and deferred_contents_ contains the most recent contents. | 195 // and deferred_contents_ contains the most recent contents. |
220 bool deferred_page_capture_; | 196 bool deferred_page_capture_; |
221 | 197 |
222 // The ID of the page most recently reported to PageCaptured if | 198 // The ID of the page most recently reported to PageCaptured if |
223 // deferred_page_capture_ is true. | 199 // deferred_page_capture_ is true. |
224 int deferred_page_id_; | 200 int deferred_page_id_; |
225 | 201 |
226 // The contents of the page most recently reported to PageCaptured if | 202 // The contents of the page most recently reported to PageCaptured if |
227 // deferred_page_capture_ is true. | 203 // deferred_page_capture_ is true. |
228 base::string16 deferred_contents_; | 204 base::string16 deferred_contents_; |
229 | 205 |
230 #endif | |
231 | |
232 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 206 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
233 }; | 207 }; |
234 | 208 |
235 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 209 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
OLD | NEW |