• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
7 
8 #include <string>
9 
10 #include "base/basictypes.h"
11 
12 namespace translate {
13 
14 class TranslateDriver;
15 
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();
28 
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);
32 
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);
38 
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;
44 
45   // Returns true if the user is navigating through translated links.
46   bool InTranslateNavigation() const;
47 
48   // Returns true if the current page in the associated tab has been translated.
IsPageTranslated()49   bool IsPageTranslated() const { return original_lang_ != current_lang_; }
50 
original_language()51   const std::string& original_language() const { return original_lang_; }
52 
53   void SetCurrentLanguage(const std::string& language);
current_language()54   const std::string& current_language() const { return current_lang_; }
55 
page_needs_translation()56   bool page_needs_translation() const { return page_needs_translation_; }
57 
58   // Whether the page is currently in the process of being translated.
translation_pending()59   bool translation_pending() const { return translation_pending_; }
set_translation_pending(bool value)60   void set_translation_pending(bool value) { translation_pending_ = value; }
61 
62   // Whether the user has already declined to translate the page.
translation_declined()63   bool translation_declined() const { return translation_declined_; }
set_translation_declined(bool value)64   void set_translation_declined(bool value) { translation_declined_ = value; }
65 
66   // Whether the current page was navigated through an in-page (fragment)
67   // navigation.
in_page_navigation()68   bool in_page_navigation() const { return in_page_navigation_; }
69 
70   // Whether the translate is enabled.
translate_enabled()71   bool translate_enabled() const { return translate_enabled_; }
72   void SetTranslateEnabled(bool value);
73 
74   // Whether the current page's language is different from the previous
75   // language.
76   bool HasLanguageChanged() const;
77 
78  private:
79   void SetIsPageTranslated(bool value);
80 
81   // Whether the page is translated or not.
82   bool is_page_translated_;
83 
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_;
90 
91   // Same as above but for the previous page.
92   std::string prev_original_lang_;
93   std::string prev_current_lang_;
94 
95   // Provides driver-level context to the shared code of the component. Must
96   // outlive this object.
97   TranslateDriver* translate_driver_;
98 
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_;
103 
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_;
111 
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_;
116 
117   // Whether the current navigation is a fragment navigation (in page).
118   bool in_page_navigation_;
119 
120   // Whether the Translate is enabled.
121   bool translate_enabled_;
122 
123   DISALLOW_COPY_AND_ASSIGN(LanguageState);
124 };
125 
126 }  // namespace translate
127 
128 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
129