Skip to content

Commit

Permalink
feat: respect language preferences on Win/macOS
Browse files Browse the repository at this point in the history
This commit partially fixes electron#18829

Chromium uses navigator.languages (Accept-Language) to determine
fallback fonts on Windows, especially kanji/han characters in CJK.

Previously the full preferences set to OS was not given to Chromium.
For instance, when user sets 'en-US, ja-JP' to Accept-Language,
while Chromium chooses Japanese font for kanji text, but Electron
chooses Chinese font.  This is because only the first language is given
to Accept-Language on Electron.

This patch is based on electron#15532

Co-authored-by: Nitish Sakhawalkar <nitsakh@icloud.com>
Co-authored-by: Kasumi Hanazuki <kasumi@rollingapple.net>
  • Loading branch information
3 people committed Apr 25, 2020
1 parent 6ab317c commit 02de6fa
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
2 changes: 2 additions & 0 deletions filenames.gni
Expand Up @@ -109,9 +109,11 @@ filenames = {
"shell/browser/api/electron_api_web_contents.cc",
"shell/browser/api/electron_api_web_contents.h",
"shell/browser/api/electron_api_web_contents_impl.cc",
"shell/browser/api/electron_api_web_contents_linux.cc",
"shell/browser/api/electron_api_web_contents_mac.mm",
"shell/browser/api/electron_api_web_contents_view.cc",
"shell/browser/api/electron_api_web_contents_view.h",
"shell/browser/api/electron_api_web_contents_win.cc",
"shell/browser/api/electron_api_web_request.cc",
"shell/browser/api/electron_api_web_request.h",
"shell/browser/api/electron_api_web_view_manager.cc",
Expand Down
4 changes: 2 additions & 2 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -100,6 +100,7 @@
#include "third_party/blink/public/mojom/frame/find_in_page.mojom.h"
#include "third_party/blink/public/mojom/frame/fullscreen.mojom.h"
#include "third_party/blink/public/mojom/messaging/transferable_message.mojom.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/mojom/cursor_type.mojom-shared.h"
#include "ui/display/screen.h"
Expand All @@ -121,7 +122,6 @@
#endif

#if defined(OS_LINUX) || defined(OS_WIN)
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"
#include "ui/gfx/font_render_params.h"
#endif

Expand Down Expand Up @@ -535,7 +535,7 @@ void WebContents::InitWithSessionAndOptions(
managed_web_contents()->GetView()->SetDelegate(this);

auto* prefs = web_contents()->GetMutableRendererPrefs();
prefs->accept_languages = g_browser_process->GetApplicationLocale();
SetAcceptLanguages(prefs);

#if defined(OS_LINUX) || defined(OS_WIN)
// Update font settings.
Expand Down
6 changes: 6 additions & 0 deletions shell/browser/api/electron_api_web_contents.h
Expand Up @@ -579,6 +579,12 @@ class WebContents : public gin_helper::TrackableObject<WebContents>,
void InitZoomController(content::WebContents* web_contents,
const gin_helper::Dictionary& options);

void SetAcceptLanguages(blink::mojom::RendererPreferences* prefs);
#if defined(OS_WIN)
// Get OS preferred languages in Windows 10 or later
bool GetLanguagesUsingGlobalization(std::vector<base::string16>* languages);
#endif

v8::Global<v8::Value> session_;
v8::Global<v8::Value> devtools_web_contents_;
v8::Global<v8::Value> debugger_;
Expand Down
20 changes: 20 additions & 0 deletions shell/browser/api/electron_api_web_contents_linux.cc
@@ -0,0 +1,20 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/browser/api/electron_api_web_contents.h"

#include "chrome/browser/browser_process.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"

namespace electron {

namespace api {

void WebContents::SetAcceptLanguages(blink::mojom::RendererPreferences* prefs) {
prefs->accept_languages = g_browser_process->GetApplicationLocale();
}

} // namespace api

} // namespace electron
7 changes: 7 additions & 0 deletions shell/browser/api/electron_api_web_contents_mac.mm
Expand Up @@ -2,8 +2,10 @@
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "base/strings/sys_string_conversions.h"
#include "content/public/browser/render_widget_host_view.h"
#include "shell/browser/api/electron_api_web_contents.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"

#import <Cocoa/Cocoa.h>

Expand All @@ -27,6 +29,11 @@
return view->HasFocus();
}

void WebContents::SetAcceptLanguages(blink::mojom::RendererPreferences* prefs) {
prefs->accept_languages = base::SysNSStringToUTF8(
[[NSLocale preferredLanguages] componentsJoinedByString:@","]);
}

} // namespace api

} // namespace electron
82 changes: 82 additions & 0 deletions shell/browser/api/electron_api_web_contents_win.cc
@@ -0,0 +1,82 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#include "shell/browser/api/electron_api_web_contents.h"

#include <roapi.h>
#include <windows.system.userprofile.h>
#include <wrl.h>

#include "base/strings/sys_string_conversions.h"
#include "base/win/core_winrt_util.h"
#include "base/win/i18n.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "third_party/blink/public/mojom/renderer_preferences.mojom.h"

namespace electron {

namespace api {

void WebContents::SetAcceptLanguages(blink::mojom::RendererPreferences* prefs) {
std::vector<base::string16> languages;

// Attempt to use API available on Windows 10 or later, which
// returns the full list of language preferences.
if (!GetLanguagesUsingGlobalization(&languages)) {
base::win::i18n::GetThreadPreferredUILanguageList(&languages);
}

std::string accept_langs;
for (const auto& language : languages) {
accept_langs += base::SysWideToUTF8(language) + ',';
}
accept_langs.pop_back();
prefs->accept_languages = accept_langs;
}

bool WebContents::GetLanguagesUsingGlobalization(
std::vector<base::string16>* languages) {
if (base::win::GetVersion() < base::win::Version::WIN10)
return false;
if (!base::win::ResolveCoreWinRTDelayload() ||
!base::win::ScopedHString::ResolveCoreWinRTStringDelayload())
return false;

base::win::ScopedHString guid = base::win::ScopedHString::Create(
RuntimeClass_Windows_System_UserProfile_GlobalizationPreferences);
Microsoft::WRL::ComPtr<
ABI::Windows::System::UserProfile::IGlobalizationPreferencesStatics>
prefs;

HRESULT hr =
base::win::RoGetActivationFactory(guid.get(), IID_PPV_ARGS(&prefs));
if (FAILED(hr))
return false;

ABI::Windows::Foundation::Collections::IVectorView<HSTRING>* langs;
hr = prefs->get_Languages(&langs);
if (FAILED(hr))
return false;

unsigned size;
hr = langs->get_Size(&size);
if (FAILED(hr))
return false;

for (unsigned i = 0; i < size; ++i) {
HSTRING hstr;
hr = langs->GetAt(i, &hstr);
if (SUCCEEDED(hr)) {
base::WStringPiece str = base::win::ScopedHString(hstr).Get();
languages->emplace_back(str.data(), str.size());
}
}

return true;
}

} // namespace api

} // namespace electron
3 changes: 2 additions & 1 deletion spec-main/chromium-spec.ts
Expand Up @@ -319,7 +319,8 @@ describe('chromium features', () => {
const w = new BrowserWindow({ show: false });
await w.loadURL('about:blank');
const languages = await w.webContents.executeJavaScript('navigator.languages');
expect(languages).to.deep.equal([appLocale]);
expect(languages.length).to.be.greaterThan(0);
expect(languages).to.contain(appLocale);
});
});

Expand Down

0 comments on commit 02de6fa

Please sign in to comment.