Commit 370c9954 authored by Alexandre Frechette's avatar Alexandre Frechette Committed by Commit Bot

Save page language in navigation entry.

Add the page language as user data on the last committed navigation entry.
Then, make SyncSessionsRouterTabHelper listen to OnLanguageDetermined.
That way, it knows when to notify that the tab's sync data has been modified.

Bug: 957657
Change-Id: Iacde546d789d325d6700be4acaaa15dc5eef7076
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1504131
Commit-Queue: Alexandre Frechette <frechette@chromium.org>
Reviewed-by: default avataranthonyvd <anthonyvd@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#656402}
parent 5cb3a6b6
...@@ -31,7 +31,13 @@ SyncSessionsRouterTabHelper::SyncSessionsRouterTabHelper( ...@@ -31,7 +31,13 @@ SyncSessionsRouterTabHelper::SyncSessionsRouterTabHelper(
SyncSessionsWebContentsRouter* router) SyncSessionsWebContentsRouter* router)
: content::WebContentsObserver(web_contents), : content::WebContentsObserver(web_contents),
router_(router), router_(router),
source_tab_id_(SessionID::InvalidValue()) {} source_tab_id_(SessionID::InvalidValue()) {
chrome_translate_client_ =
ChromeTranslateClient::FromWebContents(web_contents);
// A translate client is not always attached to web contents (e.g. tests).
if (chrome_translate_client_)
chrome_translate_client_->translate_driver().AddObserver(this);
}
SyncSessionsRouterTabHelper::~SyncSessionsRouterTabHelper() {} SyncSessionsRouterTabHelper::~SyncSessionsRouterTabHelper() {}
...@@ -47,6 +53,8 @@ void SyncSessionsRouterTabHelper::TitleWasSet(content::NavigationEntry* entry) { ...@@ -47,6 +53,8 @@ void SyncSessionsRouterTabHelper::TitleWasSet(content::NavigationEntry* entry) {
void SyncSessionsRouterTabHelper::WebContentsDestroyed() { void SyncSessionsRouterTabHelper::WebContentsDestroyed() {
NotifyRouter(); NotifyRouter();
if (chrome_translate_client_)
chrome_translate_client_->translate_driver().RemoveObserver(this);
} }
void SyncSessionsRouterTabHelper::DidFinishLoad( void SyncSessionsRouterTabHelper::DidFinishLoad(
...@@ -70,6 +78,12 @@ void SyncSessionsRouterTabHelper::DidOpenRequestedURL( ...@@ -70,6 +78,12 @@ void SyncSessionsRouterTabHelper::DidOpenRequestedURL(
SetSourceTabIdForChild(new_contents); SetSourceTabIdForChild(new_contents);
} }
void SyncSessionsRouterTabHelper::OnLanguageDetermined(
const translate::LanguageDetectionDetails& details) {
// TODO (crbug.com/957657): NotifyRouter() when language is synced on
// notification.
}
void SyncSessionsRouterTabHelper::SetSourceTabIdForChild( void SyncSessionsRouterTabHelper::SetSourceTabIdForChild(
content::WebContents* child_contents) { content::WebContents* child_contents) {
SessionID source_tab_id = SessionTabHelper::IdForTab(web_contents()); SessionID source_tab_id = SessionTabHelper::IdForTab(web_contents());
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#ifndef CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSIONS_ROUTER_TAB_HELPER_H_ #ifndef CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSIONS_ROUTER_TAB_HELPER_H_
#define CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSIONS_ROUTER_TAB_HELPER_H_ #define CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSIONS_ROUTER_TAB_HELPER_H_
#include "chrome/browser/translate/chrome_translate_client.h"
#include "components/sessions/core/session_id.h" #include "components/sessions/core/session_id.h"
#include "components/translate/content/browser/content_translate_driver.h"
#include "content/public/browser/web_contents_observer.h" #include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h" #include "content/public/browser/web_contents_user_data.h"
...@@ -22,7 +24,8 @@ class SyncSessionsWebContentsRouter; ...@@ -22,7 +24,8 @@ class SyncSessionsWebContentsRouter;
// https://chromium.googlesource.com/chromium/src/+/master/docs/tab_helpers.md // https://chromium.googlesource.com/chromium/src/+/master/docs/tab_helpers.md
class SyncSessionsRouterTabHelper class SyncSessionsRouterTabHelper
: public content::WebContentsUserData<SyncSessionsRouterTabHelper>, : public content::WebContentsUserData<SyncSessionsRouterTabHelper>,
public content::WebContentsObserver { public content::WebContentsObserver,
public translate::ContentTranslateDriver::Observer {
public: public:
~SyncSessionsRouterTabHelper() override; ~SyncSessionsRouterTabHelper() override;
...@@ -46,6 +49,10 @@ class SyncSessionsRouterTabHelper ...@@ -46,6 +49,10 @@ class SyncSessionsRouterTabHelper
bool started_from_context_menu, bool started_from_context_menu,
bool renderer_initiated) override; bool renderer_initiated) override;
// ContentTranslateDriver::Observer implementation.
void OnLanguageDetermined(
const translate::LanguageDetectionDetails& details) override;
// Sets the source tab id for the given child WebContents to the id of the // Sets the source tab id for the given child WebContents to the id of the
// WebContents that owns this helper. // WebContents that owns this helper.
void SetSourceTabIdForChild(content::WebContents* child_contents); void SetSourceTabIdForChild(content::WebContents* child_contents);
...@@ -76,6 +83,8 @@ class SyncSessionsRouterTabHelper ...@@ -76,6 +83,8 @@ class SyncSessionsRouterTabHelper
// * Click on a link with target='_blank'. // * Click on a link with target='_blank'.
SessionID source_tab_id_; SessionID source_tab_id_;
ChromeTranslateClient* chrome_translate_client_;
WEB_CONTENTS_USER_DATA_KEY_DECL(); WEB_CONTENTS_USER_DATA_KEY_DECL();
DISALLOW_COPY_AND_ASSIGN(SyncSessionsRouterTabHelper); DISALLOW_COPY_AND_ASSIGN(SyncSessionsRouterTabHelper);
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "components/translate/content/browser/content_translate_driver.h" #include "components/translate/content/browser/content_translate_driver.h"
#include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
...@@ -11,6 +12,8 @@ ...@@ -11,6 +12,8 @@
#include "base/location.h" #include "base/location.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/strings/utf_string_conversions.h"
#include "base/supports_user_data.h"
#include "base/threading/thread_task_runner_handle.h" #include "base/threading/thread_task_runner_handle.h"
#include "components/google/core/common/google_util.h" #include "components/google/core/common/google_util.h"
#include "components/language/core/browser/url_language_histogram.h" #include "components/language/core/browser/url_language_histogram.h"
...@@ -41,6 +44,15 @@ namespace { ...@@ -41,6 +44,15 @@ namespace {
// loading before giving up the translation // loading before giving up the translation
const int kMaxTranslateLoadCheckAttempts = 20; const int kMaxTranslateLoadCheckAttempts = 20;
// The key used to store page language in the NavigationEntry;
const char kPageLanguageKey[] = "page_language";
struct LanguageDectionData : public base::SupportsUserData::Data {
// The adopted language. An ISO 639 language code (two letters, except for
// Chinese where a localization is necessary).
std::string adopted_language;
};
} // namespace } // namespace
ContentTranslateDriver::ContentTranslateDriver( ContentTranslateDriver::ContentTranslateDriver(
...@@ -294,9 +306,18 @@ void ContentTranslateDriver::RegisterPage( ...@@ -294,9 +306,18 @@ void ContentTranslateDriver::RegisterPage(
translate_manager_->GetLanguageState().LanguageDetermined( translate_manager_->GetLanguageState().LanguageDetermined(
details.adopted_language, page_needs_translation); details.adopted_language, page_needs_translation);
if (web_contents()) if (web_contents()) {
translate_manager_->InitiateTranslation(details.adopted_language); translate_manager_->InitiateTranslation(details.adopted_language);
// Save the page language on the navigation entry so it can be synced.
auto* const entry = web_contents()->GetController().GetLastCommittedEntry();
if (entry != nullptr) {
auto data = std::make_unique<LanguageDectionData>();
data->adopted_language = details.adopted_language;
entry->SetUserData(kPageLanguageKey, std::move(data));
}
}
for (auto& observer : observer_list_) for (auto& observer : observer_list_)
observer.OnLanguageDetermined(details); observer.OnLanguageDetermined(details);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment