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.
4
5 #include "components/translate/core/browser/language_state.h"
6
7 #include "base/logging.h"
8 #include "components/translate/core/browser/translate_driver.h"
9
10 namespace translate {
11
LanguageState(TranslateDriver * driver)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_);
21 }
22
~LanguageState()23 LanguageState::~LanguageState() {
24 }
25
DidNavigate(bool in_page_navigation,bool is_main_frame,bool reload)26 void LanguageState::DidNavigate(bool in_page_navigation,
27 bool is_main_frame,
28 bool reload) {
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.
32
33 if (reload) {
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_;
38 } else {
39 prev_original_lang_ = original_lang_;
40 prev_current_lang_ = current_lang_;
41 original_lang_.clear();
42 current_lang_.clear();
43 }
44
45 SetIsPageTranslated(false);
46
47 translation_pending_ = false;
48 translation_declined_ = false;
49
50 SetTranslateEnabled(false);
51 }
52
LanguageDetermined(const std::string & page_language,bool page_needs_translation)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.
59 return;
60 }
61 page_needs_translation_ = page_needs_translation;
62 original_lang_ = page_language;
63 current_lang_ = page_language;
64 SetIsPageTranslated(false);
65 }
66
InTranslateNavigation() const67 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.
73 return
74 !translation_pending_ &&
75 prev_original_lang_ == original_lang_ &&
76 prev_original_lang_ != prev_current_lang_ &&
77 translate_driver_->IsLinkNavigation();
78 }
79
SetCurrentLanguage(const std::string & language)80 void LanguageState::SetCurrentLanguage(const std::string& language) {
81 current_lang_ = language;
82 SetIsPageTranslated(current_lang_ != original_lang_);
83 }
84
AutoTranslateTo() const85 std::string LanguageState::AutoTranslateTo() const {
86 if (InTranslateNavigation() && !is_page_translated_)
87 return prev_current_lang_;
88
89 return std::string();
90 }
91
SetTranslateEnabled(bool value)92 void LanguageState::SetTranslateEnabled(bool value) {
93 if (translate_enabled_ == value)
94 return;
95
96 translate_enabled_ = value;
97 translate_driver_->OnTranslateEnabledChanged();
98 }
99
HasLanguageChanged() const100 bool LanguageState::HasLanguageChanged() const {
101 return original_lang_ != prev_original_lang_;
102 }
103
SetIsPageTranslated(bool value)104 void LanguageState::SetIsPageTranslated(bool value) {
105 if (is_page_translated_ == value)
106 return;
107
108 is_page_translated_ = value;
109 translate_driver_->OnIsPageTranslatedChanged();
110
111 // With the translation done, the translate feature must be enabled.
112 if (is_page_translated_)
113 SetTranslateEnabled(true);
114 }
115
116 } // namespace translate
117