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. 4 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_LANGUAGE_STATE_H_ 6 #define CHROME_BROWSER_TAB_CONTENTS_LANGUAGE_STATE_H_ 7 #pragma once 8 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "content/browser/tab_contents/navigation_controller.h" 13 14 // This class holds the language state of the current page. 15 // There is one LanguageState instance per TabContents. 16 // It is used to determine when navigating to a new page whether it should 17 // automatically be translated. 18 // This auto-translate behavior is the expected behavior when: 19 // - user is on page in language A that they had translated to language B. 20 // - user clicks a link in that page that takes them to a page also in language 21 // A. 22 23 class LanguageState { 24 public: 25 explicit LanguageState(NavigationController* nav_controller); 26 ~LanguageState(); 27 28 // Should be called when the page did a new navigation (whether it is a main 29 // frame or sub-frame navigation). 30 void DidNavigate(const NavigationController::LoadCommittedDetails& details); 31 32 // Should be called when the language of the page has been determined. 33 // |page_translatable| when false indicates that the browser should not offer 34 // to translate the page. 35 void LanguageDetermined(const std::string& page_language, 36 bool page_translatable); 37 38 // Returns the language the current page should be translated to, based on the 39 // previous page languages and the transition. This should be called after 40 // the language page has been determined. 41 // Returns an empty string if the page should not be auto-translated. 42 std::string AutoTranslateTo() const; 43 44 // Returns true if the current page in the associated tab has been translated. IsPageTranslated()45 bool IsPageTranslated() const { return original_lang_ != current_lang_; } 46 original_language()47 const std::string& original_language() const { return original_lang_; } 48 set_current_language(const std::string & language)49 void set_current_language(const std::string& language) { 50 current_lang_ = language; 51 } current_language()52 const std::string& current_language() const { return current_lang_; } 53 page_translatable()54 bool page_translatable() const { return page_translatable_; } 55 56 // Whether the page is currently in the process of being translated. translation_pending()57 bool translation_pending() const { return translation_pending_; } set_translation_pending(bool value)58 void set_translation_pending(bool value) { translation_pending_ = value; } 59 60 // Whether the user has already declined to translate the page. translation_declined()61 bool translation_declined() const { return translation_declined_; } set_translation_declined(bool value)62 void set_translation_declined(bool value) { translation_declined_ = value; } 63 64 // Whether the current page was navigated through an in-page (fragment) 65 // navigation. in_page_navigation()66 bool in_page_navigation() const { return in_page_navigation_; } 67 68 private: 69 // The languages this page is in. Note that current_lang_ is different from 70 // original_lang_ when the page has been translated. 71 // Note that these might be empty if the page language has not been determined 72 // yet. 73 std::string original_lang_; 74 std::string current_lang_; 75 76 // Same as above but for the previous page. 77 std::string prev_original_lang_; 78 std::string prev_current_lang_; 79 80 // The navigation controller of the tab we are associated with. 81 NavigationController* navigation_controller_; 82 83 // Whether it is OK to offer to translate the page. Some pages explictly 84 // specify that they should not be translated by the browser (this is the case 85 // for GMail for example, which provides its own translation features). 86 bool page_translatable_; 87 88 // Whether a translation is currently pending (TabContents waiting for the 89 // PAGE_TRANSLATED notification). This is needed to avoid sending duplicate 90 // translate requests to a page. TranslateManager initiates translations 91 // when it received the LANGUAGE_DETERMINED notification. This is sent by 92 // the renderer with the page contents, every time the load stops for the 93 // main frame, so we may get several. 94 // TODO(jcampan): make the renderer send the language just once per navigation 95 // then we can get rid of that state. 96 bool translation_pending_; 97 98 // Whether the user has declined to translate the page (by closing the infobar 99 // for example). This is necessary as a new infobar could be shown if a new 100 // load happens in the page after the user closed the infobar. 101 bool translation_declined_; 102 103 // Whether the current navigation is a fragment navigation (in page). 104 bool in_page_navigation_; 105 106 DISALLOW_COPY_AND_ASSIGN(LanguageState); 107 }; 108 109 #endif // CHROME_BROWSER_TAB_CONTENTS_LANGUAGE_STATE_H_ 110