Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit 7393f13

Browse files
[Cocoa] Global preferences are not accessible in the WebContent process when CFPrefs direct mode is enabled
https://bugs.webkit.org/show_bug.cgi?id=211075 Source/WebKit: <rdar://problem/61866711> Reviewed by Brent Fulgham. Global preferences in the domain 'kCFPreferencesAnyApplication' are not readable in the WebContent process when CFPrefs direct mode is enabled. Fix this by transferring a select set of global preferences to the WebContent process on startup. API test: WebKit.GlobalPreferences * Shared/WebProcessCreationParameters.cpp: (WebKit::WebProcessCreationParameters::encode const): (WebKit::WebProcessCreationParameters::decode): * Shared/WebProcessCreationParameters.h: * UIProcess/Cocoa/WebProcessPoolCocoa.mm: (WebKit::WebProcessPool::platformInitializeWebProcess): * WebProcess/cocoa/WebProcessCocoa.mm: (WebKit::setGlobalPreferences): (WebKit::WebProcess::platformInitializeWebProcess): Tools: Reviewed by Brent Fulgham. * TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm: (TEST): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@260840 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 95b6f5c commit 7393f13

File tree

7 files changed

+127
-2
lines changed

7 files changed

+127
-2
lines changed

Source/WebKit/ChangeLog

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
2020-04-28 Per Arne Vollan <[email protected]>
2+
3+
[Cocoa] Global preferences are not accessible in the WebContent process when CFPrefs direct mode is enabled
4+
https://bugs.webkit.org/show_bug.cgi?id=211075
5+
<rdar://problem/61866711>
6+
7+
Reviewed by Brent Fulgham.
8+
9+
Global preferences in the domain 'kCFPreferencesAnyApplication' are not readable in the WebContent process when CFPrefs direct mode
10+
is enabled. Fix this by transferring a select set of global preferences to the WebContent process on startup.
11+
12+
API test: WebKit.GlobalPreferences
13+
14+
* Shared/WebProcessCreationParameters.cpp:
15+
(WebKit::WebProcessCreationParameters::encode const):
16+
(WebKit::WebProcessCreationParameters::decode):
17+
* Shared/WebProcessCreationParameters.h:
18+
* UIProcess/Cocoa/WebProcessPoolCocoa.mm:
19+
(WebKit::WebProcessPool::platformInitializeWebProcess):
20+
* WebProcess/cocoa/WebProcessCocoa.mm:
21+
(WebKit::setGlobalPreferences):
22+
(WebKit::WebProcess::platformInitializeWebProcess):
23+
124
2020-04-28 Peng Liu <[email protected]>
225

326
Remove the WebKit.plist for Feature Flags

Source/WebKit/Shared/WebProcessCreationParameters.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ void WebProcessCreationParameters::encode(IPC::Encoder& encoder) const
194194
encoder << mediaExtensionHandles;
195195
#if ENABLE(CFPREFS_DIRECT_MODE)
196196
encoder << preferencesExtensionHandles;
197+
encoder << encodedGlobalPreferences;
197198
#endif
198199
#endif
199200

@@ -542,6 +543,12 @@ bool WebProcessCreationParameters::decode(IPC::Decoder& decoder, WebProcessCreat
542543
if (!preferencesExtensionHandles)
543544
return false;
544545
parameters.preferencesExtensionHandles = WTFMove(*preferencesExtensionHandles);
546+
547+
Optional<String> encodedGlobalPreferences;
548+
decoder >> encodedGlobalPreferences;
549+
if (!encodedGlobalPreferences)
550+
return false;
551+
parameters.encodedGlobalPreferences = WTFMove(*encodedGlobalPreferences);
545552
#endif
546553
#endif
547554

Source/WebKit/Shared/WebProcessCreationParameters.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ struct WebProcessCreationParameters {
231231
SandboxExtension::HandleArray mediaExtensionHandles; // FIXME(207716): Remove when GPU process is complete.
232232
#if ENABLE(CFPREFS_DIRECT_MODE)
233233
Optional<SandboxExtension::HandleArray> preferencesExtensionHandles;
234+
String encodedGlobalPreferences;
234235
#endif
235236
#endif
236237

Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ static bool isInternalInstall()
454454
SandboxExtension::createHandleForMachLookup(services[i], WTF::nullopt, parameters.mediaExtensionHandles[i]);
455455
}
456456

457-
#if ENABLE(CFPREFS_DIRECT_MODE) && PLATFORM(IOS_FAMILY)
457+
#if ENABLE(CFPREFS_DIRECT_MODE)
458+
#if PLATFORM(IOS_FAMILY)
458459
if (_AXSApplicationAccessibilityEnabled()) {
459460
SandboxExtension::HandleArray preferencesExtensionHandles;
460461

@@ -472,6 +473,29 @@ static bool isInternalInstall()
472473
parameters.preferencesExtensionHandles = WTFMove(preferencesExtensionHandles);
473474
}
474475
#endif
476+
477+
auto globalPreferencesDictionary = adoptCF(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, nullptr, nullptr));
478+
static CFStringRef keys[] = {
479+
CFSTR("AppleLanguages"),
480+
CFSTR("AppleLanguagesSchemaVersion"),
481+
CFSTR("AppleLocale")
482+
};
483+
for (size_t i = 0; i < std::size(keys); ++i) {
484+
auto value = adoptCF(CFPreferencesCopyAppValue(keys[i], CFSTR("kCFPreferencesAnyApplication")));
485+
if (!value)
486+
continue;
487+
CFDictionaryAddValue(globalPreferencesDictionary.get(), keys[i], value.get());
488+
}
489+
if (CFDictionaryGetCount(globalPreferencesDictionary.get()) > 0) {
490+
NSError *e = nil;
491+
auto data = retainPtr([NSKeyedArchiver archivedDataWithRootObject:(__bridge NSMutableDictionary *)globalPreferencesDictionary.get() requiringSecureCoding:YES error:&e]);
492+
if (e) {
493+
ASSERT_NOT_REACHED();
494+
WTFLogAlways("Failed to archive global preferences dictionary with NSKeyedArchiver.");
495+
} else
496+
parameters.encodedGlobalPreferences = String([data base64EncodedStringWithOptions:0]);
497+
}
498+
#endif
475499
}
476500

477501
void WebProcessPool::platformInitializeNetworkProcess(NetworkProcessCreationParameters& parameters)

Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,37 @@ static id NSApplicationAccessibilityFocusedUIElement(NSApplication*, SEL)
165165
}
166166
#endif
167167

168+
#if ENABLE(CFPREFS_DIRECT_MODE)
169+
static void setGlobalPreferences(const String& encodedGlobalPreferences)
170+
{
171+
if (encodedGlobalPreferences.isEmpty())
172+
return;
173+
174+
auto encodedData = adoptNS([[NSData alloc] initWithBase64EncodedString:encodedGlobalPreferences options:0]);
175+
if (!encodedData)
176+
return;
177+
178+
NSError *err = nil;
179+
auto classes = [NSSet setWithArray:@[[NSString class], [NSNumber class], [NSDate class], [NSDictionary class], [NSArray class], [NSData class]]];
180+
id globalPreferencesDictionary = [NSKeyedUnarchiver unarchivedObjectOfClasses:classes fromData:encodedData.get() error:&err];
181+
if (err) {
182+
ASSERT_NOT_REACHED();
183+
WTFLogAlways("Failed to unarchive global preferences dictionary with NSKeyedUnarchiver.");
184+
return;
185+
}
186+
[globalPreferencesDictionary enumerateKeysAndObjectsUsingBlock: ^(NSString *key, id value, BOOL* stop) {
187+
if (value)
188+
CFPreferencesSetAppValue(static_cast<CFStringRef>(key), static_cast<CFPropertyListRef>(value), CFSTR("kCFPreferencesAnyApplication"));
189+
}];
190+
}
191+
#endif
192+
168193
void WebProcess::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
169194
{
195+
#if ENABLE(CFPREFS_DIRECT_MODE)
196+
setGlobalPreferences(parameters.encodedGlobalPreferences);
197+
#endif
198+
170199
// Map Launch Services database. This should be done as early as possible, as the mapping will fail
171200
// if 'com.apple.lsd.mapdb' is being accessed before this.
172201
if (parameters.mapDBExtensionHandle) {
@@ -179,7 +208,6 @@ static id NSApplicationAccessibilityFocusedUIElement(NSApplication*, SEL)
179208
ASSERT_UNUSED(ok, ok);
180209
}
181210

182-
183211
#if PLATFORM(IOS_FAMILY)
184212
if (parameters.runningboardExtensionHandle) {
185213
auto extension = SandboxExtension::create(WTFMove(*parameters.runningboardExtensionHandle));

Tools/ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2020-04-28 Per Arne Vollan <[email protected]>
2+
3+
[Cocoa] Global preferences are not accessible in the WebContent process when CFPrefs direct mode is enabled
4+
https://bugs.webkit.org/show_bug.cgi?id=211075
5+
6+
Reviewed by Brent Fulgham.
7+
8+
* TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm:
9+
(TEST):
10+
111
2020-04-28 Jonathan Bedard <[email protected]>
212

313
results.webkit.org: A suite running multiple times in a commit for a single configuration may overwrite upload metadata

Tools/TestWebKitAPI/Tests/WebKit/PreferenceChanges.mm

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,36 @@ - (void)preferenceDidChange:(NSString *)domain key:(NSString *)key encodedValue:
276276
TestWebKitAPI::Util::run(&done);
277277
}
278278

279+
TEST(WebKit, GlobalPreferences)
280+
{
281+
NSString *globalDomain = @"kCFPreferencesAnyApplication";
282+
NSString *globalKey = @"AppleLanguages";
283+
284+
NSArray *languages = @[@"en-US", @"nb-US"];
285+
286+
CFPreferencesSetAppValue(static_cast<CFStringRef>(globalKey), static_cast<CFArrayRef>(languages), static_cast<CFStringRef>(globalDomain));
287+
288+
auto userDefaults = adoptNS([[NSUserDefaults alloc] initWithSuiteName:globalDomain]);
289+
[userDefaults.get() setObject:languages forKey:globalKey];
290+
291+
auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
292+
WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
293+
configuration.get().processPool = (WKProcessPool *)context.get();
294+
auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
295+
296+
auto preferenceValue = [&] {
297+
NSString *js = [NSString stringWithFormat:@"window.internals.encodedPreferenceValue(\"%@\",\"%@\")", globalDomain, globalKey];
298+
return [webView stringByEvaluatingJavaScript:js];
299+
};
300+
301+
auto encodedString = preferenceValue();
302+
auto encodedData = adoptNS([[NSData alloc] initWithBase64EncodedString:encodedString options:0]);
303+
ASSERT_TRUE(encodedData);
304+
NSError *err = nil;
305+
auto object = retainPtr([NSKeyedUnarchiver unarchivedObjectOfClass:[NSObject class] fromData:encodedData.get() error:&err]);
306+
ASSERT_TRUE(!err);
307+
ASSERT_TRUE(object);
308+
ASSERT_TRUE([object isEqual:languages]);
309+
}
310+
279311
#endif // WK_HAVE_C_SPI

0 commit comments

Comments
 (0)