Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Side by Side Diff: chrome/browser/translate/translate_tab_helper.cc

Issue 290573013: LanguageState should be owned by TranslateManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Inlining the DidNavigate function Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2011 The Chromium Authors. All rights reserved. 1 // Copyright 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_tab_helper.h" 5 #include "chrome/browser/translate/translate_tab_helper.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h" 10 #include "base/prefs/pref_service.h"
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 : content::WebContentsObserver(web_contents), 73 : content::WebContentsObserver(web_contents),
74 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), 74 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
75 translate_driver_(&web_contents->GetController()), 75 translate_driver_(&web_contents->GetController()),
76 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)), 76 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)),
77 weak_pointer_factory_(this) {} 77 weak_pointer_factory_(this) {}
78 78
79 TranslateTabHelper::~TranslateTabHelper() { 79 TranslateTabHelper::~TranslateTabHelper() {
80 } 80 }
81 81
82 LanguageState& TranslateTabHelper::GetLanguageState() { 82 LanguageState& TranslateTabHelper::GetLanguageState() {
83 return translate_driver_.GetLanguageState(); 83 return translate_manager_->GetLanguageState();
84 } 84 }
85 85
86 // static 86 // static
87 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs( 87 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs(
88 PrefService* prefs) { 88 PrefService* prefs) {
89 #if defined(OS_CHROMEOS) 89 #if defined(OS_CHROMEOS)
90 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; 90 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages;
91 #else 91 #else
92 const char* preferred_languages_prefs = NULL; 92 const char* preferred_languages_prefs = NULL;
93 #endif 93 #endif
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 264 }
265 265
266 // If the navigation happened while offline don't show the translate 266 // If the navigation happened while offline don't show the translate
267 // bar since there will be nothing to translate. 267 // bar since there will be nothing to translate.
268 if (load_details.http_status_code == 0 || 268 if (load_details.http_status_code == 0 ||
269 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { 269 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
270 return; 270 return;
271 } 271 }
272 272
273 if (!load_details.is_main_frame && 273 if (!load_details.is_main_frame &&
274 translate_driver_.GetLanguageState().translation_declined()) { 274 GetLanguageState().translation_declined()) {
275 // Some sites (such as Google map) may trigger sub-frame navigations 275 // Some sites (such as Google map) may trigger sub-frame navigations
276 // when the user interacts with the page. We don't want to show a new 276 // when the user interacts with the page. We don't want to show a new
277 // infobar if the user already dismissed one in that case. 277 // infobar if the user already dismissed one in that case.
278 return; 278 return;
279 } 279 }
280 280
281 // If not a reload, return. 281 // If not a reload, return.
282 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && 282 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD &&
283 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) { 283 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
284 return; 284 return;
285 } 285 }
286 286
287 if (!translate_driver_.GetLanguageState().page_needs_translation()) 287 if (!GetLanguageState().page_needs_translation())
288 return; 288 return;
289 289
290 // Note that we delay it as the ordering of the processing of this callback 290 // Note that we delay it as the ordering of the processing of this callback
291 // by WebContentsObservers is undefined and might result in the current 291 // by WebContentsObservers is undefined and might result in the current
292 // infobars being removed. Since the translation initiation process might add 292 // infobars being removed. Since the translation initiation process might add
293 // an infobar, it must be done after that. 293 // an infobar, it must be done after that.
294 base::MessageLoop::current()->PostTask( 294 base::MessageLoop::current()->PostTask(
295 FROM_HERE, 295 FROM_HERE,
296 base::Bind(&TranslateTabHelper::InitiateTranslation, 296 base::Bind(&TranslateTabHelper::InitiateTranslation,
297 weak_pointer_factory_.GetWeakPtr(), 297 weak_pointer_factory_.GetWeakPtr(),
298 translate_driver_.GetLanguageState().original_language(), 298 GetLanguageState().original_language(),
299 0)); 299 0));
300 } 300 }
301 301
302 void TranslateTabHelper::DidNavigateAnyFrame( 302 void TranslateTabHelper::DidNavigateAnyFrame(
303 const content::LoadCommittedDetails& details, 303 const content::LoadCommittedDetails& details,
304 const content::FrameNavigateParams& params) { 304 const content::FrameNavigateParams& params) {
305 // Let the LanguageState clear its state. 305 // Let the LanguageState clear its state.
306 translate_driver_.DidNavigate(details); 306 const bool reload =
307 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD ||
308 details.type == content::NAVIGATION_TYPE_SAME_PAGE;
309 GetLanguageState().DidNavigate(
310 details.is_in_page, details.is_main_frame, reload);
307 } 311 }
308 312
309 void TranslateTabHelper::WebContentsDestroyed() { 313 void TranslateTabHelper::WebContentsDestroyed() {
310 // Translation process can be interrupted. 314 // Translation process can be interrupted.
311 // Destroying the TranslateManager now guarantees that it never has to deal 315 // Destroying the TranslateManager now guarantees that it never has to deal
312 // with NULL WebContents. 316 // with NULL WebContents.
313 translate_manager_.reset(); 317 translate_manager_.reset();
314 } 318 }
315 319
316 #if defined(CLD2_DYNAMIC_MODE) 320 #if defined(CLD2_DYNAMIC_MODE)
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 s_cached_file_ = file.release(); 445 s_cached_file_ = file.release();
442 s_cached_data_offset_ = data_offset; 446 s_cached_data_offset_ = data_offset;
443 s_cached_data_length_ = data_length; 447 s_cached_data_length_ = data_length;
444 } 448 }
445 } 449 }
446 } 450 }
447 #endif // defined(CLD2_DYNAMIC_MODE) 451 #endif // defined(CLD2_DYNAMIC_MODE)
448 452
449 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang, 453 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang,
450 int attempt) { 454 int attempt) {
451 if (translate_driver_.GetLanguageState().translation_pending()) 455 if (GetLanguageState().translation_pending())
452 return; 456 return;
453 457
454 // During a reload we need web content to be available before the 458 // During a reload we need web content to be available before the
455 // translate script is executed. Otherwise we will run the translate script on 459 // translate script is executed. Otherwise we will run the translate script on
456 // an empty DOM which will fail. Therefore we wait a bit to see if the page 460 // an empty DOM which will fail. Therefore we wait a bit to see if the page
457 // has finished. 461 // has finished.
458 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { 462 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
459 int backoff = attempt * kMaxTranslateLoadCheckAttempts; 463 int backoff = attempt * kMaxTranslateLoadCheckAttempts;
460 base::MessageLoop::current()->PostDelayedTask( 464 base::MessageLoop::current()->PostDelayedTask(
461 FROM_HERE, 465 FROM_HERE,
462 base::Bind(&TranslateTabHelper::InitiateTranslation, 466 base::Bind(&TranslateTabHelper::InitiateTranslation,
463 weak_pointer_factory_.GetWeakPtr(), 467 weak_pointer_factory_.GetWeakPtr(),
464 page_lang, 468 page_lang,
465 ++attempt), 469 ++attempt),
466 base::TimeDelta::FromMilliseconds(backoff)); 470 base::TimeDelta::FromMilliseconds(backoff));
467 return; 471 return;
468 } 472 }
469 473
470 translate_manager_->InitiateTranslation( 474 translate_manager_->InitiateTranslation(
471 TranslateDownloadManager::GetLanguageCode(page_lang)); 475 TranslateDownloadManager::GetLanguageCode(page_lang));
472 } 476 }
473 477
474 void TranslateTabHelper::OnLanguageDetermined( 478 void TranslateTabHelper::OnLanguageDetermined(
475 const LanguageDetectionDetails& details, 479 const LanguageDetectionDetails& details,
476 bool page_needs_translation) { 480 bool page_needs_translation) {
477 translate_driver_.GetLanguageState().LanguageDetermined( 481 GetLanguageState().LanguageDetermined(
478 details.adopted_language, page_needs_translation); 482 details.adopted_language, page_needs_translation);
479 483
480 if (web_contents()) 484 if (web_contents())
481 translate_manager_->InitiateTranslation(details.adopted_language); 485 translate_manager_->InitiateTranslation(details.adopted_language);
482 486
483 content::NotificationService::current()->Notify( 487 content::NotificationService::current()->Notify(
484 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 488 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
485 content::Source<content::WebContents>(web_contents()), 489 content::Source<content::WebContents>(web_contents()),
486 content::Details<const LanguageDetectionDetails>(&details)); 490 content::Details<const LanguageDetectionDetails>(&details));
487 } 491 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 if (GetLanguageState().InTranslateNavigation()) 544 if (GetLanguageState().InTranslateNavigation())
541 return; 545 return;
542 } 546 }
543 547
544 TranslateBubbleFactory::Show( 548 TranslateBubbleFactory::Show(
545 browser->window(), web_contents(), step, error_type); 549 browser->window(), web_contents(), step, error_type);
546 #else 550 #else
547 NOTREACHED(); 551 NOTREACHED();
548 #endif 552 #endif
549 } 553 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698