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/browser/translate/translate_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/histogram.h" | 9 #include "base/histogram.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 }; | 122 }; |
123 | 123 |
124 const char* const kTranslateScriptURL = | 124 const char* const kTranslateScriptURL = |
125 "http://translate.google.com/translate_a/element.js?" | 125 "http://translate.google.com/translate_a/element.js?" |
126 "cb=cr.googleTranslate.onTranslateElementLoad"; | 126 "cb=cr.googleTranslate.onTranslateElementLoad"; |
127 const char* const kTranslateScriptHeader = | 127 const char* const kTranslateScriptHeader = |
128 "Google-Translate-Element-Mode: library"; | 128 "Google-Translate-Element-Mode: library"; |
129 const char* const kReportLanguageDetectionErrorURL = | 129 const char* const kReportLanguageDetectionErrorURL = |
130 "http://translate.google.com/translate_error"; | 130 "http://translate.google.com/translate_error"; |
131 | 131 |
| 132 const int kTranslateScriptExpirationDelayMS = 24 * 60 * 60 * 1000; // 1 day. |
| 133 |
132 } // namespace | 134 } // namespace |
133 | 135 |
134 // static | 136 // static |
135 base::LazyInstance<std::set<std::string> > | 137 base::LazyInstance<std::set<std::string> > |
136 TranslateManager::supported_languages_(base::LINKER_INITIALIZED); | 138 TranslateManager::supported_languages_(base::LINKER_INITIALIZED); |
137 | 139 |
138 TranslateManager::~TranslateManager() { | 140 TranslateManager::~TranslateManager() { |
139 } | 141 } |
140 | 142 |
141 // static | 143 // static |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 translate_script_request_pending_ = false; | 263 translate_script_request_pending_ = false; |
262 bool error = | 264 bool error = |
263 (status.status() != URLRequestStatus::SUCCESS || response_code != 200); | 265 (status.status() != URLRequestStatus::SUCCESS || response_code != 200); |
264 | 266 |
265 if (!error) { | 267 if (!error) { |
266 base::StringPiece str = ResourceBundle::GetSharedInstance(). | 268 base::StringPiece str = ResourceBundle::GetSharedInstance(). |
267 GetRawDataResource(IDR_TRANSLATE_JS); | 269 GetRawDataResource(IDR_TRANSLATE_JS); |
268 DCHECK(translate_script_.empty()); | 270 DCHECK(translate_script_.empty()); |
269 str.CopyToString(&translate_script_); | 271 str.CopyToString(&translate_script_); |
270 translate_script_ += "\n" + data; | 272 translate_script_ += "\n" + data; |
| 273 // We'll expire the cached script after some time, to make sure long running |
| 274 // browsers still get fixes that might get pushed with newer scripts. |
| 275 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 276 method_factory_.NewRunnableMethod( |
| 277 &TranslateManager::ClearTranslateScript), |
| 278 translate_script_expiration_delay_); |
271 } | 279 } |
272 | 280 |
273 // Process any pending requests. | 281 // Process any pending requests. |
274 std::vector<PendingRequest>::const_iterator iter; | 282 std::vector<PendingRequest>::const_iterator iter; |
275 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); | 283 for (iter = pending_requests_.begin(); iter != pending_requests_.end(); |
276 ++iter) { | 284 ++iter) { |
277 const PendingRequest& request = *iter; | 285 const PendingRequest& request = *iter; |
278 TabContents* tab = tab_util::GetTabContentsByID(request.render_process_id, | 286 TabContents* tab = tab_util::GetTabContentsByID(request.render_process_id, |
279 request.render_view_id); | 287 request.render_view_id); |
280 if (!tab) { | 288 if (!tab) { |
(...skipping 19 matching lines...) Expand all Loading... |
300 pending_requests_.clear(); | 308 pending_requests_.clear(); |
301 } | 309 } |
302 | 310 |
303 // static | 311 // static |
304 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 312 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { |
305 return GetTranslateInfoBarDelegate(tab) != NULL; | 313 return GetTranslateInfoBarDelegate(tab) != NULL; |
306 } | 314 } |
307 | 315 |
308 TranslateManager::TranslateManager() | 316 TranslateManager::TranslateManager() |
309 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 317 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 318 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS), |
310 translate_script_request_pending_(false) { | 319 translate_script_request_pending_(false) { |
311 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 320 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
312 NotificationService::AllSources()); | 321 NotificationService::AllSources()); |
313 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, | 322 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, |
314 NotificationService::AllSources()); | 323 NotificationService::AllSources()); |
315 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, | 324 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, |
316 NotificationService::AllSources()); | 325 NotificationService::AllSources()); |
317 } | 326 } |
318 | 327 |
319 void TranslateManager::InitiateTranslation(TabContents* tab, | 328 void TranslateManager::InitiateTranslation(TabContents* tab, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
364 | 373 |
365 std::string auto_translate_to = tab->language_state().AutoTranslateTo(); | 374 std::string auto_translate_to = tab->language_state().AutoTranslateTo(); |
366 if (!auto_translate_to.empty()) { | 375 if (!auto_translate_to.empty()) { |
367 // This page was navigated through a click from a translated page. | 376 // This page was navigated through a click from a translated page. |
368 TranslatePage(tab, page_lang, auto_translate_to); | 377 TranslatePage(tab, page_lang, auto_translate_to); |
369 return; | 378 return; |
370 } | 379 } |
371 | 380 |
372 // Prompts the user if he/she wants the page translated. | 381 // Prompts the user if he/she wants the page translated. |
373 tab->AddInfoBar(TranslateInfoBarDelegate::CreateDelegate( | 382 tab->AddInfoBar(TranslateInfoBarDelegate::CreateDelegate( |
374 TranslateInfoBarDelegate::kBeforeTranslate, tab, | 383 TranslateInfoBarDelegate::BEFORE_TRANSLATE, tab, |
375 page_lang, target_lang)); | 384 page_lang, target_lang)); |
376 } | 385 } |
377 | 386 |
378 void TranslateManager::InitiateTranslationPosted( | 387 void TranslateManager::InitiateTranslationPosted( |
379 int process_id, int render_id, const std::string& page_lang) { | 388 int process_id, int render_id, const std::string& page_lang) { |
380 // The tab might have been closed. | 389 // The tab might have been closed. |
381 TabContents* tab = tab_util::GetTabContentsByID(process_id, render_id); | 390 TabContents* tab = tab_util::GetTabContentsByID(process_id, render_id); |
382 if (!tab || tab->language_state().translation_pending()) | 391 if (!tab || tab->language_state().translation_pending()) |
383 return; | 392 return; |
384 | 393 |
385 InitiateTranslation(tab, page_lang); | 394 InitiateTranslation(tab, page_lang); |
386 } | 395 } |
387 | 396 |
388 void TranslateManager::TranslatePage(TabContents* tab_contents, | 397 void TranslateManager::TranslatePage(TabContents* tab_contents, |
389 const std::string& source_lang, | 398 const std::string& source_lang, |
390 const std::string& target_lang) { | 399 const std::string& target_lang) { |
391 NavigationEntry* entry = tab_contents->controller().GetActiveEntry(); | 400 NavigationEntry* entry = tab_contents->controller().GetActiveEntry(); |
392 if (!entry) { | 401 if (!entry) { |
393 NOTREACHED(); | 402 NOTREACHED(); |
394 return; | 403 return; |
395 } | 404 } |
396 | 405 |
397 TranslateInfoBarDelegate* infobar = GetTranslateInfoBarDelegate(tab_contents); | 406 TranslateInfoBarDelegate* infobar = GetTranslateInfoBarDelegate(tab_contents); |
398 if (infobar) { | 407 if (infobar) { |
399 // We don't show the translating infobar if no translate infobar is already | 408 // We don't show the translating infobar if no translate infobar is already |
400 // showing (that is the case when the translation was triggered by the | 409 // showing (that is the case when the translation was triggered by the |
401 // "always translate" for example). | 410 // "always translate" for example). |
402 infobar = TranslateInfoBarDelegate::CreateDelegate( | 411 infobar = TranslateInfoBarDelegate::CreateDelegate( |
403 TranslateInfoBarDelegate::kTranslating, tab_contents, | 412 TranslateInfoBarDelegate::TRANSLATING, tab_contents, |
404 source_lang, target_lang); | 413 source_lang, target_lang); |
405 ShowInfoBar(tab_contents, infobar); | 414 ShowInfoBar(tab_contents, infobar); |
406 } | 415 } |
407 | 416 |
408 if (!translate_script_.empty()) { | 417 if (!translate_script_.empty()) { |
409 DoTranslatePage(tab_contents, translate_script_, source_lang, target_lang); | 418 DoTranslatePage(tab_contents, translate_script_, source_lang, target_lang); |
410 return; | 419 return; |
411 } | 420 } |
412 | 421 |
413 // The script is not available yet. Queue that request and query for the | 422 // The script is not available yet. Queue that request and query for the |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 } else if (!IsSupportedLanguage(details->source_language)) { | 491 } else if (!IsSupportedLanguage(details->source_language)) { |
483 // TODO(jcivelli): http://crbug.com/9390 We should change the "after | 492 // TODO(jcivelli): http://crbug.com/9390 We should change the "after |
484 // translate" infobar to support unknown as the original | 493 // translate" infobar to support unknown as the original |
485 // language. | 494 // language. |
486 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); | 495 UMA_HISTOGRAM_COUNTS("Translate.ServerReportedUnsupportedLanguage", 1); |
487 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( | 496 infobar = TranslateInfoBarDelegate::CreateErrorDelegate( |
488 TranslateErrors::UNSUPPORTED_LANGUAGE, tab, | 497 TranslateErrors::UNSUPPORTED_LANGUAGE, tab, |
489 details->source_language, details->target_language); | 498 details->source_language, details->target_language); |
490 } else { | 499 } else { |
491 infobar = TranslateInfoBarDelegate::CreateDelegate( | 500 infobar = TranslateInfoBarDelegate::CreateDelegate( |
492 TranslateInfoBarDelegate::kAfterTranslate, tab, | 501 TranslateInfoBarDelegate::AFTER_TRANSLATE, tab, |
493 details->source_language, details->target_language); | 502 details->source_language, details->target_language); |
494 } | 503 } |
495 ShowInfoBar(tab, infobar); | 504 ShowInfoBar(tab, infobar); |
496 } | 505 } |
497 | 506 |
498 bool TranslateManager::IsAcceptLanguage(TabContents* tab, | 507 bool TranslateManager::IsAcceptLanguage(TabContents* tab, |
499 const std::string& language) { | 508 const std::string& language) { |
500 PrefService* pref_service = tab->profile()->GetPrefs(); | 509 PrefService* pref_service = tab->profile()->GetPrefs(); |
501 PrefServiceLanguagesMap::const_iterator iter = | 510 PrefServiceLanguagesMap::const_iterator iter = |
502 accept_languages_.find(pref_service); | 511 accept_languages_.find(pref_service); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
583 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( | 592 TranslateInfoBarDelegate* TranslateManager::GetTranslateInfoBarDelegate( |
584 TabContents* tab) { | 593 TabContents* tab) { |
585 for (int i = 0; i < tab->infobar_delegate_count(); ++i) { | 594 for (int i = 0; i < tab->infobar_delegate_count(); ++i) { |
586 TranslateInfoBarDelegate* delegate = | 595 TranslateInfoBarDelegate* delegate = |
587 tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 596 tab->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
588 if (delegate) | 597 if (delegate) |
589 return delegate; | 598 return delegate; |
590 } | 599 } |
591 return NULL; | 600 return NULL; |
592 } | 601 } |
OLD | NEW |