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