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/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "components/translate/core/common/translate_errors.h" | 13 #include "components/translate/core/common/translate_errors.h" |
14 #include "content/public/renderer/render_view_observer.h" | 14 #include "content/public/renderer/render_view_observer.h" |
15 #include "ipc/ipc_platform_file.h" | |
16 #include "url/gurl.h" | |
15 | 17 |
16 namespace blink { | 18 namespace blink { |
17 class WebDocument; | 19 class WebDocument; |
18 class WebFrame; | 20 class WebFrame; |
19 } | 21 } |
20 | 22 |
21 // This class deals with page translation. | 23 // This class deals with page translation. |
22 // There is one TranslateHelper per RenderView. | 24 // There is one TranslateHelper per RenderView. |
23 | 25 |
24 class TranslateHelper : public content::RenderViewObserver { | 26 class TranslateHelper : public content::RenderViewObserver { |
25 public: | 27 public: |
26 explicit TranslateHelper(content::RenderView* render_view); | 28 explicit TranslateHelper(content::RenderView* render_view); |
27 virtual ~TranslateHelper(); | 29 virtual ~TranslateHelper(); |
28 | 30 |
29 // Informs us that the page's text has been extracted. | 31 // Informs us that the page's text has been extracted. |
30 void PageCaptured(int page_id, const base::string16& contents); | 32 void PageCaptured(int page_id, const base::string16& contents); |
31 | 33 |
34 // Lets the translation system know that we are preparing to navigate to | |
35 // the specified URL. If there is anything that can or should be done before | |
36 // this URL loads, this is the time to prepare for it. | |
37 void PrepareForUrl(const GURL& url); | |
38 | |
32 protected: | 39 protected: |
33 // The following methods are protected so they can be overridden in | 40 // The following methods are protected so they can be overridden in |
34 // unit-tests. | 41 // unit-tests. |
35 void OnTranslatePage(int page_id, | 42 void OnTranslatePage(int page_id, |
36 const std::string& translate_script, | 43 const std::string& translate_script, |
37 const std::string& source_lang, | 44 const std::string& source_lang, |
38 const std::string& target_lang); | 45 const std::string& target_lang); |
39 void OnRevertTranslation(int page_id); | 46 void OnRevertTranslation(int page_id); |
40 | 47 |
41 // Returns true if the translate library is available, meaning the JavaScript | 48 // Returns true if the translate library is available, meaning the JavaScript |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 std::string source_lang_; | 148 std::string source_lang_; |
142 std::string target_lang_; | 149 std::string target_lang_; |
143 | 150 |
144 // Time when a page langauge is determined. This is used to know a duration | 151 // Time when a page langauge is determined. This is used to know a duration |
145 // time from showing infobar to requesting translation. | 152 // time from showing infobar to requesting translation. |
146 base::TimeTicks language_determined_time_; | 153 base::TimeTicks language_determined_time_; |
147 | 154 |
148 // Method factory used to make calls to TranslatePageImpl. | 155 // Method factory used to make calls to TranslatePageImpl. |
149 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 156 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
150 | 157 |
158 #if defined(CLD2_DYNAMIC_MODE) | |
159 // Do not ask for CLD data any more. | |
160 void CancelCLD2DataFilePolling(); | |
161 | |
162 // Whether or not we have started polling for CLD2 data | |
163 bool cld2_data_file_polling_started; | |
164 | |
165 // Whether or not CancelCLD2DataFilePolling has been called | |
166 bool cld2_data_file_polling_canceled; | |
bulach
2014/03/23 20:57:02
nit: this and the one above, append a "_"
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
167 | |
168 // Whether or not a PageCaptured event arrived prior to CLD data becoming | |
169 // available. If true, deferred_page_id_ contains the most recent page ID | |
170 // and deferred_contents_ contains the most recent contents. | |
171 bool deferred_page_capture_; | |
172 | |
173 // The ID of the page most recently reported to PageCaptured if | |
174 // deferred_page_capture_ is true. | |
175 int deferred_page_id_; | |
176 | |
177 // The contents of the page most recently reported to PageCaptured if | |
178 // deferred_page_capture_ is true. | |
179 base::string16 deferred_contents_; | |
180 | |
181 // Invoked when PageCaptured is called prior to obtaining CLD data. This | |
182 // method stores the page ID into deferred_page_id_ and COPIES the contents | |
183 // of the page, and sets deferred_page_capture_ to true. When CLD data is | |
184 // eventually received (in OnCLDDataAvailable), any deferred request will be | |
185 // "resurrected" and allowed to proceed automatically, assuming that the | |
186 // page ID has not changed. | |
187 void DeferPageCaptured(const int page_id, const base::string16& contents); | |
188 | |
189 // Immediately send an IPC request to the browser process to get the CLD | |
190 // data file. In most cases, the file will already exist and we will only | |
191 // poll once; but since the file might need to be downloaded first, we must | |
192 // poll indefinitely until a ChromeViewMsg_CLDDataAvailable message is | |
193 // received from the browser process. | |
194 // Polling will automatically halt as soon as the renderer obtains a | |
195 // reference to the data file. | |
196 void SendCLD2DataFileRequest(int delay_millis, int next_delay_millis); | |
197 | |
198 // Invoked when a ChromeViewMsg_CLDDataAvailable message is received from | |
199 // the browser process, providing us with a file handle for the CLD data | |
200 // file. If a PageCaptured request was previously deferred with | |
201 // DeferPageCaptured and the page ID has not yet changed, the PageCaptured | |
202 // is reinvoked to "resurrect" the language detection pathway. | |
203 void OnCLDDataAvailable(IPC::PlatformFileForTransit ipc_file_handle); | |
204 | |
205 // After receiving data in OnCLDDataAvailable, loads the data into CLD2. | |
206 void LoadCLDDData(IPC::PlatformFileForTransit ipc_file_handle); | |
207 #endif | |
151 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 208 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
152 }; | 209 }; |
153 | 210 |
154 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 211 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
OLD | NEW |