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

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: Deleting unwanted forward references 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"
11 #include "base/strings/string_split.h" 11 #include "base/strings/string_split.h"
12 #include "chrome/browser/chrome_notification_types.h" 12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/infobars/infobar_service.h" 13 #include "chrome/browser/infobars/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/translate/translate_accept_languages_factory.h" 15 #include "chrome/browser/translate/translate_accept_languages_factory.h"
16 #include "chrome/browser/translate/translate_infobar_delegate.h" 16 #include "chrome/browser/translate/translate_infobar_delegate.h"
17 #include "chrome/browser/translate/translate_service.h" 17 #include "chrome/browser/translate/translate_service.h"
18 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_finder.h" 19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/browser_tabstrip.h" 20 #include "chrome/browser/ui/browser_tabstrip.h"
21 #include "chrome/browser/ui/browser_window.h" 21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/browser/ui/tabs/tab_strip_model.h"
23 #include "chrome/browser/ui/translate/translate_bubble_factory.h" 23 #include "chrome/browser/ui/translate/translate_bubble_factory.h"
24 #include "chrome/common/pref_names.h" 24 #include "chrome/common/pref_names.h"
25 #include "components/translate/content/common/translate_messages.h" 25 #include "components/translate/content/common/translate_messages.h"
26 #include "components/translate/core/browser/language_state.h"
26 #include "components/translate/core/browser/page_translated_details.h" 27 #include "components/translate/core/browser/page_translated_details.h"
27 #include "components/translate/core/browser/translate_accept_languages.h" 28 #include "components/translate/core/browser/translate_accept_languages.h"
28 #include "components/translate/core/browser/translate_download_manager.h" 29 #include "components/translate/core/browser/translate_download_manager.h"
29 #include "components/translate/core/browser/translate_manager.h" 30 #include "components/translate/core/browser/translate_manager.h"
30 #include "components/translate/core/browser/translate_prefs.h" 31 #include "components/translate/core/browser/translate_prefs.h"
31 #include "components/translate/core/common/language_detection_details.h" 32 #include "components/translate/core/common/language_detection_details.h"
32 #include "content/public/browser/navigation_details.h" 33 #include "content/public/browser/navigation_details.h"
33 #include "content/public/browser/navigation_entry.h" 34 #include "content/public/browser/navigation_entry.h"
34 #include "content/public/browser/notification_service.h" 35 #include "content/public/browser/notification_service.h"
35 #include "content/public/browser/render_view_host.h" 36 #include "content/public/browser/render_view_host.h"
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 : content::WebContentsObserver(web_contents), 74 : content::WebContentsObserver(web_contents),
74 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), 75 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
75 translate_driver_(&web_contents->GetController()), 76 translate_driver_(&web_contents->GetController()),
76 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)), 77 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)),
77 weak_pointer_factory_(this) {} 78 weak_pointer_factory_(this) {}
78 79
79 TranslateTabHelper::~TranslateTabHelper() { 80 TranslateTabHelper::~TranslateTabHelper() {
80 } 81 }
81 82
82 LanguageState& TranslateTabHelper::GetLanguageState() { 83 LanguageState& TranslateTabHelper::GetLanguageState() {
83 return translate_driver_.GetLanguageState(); 84 return translate_manager_->GetLanguageState();
84 } 85 }
85 86
86 // static 87 // static
87 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs( 88 scoped_ptr<TranslatePrefs> TranslateTabHelper::CreateTranslatePrefs(
88 PrefService* prefs) { 89 PrefService* prefs) {
89 #if defined(OS_CHROMEOS) 90 #if defined(OS_CHROMEOS)
90 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; 91 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages;
91 #else 92 #else
92 const char* preferred_languages_prefs = NULL; 93 const char* preferred_languages_prefs = NULL;
93 #endif 94 #endif
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 } 265 }
265 266
266 // If the navigation happened while offline don't show the translate 267 // If the navigation happened while offline don't show the translate
267 // bar since there will be nothing to translate. 268 // bar since there will be nothing to translate.
268 if (load_details.http_status_code == 0 || 269 if (load_details.http_status_code == 0 ||
269 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { 270 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
270 return; 271 return;
271 } 272 }
272 273
273 if (!load_details.is_main_frame && 274 if (!load_details.is_main_frame &&
274 translate_driver_.GetLanguageState().translation_declined()) { 275 GetLanguageState().translation_declined()) {
275 // Some sites (such as Google map) may trigger sub-frame navigations 276 // 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 277 // 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. 278 // infobar if the user already dismissed one in that case.
278 return; 279 return;
279 } 280 }
280 281
281 // If not a reload, return. 282 // If not a reload, return.
282 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && 283 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD &&
283 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) { 284 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
284 return; 285 return;
285 } 286 }
286 287
287 if (!translate_driver_.GetLanguageState().page_needs_translation()) 288 if (!GetLanguageState().page_needs_translation())
288 return; 289 return;
289 290
290 // Note that we delay it as the ordering of the processing of this callback 291 // 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 292 // by WebContentsObservers is undefined and might result in the current
292 // infobars being removed. Since the translation initiation process might add 293 // infobars being removed. Since the translation initiation process might add
293 // an infobar, it must be done after that. 294 // an infobar, it must be done after that.
294 base::MessageLoop::current()->PostTask( 295 base::MessageLoop::current()->PostTask(
295 FROM_HERE, 296 FROM_HERE,
296 base::Bind(&TranslateTabHelper::InitiateTranslation, 297 base::Bind(&TranslateTabHelper::InitiateTranslation,
297 weak_pointer_factory_.GetWeakPtr(), 298 weak_pointer_factory_.GetWeakPtr(),
298 translate_driver_.GetLanguageState().original_language(), 299 GetLanguageState().original_language(),
299 0)); 300 0));
300 } 301 }
301 302
302 void TranslateTabHelper::DidNavigateAnyFrame( 303 void TranslateTabHelper::DidNavigateAnyFrame(
303 const content::LoadCommittedDetails& details, 304 const content::LoadCommittedDetails& details,
304 const content::FrameNavigateParams& params) { 305 const content::FrameNavigateParams& params) {
305 // Let the LanguageState clear its state. 306 // Let the LanguageState clear its state.
306 translate_driver_.DidNavigate(details); 307 const bool reload =
308 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD ||
309 details.type == content::NAVIGATION_TYPE_SAME_PAGE;
310 GetLanguageState().DidNavigate(
311 details.is_in_page, details.is_main_frame, reload);
307 } 312 }
308 313
309 void TranslateTabHelper::WebContentsDestroyed() { 314 void TranslateTabHelper::WebContentsDestroyed() {
310 // Translation process can be interrupted. 315 // Translation process can be interrupted.
311 // Destroying the TranslateManager now guarantees that it never has to deal 316 // Destroying the TranslateManager now guarantees that it never has to deal
312 // with NULL WebContents. 317 // with NULL WebContents.
313 translate_manager_.reset(); 318 translate_manager_.reset();
314 } 319 }
315 320
316 #if defined(CLD2_DYNAMIC_MODE) 321 #if defined(CLD2_DYNAMIC_MODE)
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 s_cached_file_ = file.release(); 446 s_cached_file_ = file.release();
442 s_cached_data_offset_ = data_offset; 447 s_cached_data_offset_ = data_offset;
443 s_cached_data_length_ = data_length; 448 s_cached_data_length_ = data_length;
444 } 449 }
445 } 450 }
446 } 451 }
447 #endif // defined(CLD2_DYNAMIC_MODE) 452 #endif // defined(CLD2_DYNAMIC_MODE)
448 453
449 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang, 454 void TranslateTabHelper::InitiateTranslation(const std::string& page_lang,
450 int attempt) { 455 int attempt) {
451 if (translate_driver_.GetLanguageState().translation_pending()) 456 if (GetLanguageState().translation_pending())
452 return; 457 return;
453 458
454 // During a reload we need web content to be available before the 459 // 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 460 // 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 461 // an empty DOM which will fail. Therefore we wait a bit to see if the page
457 // has finished. 462 // has finished.
458 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { 463 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
459 int backoff = attempt * kMaxTranslateLoadCheckAttempts; 464 int backoff = attempt * kMaxTranslateLoadCheckAttempts;
460 base::MessageLoop::current()->PostDelayedTask( 465 base::MessageLoop::current()->PostDelayedTask(
461 FROM_HERE, 466 FROM_HERE,
462 base::Bind(&TranslateTabHelper::InitiateTranslation, 467 base::Bind(&TranslateTabHelper::InitiateTranslation,
463 weak_pointer_factory_.GetWeakPtr(), 468 weak_pointer_factory_.GetWeakPtr(),
464 page_lang, 469 page_lang,
465 ++attempt), 470 ++attempt),
466 base::TimeDelta::FromMilliseconds(backoff)); 471 base::TimeDelta::FromMilliseconds(backoff));
467 return; 472 return;
468 } 473 }
469 474
470 translate_manager_->InitiateTranslation( 475 translate_manager_->InitiateTranslation(
471 TranslateDownloadManager::GetLanguageCode(page_lang)); 476 TranslateDownloadManager::GetLanguageCode(page_lang));
472 } 477 }
473 478
474 void TranslateTabHelper::OnLanguageDetermined( 479 void TranslateTabHelper::OnLanguageDetermined(
475 const LanguageDetectionDetails& details, 480 const LanguageDetectionDetails& details,
476 bool page_needs_translation) { 481 bool page_needs_translation) {
477 translate_driver_.GetLanguageState().LanguageDetermined( 482 GetLanguageState().LanguageDetermined(
478 details.adopted_language, page_needs_translation); 483 details.adopted_language, page_needs_translation);
479 484
480 if (web_contents()) 485 if (web_contents())
481 translate_manager_->InitiateTranslation(details.adopted_language); 486 translate_manager_->InitiateTranslation(details.adopted_language);
482 487
483 content::NotificationService::current()->Notify( 488 content::NotificationService::current()->Notify(
484 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, 489 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED,
485 content::Source<content::WebContents>(web_contents()), 490 content::Source<content::WebContents>(web_contents()),
486 content::Details<const LanguageDetectionDetails>(&details)); 491 content::Details<const LanguageDetectionDetails>(&details));
487 } 492 }
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
540 if (GetLanguageState().InTranslateNavigation()) 545 if (GetLanguageState().InTranslateNavigation())
541 return; 546 return;
542 } 547 }
543 548
544 TranslateBubbleFactory::Show( 549 TranslateBubbleFactory::Show(
545 browser->window(), web_contents(), step, error_type); 550 browser->window(), web_contents(), step, error_type);
546 #else 551 #else
547 NOTREACHED(); 552 NOTREACHED();
548 #endif 553 #endif
549 } 554 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698