1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/translate/core/browser/language_state.h"
7 #include "base/logging.h"
8 #include "components/translate/core/browser/translate_driver.h"
12 LanguageState::LanguageState(TranslateDriver
* driver
)
13 : is_page_translated_(false),
14 translate_driver_(driver
),
15 page_needs_translation_(false),
16 translation_pending_(false),
17 translation_declined_(false),
18 in_page_navigation_(false),
19 translate_enabled_(false) {
20 DCHECK(translate_driver_
);
23 LanguageState::~LanguageState() {
26 void LanguageState::DidNavigate(bool in_page_navigation
,
29 in_page_navigation_
= in_page_navigation
;
30 if (in_page_navigation_
|| !is_main_frame
)
31 return; // Don't reset our states, the page has not changed.
34 // We might not get a LanguageDetermined notifications on reloads. Make sure
35 // to keep the original language and to set current_lang_ so
36 // IsPageTranslated() returns false.
37 current_lang_
= original_lang_
;
39 prev_original_lang_
= original_lang_
;
40 prev_current_lang_
= current_lang_
;
41 original_lang_
.clear();
42 current_lang_
.clear();
45 SetIsPageTranslated(false);
47 translation_pending_
= false;
48 translation_declined_
= false;
50 SetTranslateEnabled(false);
53 void LanguageState::LanguageDetermined(const std::string
& page_language
,
54 bool page_needs_translation
) {
55 if (in_page_navigation_
&& !original_lang_
.empty()) {
56 // In-page navigation, we don't expect our states to change.
57 // Note that we'll set the languages if original_lang_ is empty. This might
58 // happen if the we did not get called on the top-page.
61 page_needs_translation_
= page_needs_translation
;
62 original_lang_
= page_language
;
63 current_lang_
= page_language
;
64 SetIsPageTranslated(false);
67 bool LanguageState::InTranslateNavigation() const {
68 // The user is in the same translate session if
69 // - no translation is pending
70 // - this page is in the same language as the previous page
71 // - the previous page had been translated
72 // - the new page was navigated through a link.
74 !translation_pending_
&&
75 prev_original_lang_
== original_lang_
&&
76 prev_original_lang_
!= prev_current_lang_
&&
77 translate_driver_
->IsLinkNavigation();
80 void LanguageState::SetCurrentLanguage(const std::string
& language
) {
81 current_lang_
= language
;
82 SetIsPageTranslated(current_lang_
!= original_lang_
);
85 std::string
LanguageState::AutoTranslateTo() const {
86 if (InTranslateNavigation() && !is_page_translated_
)
87 return prev_current_lang_
;
92 void LanguageState::SetTranslateEnabled(bool value
) {
93 if (translate_enabled_
== value
)
96 translate_enabled_
= value
;
97 translate_driver_
->OnTranslateEnabledChanged();
100 bool LanguageState::HasLanguageChanged() const {
101 return original_lang_
!= prev_original_lang_
;
104 void LanguageState::SetIsPageTranslated(bool value
) {
105 if (is_page_translated_
== value
)
108 is_page_translated_
= value
;
109 translate_driver_
->OnIsPageTranslatedChanged();
111 // With the translation done, the translate feature must be enabled.
112 if (is_page_translated_
)
113 SetTranslateEnabled(true);
116 } // namespace translate