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