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