Commit 606ee9b3 authored by Nicolas Ouellet-payeur's avatar Nicolas Ouellet-payeur Committed by Commit Bot

Add 'translate_recent_target' to chrome://translate-internals

This should make it easier to debug Translate internals.

UI changes:

1. Add 'translate_recent_target' to the Preferences dump in the
   translate-internals page.

2. Add a box for the user to enter a new value for 'translate_recent_target',
   and an 'update' button. When the user clicks the button, the preference is
   updated.

Bug: 855176
Cq-Include-Trybots: luci.chromium.try:closure_compilation
Change-Id: I97437c792c8b6efb73bdb6d1c21f05c5ffff7d8a
Reviewed-on: https://chromium-review.googlesource.com/1110739Reviewed-by: default avataranthonyvd <anthonyvd@chromium.org>
Reviewed-by: default avatarHajime Hoshi <hajimehoshi@chromium.org>
Commit-Queue: Nicolas Ouellet-Payeur <nicolaso@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570747}
parent e9e46b9e
...@@ -43,4 +43,10 @@ found in the LICENSE file. ...@@ -43,4 +43,10 @@ found in the LICENSE file.
<h2 id="override-variations-country">Override Variations Country</h2> <h2 id="override-variations-country">Override Variations Country</h2>
<p id="country-override"></p> <p id="country-override"></p>
</section> </section>
<section>
<h2 id="override-recent-target">
Override <code>translate_recent_target</code>
</h2>
<p id="recent-override"></p>
</section>
</div> </div>
...@@ -212,6 +212,18 @@ cr.define('cr.translateInternals', function() { ...@@ -212,6 +212,18 @@ cr.define('cr.translateInternals', function() {
p.appendChild(createDismissingButton( p.appendChild(createDismissingButton(
chrome.send.bind(null, 'removePrefItem', ['too_often_denied']))); chrome.send.bind(null, 'removePrefItem', ['too_often_denied'])));
if ('translate_recent_target' in detail) {
var recentTarget = detail['translate_recent_target'];
p = $('recent-override');
p.innerHTML = '';
appendTextFieldWithButton(p, recentTarget, function(value) {
chrome.send('setRecentTargetLanguage', [value]);
});
}
p = document.querySelector('#prefs-dump p'); p = document.querySelector('#prefs-dump p');
var content = JSON.stringify(detail, null, 2); var content = JSON.stringify(detail, null, 2);
p.textContent = content; p.textContent = content;
...@@ -264,18 +276,9 @@ cr.define('cr.translateInternals', function() { ...@@ -264,18 +276,9 @@ cr.define('cr.translateInternals', function() {
'with a new value received from the variations server when ' + 'with a new value received from the variations server when ' +
'Chrome is updated.'); 'Chrome is updated.');
var input = document.createElement('input'); appendTextFieldWithButton(p, country, function(value) {
input.type = 'text'; chrome.send('overrideCountry', [value]);
input.value = country; });
var button = document.createElement('button');
button.textContent = 'update';
button.addEventListener('click', function() {
chrome.send('overrideCountry', [input.value]);
}, false);
p.appendChild(input);
p.appendChild(document.createElement('br'));
p.appendChild(button);
if ('update' in details && details['update']) { if ('update' in details && details['update']) {
var div1 = document.createElement('div'); var div1 = document.createElement('div');
...@@ -431,6 +434,32 @@ cr.define('cr.translateInternals', function() { ...@@ -431,6 +434,32 @@ cr.define('cr.translateInternals', function() {
tbody.appendChild(tr); tbody.appendChild(tr);
} }
/**
* Appends an <input type="text" /> and a button to `elt`, and sets the value
* of the <input> to `value`. When the button is clicked,
* `buttonClickCallback` is called with the value of the <input> field.
*
* @param {HTMLElement} elt Container element to append to.
* @param {string} value Initial value of the <input> element.
* @param {Function} buttonClickCallback Function to call when the button is
* clicked.
*/
function appendTextFieldWithButton(elt, value, buttonClickCallback) {
var input = document.createElement('input');
input.type = 'text';
input.value = value;
var button = document.createElement('button');
button.textContent = 'update';
button.addEventListener('click', function() {
buttonClickCallback(input.value);
}, false);
elt.appendChild(input);
elt.appendChild(document.createElement('br'));
elt.appendChild(button);
}
/** /**
* The callback entry point from the browser. This function will be * The callback entry point from the browser. This function will be
* called by the browser. * called by the browser.
......
...@@ -63,6 +63,10 @@ void TranslateInternalsHandler::RegisterMessages() { ...@@ -63,6 +63,10 @@ void TranslateInternalsHandler::RegisterMessages() {
"removePrefItem", "removePrefItem",
base::BindRepeating(&TranslateInternalsHandler::OnRemovePrefItem, base::BindRepeating(&TranslateInternalsHandler::OnRemovePrefItem,
base::Unretained(this))); base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"setRecentTargetLanguage",
base::BindRepeating(&TranslateInternalsHandler::OnSetRecentTargetLanguage,
base::Unretained(this)));
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
"requestInfo", "requestInfo",
base::BindRepeating(&TranslateInternalsHandler::OnRequestInfo, base::BindRepeating(&TranslateInternalsHandler::OnRequestInfo,
...@@ -164,6 +168,24 @@ void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) { ...@@ -164,6 +168,24 @@ void TranslateInternalsHandler::OnRemovePrefItem(const base::ListValue* args) {
SendPrefsToJs(); SendPrefsToJs();
} }
void TranslateInternalsHandler::OnSetRecentTargetLanguage(
const base::ListValue* args) {
content::WebContents* web_contents = web_ui()->GetWebContents();
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
PrefService* prefs = profile->GetOriginalProfile()->GetPrefs();
std::unique_ptr<translate::TranslatePrefs> translate_prefs(
ChromeTranslateClient::CreateTranslatePrefs(prefs));
std::string new_value;
if (!args->GetString(0, &new_value))
return;
translate_prefs->SetRecentTargetLanguage(new_value);
SendPrefsToJs();
}
void TranslateInternalsHandler::OnOverrideCountry(const base::ListValue* args) { void TranslateInternalsHandler::OnOverrideCountry(const base::ListValue* args) {
std::string country; std::string country;
if (args->GetString(0, &country)) { if (args->GetString(0, &country)) {
...@@ -199,6 +221,7 @@ void TranslateInternalsHandler::SendPrefsToJs() { ...@@ -199,6 +221,7 @@ void TranslateInternalsHandler::SendPrefsToJs() {
static const char* keys[] = { static const char* keys[] = {
prefs::kOfferTranslateEnabled, prefs::kOfferTranslateEnabled,
translate::TranslatePrefs::kPrefTranslateRecentTarget,
translate::TranslatePrefs::kPrefTranslateBlockedLanguages, translate::TranslatePrefs::kPrefTranslateBlockedLanguages,
translate::TranslatePrefs::kPrefTranslateSiteBlacklist, translate::TranslatePrefs::kPrefTranslateSiteBlacklist,
translate::TranslatePrefs::kPrefTranslateWhitelists, translate::TranslatePrefs::kPrefTranslateWhitelists,
......
...@@ -59,6 +59,11 @@ class TranslateInternalsHandler : public content::WebUIMessageHandler, ...@@ -59,6 +59,11 @@ class TranslateInternalsHandler : public content::WebUIMessageHandler,
// when UI requests to remove an item in the preference. // when UI requests to remove an item in the preference.
void OnRemovePrefItem(const base::ListValue* args); void OnRemovePrefItem(const base::ListValue* args);
// Handles the JavaScript message 'setRecentTargetLanguage'. This message is
// sent when the UI requests to change the 'translate_recent_target'
// preference.
void OnSetRecentTargetLanguage(const base::ListValue* args);
// Handles the Javascript message 'overrideCountry'. This message is sent // Handles the Javascript message 'overrideCountry'. This message is sent
// when UI requests to override the stored country. // when UI requests to override the stored country.
void OnOverrideCountry(const base::ListValue* country); void OnOverrideCountry(const base::ListValue* country);
......
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