| 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/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/translate/content/renderer/renderer_cld_data_provider.h" |
| 14 #include "components/translate/core/common/translate_errors.h" | 16 #include "components/translate/core/common/translate_errors.h" |
| 15 #include "content/public/renderer/render_view_observer.h" | 17 #include "content/public/renderer/render_view_observer.h" |
| 16 | |
| 17 #if defined(CLD2_DYNAMIC_MODE) | |
| 18 #include "base/files/file.h" | |
| 19 #include "base/files/memory_mapped_file.h" | |
| 20 #include "base/lazy_instance.h" | |
| 21 #include "ipc/ipc_platform_file.h" | |
| 22 #include "url/gurl.h" | 18 #include "url/gurl.h" |
| 23 #endif | |
| 24 | 19 |
| 25 namespace blink { | 20 namespace blink { |
| 26 class WebDocument; | 21 class WebDocument; |
| 27 class WebFrame; | 22 class WebFrame; |
| 28 } | 23 } |
| 29 | 24 |
| 25 namespace content { |
| 26 class RendererCldDataProvider; |
| 27 } |
| 28 |
| 30 // This class deals with page translation. | 29 // This class deals with page translation. |
| 31 // There is one TranslateHelper per RenderView. | 30 // There is one TranslateHelper per RenderView. |
| 32 | 31 |
| 33 class TranslateHelper : public content::RenderViewObserver { | 32 class TranslateHelper : public content::RenderViewObserver { |
| 34 public: | 33 public: |
| 35 explicit TranslateHelper(content::RenderView* render_view); | 34 explicit TranslateHelper(content::RenderView* render_view); |
| 36 virtual ~TranslateHelper(); | 35 virtual ~TranslateHelper(); |
| 37 | 36 |
| 38 // Informs us that the page's text has been extracted. | 37 // Informs us that the page's text has been extracted. |
| 39 void PageCaptured(int page_id, const base::string16& contents); | 38 void PageCaptured(int page_id, const base::string16& contents); |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 void TranslatePageImpl(int count); | 138 void TranslatePageImpl(int count); |
| 140 | 139 |
| 141 // Sends a message to the browser to notify it that the translation failed | 140 // Sends a message to the browser to notify it that the translation failed |
| 142 // with |error|. | 141 // with |error|. |
| 143 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); | 142 void NotifyBrowserTranslationFailed(TranslateErrors::Type error); |
| 144 | 143 |
| 145 // Convenience method to access the main frame. Can return NULL, typically | 144 // Convenience method to access the main frame. Can return NULL, typically |
| 146 // if the page is being closed. | 145 // if the page is being closed. |
| 147 blink::WebFrame* GetMainFrame(); | 146 blink::WebFrame* GetMainFrame(); |
| 148 | 147 |
| 148 // Do not ask for CLD data any more. |
| 149 void CancelCldDataPolling(); |
| 150 |
| 151 // Invoked when PageCaptured is called prior to obtaining CLD data. This |
| 152 // method stores the page ID into deferred_page_id_ and COPIES the contents |
| 153 // of the page, then sets deferred_page_capture_ to true. When CLD data is |
| 154 // eventually received (in OnCldDataAvailable), any deferred request will be |
| 155 // "resurrected" and allowed to proceed automatically, assuming that the |
| 156 // page ID has not changed. |
| 157 void DeferPageCaptured(const int page_id, const base::string16& contents); |
| 158 |
| 159 // Start polling for CLD data. |
| 160 // Polling will automatically halt as soon as the renderer obtains a |
| 161 // reference to the data file. |
| 162 void SendCldDataRequest(const int delay_millis, const int next_delay_millis); |
| 163 |
| 164 // Callback triggered when CLD data becomes available. |
| 165 void OnCldDataAvailable(); |
| 166 |
| 149 // ID to represent a page which TranslateHelper captured and determined a | 167 // ID to represent a page which TranslateHelper captured and determined a |
| 150 // content language. | 168 // content language. |
| 151 int page_id_; | 169 int page_id_; |
| 152 | 170 |
| 153 // The states associated with the current translation. | 171 // The states associated with the current translation. |
| 154 bool translation_pending_; | 172 bool translation_pending_; |
| 155 std::string source_lang_; | 173 std::string source_lang_; |
| 156 std::string target_lang_; | 174 std::string target_lang_; |
| 157 | 175 |
| 158 // Time when a page langauge is determined. This is used to know a duration | 176 // Time when a page langauge is determined. This is used to know a duration |
| 159 // time from showing infobar to requesting translation. | 177 // time from showing infobar to requesting translation. |
| 160 base::TimeTicks language_determined_time_; | 178 base::TimeTicks language_determined_time_; |
| 161 | 179 |
| 162 // Method factory used to make calls to TranslatePageImpl. | 180 // Method factory used to make calls to TranslatePageImpl. |
| 163 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 181 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
| 164 | 182 |
| 165 #if defined(CLD2_DYNAMIC_MODE) | 183 // Provides CLD data for this process. |
| 166 // Do not ask for CLD data any more. | 184 scoped_ptr<translate::RendererCldDataProvider> cld_data_provider_; |
| 167 void CancelCLD2DataFilePolling(); | |
| 168 | |
| 169 // Invoked when PageCaptured is called prior to obtaining CLD data. This | |
| 170 // method stores the page ID into deferred_page_id_ and COPIES the contents | |
| 171 // of the page, then sets deferred_page_capture_ to true. When CLD data is | |
| 172 // eventually received (in OnCLDDataAvailable), any deferred request will be | |
| 173 // "resurrected" and allowed to proceed automatically, assuming that the | |
| 174 // page ID has not changed. | |
| 175 void DeferPageCaptured(const int page_id, const base::string16& contents); | |
| 176 | |
| 177 // Immediately send an IPC request to the browser process to get the CLD | |
| 178 // data file. In most cases, the file will already exist and we will only | |
| 179 // poll once; but since the file might need to be downloaded first, poll | |
| 180 // indefinitely until a ChromeViewMsg_CLDDataAvailable message is received | |
| 181 // from the browser process. | |
| 182 // Polling will automatically halt as soon as the renderer obtains a | |
| 183 // reference to the data file. | |
| 184 void SendCLD2DataFileRequest(const int delay_millis, | |
| 185 const int next_delay_millis); | |
| 186 | |
| 187 // Invoked when a ChromeViewMsg_CLDDataAvailable message is received from | |
| 188 // the browser process, providing a file handle for the CLD data file. If a | |
| 189 // PageCaptured request was previously deferred with DeferPageCaptured and | |
| 190 // the page ID has not yet changed, the PageCaptured is reinvoked to | |
| 191 // "resurrect" the language detection pathway. | |
| 192 void OnCLDDataAvailable(const IPC::PlatformFileForTransit ipc_file_handle, | |
| 193 const uint64 data_offset, | |
| 194 const uint64 data_length); | |
| 195 | |
| 196 // After receiving data in OnCLDDataAvailable, loads the data into CLD2. | |
| 197 void LoadCLDDData(base::File file, | |
| 198 const uint64 data_offset, | |
| 199 const uint64 data_length); | |
| 200 | |
| 201 // A struct that contains the pointer to the CLD mmap. Used so that we can | |
| 202 // leverage LazyInstance:Leaky to properly scope the lifetime of the mmap. | |
| 203 struct CLDMmapWrapper { | |
| 204 CLDMmapWrapper() { | |
| 205 value = NULL; | |
| 206 } | |
| 207 base::MemoryMappedFile* value; | |
| 208 }; | |
| 209 static base::LazyInstance<CLDMmapWrapper>::Leaky s_cld_mmap_; | |
| 210 | 185 |
| 211 // Whether or not polling for CLD2 data has started. | 186 // Whether or not polling for CLD2 data has started. |
| 212 bool cld2_data_file_polling_started_; | 187 bool cld_data_polling_started_; |
| 213 | 188 |
| 214 // Whether or not CancelCLD2DataFilePolling has been called. | 189 // Whether or not CancelCldDataPolling has been called. |
| 215 bool cld2_data_file_polling_canceled_; | 190 bool cld_data_polling_canceled_; |
| 216 | 191 |
| 217 // Whether or not a PageCaptured event arrived prior to CLD data becoming | 192 // Whether or not a PageCaptured event arrived prior to CLD data becoming |
| 218 // available. If true, deferred_page_id_ contains the most recent page ID | 193 // available. If true, deferred_page_id_ contains the most recent page ID |
| 219 // and deferred_contents_ contains the most recent contents. | 194 // and deferred_contents_ contains the most recent contents. |
| 220 bool deferred_page_capture_; | 195 bool deferred_page_capture_; |
| 221 | 196 |
| 222 // The ID of the page most recently reported to PageCaptured if | 197 // The ID of the page most recently reported to PageCaptured if |
| 223 // deferred_page_capture_ is true. | 198 // deferred_page_capture_ is true. |
| 224 int deferred_page_id_; | 199 int deferred_page_id_; |
| 225 | 200 |
| 226 // The contents of the page most recently reported to PageCaptured if | 201 // The contents of the page most recently reported to PageCaptured if |
| 227 // deferred_page_capture_ is true. | 202 // deferred_page_capture_ is true. |
| 228 base::string16 deferred_contents_; | 203 base::string16 deferred_contents_; |
| 229 | 204 |
| 230 #endif | |
| 231 | |
| 232 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 205 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
| 233 }; | 206 }; |
| 234 | 207 |
| 235 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 208 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
| OLD | NEW |