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 #include "chrome/browser/translate/translate_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
342 NOTREACHED(); | 342 NOTREACHED(); |
343 } | 343 } |
344 } | 344 } |
345 | 345 |
346 void TranslateManager::OnURLFetchComplete(const URLFetcher* source, | 346 void TranslateManager::OnURLFetchComplete(const URLFetcher* source, |
347 const GURL& url, | 347 const GURL& url, |
348 const net::URLRequestStatus& status, | 348 const net::URLRequestStatus& status, |
349 int response_code, | 349 int response_code, |
350 const net::ResponseCookies& cookies, | 350 const net::ResponseCookies& cookies, |
351 const std::string& data) { | 351 const std::string& data) { |
352 scoped_ptr<const URLFetcher> delete_ptr(source); | 352 if (translate_script_request_pending_.get() != source && |
353 DCHECK(translate_script_request_pending_ || language_list_request_pending_); | 353 language_list_request_pending_.get() != source) { |
354 // We quickly recognize that we are handling a translate script request | 354 // Looks like crash on Mac is possibly caused with callback entering here |
cbentzel
2011/06/24 19:56:08
Should you add a CHECK here so we can see failures
| |
355 // if we don't have a language_list_request_pending_. Otherwise we do the | 355 // with unknown fetcher when network is refreshed. |
356 // more expensive check of confirming we got the kTranslateScriptURL in the | 356 scoped_ptr<const URLFetcher> delete_ptr(source); |
357 // rare case where we would have both requests pending at the same time. | 357 return; |
358 bool translate_script_request = !language_list_request_pending_ || | 358 } |
359 url == GURL(kTranslateScriptURL); | |
360 // Here we make sure that if we didn't get the translate_script_request, | |
361 // we actually got a language_list_request. | |
362 DCHECK(translate_script_request || url == GURL(kLanguageListFetchURL)); | |
363 if (translate_script_request) | |
364 translate_script_request_pending_ = false; | |
365 else | |
366 language_list_request_pending_ = false; | |
367 | 359 |
368 bool error = | 360 bool error = (status.status() != net::URLRequestStatus::SUCCESS || |
369 (status.status() != net::URLRequestStatus::SUCCESS || | 361 response_code != 200); |
370 response_code != 200); | 362 if (translate_script_request_pending_.get() == source) { |
371 | 363 scoped_ptr<const URLFetcher> delete_ptr( |
372 if (translate_script_request) { | 364 translate_script_request_pending_.release()); |
373 if (!error) { | 365 if (!error) { |
374 base::StringPiece str = ResourceBundle::GetSharedInstance(). | 366 base::StringPiece str = ResourceBundle::GetSharedInstance(). |
375 GetRawDataResource(IDR_TRANSLATE_JS); | 367 GetRawDataResource(IDR_TRANSLATE_JS); |
376 DCHECK(translate_script_.empty()); | 368 DCHECK(translate_script_.empty()); |
377 str.CopyToString(&translate_script_); | 369 str.CopyToString(&translate_script_); |
378 translate_script_ += "\n" + data; | 370 translate_script_ += "\n" + data; |
379 // We'll expire the cached script after some time, to make sure long | 371 // We'll expire the cached script after some time, to make sure long |
380 // running browsers still get fixes that might get pushed with newer | 372 // running browsers still get fixes that might get pushed with newer |
381 // scripts. | 373 // scripts. |
382 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 374 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
(...skipping 22 matching lines...) Expand all Loading... | |
405 ShowInfoBar(tab, TranslateInfoBarDelegate::CreateErrorDelegate( | 397 ShowInfoBar(tab, TranslateInfoBarDelegate::CreateErrorDelegate( |
406 TranslateErrors::NETWORK, tab, | 398 TranslateErrors::NETWORK, tab, |
407 request.source_lang, request.target_lang)); | 399 request.source_lang, request.target_lang)); |
408 } else { | 400 } else { |
409 // Translate the page. | 401 // Translate the page. |
410 DoTranslatePage(tab, translate_script_, | 402 DoTranslatePage(tab, translate_script_, |
411 request.source_lang, request.target_lang); | 403 request.source_lang, request.target_lang); |
412 } | 404 } |
413 } | 405 } |
414 pending_requests_.clear(); | 406 pending_requests_.clear(); |
415 } else { // if (translate_script_request) | 407 } else { // if (translate_script_request_pending_.get() == source) |
408 scoped_ptr<const URLFetcher> delete_ptr( | |
409 language_list_request_pending_.release()); | |
416 if (!error) | 410 if (!error) |
417 SetSupportedLanguages(data); | 411 SetSupportedLanguages(data); |
418 else | 412 else |
419 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; | 413 VLOG(1) << "Failed to Fetch languages from: " << kLanguageListFetchURL; |
420 } | 414 } |
421 } | 415 } |
422 | 416 |
423 // static | 417 // static |
424 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 418 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { |
425 return GetTranslateInfoBarDelegate(tab) != NULL; | 419 return GetTranslateInfoBarDelegate(tab) != NULL; |
426 } | 420 } |
427 | 421 |
428 TranslateManager::TranslateManager() | 422 TranslateManager::TranslateManager() |
429 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 423 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
430 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS), | 424 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { |
431 translate_script_request_pending_(false), | |
432 language_list_request_pending_(false) { | |
433 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 425 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
434 NotificationService::AllSources()); | 426 NotificationService::AllSources()); |
435 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, | 427 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, |
436 NotificationService::AllSources()); | 428 NotificationService::AllSources()); |
437 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, | 429 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, |
438 NotificationService::AllSources()); | 430 NotificationService::AllSources()); |
439 } | 431 } |
440 | 432 |
441 void TranslateManager::InitiateTranslation(TabContents* tab, | 433 void TranslateManager::InitiateTranslation(TabContents* tab, |
442 const std::string& page_lang) { | 434 const std::string& page_lang) { |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
698 // the default Accept-Language values for most locales, remove this | 690 // the default Accept-Language values for most locales, remove this |
699 // special-casing. | 691 // special-casing. |
700 if (accept_lang != "en" || is_ui_english) | 692 if (accept_lang != "en" || is_ui_english) |
701 accept_langs_set.insert(accept_lang); | 693 accept_langs_set.insert(accept_lang); |
702 } | 694 } |
703 accept_languages_[prefs] = accept_langs_set; | 695 accept_languages_[prefs] = accept_langs_set; |
704 } | 696 } |
705 | 697 |
706 void TranslateManager::FetchLanguageListFromTranslateServer( | 698 void TranslateManager::FetchLanguageListFromTranslateServer( |
707 PrefService* prefs) { | 699 PrefService* prefs) { |
708 if (language_list_request_pending_) | 700 if (language_list_request_pending_.get() != NULL) |
709 return; | 701 return; |
710 | 702 |
711 // We don't want to do this when translate is disabled. | 703 // We don't want to do this when translate is disabled. |
712 DCHECK(prefs != NULL); | 704 DCHECK(prefs != NULL); |
713 if (CommandLine::ForCurrentProcess()->HasSwitch( | 705 if (CommandLine::ForCurrentProcess()->HasSwitch( |
714 switches::kDisableTranslate) || | 706 switches::kDisableTranslate) || |
715 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { | 707 (prefs != NULL && !prefs->GetBoolean(prefs::kEnableTranslate))) { |
716 return; | 708 return; |
717 } | 709 } |
718 | 710 |
719 language_list_request_pending_ = true; | 711 language_list_request_pending_.reset(URLFetcher::Create( |
720 URLFetcher* fetcher = URLFetcher::Create(1, GURL(kLanguageListFetchURL), | 712 1, GURL(kLanguageListFetchURL), URLFetcher::GET, this)); |
721 URLFetcher::GET, this); | 713 language_list_request_pending_->set_request_context( |
722 fetcher->set_request_context(Profile::GetDefaultRequestContext()); | 714 Profile::GetDefaultRequestContext()); |
723 fetcher->set_max_retries(kMaxRetryLanguageListFetch); | 715 language_list_request_pending_->set_max_retries(kMaxRetryLanguageListFetch); |
724 fetcher->Start(); | 716 language_list_request_pending_->Start(); |
717 } | |
718 | |
719 void TranslateManager::CleanupPendingUlrFetcher() { | |
720 language_list_request_pending_.reset(); | |
721 translate_script_request_pending_.reset(); | |
725 } | 722 } |
726 | 723 |
727 void TranslateManager::RequestTranslateScript() { | 724 void TranslateManager::RequestTranslateScript() { |
728 if (translate_script_request_pending_) | 725 if (translate_script_request_pending_.get() != NULL) |
729 return; | 726 return; |
730 | 727 |
731 translate_script_request_pending_ = true; | 728 translate_script_request_pending_.reset(URLFetcher::Create( |
732 URLFetcher* fetcher = URLFetcher::Create(0, GURL(kTranslateScriptURL), | 729 0, GURL(kTranslateScriptURL), URLFetcher::GET, this)); |
733 URLFetcher::GET, this); | 730 translate_script_request_pending_->set_request_context( |
734 fetcher->set_request_context(Profile::GetDefaultRequestContext()); | 731 Profile::GetDefaultRequestContext()); |
735 fetcher->set_extra_request_headers(kTranslateScriptHeader); | 732 translate_script_request_pending_->set_extra_request_headers( |
736 fetcher->Start(); | 733 kTranslateScriptHeader); |
734 translate_script_request_pending_->Start(); | |
737 } | 735 } |
738 | 736 |
739 void TranslateManager::ShowInfoBar(TabContents* tab, | 737 void TranslateManager::ShowInfoBar(TabContents* tab, |
740 TranslateInfoBarDelegate* infobar) { | 738 TranslateInfoBarDelegate* infobar) { |
741 TranslateInfoBarDelegate* old_infobar = GetTranslateInfoBarDelegate(tab); | 739 TranslateInfoBarDelegate* old_infobar = GetTranslateInfoBarDelegate(tab); |
742 infobar->UpdateBackgroundAnimation(old_infobar); | 740 infobar->UpdateBackgroundAnimation(old_infobar); |
743 TabContentsWrapper* wrapper = | 741 TabContentsWrapper* wrapper = |
744 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 742 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
745 if (old_infobar) { | 743 if (old_infobar) { |
746 // There already is a translate infobar, simply replace it. | 744 // There already is a translate infobar, simply replace it. |
(...skipping 16 matching lines...) Expand all Loading... | |
763 TabContentsWrapper* wrapper = | 761 TabContentsWrapper* wrapper = |
764 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 762 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
765 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { | 763 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { |
766 TranslateInfoBarDelegate* delegate = | 764 TranslateInfoBarDelegate* delegate = |
767 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 765 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
768 if (delegate) | 766 if (delegate) |
769 return delegate; | 767 return delegate; |
770 } | 768 } |
771 return NULL; | 769 return NULL; |
772 } | 770 } |
OLD | NEW |