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