Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / translate / core / browser / language_state.h
blob4bd7f4bae396cc2b67861e6b32e2a531c9d19616
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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
8 #include <string>
10 #include "base/basictypes.h"
12 namespace translate {
14 class TranslateDriver;
16 // This class holds the language state of the current page.
17 // There is one LanguageState instance per tab.
18 // It is used to determine when navigating to a new page whether it should
19 // automatically be translated.
20 // This auto-translate behavior is the expected behavior when:
21 // - user is on page in language A that they had translated to language B.
22 // - user clicks a link in that page that takes them to a page also in language
23 // A.
24 class LanguageState {
25 public:
26 explicit LanguageState(TranslateDriver* driver);
27 ~LanguageState();
29 // Should be called when the page did a new navigation (whether it is a main
30 // frame or sub-frame navigation).
31 void DidNavigate(bool in_page_navigation, bool is_main_frame, bool reload);
33 // Should be called when the language of the page has been determined.
34 // |page_needs_translation| when false indicates that the browser should not
35 // offer to translate the page.
36 void LanguageDetermined(const std::string& page_language,
37 bool page_needs_translation);
39 // Returns the language the current page should be translated to, based on the
40 // previous page languages and the transition. This should be called after
41 // the language page has been determined.
42 // Returns an empty string if the page should not be auto-translated.
43 std::string AutoTranslateTo() const;
45 // Returns true if the user is navigating through translated links.
46 bool InTranslateNavigation() const;
48 // Returns true if the current page in the associated tab has been translated.
49 bool IsPageTranslated() const { return original_lang_ != current_lang_; }
51 const std::string& original_language() const { return original_lang_; }
53 void SetCurrentLanguage(const std::string& language);
54 const std::string& current_language() const { return current_lang_; }
56 bool page_needs_translation() const { return page_needs_translation_; }
58 // Whether the page is currently in the process of being translated.
59 bool translation_pending() const { return translation_pending_; }
60 void set_translation_pending(bool value) { translation_pending_ = value; }
62 // Whether the user has already declined to translate the page.
63 bool translation_declined() const { return translation_declined_; }
64 void set_translation_declined(bool value) { translation_declined_ = value; }
66 // Whether the current page was navigated through an in-page (fragment)
67 // navigation.
68 bool in_page_navigation() const { return in_page_navigation_; }
70 // Whether the translate is enabled.
71 bool translate_enabled() const { return translate_enabled_; }
72 void SetTranslateEnabled(bool value);
74 // Whether the current page's language is different from the previous
75 // language.
76 bool HasLanguageChanged() const;
78 private:
79 void SetIsPageTranslated(bool value);
81 // Whether the page is translated or not.
82 bool is_page_translated_;
84 // The languages this page is in. Note that current_lang_ is different from
85 // original_lang_ when the page has been translated.
86 // Note that these might be empty if the page language has not been determined
87 // yet.
88 std::string original_lang_;
89 std::string current_lang_;
91 // Same as above but for the previous page.
92 std::string prev_original_lang_;
93 std::string prev_current_lang_;
95 // Provides driver-level context to the shared code of the component. Must
96 // outlive this object.
97 TranslateDriver* translate_driver_;
99 // Whether it is OK to offer to translate the page. Some pages explictly
100 // specify that they should not be translated by the browser (this is the case
101 // for GMail for example, which provides its own translation features).
102 bool page_needs_translation_;
104 // Whether a translation is currently pending.
105 // This is needed to avoid sending duplicate translate requests to a page.
106 // Translations may be initiated every time the load stops for the main frame,
107 // which may happen several times.
108 // TODO(jcampan): make the client send the language just once per navigation
109 // then we can get rid of that state.
110 bool translation_pending_;
112 // Whether the user has declined to translate the page (by closing the infobar
113 // for example). This is necessary as a new infobar could be shown if a new
114 // load happens in the page after the user closed the infobar.
115 bool translation_declined_;
117 // Whether the current navigation is a fragment navigation (in page).
118 bool in_page_navigation_;
120 // Whether the Translate is enabled.
121 bool translate_enabled_;
123 DISALLOW_COPY_AND_ASSIGN(LanguageState);
126 } // namespace translate
128 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_