summaryrefslogtreecommitdiffstats
path: root/chrome/browser/tab_contents
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-12 21:53:55 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-12 21:53:55 +0000
commit3b1c18191d41eac0a551b3009269a259757ca070 (patch)
tree5f466b716abc81ac3bb67262522ba1af0d699588 /chrome/browser/tab_contents
parent7d9ad0b30c0775a7345e965294c9f86bb0d543e5 (diff)
downloadchromium_src-3b1c18191d41eac0a551b3009269a259757ca070.zip
chromium_src-3b1c18191d41eac0a551b3009269a259757ca070.tar.gz
chromium_src-3b1c18191d41eac0a551b3009269a259757ca070.tar.bz2
Implements the auto-translate on click: if you have translated
a page and are navigating to a new page in the same language by clicking a link, the new page is automatically translated. In order to do that I moved the language state from the navigation entry to some dedicated class that each TabContents owns. Also added some basic unit-testing for good measure. BUG=35477 TEST=See bug steps. Run unit-tests. Review URL: http://codereview.chromium.org/596092 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38961 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/tab_contents')
-rw-r--r--chrome/browser/tab_contents/language_state.cc51
-rw-r--r--chrome/browser/tab_contents/language_state.h84
-rw-r--r--chrome/browser/tab_contents/navigation_entry.h10
-rw-r--r--chrome/browser/tab_contents/tab_contents.cc15
-rw-r--r--chrome/browser/tab_contents/tab_contents.h8
5 files changed, 152 insertions, 16 deletions
diff --git a/chrome/browser/tab_contents/language_state.cc b/chrome/browser/tab_contents/language_state.cc
new file mode 100644
index 0000000..8522ed3
--- /dev/null
+++ b/chrome/browser/tab_contents/language_state.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/tab_contents/language_state.h"
+
+#include "chrome/browser/tab_contents/navigation_controller.h"
+#include "chrome/browser/tab_contents/navigation_entry.h"
+
+LanguageState::LanguageState(NavigationController* nav_controller)
+ : navigation_controller_(nav_controller),
+ translation_pending_(false) {
+}
+
+LanguageState::~LanguageState() {
+}
+
+void LanguageState::DidNavigate() {
+ prev_original_lang_ = original_lang_;
+ prev_current_lang_ = current_lang_;
+
+ original_lang_.clear();
+ current_lang_.clear();
+
+ translation_pending_ = false;
+}
+
+void LanguageState::LanguageDetermined(const std::string& page_language) {
+ original_lang_ = page_language;
+ current_lang_ = page_language;
+}
+
+std::string LanguageState::AutoTranslateTo() const {
+ // Only auto-translate if:
+ // - no translation is pending
+ // - this page is in the same language as the previous page
+ // - the previous page had been translated
+ // - this page is not already translated
+ // - the new page was navigated through a link.
+ if (!translation_pending_ &&
+ prev_original_lang_ == original_lang_ &&
+ prev_original_lang_ != prev_current_lang_ &&
+ original_lang_ == current_lang_ &&
+ navigation_controller_->GetActiveEntry() &&
+ navigation_controller_->GetActiveEntry()->transition_type() ==
+ PageTransition::LINK) {
+ return prev_current_lang_;
+ }
+
+ return std::string();
+}
diff --git a/chrome/browser/tab_contents/language_state.h b/chrome/browser/tab_contents/language_state.h
new file mode 100644
index 0000000..d5c7df6
--- /dev/null
+++ b/chrome/browser/tab_contents/language_state.h
@@ -0,0 +1,84 @@
+// Copyright (c) 2010 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_TAB_CONTENTS_LANGUAGE_STATE_H_
+#define CHROME_BROWSER_TAB_CONTENTS_LANGUAGE_STATE_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+
+class NavigationController;
+
+// This class holds the language state of the current page.
+// There is one LanguageState instance per TabContents.
+// It is used to determine when navigating to a new page whether it should
+// automatically be translated.
+// This auto-translate behavior is the expected behavior when:
+// - user is on page in language A that they had translated to language B.
+// - user clicks a link in that page that takes them to a page also in language
+// A.
+
+class LanguageState {
+ public:
+ explicit LanguageState(NavigationController* nav_controller);
+ ~LanguageState();
+
+ // Should be called when the page did a new navigation (whether it is a main
+ // frame or sub-frame navigation).
+ void DidNavigate();
+
+ // Should be called when the language of the page has been determined.
+ void LanguageDetermined(const std::string& page_language);
+
+ // Returns the language the current page should be translated to, based on the
+ // previous page languages and the transition. This should be called after
+ // the language page has been determined.
+ // Returns an empty string if the page should not be auto-translated.
+ std::string AutoTranslateTo() const;
+
+ // Returns true if the current page in the associated tab has been translated.
+ bool IsPageTranslated() const { return original_lang_ != current_lang_; }
+
+ const std::string& original_language() const { return original_lang_; }
+
+ void set_current_language(const std::string& language) {
+ current_lang_ = language;
+ }
+ const std::string& current_language() const { return current_lang_; }
+
+ // Whether the page is currently in the process of being translated.
+ bool translation_pending() const { return translation_pending_; }
+ void set_translation_pending(bool value) { translation_pending_ = value; }
+
+ private:
+ // The languages this page is in. Note that current_lang_ is different from
+ // original_lang_ when the page has been translated.
+ // Note that these might be empty if the page language has not been determined
+ // yet.
+ std::string original_lang_;
+ std::string current_lang_;
+
+ // Same as above but for the previous page.
+ std::string prev_original_lang_;
+ std::string prev_current_lang_;
+
+ // The navigation controller of the tab we are associated with.
+ NavigationController* navigation_controller_;
+
+ // Whether a translation is currently pending (TabContents waiting for the
+ // PAGE_TRANSLATED notification). This is needed to avoid sending duplicate
+ // translate requests to a page. TranslateManager initiates translations
+ // when it received the LANGUAGE_DETERMINED notification. This is sent by
+ // the renderer with the page contents, every time the load stops for the
+ // main frame, so we may get several.
+ // TODO(jcampan): make the renderer send the language just once per navigation
+ // then we can get rid of that state.
+ bool translation_pending_;
+
+ DISALLOW_COPY_AND_ASSIGN(LanguageState);
+};
+
+#endif // CHROME_BROWSER_TAB_CONTENTS_LANGUAGE_STATE_H_
+
diff --git a/chrome/browser/tab_contents/navigation_entry.h b/chrome/browser/tab_contents/navigation_entry.h
index df6ad06..e679c10 100644
--- a/chrome/browser/tab_contents/navigation_entry.h
+++ b/chrome/browser/tab_contents/navigation_entry.h
@@ -389,15 +389,6 @@ class NavigationEntry {
return restore_type_;
}
- // The ISO 639-1 language code (ex: en, fr, zh...) for the page.
- // Can be empty if the language was not detected yet or is unknown.
- void set_language(const std::string& language) {
- language_ = language;
- }
- std::string language() const {
- return language_;
- }
-
private:
// WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
// Session/Tab restore save portions of this class so that it can be recreated
@@ -422,7 +413,6 @@ class NavigationEntry {
GURL user_typed_url_;
bool has_post_data_;
RestoreType restore_type_;
- std::string language_; // ISO 639-1 language code.
// This is a cached version of the result of GetTitleForDisplay. It prevents
// us from having to do URL formatting on the URL evey time the title is
diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc
index 2f0dcd9..ed15084 100644
--- a/chrome/browser/tab_contents/tab_contents.cc
+++ b/chrome/browser/tab_contents/tab_contents.cc
@@ -269,7 +269,8 @@ TabContents::TabContents(Profile* profile,
is_showing_before_unload_dialog_(false),
renderer_preferences_(),
opener_dom_ui_type_(DOMUIFactory::kNoDOMUI),
- app_extension_(NULL) {
+ app_extension_(NULL),
+ language_state_(&controller_) {
ClearBlockedContentSettings();
renderer_preferences_util::UpdateFromSystemSettings(
&renderer_preferences_, profile);
@@ -811,6 +812,7 @@ void TabContents::TranslatePage(const std::string& source_lang,
NOTREACHED();
return;
}
+ language_state_.set_translation_pending(true);
render_view_host()->TranslatePage(entry->page_id(), source_lang, target_lang);
}
@@ -1503,6 +1505,9 @@ void TabContents::DidNavigateAnyFramePostCommit(
// cleaned up and covered by tests.
if (params.password_form.origin.is_valid())
GetPasswordManager()->ProvisionallySavePassword(params.password_form);
+
+ // Let the LanguageState clear its state.
+ language_state_.DidNavigate();
}
void TabContents::CloseConstrainedWindows() {
@@ -1860,11 +1865,7 @@ void TabContents::OnPageContents(const GURL& url,
}
}
- NavigationEntry* entry = controller_.GetActiveEntry();
- if (GetRenderProcessHost()->id() == renderer_process_id &&
- entry && entry->page_id() == page_id) {
- entry->set_language(language);
- }
+ language_state_.LanguageDetermined(language);
std::string lang = language;
NotificationService::current()->Notify(
@@ -1876,6 +1877,8 @@ void TabContents::OnPageContents(const GURL& url,
void TabContents::OnPageTranslated(int32 page_id,
const std::string& original_lang,
const std::string& translated_lang) {
+ language_state_.set_current_language(translated_lang);
+ language_state_.set_translation_pending(false);
std::pair<std::string, std::string> lang_pair =
std::make_pair(original_lang, translated_lang);
NotificationService::current()->Notify(
diff --git a/chrome/browser/tab_contents/tab_contents.h b/chrome/browser/tab_contents/tab_contents.h
index 2c7e5be..4d11745 100644
--- a/chrome/browser/tab_contents/tab_contents.h
+++ b/chrome/browser/tab_contents/tab_contents.h
@@ -30,6 +30,7 @@
#include "chrome/browser/renderer_host/render_view_host_delegate.h"
#include "chrome/browser/tab_contents/constrained_window.h"
#include "chrome/browser/tab_contents/infobar_delegate.h"
+#include "chrome/browser/tab_contents/language_state.h"
#include "chrome/browser/tab_contents/navigation_controller.h"
#include "chrome/browser/tab_contents/navigation_entry.h"
#include "chrome/browser/tab_contents/page_navigator.h"
@@ -639,6 +640,10 @@ class TabContents : public PageNavigator,
return request_context_;
}
+ LanguageState& language_state() {
+ return language_state_;
+ }
+
// Creates a duplicate of this TabContents. The returned TabContents is
// configured such that the renderer has not been loaded (it'll load the first
// time it is selected).
@@ -1180,6 +1185,9 @@ class TabContents : public PageNavigator,
// created for.
Extension* app_extension_;
+ // Information about the language the page is in and has been translated to.
+ LanguageState language_state_;
+
// ---------------------------------------------------------------------------
DISALLOW_COPY_AND_ASSIGN(TabContents);