Skip to content

Commit da75a01

Browse files
Make WebLocalizedString() thread-safe
https://bugs.webkit.org/show_bug.cgi?id=230954 Reviewed by Alexey Proskuryakov. Make WebLocalizedString() thread-safe. It takes minimal effort to make it thread-safe given that the NSBundle API is thread-safe [1] and clients keep calling it on the background thread. [1] https://developer.apple.com/documentation/foundation/nslocalizedstringwithdefaultvalue "As of OS X 10.11 and iOS 9, NSBundle is thread-safe. As such, you can safely call NSLocalizedStringWithDefaultValue from any execution context." * Misc/WebLocalizableStrings.mm: git-svn-id: http://svn.webkit.org/repository/webkit/trunk@283225 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent a5511e2 commit da75a01

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

Source/WebKitLegacy/mac/ChangeLog

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
2021-09-29 Chris Dumez <[email protected]>
2+
3+
Make WebLocalizedString() thread-safe
4+
https://bugs.webkit.org/show_bug.cgi?id=230954
5+
6+
Reviewed by Alexey Proskuryakov.
7+
8+
Make WebLocalizedString() thread-safe. It takes minimal effort to make it thread-safe given
9+
that the NSBundle API is thread-safe [1] and clients keep calling it on the background thread.
10+
11+
[1] https://developer.apple.com/documentation/foundation/nslocalizedstringwithdefaultvalue
12+
"As of OS X 10.11 and iOS 9, NSBundle is thread-safe. As such, you can safely call
13+
NSLocalizedStringWithDefaultValue from any execution context."
14+
15+
* Misc/WebLocalizableStrings.mm:
16+
117
2021-09-28 Chris Dumez <[email protected]>
218

319
Move Cross-Origin-Opener-Policy handling to the NetworkProcess

Source/WebKitLegacy/mac/Misc/WebLocalizableStrings.mm

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,13 @@
3737

3838
NSString *WebLocalizedString(WebLocalizableStringsBundle *stringsBundle, const char *key)
3939
{
40-
// This function is not thread-safe due at least to its unguarded use of the mainBundle static variable
41-
// and its use of [NSBundle localizedStringForKey:::], which is not guaranteed to be thread-safe. If
42-
// we decide we need to use this on background threads, we'll need to add locking here and make sure
43-
// it doesn't affect performance.
44-
#if !PLATFORM(IOS_FAMILY)
45-
ASSERT(isMainThread());
46-
#endif
47-
4840
NSBundle *bundle;
4941
if (stringsBundle == NULL) {
50-
static NeverDestroyed<RetainPtr<NSBundle>> mainBundle = [NSBundle mainBundle];
42+
static LazyNeverDestroyed<RetainPtr<NSBundle>> mainBundle;
43+
static std::once_flag flag;
44+
std::call_once(flag, [] () {
45+
mainBundle.construct([NSBundle mainBundle]);
46+
});
5147
ASSERT(mainBundle.get());
5248
bundle = mainBundle.get().get();
5349
} else {

0 commit comments

Comments
 (0)