Commit bc82214c authored by rbpotter's avatar rbpotter Committed by Commit Bot

Print Preview: Add query parameter indicating locale for GCP requests

If the user's preferred language for the selected Google Cloud Print
(GCP) account does not match the language for the Chrome UI, the names
and descriptions of cloud printers will appear in the account's
preferred language, mismatching the langauge of the rest of the UI.
This also means these printer names/descriptions are not searchable in
the Chrome UI language, which is confusing. Send the locale as a query
parameter in cloud print requests so that the GCP server sends printer
names and descriptions in the appropriate language.

Note: the Accepts-Language header is ignored by the GCP server.

Bug: 974869
Change-Id: Ia63432d2ef83206a7985f38bf83f88ae6a24b63b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1663400
Commit-Queue: Rebekah Potter <rbpotter@chromium.org>
Reviewed-by: default avatarDemetrios Papadopoulos <dpapad@chromium.org>
Cr-Commit-Position: refs/heads/master@{#670353}
parent 1ddb0bec
...@@ -198,29 +198,35 @@ cr.define('cloudprint', function() { ...@@ -198,29 +198,35 @@ cr.define('cloudprint', function() {
* @private * @private
*/ */
buildRequest_(method, action, params, origin, account, callback) { buildRequest_(method, action, params, origin, account, callback) {
let url = this.baseUrl_ + '/' + action + '?xsrf='; const url = new URL(this.baseUrl_ + '/' + action);
const searchParams = url.searchParams;
if (origin == print_preview.DestinationOrigin.COOKIES) { if (origin == print_preview.DestinationOrigin.COOKIES) {
const xsrfToken = this.xsrfTokens_[account]; const xsrfToken = this.xsrfTokens_[account];
if (!xsrfToken) { if (!xsrfToken) {
searchParams.append('xsrf', '');
// TODO(rltoscano): Should throw an error if not a read-only action or // TODO(rltoscano): Should throw an error if not a read-only action or
// issue an xsrf token request. // issue an xsrf token request.
} else { } else {
url = url + xsrfToken; searchParams.append('xsrf', xsrfToken);
} }
if (account) { if (account) {
const index = this.userSessionIndex_[account] || 0; const index = this.userSessionIndex_[account] || 0;
if (index > 0) { if (index > 0) {
url += '&authuser=' + index; searchParams.append('authuser', index.toString());
} }
} }
} else {
searchParams.append('xsrf', '');
} }
// Add locale
searchParams.append('hl', window.navigator.language);
let body = null; let body = null;
if (params) { if (params) {
if (method == 'GET') { if (method == 'GET') {
url = params.reduce(function(partialUrl, param) { params.forEach(param => {
return partialUrl + '&' + param.name + '=' + searchParams.append(param.name, encodeURIComponent(param.value));
encodeURIComponent(param.value); });
}, url);
} else if (method == 'POST') { } else if (method == 'POST') {
body = params.reduce(function(partialBody, param) { body = params.reduce(function(partialBody, param) {
return partialBody + 'Content-Disposition: form-data; name=\"' + return partialBody + 'Content-Disposition: form-data; name=\"' +
...@@ -239,7 +245,7 @@ cr.define('cloudprint', function() { ...@@ -239,7 +245,7 @@ cr.define('cloudprint', function() {
} }
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
xhr.open(method, url, true); xhr.open(method, url.toString(), true);
xhr.withCredentials = (origin == print_preview.DestinationOrigin.COOKIES); xhr.withCredentials = (origin == print_preview.DestinationOrigin.COOKIES);
for (const header in headers) { for (const header in headers) {
xhr.setRequestHeader(header, headers[header]); xhr.setRequestHeader(header, headers[header]);
......
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