diff --git a/filenames.gni b/filenames.gni index ca3db4b5cddf7..e312a6eb70cf2 100644 --- a/filenames.gni +++ b/filenames.gni @@ -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", diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index b65096d368ad3..e61a7aa5a596b 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -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" @@ -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 @@ -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. diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 980ff85b0630f..e4f5977f1fd44 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -579,6 +579,12 @@ class WebContents : public gin_helper::TrackableObject, 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* languages); +#endif + v8::Global session_; v8::Global devtools_web_contents_; v8::Global debugger_; diff --git a/shell/browser/api/electron_api_web_contents_linux.cc b/shell/browser/api/electron_api_web_contents_linux.cc new file mode 100644 index 0000000000000..51e5fcb20e630 --- /dev/null +++ b/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 diff --git a/shell/browser/api/electron_api_web_contents_mac.mm b/shell/browser/api/electron_api_web_contents_mac.mm index ea70eed08d522..54c726374ea77 100644 --- a/shell/browser/api/electron_api_web_contents_mac.mm +++ b/shell/browser/api/electron_api_web_contents_mac.mm @@ -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 @@ -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 diff --git a/shell/browser/api/electron_api_web_contents_win.cc b/shell/browser/api/electron_api_web_contents_win.cc new file mode 100644 index 0000000000000..598833da56c32 --- /dev/null +++ b/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 +#include +#include + +#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 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* 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* 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 diff --git a/spec-main/chromium-spec.ts b/spec-main/chromium-spec.ts index 2a4320d393427..894f856baa6ce 100644 --- a/spec-main/chromium-spec.ts +++ b/spec-main/chromium-spec.ts @@ -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); }); });