| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "chrome/renderer/translate_helper.h" | 5 #include "chrome/renderer/translate_helper.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 // If the page explicitly specifies a language, use it, otherwise we'll | 63 // If the page explicitly specifies a language, use it, otherwise we'll |
| 64 // determine it based on the text content using the CLD. | 64 // determine it based on the text content using the CLD. |
| 65 std::string language = GetPageLanguageFromMetaTag(&document); | 65 std::string language = GetPageLanguageFromMetaTag(&document); |
| 66 if (language.empty()) { | 66 if (language.empty()) { |
| 67 base::TimeTicks begin_time = base::TimeTicks::Now(); | 67 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 68 language = DetermineTextLanguage(contents); | 68 language = DetermineTextLanguage(contents); |
| 69 UMA_HISTOGRAM_MEDIUM_TIMES("Renderer4.LanguageDetection", | 69 UMA_HISTOGRAM_MEDIUM_TIMES("Renderer4.LanguageDetection", |
| 70 base::TimeTicks::Now() - begin_time); | 70 base::TimeTicks::Now() - begin_time); |
| 71 } | 71 } |
| 72 | 72 |
| 73 Send(new ViewHostMsg_TranslateLanguageDetermined( | 73 Send(new ChromeViewHostMsg_TranslateLanguageDetermined( |
| 74 routing_id(), language, IsPageTranslatable(&document))); | 74 routing_id(), language, IsPageTranslatable(&document))); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void TranslateHelper::CancelPendingTranslation() { | 77 void TranslateHelper::CancelPendingTranslation() { |
| 78 method_factory_.RevokeAll(); | 78 method_factory_.RevokeAll(); |
| 79 translation_pending_ = false; | 79 translation_pending_ = false; |
| 80 page_id_ = -1; | 80 page_id_ = -1; |
| 81 source_lang_.clear(); | 81 source_lang_.clear(); |
| 82 target_lang_.clear(); | 82 target_lang_.clear(); |
| 83 } | 83 } |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 return false; | 228 return false; |
| 229 } | 229 } |
| 230 | 230 |
| 231 //////////////////////////////////////////////////////////////////////////////// | 231 //////////////////////////////////////////////////////////////////////////////// |
| 232 // TranslateHelper, private: | 232 // TranslateHelper, private: |
| 233 // | 233 // |
| 234 | 234 |
| 235 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { | 235 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { |
| 236 bool handled = true; | 236 bool handled = true; |
| 237 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) | 237 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) |
| 238 IPC_MESSAGE_HANDLER(ViewMsg_TranslatePage, OnTranslatePage) | 238 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) |
| 239 IPC_MESSAGE_HANDLER(ViewMsg_RevertTranslation, OnRevertTranslation) | 239 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) |
| 240 IPC_MESSAGE_UNHANDLED(handled = false) | 240 IPC_MESSAGE_UNHANDLED(handled = false) |
| 241 IPC_END_MESSAGE_MAP() | 241 IPC_END_MESSAGE_MAP() |
| 242 return handled; | 242 return handled; |
| 243 } | 243 } |
| 244 | 244 |
| 245 void TranslateHelper::OnTranslatePage(int page_id, | 245 void TranslateHelper::OnTranslatePage(int page_id, |
| 246 const std::string& translate_script, | 246 const std::string& translate_script, |
| 247 const std::string& source_lang, | 247 const std::string& source_lang, |
| 248 const std::string& target_lang) { | 248 const std::string& target_lang) { |
| 249 if (render_view()->page_id() != page_id) | 249 if (render_view()->page_id() != page_id) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 NOTREACHED(); | 329 NOTREACHED(); |
| 330 return; | 330 return; |
| 331 } | 331 } |
| 332 | 332 |
| 333 translation_pending_ = false; | 333 translation_pending_ = false; |
| 334 | 334 |
| 335 if (autofill_) | 335 if (autofill_) |
| 336 autofill_->FrameTranslated(render_view()->webview()->mainFrame()); | 336 autofill_->FrameTranslated(render_view()->webview()->mainFrame()); |
| 337 | 337 |
| 338 // Notify the browser we are done. | 338 // Notify the browser we are done. |
| 339 render_view()->Send(new ViewHostMsg_PageTranslated( | 339 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 340 render_view()->routing_id(), render_view()->page_id(), | 340 render_view()->routing_id(), render_view()->page_id(), |
| 341 actual_source_lang, target_lang_, TranslateErrors::NONE)); | 341 actual_source_lang, target_lang_, TranslateErrors::NONE)); |
| 342 return; | 342 return; |
| 343 } | 343 } |
| 344 | 344 |
| 345 // The translation is still pending, check again later. | 345 // The translation is still pending, check again later. |
| 346 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 346 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 347 method_factory_.NewRunnableMethod(&TranslateHelper::CheckTranslateStatus), | 347 method_factory_.NewRunnableMethod(&TranslateHelper::CheckTranslateStatus), |
| 348 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); | 348 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); |
| 349 } | 349 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 418 // Check the status of the translation. | 418 // Check the status of the translation. |
| 419 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 419 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 420 method_factory_.NewRunnableMethod(&TranslateHelper::CheckTranslateStatus), | 420 method_factory_.NewRunnableMethod(&TranslateHelper::CheckTranslateStatus), |
| 421 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); | 421 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); |
| 422 } | 422 } |
| 423 | 423 |
| 424 void TranslateHelper::NotifyBrowserTranslationFailed( | 424 void TranslateHelper::NotifyBrowserTranslationFailed( |
| 425 TranslateErrors::Type error) { | 425 TranslateErrors::Type error) { |
| 426 translation_pending_ = false; | 426 translation_pending_ = false; |
| 427 // Notify the browser there was an error. | 427 // Notify the browser there was an error. |
| 428 render_view()->Send(new ViewHostMsg_PageTranslated( | 428 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 429 render_view()->routing_id(), page_id_, source_lang_, target_lang_, error))
; | 429 render_view()->routing_id(), page_id_, source_lang_, target_lang_, error))
; |
| 430 } | 430 } |
| 431 | 431 |
| 432 WebFrame* TranslateHelper::GetMainFrame() { | 432 WebFrame* TranslateHelper::GetMainFrame() { |
| 433 WebView* web_view = render_view()->webview(); | 433 WebView* web_view = render_view()->webview(); |
| 434 if (!web_view) { | 434 if (!web_view) { |
| 435 // When the WebView is going away, the render view should have called | 435 // When the WebView is going away, the render view should have called |
| 436 // CancelPendingTranslation() which should have stopped any pending work, so | 436 // CancelPendingTranslation() which should have stopped any pending work, so |
| 437 // that case should not happen. | 437 // that case should not happen. |
| 438 NOTREACHED(); | 438 NOTREACHED(); |
| 439 return NULL; | 439 return NULL; |
| 440 } | 440 } |
| 441 return web_view->mainFrame(); | 441 return web_view->mainFrame(); |
| 442 } | 442 } |
| OLD | NEW |