Skip to content

Commit

Permalink
refactor: Move OS utility to language_util
Browse files Browse the repository at this point in the history
  • Loading branch information
sorah committed Apr 28, 2020
1 parent 02de6fa commit d947ed9
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 50 deletions.
6 changes: 4 additions & 2 deletions filenames.gni
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,9 @@ 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 Expand Up @@ -547,6 +545,10 @@ filenames = {
"shell/common/key_weak_map.h",
"shell/common/keyboard_util.cc",
"shell/common/keyboard_util.h",
"shell/common/language_util.h",
"shell/common/language_util_linux.cc",
"shell/common/language_util_mac.mm",
"shell/common/language_util_win.cc",
"shell/common/mac/main_application_bundle.h",
"shell/common/mac/main_application_bundle.mm",
"shell/common/mouse_util.cc",
Expand Down
19 changes: 18 additions & 1 deletion shell/browser/api/electron_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/language_util.h"
#include "shell/common/mouse_util.h"
#include "shell/common/node_includes.h"
#include "shell/common/options_switches.h"
Expand All @@ -102,6 +103,7 @@
#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/l10n/l10n_util.h"
#include "ui/base/mojom/cursor_type.mojom-shared.h"
#include "ui/display/screen.h"
#include "ui/events/base_event_utils.h"
Expand Down Expand Up @@ -535,7 +537,22 @@ void WebContents::InitWithSessionAndOptions(
managed_web_contents()->GetView()->SetDelegate(this);

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

// Collect preferred languages from OS and browser process. accept_languages
// affects HTTP header, navigator.languages, and CJK fallback font seleciton.
//
// Note that an application locale set to the browser process might be
// different with the one set to the preference list.
// (e.g. overridden with --lang)
std::string accept_languages =
g_browser_process->GetApplicationLocale() + ",";
for (auto const& language : electron::GetPreferredLanguages()) {
if (language == g_browser_process->GetApplicationLocale())
continue;
accept_languages += language + ",";
}
accept_languages.pop_back();
prefs->accept_languages = accept_languages;

#if defined(OS_LINUX) || defined(OS_WIN)
// Update font settings.
Expand Down
6 changes: 0 additions & 6 deletions shell/browser/api/electron_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,6 @@ 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: 0 additions & 20 deletions shell/browser/api/electron_api_web_contents_linux.cc

This file was deleted.

5 changes: 0 additions & 5 deletions shell/browser/api/electron_api_web_contents_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
return view->HasFocus();
}

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

} // namespace api

} // namespace electron
26 changes: 26 additions & 0 deletions shell/common/language_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2020 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.

#ifndef SHELL_COMMON_LANGUAGE_UTIL_H_
#define SHELL_COMMON_LANGUAGE_UTIL_H_

#include <string>
#include <vector>

#include "base/strings/string16.h"

namespace electron {

// Return a list of user preferred languages from OS. The list doesn't include
// overrides from command line arguments.
std::vector<std::string> GetPreferredLanguages();

#if defined(OS_WIN)
bool GetPreferredLanguagesUsingGlobalization(
std::vector<base::string16>* languages);
#endif

} // namespace electron

#endif // SHELL_COMMON_LANGUAGE_UTIL_H_
16 changes: 16 additions & 0 deletions shell/common/language_util_linux.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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/common/language_util.h"

#include "ui/base/l10n/l10n_util.h"

namespace electron {

std::vector<std::string> GetPreferredLanguages() {
std::vector<std::string> languages = {l10n_util::GetApplicationLocale("")};
return languages;
}

} // namespace electron
23 changes: 23 additions & 0 deletions shell/common/language_util_mac.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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/common/language_util.h"

#import <Cocoa/Cocoa.h>

#include "base/strings/sys_string_conversions.h"

namespace electron {

std::vector<std::string> GetPreferredLanguages() {
std::vector<std::string> languages;
[[NSLocale preferredLanguages]
enumerateObjectsUsingBlock:^(NSString* language, NSUInteger i,
BOOL* stop) {
languages.push_back(base::SysNSStringToUTF8(language));
}];
return languages;
}

} // namespace electron
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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 "shell/common/language_util.h"

#include <roapi.h>
#include <windows.system.userprofile.h>
Expand All @@ -13,30 +13,26 @@
#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;
std::vector<std::string> GetPreferredLanguages() {
std::vector<base::string16> languages16;

// 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);
if (!GetPreferredLanguagesUsingGlobalization(&languages16)) {
base::win::i18n::GetThreadPreferredUILanguageList(&languages16);
}

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

bool WebContents::GetLanguagesUsingGlobalization(
bool GetPreferredLanguagesUsingGlobalization(
std::vector<base::string16>* languages) {
if (base::win::GetVersion() < base::win::Version::WIN10)
return false;
Expand Down Expand Up @@ -77,6 +73,4 @@ bool WebContents::GetLanguagesUsingGlobalization(
return true;
}

} // namespace api

} // namespace electron

0 comments on commit d947ed9

Please sign in to comment.