| 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" |
| 11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
| 12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/autofill/autofill_manager.h" | 15 #include "chrome/browser/autofill/autofill_manager.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/prefs/pref_service.h" | 17 #include "chrome/browser/prefs/pref_service.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/tab_contents/language_state.h" | 19 #include "chrome/browser/tab_contents/language_state.h" |
| 20 #include "chrome/browser/tab_contents/tab_util.h" | 20 #include "chrome/browser/tab_contents/tab_util.h" |
| 21 #include "chrome/browser/tabs/tab_strip_model.h" | 21 #include "chrome/browser/tabs/tab_strip_model.h" |
| 22 #include "chrome/browser/translate/page_translated_details.h" | 22 #include "chrome/browser/translate/page_translated_details.h" |
| 23 #include "chrome/browser/translate/translate_infobar_delegate.h" | 23 #include "chrome/browser/translate/translate_infobar_delegate.h" |
| 24 #include "chrome/browser/translate/translate_tab_helper.h" | 24 #include "chrome/browser/translate/translate_tab_helper.h" |
| 25 #include "chrome/browser/translate/translate_prefs.h" | 25 #include "chrome/browser/translate/translate_prefs.h" |
| 26 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
| 27 #include "chrome/browser/ui/browser_list.h" | 27 #include "chrome/browser/ui/browser_list.h" |
| 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 28 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 29 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
| 30 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
| 31 #include "chrome/common/render_messages.h" | 32 #include "chrome/common/render_messages.h" |
| 32 #include "chrome/common/translate_errors.h" | 33 #include "chrome/common/translate_errors.h" |
| 33 #include "chrome/common/url_constants.h" | 34 #include "chrome/common/url_constants.h" |
| 34 #include "content/browser/renderer_host/render_process_host.h" | 35 #include "content/browser/renderer_host/render_process_host.h" |
| 35 #include "content/browser/renderer_host/render_view_host.h" | 36 #include "content/browser/renderer_host/render_view_host.h" |
| 36 #include "content/browser/tab_contents/navigation_details.h" | 37 #include "content/browser/tab_contents/navigation_details.h" |
| 37 #include "content/browser/tab_contents/navigation_entry.h" | 38 #include "content/browser/tab_contents/navigation_entry.h" |
| 38 #include "content/browser/tab_contents/tab_contents.h" | 39 #include "content/browser/tab_contents/tab_contents.h" |
| 40 #include "content/common/content_notification_types.h" |
| 39 #include "content/common/notification_details.h" | 41 #include "content/common/notification_details.h" |
| 40 #include "content/common/notification_service.h" | 42 #include "content/common/notification_service.h" |
| 41 #include "content/common/notification_source.h" | 43 #include "content/common/notification_source.h" |
| 42 #include "content/common/notification_type.h" | |
| 43 #include "grit/browser_resources.h" | 44 #include "grit/browser_resources.h" |
| 44 #include "net/base/escape.h" | 45 #include "net/base/escape.h" |
| 45 #include "net/url_request/url_request_status.h" | 46 #include "net/url_request/url_request_status.h" |
| 46 #include "ui/base/resource/resource_bundle.h" | 47 #include "ui/base/resource/resource_bundle.h" |
| 47 | 48 |
| 48 namespace { | 49 namespace { |
| 49 | 50 |
| 50 // The list of languages the Google translation server supports. | 51 // The list of languages the Google translation server supports. |
| 51 // For information, here is the list of languages that Chrome can be run in | 52 // For information, here is the list of languages that Chrome can be run in |
| 52 // but that the translation server does not support: | 53 // but that the translation server does not support: |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 240 return chrome_locale; | 241 return chrome_locale; |
| 241 return chrome_locale.substr(0, hypen_index); | 242 return chrome_locale.substr(0, hypen_index); |
| 242 } | 243 } |
| 243 | 244 |
| 244 // static | 245 // static |
| 245 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { | 246 bool TranslateManager::IsSupportedLanguage(const std::string& page_language) { |
| 246 InitSupportedLanguages(); | 247 InitSupportedLanguages(); |
| 247 return supported_languages_.Pointer()->count(page_language) != 0; | 248 return supported_languages_.Pointer()->count(page_language) != 0; |
| 248 } | 249 } |
| 249 | 250 |
| 250 void TranslateManager::Observe(NotificationType type, | 251 void TranslateManager::Observe(int type, |
| 251 const NotificationSource& source, | 252 const NotificationSource& source, |
| 252 const NotificationDetails& details) { | 253 const NotificationDetails& details) { |
| 253 switch (type.value) { | 254 switch (type) { |
| 254 case NotificationType::NAV_ENTRY_COMMITTED: { | 255 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
| 255 NavigationController* controller = | 256 NavigationController* controller = |
| 256 Source<NavigationController>(source).ptr(); | 257 Source<NavigationController>(source).ptr(); |
| 257 content::LoadCommittedDetails* load_details = | 258 content::LoadCommittedDetails* load_details = |
| 258 Details<content::LoadCommittedDetails>(details).ptr(); | 259 Details<content::LoadCommittedDetails>(details).ptr(); |
| 259 NavigationEntry* entry = controller->GetActiveEntry(); | 260 NavigationEntry* entry = controller->GetActiveEntry(); |
| 260 if (!entry) { | 261 if (!entry) { |
| 261 NOTREACHED(); | 262 NOTREACHED(); |
| 262 return; | 263 return; |
| 263 } | 264 } |
| 264 | 265 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 287 // current infobars. Since InitTranslation might add an infobar, it must | 288 // current infobars. Since InitTranslation might add an infobar, it must |
| 288 // be done after that. | 289 // be done after that. |
| 289 MessageLoop::current()->PostTask(FROM_HERE, | 290 MessageLoop::current()->PostTask(FROM_HERE, |
| 290 method_factory_.NewRunnableMethod( | 291 method_factory_.NewRunnableMethod( |
| 291 &TranslateManager::InitiateTranslationPosted, | 292 &TranslateManager::InitiateTranslationPosted, |
| 292 controller->tab_contents()->render_view_host()->process()->id(), | 293 controller->tab_contents()->render_view_host()->process()->id(), |
| 293 controller->tab_contents()->render_view_host()->routing_id(), | 294 controller->tab_contents()->render_view_host()->routing_id(), |
| 294 helper->language_state().original_language())); | 295 helper->language_state().original_language())); |
| 295 break; | 296 break; |
| 296 } | 297 } |
| 297 case NotificationType::TAB_LANGUAGE_DETERMINED: { | 298 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
| 298 TabContents* tab = Source<TabContents>(source).ptr(); | 299 TabContents* tab = Source<TabContents>(source).ptr(); |
| 299 // We may get this notifications multiple times. Make sure to translate | 300 // We may get this notifications multiple times. Make sure to translate |
| 300 // only once. | 301 // only once. |
| 301 TabContentsWrapper* wrapper = | 302 TabContentsWrapper* wrapper = |
| 302 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 303 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 303 LanguageState& language_state = | 304 LanguageState& language_state = |
| 304 wrapper->translate_tab_helper()->language_state(); | 305 wrapper->translate_tab_helper()->language_state(); |
| 305 if (language_state.page_translatable() && | 306 if (language_state.page_translatable() && |
| 306 !language_state.translation_pending() && | 307 !language_state.translation_pending() && |
| 307 !language_state.translation_declined() && | 308 !language_state.translation_declined() && |
| 308 !language_state.IsPageTranslated()) { | 309 !language_state.IsPageTranslated()) { |
| 309 std::string language = *(Details<std::string>(details).ptr()); | 310 std::string language = *(Details<std::string>(details).ptr()); |
| 310 InitiateTranslation(tab, language); | 311 InitiateTranslation(tab, language); |
| 311 } | 312 } |
| 312 break; | 313 break; |
| 313 } | 314 } |
| 314 case NotificationType::PAGE_TRANSLATED: { | 315 case chrome::NOTIFICATION_PAGE_TRANSLATED: { |
| 315 // Only add translate infobar if it doesn't exist; if it already exists, | 316 // Only add translate infobar if it doesn't exist; if it already exists, |
| 316 // just update the state, the actual infobar would have received the same | 317 // just update the state, the actual infobar would have received the same |
| 317 // notification and update the visual display accordingly. | 318 // notification and update the visual display accordingly. |
| 318 TabContents* tab = Source<TabContents>(source).ptr(); | 319 TabContents* tab = Source<TabContents>(source).ptr(); |
| 319 PageTranslatedDetails* page_translated_details = | 320 PageTranslatedDetails* page_translated_details = |
| 320 Details<PageTranslatedDetails>(details).ptr(); | 321 Details<PageTranslatedDetails>(details).ptr(); |
| 321 PageTranslated(tab, page_translated_details); | 322 PageTranslated(tab, page_translated_details); |
| 322 break; | 323 break; |
| 323 } | 324 } |
| 324 case NotificationType::PROFILE_DESTROYED: { | 325 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 325 Profile* profile = Source<Profile>(source).ptr(); | 326 Profile* profile = Source<Profile>(source).ptr(); |
| 326 notification_registrar_.Remove(this, NotificationType::PROFILE_DESTROYED, | 327 notification_registrar_.Remove(this, |
| 328 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 327 source); | 329 source); |
| 328 size_t count = accept_languages_.erase(profile->GetPrefs()); | 330 size_t count = accept_languages_.erase(profile->GetPrefs()); |
| 329 // We should know about this profile since we are listening for | 331 // We should know about this profile since we are listening for |
| 330 // notifications on it. | 332 // notifications on it. |
| 331 DCHECK(count > 0); | 333 DCHECK(count > 0); |
| 332 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); | 334 pref_change_registrar_.Remove(prefs::kAcceptLanguages, this); |
| 333 break; | 335 break; |
| 334 } | 336 } |
| 335 case NotificationType::PREF_CHANGED: { | 337 case chrome::NOTIFICATION_PREF_CHANGED: { |
| 336 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); | 338 DCHECK(*Details<std::string>(details).ptr() == prefs::kAcceptLanguages); |
| 337 PrefService* prefs = Source<PrefService>(source).ptr(); | 339 PrefService* prefs = Source<PrefService>(source).ptr(); |
| 338 InitAcceptLanguages(prefs); | 340 InitAcceptLanguages(prefs); |
| 339 break; | 341 break; |
| 340 } | 342 } |
| 341 default: | 343 default: |
| 342 NOTREACHED(); | 344 NOTREACHED(); |
| 343 } | 345 } |
| 344 } | 346 } |
| 345 | 347 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 415 } | 417 } |
| 416 | 418 |
| 417 // static | 419 // static |
| 418 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { | 420 bool TranslateManager::IsShowingTranslateInfobar(TabContents* tab) { |
| 419 return GetTranslateInfoBarDelegate(tab) != NULL; | 421 return GetTranslateInfoBarDelegate(tab) != NULL; |
| 420 } | 422 } |
| 421 | 423 |
| 422 TranslateManager::TranslateManager() | 424 TranslateManager::TranslateManager() |
| 423 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 425 : ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
| 424 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { | 426 translate_script_expiration_delay_(kTranslateScriptExpirationDelayMS) { |
| 425 notification_registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 427 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 426 NotificationService::AllSources()); | 428 NotificationService::AllSources()); |
| 427 notification_registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, | 429 notification_registrar_.Add(this, |
| 430 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 428 NotificationService::AllSources()); | 431 NotificationService::AllSources()); |
| 429 notification_registrar_.Add(this, NotificationType::PAGE_TRANSLATED, | 432 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, |
| 430 NotificationService::AllSources()); | 433 NotificationService::AllSources()); |
| 431 } | 434 } |
| 432 | 435 |
| 433 void TranslateManager::InitiateTranslation(TabContents* tab, | 436 void TranslateManager::InitiateTranslation(TabContents* tab, |
| 434 const std::string& page_lang) { | 437 const std::string& page_lang) { |
| 435 PrefService* prefs = tab->profile()->GetOriginalProfile()->GetPrefs(); | 438 PrefService* prefs = tab->profile()->GetOriginalProfile()->GetPrefs(); |
| 436 if (!prefs->GetBoolean(prefs::kEnableTranslate)) | 439 if (!prefs->GetBoolean(prefs::kEnableTranslate)) |
| 437 return; | 440 return; |
| 438 | 441 |
| 439 pref_change_registrar_.Init(prefs); | 442 pref_change_registrar_.Init(prefs); |
| (...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 | 649 |
| 647 bool TranslateManager::IsAcceptLanguage(TabContents* tab, | 650 bool TranslateManager::IsAcceptLanguage(TabContents* tab, |
| 648 const std::string& language) { | 651 const std::string& language) { |
| 649 PrefService* pref_service = tab->profile()->GetOriginalProfile()->GetPrefs(); | 652 PrefService* pref_service = tab->profile()->GetOriginalProfile()->GetPrefs(); |
| 650 PrefServiceLanguagesMap::const_iterator iter = | 653 PrefServiceLanguagesMap::const_iterator iter = |
| 651 accept_languages_.find(pref_service); | 654 accept_languages_.find(pref_service); |
| 652 if (iter == accept_languages_.end()) { | 655 if (iter == accept_languages_.end()) { |
| 653 InitAcceptLanguages(pref_service); | 656 InitAcceptLanguages(pref_service); |
| 654 // Listen for this profile going away, in which case we would need to clear | 657 // Listen for this profile going away, in which case we would need to clear |
| 655 // the accepted languages for the profile. | 658 // the accepted languages for the profile. |
| 656 notification_registrar_.Add(this, NotificationType::PROFILE_DESTROYED, | 659 notification_registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 657 Source<Profile>(tab->profile())); | 660 Source<Profile>(tab->profile())); |
| 658 // Also start listening for changes in the accept languages. | 661 // Also start listening for changes in the accept languages. |
| 659 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); | 662 pref_change_registrar_.Add(prefs::kAcceptLanguages, this); |
| 660 | 663 |
| 661 iter = accept_languages_.find(pref_service); | 664 iter = accept_languages_.find(pref_service); |
| 662 } | 665 } |
| 663 | 666 |
| 664 return iter->second.count(language) != 0; | 667 return iter->second.count(language) != 0; |
| 665 } | 668 } |
| 666 | 669 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 761 TabContentsWrapper* wrapper = | 764 TabContentsWrapper* wrapper = |
| 762 TabContentsWrapper::GetCurrentWrapperForContents(tab); | 765 TabContentsWrapper::GetCurrentWrapperForContents(tab); |
| 763 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { | 766 for (size_t i = 0; i < wrapper->infobar_count(); ++i) { |
| 764 TranslateInfoBarDelegate* delegate = | 767 TranslateInfoBarDelegate* delegate = |
| 765 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); | 768 wrapper->GetInfoBarDelegateAt(i)->AsTranslateInfoBarDelegate(); |
| 766 if (delegate) | 769 if (delegate) |
| 767 return delegate; | 770 return delegate; |
| 768 } | 771 } |
| 769 return NULL; | 772 return NULL; |
| 770 } | 773 } |
| OLD | NEW |