cros: Remove default pinned apps trial.
[chromium-blink-merge.git] / chrome / browser / tab_contents / language_state.cc
blob90296a8506d7b45186e0b23fa41065548207668e
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
3 // found in the LICENSE file.
5 #include "chrome/browser/tab_contents/language_state.h"
7 #include "chrome/browser/tab_contents/language_state_observer.h"
8 #include "content/public/browser/navigation_controller.h"
9 #include "content/public/browser/navigation_details.h"
10 #include "content/public/browser/navigation_entry.h"
11 #include "content/public/browser/web_contents.h"
13 using content::NavigationController;
15 LanguageState::LanguageState(NavigationController* nav_controller)
16 : is_page_translated_(false),
17 navigation_controller_(nav_controller),
18 page_needs_translation_(false),
19 translation_pending_(false),
20 translation_declined_(false),
21 in_page_navigation_(false),
22 translate_enabled_(false),
23 observer_(NULL) {
26 LanguageState::~LanguageState() {
29 void LanguageState::DidNavigate(
30 const content::LoadCommittedDetails& details) {
31 in_page_navigation_ = details.is_in_page;
32 if (in_page_navigation_ || !details.is_main_frame)
33 return; // Don't reset our states, the page has not changed.
35 bool reload =
36 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD ||
37 details.type == content::NAVIGATION_TYPE_SAME_PAGE;
38 if (reload) {
39 // We might not get a LanguageDetermined notifications on reloads. Make sure
40 // to keep the original language and to set current_lang_ so
41 // IsPageTranslated() returns false.
42 current_lang_ = original_lang_;
43 } else {
44 prev_original_lang_ = original_lang_;
45 prev_current_lang_ = current_lang_;
46 original_lang_.clear();
47 current_lang_.clear();
50 SetIsPageTranslated(false);
52 translation_pending_ = false;
53 translation_declined_ = false;
55 SetTranslateEnabled(false);
58 void LanguageState::LanguageDetermined(const std::string& page_language,
59 bool page_needs_translation) {
60 if (in_page_navigation_ && !original_lang_.empty()) {
61 // In-page navigation, we don't expect our states to change.
62 // Note that we'll set the languages if original_lang_ is empty. This might
63 // happen if the we did not get called on the top-page.
64 return;
66 page_needs_translation_ = page_needs_translation;
67 original_lang_ = page_language;
68 current_lang_ = page_language;
69 SetIsPageTranslated(false);
72 bool LanguageState::InTranslateNavigation() const {
73 // The user is in the same translate session if
74 // - no translation is pending
75 // - this page is in the same language as the previous page
76 // - the previous page had been translated
77 // - the new page was navigated through a link.
78 return
79 !translation_pending_ &&
80 prev_original_lang_ == original_lang_ &&
81 prev_original_lang_ != prev_current_lang_ &&
82 navigation_controller_ &&
83 navigation_controller_->GetActiveEntry() &&
84 navigation_controller_->GetActiveEntry()->GetTransitionType() ==
85 content::PAGE_TRANSITION_LINK;
88 void LanguageState::SetCurrentLanguage(const std::string& language) {
89 current_lang_ = language;
90 SetIsPageTranslated(current_lang_ != original_lang_);
93 std::string LanguageState::AutoTranslateTo() const {
94 if (InTranslateNavigation() && !is_page_translated_)
95 return prev_current_lang_;
97 return std::string();
100 void LanguageState::SetTranslateEnabled(bool value) {
101 if (translate_enabled_ == value)
102 return;
104 translate_enabled_ = value;
105 if (observer_) {
106 content::WebContents* web_contents =
107 navigation_controller_->GetWebContents();
108 observer_->OnTranslateEnabledChanged(web_contents);
112 bool LanguageState::HasLanguageChanged() const {
113 return original_lang_ != prev_original_lang_;
116 void LanguageState::SetIsPageTranslated(bool value) {
117 if (is_page_translated_ == value)
118 return;
120 is_page_translated_ = value;
121 if (observer_) {
122 content::WebContents* web_contents =
123 navigation_controller_->GetWebContents();
124 observer_->OnIsPageTranslatedChanged(web_contents);
127 // With the translation done, the translate feature must be enabled.
128 if (is_page_translated_)
129 SetTranslateEnabled(true);