Chromium Code Reviews| 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 // The mmap. The mmap destructor will unmap the memory segment and close the | |
| 165 // file handle; it must be held throughought the lifetime of the process. | |
| 166 static base::MemoryMappedFile* s_cld2_data_file_mmap_; | |
|
bulach
2014/03/26 19:06:46
remove it?
andrewhayden
2014/03/27 09:36:59
Whoops! thanks.
| |
| 167 | |
| 168 // Invoked when PageCaptured is called prior to obtaining CLD data. This | |
| 169 // method stores the page ID into deferred_page_id_ and COPIES the contents | |
| 170 // of the page, then sets deferred_page_capture_ to true. When CLD data is | |
| 171 // eventually received (in OnCLDDataAvailable), any deferred request will be | |
| 172 // "resurrected" and allowed to proceed automatically, assuming that the | |
| 173 // page ID has not changed. | |
| 174 void DeferPageCaptured(const int page_id, const base::string16& contents); | |
| 175 | |
| 176 // Immediately send an IPC request to the browser process to get the CLD | |
| 177 // data file. In most cases, the file will already exist and we will only | |
| 178 // poll once; but since the file might need to be downloaded first, poll | |
| 179 // indefinitely until a ChromeViewMsg_CLDDataAvailable message is received | |
| 180 // from the browser process. | |
| 181 // Polling will automatically halt as soon as the renderer obtains a | |
| 182 // reference to the data file. | |
| 183 void SendCLD2DataFileRequest(const int delay_millis, | |
| 184 const int next_delay_millis); | |
| 185 | |
| 186 // Invoked when a ChromeViewMsg_CLDDataAvailable message is received from | |
| 187 // the browser process, providing a file handle for the CLD data file. If a | |
| 188 // PageCaptured request was previously deferred with DeferPageCaptured and | |
| 189 // the page ID has not yet changed, the PageCaptured is reinvoked to | |
| 190 // "resurrect" the language detection pathway. | |
| 191 void OnCLDDataAvailable(const IPC::PlatformFileForTransit ipc_file_handle, | |
| 192 const int64 data_offset, | |
| 193 const int64 data_length); | |
| 194 | |
| 195 // After receiving data in OnCLDDataAvailable, loads the data into CLD2. | |
| 196 void LoadCLDDData(const IPC::PlatformFileForTransit ipc_file_handle, | |
| 197 const int64 data_offset, | |
| 198 const int64 data_length); | |
| 199 | |
| 200 // A struct that contains the pointer to the CLD mmap. Used so that we can | |
| 201 // leverage LazyInstance:Leaky to properly scope the lifetime of the mmap. | |
| 202 struct CLDMmapWrapper { | |
| 203 CLDMmapWrapper() { | |
| 204 value = NULL; | |
| 205 } | |
| 206 base::MemoryMappedFile* value; | |
| 207 }; | |
| 208 static base::LazyInstance<CLDMmapWrapper>::Leaky s_cld_mmap_; | |
| 209 | |
| 210 // Whether or not polling for CLD2 data has started. | |
| 211 bool cld2_data_file_polling_started_; | |
| 212 | |
| 213 // Whether or not CancelCLD2DataFilePolling has been called. | |
| 214 bool cld2_data_file_polling_canceled_; | |
| 215 | |
| 216 // Whether or not a PageCaptured event arrived prior to CLD data becoming | |
| 217 // available. If true, deferred_page_id_ contains the most recent page ID | |
| 218 // and deferred_contents_ contains the most recent contents. | |
| 219 bool deferred_page_capture_; | |
| 220 | |
| 221 // The ID of the page most recently reported to PageCaptured if | |
| 222 // deferred_page_capture_ is true. | |
| 223 int deferred_page_id_; | |
| 224 | |
| 225 // The contents of the page most recently reported to PageCaptured if | |
| 226 // deferred_page_capture_ is true. | |
| 227 base::string16 deferred_contents_; | |
| 228 | |
| 229 #endif | |
| 230 | |
| 151 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 231 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
| 152 }; | 232 }; |
| 153 | 233 |
| 154 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 234 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
| OLD | NEW |