Chromium Code Reviews| 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/tab_contents/language_state.h" | 5 #include "chrome/browser/tab_contents/language_state.h" |
| 6 | 6 |
| 7 #include "content/public/browser/navigation_controller.h" | 7 #include "content/public/browser/navigation_controller.h" |
| 8 #include "content/public/browser/navigation_details.h" | 8 #include "content/public/browser/navigation_details.h" |
| 9 #include "content/public/browser/navigation_entry.h" | 9 #include "content/public/browser/navigation_entry.h" |
| 10 #include "content/public/browser/web_contents.h" | |
| 10 | 11 |
| 11 using content::NavigationController; | 12 using content::NavigationController; |
| 12 | 13 |
| 13 LanguageState::LanguageState(NavigationController* nav_controller) | 14 LanguageState::LanguageState(NavigationController* nav_controller) |
| 14 : navigation_controller_(nav_controller), | 15 : navigation_controller_(nav_controller), |
| 15 page_needs_translation_(false), | 16 page_needs_translation_(false), |
| 16 translation_pending_(false), | 17 translation_pending_(false), |
| 17 translation_declined_(false), | 18 translation_declined_(false), |
| 18 in_page_navigation_(false) { | 19 in_page_navigation_(false), |
| 20 translate_enabled_(false), | |
| 21 observer_(NULL) { | |
| 19 } | 22 } |
| 20 | 23 |
| 21 LanguageState::~LanguageState() { | 24 LanguageState::~LanguageState() { |
| 22 } | 25 } |
| 23 | 26 |
| 24 void LanguageState::DidNavigate( | 27 void LanguageState::DidNavigate( |
| 25 const content::LoadCommittedDetails& details) { | 28 const content::LoadCommittedDetails& details) { |
| 26 in_page_navigation_ = details.is_in_page; | 29 in_page_navigation_ = details.is_in_page; |
| 27 if (in_page_navigation_ || !details.is_main_frame) | 30 if (in_page_navigation_ || !details.is_main_frame) |
| 28 return; // Don't reset our states, the page has not changed. | 31 return; // Don't reset our states, the page has not changed. |
| 29 | 32 |
| 30 bool reload = | 33 bool reload = |
| 31 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD || | 34 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD || |
| 32 details.type == content::NAVIGATION_TYPE_SAME_PAGE; | 35 details.type == content::NAVIGATION_TYPE_SAME_PAGE; |
| 33 if (reload) { | 36 if (reload) { |
| 34 // We might not get a LanguageDetermined notifications on reloads. Make sure | 37 // We might not get a LanguageDetermined notifications on reloads. Make sure |
| 35 // to keep the original language and to set current_lang_ so | 38 // to keep the original language and to set current_lang_ so |
| 36 // IsPageTranslated() returns false. | 39 // IsPageTranslated() returns false. |
| 37 current_lang_ = original_lang_; | 40 current_lang_ = original_lang_; |
| 38 } else { | 41 } else { |
| 39 prev_original_lang_ = original_lang_; | 42 prev_original_lang_ = original_lang_; |
| 40 prev_current_lang_ = current_lang_; | 43 prev_current_lang_ = current_lang_; |
| 41 original_lang_.clear(); | 44 original_lang_.clear(); |
| 42 current_lang_.clear(); | 45 current_lang_.clear(); |
| 43 } | 46 } |
| 44 | 47 |
| 45 translation_pending_ = false; | 48 translation_pending_ = false; |
| 46 translation_declined_ = false; | 49 translation_declined_ = false; |
| 50 | |
| 51 SetTranslateEnabled(false); | |
|
sky
2013/10/18 14:31:10
It's unusual that this property implicitly changes
hajimehoshi
2013/10/22 10:30:25
This is necessary to hide the translate icon on th
| |
| 47 } | 52 } |
| 48 | 53 |
| 49 void LanguageState::LanguageDetermined(const std::string& page_language, | 54 void LanguageState::LanguageDetermined(const std::string& page_language, |
| 50 bool page_needs_translation) { | 55 bool page_needs_translation) { |
| 51 if (in_page_navigation_ && !original_lang_.empty()) { | 56 if (in_page_navigation_ && !original_lang_.empty()) { |
| 52 // In-page navigation, we don't expect our states to change. | 57 // In-page navigation, we don't expect our states to change. |
| 53 // Note that we'll set the languages if original_lang_ is empty. This might | 58 // Note that we'll set the languages if original_lang_ is empty. This might |
| 54 // happen if the we did not get called on the top-page. | 59 // happen if the we did not get called on the top-page. |
| 55 return; | 60 return; |
| 56 } | 61 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 77 | 82 |
| 78 std::string LanguageState::AutoTranslateTo() const { | 83 std::string LanguageState::AutoTranslateTo() const { |
| 79 if (InTranslateNavigation() && | 84 if (InTranslateNavigation() && |
| 80 // The page is not yet translated. | 85 // The page is not yet translated. |
| 81 original_lang_ == current_lang_ ) { | 86 original_lang_ == current_lang_ ) { |
| 82 return prev_current_lang_; | 87 return prev_current_lang_; |
| 83 } | 88 } |
| 84 | 89 |
| 85 return std::string(); | 90 return std::string(); |
| 86 } | 91 } |
| 92 | |
| 93 void LanguageState::SetTranslateEnabled(bool value) { | |
| 94 if (translate_enabled_ == value) | |
| 95 return; | |
| 96 | |
| 97 translate_enabled_ = value; | |
| 98 if (observer_) { | |
| 99 content::WebContents* web_contents = | |
| 100 navigation_controller_->GetWebContents(); | |
| 101 observer_->OnTranslateEnabledChanged(web_contents); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 bool LanguageState::IsLanguageChanged() const { | |
| 106 return original_lang_ != prev_original_lang_; | |
| 107 } | |
| OLD | NEW |