Merge ~osomon/oxide:navigator.languages-2 into oxide:master

Proposed by Olivier Tilloy on 2016-10-03
Status: Merged
Approved by: Chris Coulson on 2016-10-05
Approved revision: 20ffa23227535c319a7a1528614b0f8d7126c0e0
Merged at revision: 4d4261a6e29b605178880e316b89da595e09151b
Proposed branch: ~osomon/oxide:navigator.languages-2
Merge into: oxide:master
Diff against target: 143 lines (+73/-3)
6 files modified
qt/quick/api/oxideqquickwebcontext.cc (+2/-1)
qt/tests/qmltests/web_platform/tst_navigator_languages.qml (+50/-0)
shared/browser/oxide_user_agent_settings.cc (+4/-0)
shared/browser/oxide_user_agent_settings_observer.h (+2/-0)
shared/browser/web_contents_helper.cc (+14/-2)
shared/browser/web_contents_helper.h (+1/-0)
Reviewer Review Type Date Requested Status
Chris Coulson 2016-10-03 Needs Fixing on 2016-10-05
Review via email: mp+307476@code.launchpad.net

Commit Message

Ensure that navigator.languages is not empty by propagating the value of UserAgentSettings.acceptLangs.

LP: #1620528

To post a comment you must log in.
Chris Coulson (chrisccoulson) wrote :

This looks ok. Would you mind updating the documentation for WebContext::acceptLangs though? It currently only mentions the HTTP Accept-Language header (remember that navigator.languages reflects the value of this property directly, whereas it's used to generate another string for the Accept-Language header).

review: Needs Fixing
~osomon/oxide:navigator.languages-2 updated on 2016-10-05
20ffa23... by Olivier Tilloy on 2016-10-05

Updated QML documentation for WebContext::acceptLangs.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/qt/quick/api/oxideqquickwebcontext.cc b/qt/quick/api/oxideqquickwebcontext.cc
2index ed733f9..6fe1ef7 100644
3--- a/qt/quick/api/oxideqquickwebcontext.cc
4+++ b/qt/quick/api/oxideqquickwebcontext.cc
5@@ -721,7 +721,8 @@ void OxideQQuickWebContext::setCachePath(const QUrl& cacheUrl) {
6 /*!
7 \qmlproperty string WebContext::acceptLangs
8
9-The value used to determine the contents of the HTTP \e{Accept-Language} header.
10+The value used to determine the contents of \e{navigator.languages} and of the
11+HTTP \e{Accept-Language} header.
12
13 Applications can specify this by setting it to a comma delimited list of
14 language codes in order of preference (starting with the most-preferred). Oxide
15diff --git a/qt/tests/qmltests/web_platform/tst_navigator_languages.qml b/qt/tests/qmltests/web_platform/tst_navigator_languages.qml
16new file mode 100644
17index 0000000..b8d6ae2
18--- /dev/null
19+++ b/qt/tests/qmltests/web_platform/tst_navigator_languages.qml
20@@ -0,0 +1,50 @@
21+import QtQuick 2.0
22+import QtTest 1.0
23+import com.canonical.Oxide 1.0
24+import Oxide.testsupport 1.0
25+
26+TestWebView {
27+ id: webView
28+ focus: true
29+
30+ property int languageChangeEvents: 0
31+
32+ messageHandlers: [
33+ ScriptMessageHandler {
34+ msgId: "LANGUAGE-CHANGE"
35+ contexts: [ "oxide://testutils/" ]
36+ callback: function(msg) {
37+ languageChangeEvents++;
38+ }
39+ }
40+ ]
41+
42+ TestCase {
43+ name: "navigator.languages"
44+ when: windowShown
45+
46+ function get_navigator_languages() {
47+ return webView.getTestApi().evaluateCode("navigator.languages;");
48+ }
49+
50+ function initTestCase() {
51+ webView.url = "http://testsuite/empty.html";
52+ verify(webView.waitForLoadSucceeded());
53+ verify(get_navigator_languages().length > 0);
54+ }
55+
56+ function init() {
57+ webView.languageChangeEvents = 0;
58+ }
59+
60+ function test_accept_langs() {
61+ webView.getTestApi().evaluateCode(
62+ "window.addEventListener(\"languagechange\", function(event) {
63+ oxide.sendMessage(\"LANGUAGE-CHANGE\");
64+ });", true);
65+ SingletonTestWebContext.acceptLangs = "fr,es,ca";
66+ compare(get_navigator_languages(), ["fr", "es", "ca"]);
67+ tryCompare(webView, "languageChangeEvents", 1);
68+ }
69+ }
70+}
71diff --git a/shared/browser/oxide_user_agent_settings.cc b/shared/browser/oxide_user_agent_settings.cc
72index 2ff72e2..877dc31 100644
73--- a/shared/browser/oxide_user_agent_settings.cc
74+++ b/shared/browser/oxide_user_agent_settings.cc
75@@ -267,6 +267,10 @@ void UserAgentSettings::SetAcceptLangs(const std::string& accept_langs) {
76 context_->GetIOData()->GetUserAgentSettings();
77 base::AutoLock lock(io_data->lock_);
78 io_data->accept_langs_ = accept_langs;
79+
80+ FOR_EACH_OBSERVER(UserAgentSettingsObserver,
81+ observers_,
82+ NotifyAcceptLanguagesChanged());
83 }
84
85 std::vector<UserAgentSettings::UserAgentOverride>
86diff --git a/shared/browser/oxide_user_agent_settings_observer.h b/shared/browser/oxide_user_agent_settings_observer.h
87index d5cc154..9cdc045 100644
88--- a/shared/browser/oxide_user_agent_settings_observer.h
89+++ b/shared/browser/oxide_user_agent_settings_observer.h
90@@ -30,6 +30,8 @@ class UserAgentSettingsObserver {
91
92 virtual void NotifyDoNotTrackChanged() {}
93
94+ virtual void NotifyAcceptLanguagesChanged() {}
95+
96 protected:
97 UserAgentSettingsObserver();
98 UserAgentSettingsObserver(UserAgentSettings* settings);
99diff --git a/shared/browser/web_contents_helper.cc b/shared/browser/web_contents_helper.cc
100index 7e71ecb..b19a4e8 100644
101--- a/shared/browser/web_contents_helper.cc
102+++ b/shared/browser/web_contents_helper.cc
103@@ -60,8 +60,10 @@ WebContentsHelper::WebContentsHelper(content::WebContents* contents)
104
105 content::RendererPreferences* renderer_prefs =
106 web_contents_->GetMutableRendererPrefs();
107- renderer_prefs->enable_do_not_track =
108- UserAgentSettings::Get(contents->GetBrowserContext())->GetDoNotTrack();
109+ UserAgentSettings* ua_settings =
110+ UserAgentSettings::Get(contents->GetBrowserContext());
111+ renderer_prefs->enable_do_not_track = ua_settings->GetDoNotTrack();
112+ renderer_prefs->accept_languages = ua_settings->GetAcceptLangs();
113
114 // Hardcoded selection colors to match the current Ambiance theme from the
115 // Ubuntu UI Toolkit (https://bazaar.launchpad.net/~ubuntu-sdk-team/ubuntu-ui-toolkit/trunk/view/head:/src/Ubuntu/Components/Themes/Ambiance/1.3/Palette.qml)
116@@ -102,6 +104,16 @@ void WebContentsHelper::NotifyDoNotTrackChanged() {
117 SyncRendererPreferences();
118 }
119
120+void WebContentsHelper::NotifyAcceptLanguagesChanged() {
121+ content::RendererPreferences* renderer_prefs =
122+ web_contents_->GetMutableRendererPrefs();
123+ UserAgentSettings* ua_settings =
124+ UserAgentSettings::Get(web_contents_->GetBrowserContext());
125+ renderer_prefs->accept_languages = ua_settings->GetAcceptLangs();
126+
127+ SyncRendererPreferences();
128+}
129+
130 void WebContentsHelper::OnDisplayPropertiesChanged(
131 const display::Display& display) {
132 if (display.id() !=
133diff --git a/shared/browser/web_contents_helper.h b/shared/browser/web_contents_helper.h
134index c40d69e..9c46fb3 100644
135--- a/shared/browser/web_contents_helper.h
136+++ b/shared/browser/web_contents_helper.h
137@@ -82,6 +82,7 @@ class OXIDE_SHARED_EXPORT WebContentsHelper
138 // UserAgentSettingsObserver implementation
139 void NotifyPopupBlockerEnabledChanged() override;
140 void NotifyDoNotTrackChanged() override;
141+ void NotifyAcceptLanguagesChanged() override;
142
143 content::WebContents* web_contents_;
144

Subscribers

People subscribed via source and target branches