24a37de90852890a2fabe3f9a959d9d45b8c5860
[WebKit-https.git] / Tools / MiniBrowser / mac / SettingsController.m
1 /*
2  * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "SettingsController.h"
27
28 #import "AppDelegate.h"
29 #import "AppKitCompatibilityDeclarations.h"
30 #import "BrowserWindowController.h"
31 #import <WebKit/WKPreferencesPrivate.h>
32
33 #if WK_API_ENABLED
34 #import <WebKit/_WKExperimentalFeature.h>
35 #import <WebKit/_WKInternalDebugFeature.h>
36 #endif
37
38 static NSString * const defaultURL = @"http://www.webkit.org/";
39 static NSString * const DefaultURLPreferenceKey = @"DefaultURL";
40
41 static NSString * const UseWebKit2ByDefaultPreferenceKey = @"UseWebKit2ByDefault";
42 static NSString * const CreateEditorByDefaultPreferenceKey = @"CreateEditorByDefault";
43 static NSString * const LayerBordersVisiblePreferenceKey = @"LayerBordersVisible";
44 static NSString * const SimpleLineLayoutEnabledPreferenceKey = @"SimpleLineLayoutEnabled";
45 static NSString * const SimpleLineLayoutDebugBordersEnabledPreferenceKey = @"SimpleLineLayoutDebugBordersEnabled";
46 static NSString * const TiledScrollingIndicatorVisiblePreferenceKey = @"TiledScrollingIndicatorVisible";
47 static NSString * const ReserveSpaceForBannersPreferenceKey = @"ReserveSpaceForBanners";
48
49 static NSString * const ResourceUsageOverlayVisiblePreferenceKey = @"ResourceUsageOverlayVisible";
50 static NSString * const LoadsAllSiteIconsKey = @"LoadsAllSiteIcons";
51 static NSString * const UsesGameControllerFrameworkKey = @"UsesGameControllerFramework";
52 static NSString * const IncrementalRenderingSuppressedPreferenceKey = @"IncrementalRenderingSuppressed";
53 static NSString * const AcceleratedDrawingEnabledPreferenceKey = @"AcceleratedDrawingEnabled";
54 static NSString * const DisplayListDrawingEnabledPreferenceKey = @"DisplayListDrawingEnabled";
55 static NSString * const SubpixelAntialiasedLayerTextEnabledPreferenceKey = @"SubpixelAntialiasedLayerTextEnabled";
56 static NSString * const ResourceLoadStatisticsEnabledPreferenceKey = @"ResourceLoadStatisticsEnabled";
57
58 static NSString * const NonFastScrollableRegionOverlayVisiblePreferenceKey = @"NonFastScrollableRegionOverlayVisible";
59 static NSString * const WheelEventHandlerRegionOverlayVisiblePreferenceKey = @"WheelEventHandlerRegionOverlayVisible";
60
61 static NSString * const UseTransparentWindowsPreferenceKey = @"UseTransparentWindows";
62 static NSString * const UsePaginatedModePreferenceKey = @"UsePaginatedMode";
63 static NSString * const EnableSubPixelCSSOMMetricsPreferenceKey = @"EnableSubPixelCSSOMMetrics";
64
65 static NSString * const VisualViewportEnabledPreferenceKey = @"VisualViewportEnabled";
66 static NSString * const LargeImageAsyncDecodingEnabledPreferenceKey = @"LargeImageAsyncDecodingEnabled";
67 static NSString * const AnimatedImageAsyncDecodingEnabledPreferenceKey = @"AnimatedImageAsyncDecodingEnabled";
68 static NSString * const AppleColorFilterEnabledPreferenceKey = @"AppleColorFilterEnabled";
69 static NSString * const PunchOutWhiteBackgroundsInDarkModePreferenceKey = @"PunchOutWhiteBackgroundsInDarkMode";
70 static NSString * const UseSystemAppearancePreferenceKey = @"UseSystemAppearance";
71
72 // This default name intentionally overlaps with the key that WebKit2 checks when creating a view.
73 static NSString * const UseRemoteLayerTreeDrawingAreaPreferenceKey = @"WebKit2UseRemoteLayerTreeDrawingArea";
74
75 static NSString * const PerWindowWebProcessesDisabledKey = @"PerWindowWebProcessesDisabled";
76 static NSString * const NetworkCacheSpeculativeRevalidationDisabledKey = @"NetworkCacheSpeculativeRevalidationDisabled";
77 static NSString * const ProcessSwapOnNavigationKey = @"ProcessSwapOnNavigation";
78 static NSString * const ProcessSwapOnWindowOpenWithOpenerKey = @"ProcessSwapOnWindowOpenWithOpener";
79
80 typedef NS_ENUM(NSInteger, DebugOverylayMenuItemTag) {
81     NonFastScrollableRegionOverlayTag = 100,
82     WheelEventHandlerRegionOverlayTag,
83 #if WK_API_ENABLED
84     ExperimentalFeatureTag,
85     InternalDebugFeatureTag,
86 #endif
87 };
88
89 @implementation SettingsController
90
91 @synthesize menu=_menu;
92
93 + (instancetype)shared
94 {
95     static SettingsController *sharedSettingsController;
96     static dispatch_once_t onceToken;
97     dispatch_once(&onceToken, ^{
98         sharedSettingsController = [[super alloc] init];
99     });
100
101     return sharedSettingsController;
102 }
103
104 - (instancetype)init
105 {
106     self = [super init];
107     if (!self)
108         return nil;
109
110     NSArray *onByDefaultPrefs = @[
111         UseWebKit2ByDefaultPreferenceKey,
112         AcceleratedDrawingEnabledPreferenceKey,
113         SimpleLineLayoutEnabledPreferenceKey,
114 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
115         SubpixelAntialiasedLayerTextEnabledPreferenceKey,
116 #endif
117         VisualViewportEnabledPreferenceKey,
118         LargeImageAsyncDecodingEnabledPreferenceKey,
119         AnimatedImageAsyncDecodingEnabledPreferenceKey,
120     ];
121     
122     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
123     for (NSString *prefName in onByDefaultPrefs) {
124         if (![userDefaults objectForKey:prefName])
125             [userDefaults setBool:YES forKey:prefName];
126     }
127
128     return self;
129 }
130
131 - (NSMenu *)menu
132 {
133     if (!_menu)
134         [self _populateMenu];
135
136     return _menu;
137 }
138
139 - (void)_addItemWithTitle:(NSString *)title action:(SEL)action indented:(BOOL)indented
140 {
141     NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:action keyEquivalent:@""];
142     [item setTarget:self];
143     if (indented)
144         [item setIndentationLevel:1];
145     [_menu addItem:item];
146     [item release];
147 }
148
149 - (void)_addHeaderWithTitle:(NSString *)title
150 {
151     [_menu addItem:[NSMenuItem separatorItem]];
152     [_menu addItem:[[[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""] autorelease]];
153 }
154
155 - (void)_populateMenu
156 {
157     _menu = [[NSMenu alloc] initWithTitle:@"Settings"];
158
159     [self _addItemWithTitle:@"Use WebKit2 By Default" action:@selector(toggleUseWebKit2ByDefault:) indented:NO];
160     [self _addItemWithTitle:@"Create Editor By Default" action:@selector(toggleCreateEditorByDefault:) indented:NO];
161     [self _addItemWithTitle:@"Set Default URL to Current URL" action:@selector(setDefaultURLToCurrentURL:) indented:NO];
162
163     [_menu addItem:[NSMenuItem separatorItem]];
164
165     [self _addItemWithTitle:@"Use Transparent Windows" action:@selector(toggleUseTransparentWindows:) indented:NO];
166     [self _addItemWithTitle:@"Use Paginated Mode" action:@selector(toggleUsePaginatedMode:) indented:NO];
167     [self _addItemWithTitle:@"Show Layer Borders" action:@selector(toggleShowLayerBorders:) indented:NO];
168     [self _addItemWithTitle:@"Disable Simple Line Layout" action:@selector(toggleSimpleLineLayoutEnabled:) indented:NO];
169     [self _addItemWithTitle:@"Show Simple Line Layout Borders" action:@selector(toggleSimpleLineLayoutDebugBordersEnabled:) indented:NO];
170     [self _addItemWithTitle:@"Suppress Incremental Rendering in New Windows" action:@selector(toggleIncrementalRenderingSuppressed:) indented:NO];
171     [self _addItemWithTitle:@"Enable Accelerated Drawing" action:@selector(toggleAcceleratedDrawingEnabled:) indented:NO];
172     [self _addItemWithTitle:@"Enable Display List Drawing" action:@selector(toggleDisplayListDrawingEnabled:) indented:NO];
173     [self _addItemWithTitle:@"Enable Subpixel-antialiased Layer Text" action:@selector(toggleSubpixelAntialiasedLayerTextEnabled:) indented:NO];
174     [self _addItemWithTitle:@"Enable Visual Viewport" action:@selector(toggleVisualViewportEnabled:) indented:NO];
175     [self _addItemWithTitle:@"Enable Resource Load Statistics" action:@selector(toggleResourceLoadStatisticsEnabled:) indented:NO];
176     [self _addItemWithTitle:@"Enable Large Image Async Decoding" action:@selector(toggleLargeImageAsyncDecodingEnabled:) indented:NO];
177     [self _addItemWithTitle:@"Enable Animated Image Async Decoding" action:@selector(toggleAnimatedImageAsyncDecodingEnabled:) indented:NO];
178     [self _addItemWithTitle:@"Enable color-filter" action:@selector(toggleAppleColorFilterEnabled:) indented:NO];
179     [self _addItemWithTitle:@"Punch Out White Backgrounds in Dark Mode" action:@selector(togglePunchOutWhiteBackgroundsInDarkMode:) indented:NO];
180     [self _addItemWithTitle:@"Use System Appearance" action:@selector(toggleUseSystemAppearance:) indented:NO];
181     [self _addHeaderWithTitle:@"WebKit2-only Settings"];
182
183     [self _addItemWithTitle:@"Reserve Space For Banners" action:@selector(toggleReserveSpaceForBanners:) indented:YES];
184     [self _addItemWithTitle:@"Show Tiled Scrolling Indicator" action:@selector(toggleShowTiledScrollingIndicator:) indented:YES];
185     [self _addItemWithTitle:@"Use UI-Side Compositing" action:@selector(toggleUseUISideCompositing:) indented:YES];
186     [self _addItemWithTitle:@"Disable Per-Window Web Processes" action:@selector(togglePerWindowWebProcessesDisabled:) indented:YES];
187     [self _addItemWithTitle:@"Show Resource Usage Overlay" action:@selector(toggleShowResourceUsageOverlay:) indented:YES];
188     [self _addItemWithTitle:@"Load All Site Icons Per-Page" action:@selector(toggleLoadsAllSiteIcons:) indented:YES];
189     [self _addItemWithTitle:@"Use GameController.framework on macOS (Restart required)" action:@selector(toggleUsesGameControllerFramework:) indented:YES];
190     [self _addItemWithTitle:@"Disable network cache speculative revalidation" action:@selector(toggleNetworkCacheSpeculativeRevalidationDisabled:) indented:YES];
191     [self _addItemWithTitle:@"Enable Process Swap on Navigation" action:@selector(toggleProcessSwapOnNavigation:) indented:YES];
192     [self _addItemWithTitle:@"Enable Process Swap on window.open() with an opener" action:@selector(toggleProcessSwapOnWindowOpenWithOpener:) indented:YES];
193
194     NSMenuItem *debugOverlaysSubmenuItem = [[NSMenuItem alloc] initWithTitle:@"Debug Overlays" action:nil keyEquivalent:@""];
195     NSMenu *debugOverlaysMenu = [[NSMenu alloc] initWithTitle:@"Debug Overlays"];
196     [debugOverlaysSubmenuItem setSubmenu:debugOverlaysMenu];
197
198     NSMenuItem *nonFastScrollableRegionItem = [[NSMenuItem alloc] initWithTitle:@"Non-fast Scrollable Region" action:@selector(toggleDebugOverlay:) keyEquivalent:@""];
199     [nonFastScrollableRegionItem setTag:NonFastScrollableRegionOverlayTag];
200     [nonFastScrollableRegionItem setTarget:self];
201     [debugOverlaysMenu addItem:[nonFastScrollableRegionItem autorelease]];
202
203     NSMenuItem *wheelEventHandlerRegionItem = [[NSMenuItem alloc] initWithTitle:@"Wheel Event Handler Region" action:@selector(toggleDebugOverlay:) keyEquivalent:@""];
204     [wheelEventHandlerRegionItem setTag:WheelEventHandlerRegionOverlayTag];
205     [wheelEventHandlerRegionItem setTarget:self];
206     [debugOverlaysMenu addItem:[wheelEventHandlerRegionItem autorelease]];
207     [debugOverlaysMenu release];
208     
209     [_menu addItem:debugOverlaysSubmenuItem];
210     [debugOverlaysSubmenuItem release];
211
212 #if WK_API_ENABLED
213     NSMenuItem *experimentalFeaturesSubmenuItem = [[NSMenuItem alloc] initWithTitle:@"Experimental Features" action:nil keyEquivalent:@""];
214     NSMenu *experimentalFeaturesMenu = [[NSMenu alloc] initWithTitle:@"Experimental Features"];
215     [experimentalFeaturesSubmenuItem setSubmenu:experimentalFeaturesMenu];
216
217     NSArray<_WKExperimentalFeature *> *experimentalFeatures = [WKPreferences _experimentalFeatures];
218
219     for (_WKExperimentalFeature *feature in experimentalFeatures) {
220         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:feature.name action:@selector(toggleExperimentalFeature:) keyEquivalent:@""];
221         item.toolTip = feature.details;
222         item.representedObject = feature;
223
224         [item setTag:ExperimentalFeatureTag];
225         [item setTarget:self];
226         [experimentalFeaturesMenu addItem:[item autorelease]];
227     }
228
229     [_menu addItem:experimentalFeaturesSubmenuItem];
230     [experimentalFeaturesSubmenuItem release];
231     [experimentalFeaturesMenu release];
232
233     NSMenuItem *internalDebugFeaturesSubmenuItem = [[NSMenuItem alloc] initWithTitle:@"Internal Features" action:nil keyEquivalent:@""];
234     NSMenu *internalDebugFeaturesMenu = [[NSMenu alloc] initWithTitle:@"Internal Features"];
235     [internalDebugFeaturesSubmenuItem setSubmenu:internalDebugFeaturesMenu];
236
237     NSArray<_WKInternalDebugFeature *> *internalDebugFeatures = [WKPreferences _internalDebugFeatures];
238
239     for (_WKInternalDebugFeature *feature in internalDebugFeatures) {
240         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:feature.name action:@selector(toggleInternalDebugFeature:) keyEquivalent:@""];
241         item.toolTip = feature.details;
242         item.representedObject = feature;
243
244         [item setTag:InternalDebugFeatureTag];
245         [item setTarget:self];
246         [internalDebugFeaturesMenu addItem:[item autorelease]];
247     }
248
249     [_menu addItem:internalDebugFeaturesSubmenuItem];
250     [internalDebugFeaturesSubmenuItem release];
251     [internalDebugFeaturesMenu release];
252 #endif // WK_API_ENABLED
253
254     [self _addHeaderWithTitle:@"WebKit1-only Settings"];
255     [self _addItemWithTitle:@"Enable Subpixel CSSOM Metrics" action:@selector(toggleEnableSubPixelCSSOMMetrics:) indented:YES];
256 }
257
258 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
259 {
260     SEL action = [menuItem action];
261
262     if (action == @selector(toggleUseWebKit2ByDefault:))
263         [menuItem setState:[self useWebKit2ByDefault] ? NSControlStateValueOn : NSControlStateValueOff];
264     else if (action == @selector(toggleCreateEditorByDefault:))
265         [menuItem setState:[self createEditorByDefault] ? NSControlStateValueOn : NSControlStateValueOff];
266     else if (action == @selector(toggleUseTransparentWindows:))
267         [menuItem setState:[self useTransparentWindows] ? NSControlStateValueOn : NSControlStateValueOff];
268     else if (action == @selector(toggleUsePaginatedMode:))
269         [menuItem setState:[self usePaginatedMode] ? NSControlStateValueOn : NSControlStateValueOff];
270     else if (action == @selector(toggleShowLayerBorders:))
271         [menuItem setState:[self layerBordersVisible] ? NSControlStateValueOn : NSControlStateValueOff];
272     else if (action == @selector(toggleSimpleLineLayoutEnabled:))
273         [menuItem setState:[self simpleLineLayoutEnabled] ? NSControlStateValueOff : NSControlStateValueOn];
274     else if (action == @selector(toggleSimpleLineLayoutDebugBordersEnabled:))
275         [menuItem setState:[self simpleLineLayoutDebugBordersEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
276     else if (action == @selector(toggleIncrementalRenderingSuppressed:))
277         [menuItem setState:[self incrementalRenderingSuppressed] ? NSControlStateValueOn : NSControlStateValueOff];
278     else if (action == @selector(toggleAcceleratedDrawingEnabled:))
279         [menuItem setState:[self acceleratedDrawingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
280     else if (action == @selector(toggleDisplayListDrawingEnabled:))
281         [menuItem setState:[self displayListDrawingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
282     else if (action == @selector(toggleSubpixelAntialiasedLayerTextEnabled:))
283         [menuItem setState:[self subpixelAntialiasedLayerTextEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
284     else if (action == @selector(toggleResourceLoadStatisticsEnabled:))
285         [menuItem setState:[self resourceLoadStatisticsEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
286     else if (action == @selector(toggleLargeImageAsyncDecodingEnabled:))
287         [menuItem setState:[self largeImageAsyncDecodingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
288     else if (action == @selector(toggleAnimatedImageAsyncDecodingEnabled:))
289         [menuItem setState:[self animatedImageAsyncDecodingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
290     else if (action == @selector(toggleAppleColorFilterEnabled:))
291         [menuItem setState:[self appleColorFilterEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
292     else if (action == @selector(togglePunchOutWhiteBackgroundsInDarkMode:))
293         [menuItem setState:[self punchOutWhiteBackgroundsInDarkMode] ? NSControlStateValueOn : NSControlStateValueOff];
294     else if (action == @selector(toggleUseSystemAppearance:))
295         [menuItem setState:[self useSystemAppearance] ? NSControlStateValueOn : NSControlStateValueOff];
296     else if (action == @selector(toggleVisualViewportEnabled:))
297         [menuItem setState:[self visualViewportEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
298     else if (action == @selector(toggleReserveSpaceForBanners:))
299         [menuItem setState:[self isSpaceReservedForBanners] ? NSControlStateValueOn : NSControlStateValueOff];
300     else if (action == @selector(toggleShowTiledScrollingIndicator:))
301         [menuItem setState:[self tiledScrollingIndicatorVisible] ? NSControlStateValueOn : NSControlStateValueOff];
302     else if (action == @selector(toggleShowResourceUsageOverlay:))
303         [menuItem setState:[self resourceUsageOverlayVisible] ? NSControlStateValueOn : NSControlStateValueOff];
304     else if (action == @selector(toggleLoadsAllSiteIcons:))
305         [menuItem setState:[self loadsAllSiteIcons] ? NSControlStateValueOn : NSControlStateValueOff];
306     else if (action == @selector(toggleUsesGameControllerFramework:))
307         [menuItem setState:[self usesGameControllerFramework] ? NSControlStateValueOn : NSControlStateValueOff];
308     else if (action == @selector(toggleNetworkCacheSpeculativeRevalidationDisabled:))
309         [menuItem setState:[self networkCacheSpeculativeRevalidationDisabled] ? NSControlStateValueOn : NSControlStateValueOff];
310     else if (action == @selector(toggleProcessSwapOnNavigation:))
311         [menuItem setState:[self processSwapOnNavigationEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
312     else if (action == @selector(toggleProcessSwapOnWindowOpenWithOpener:))
313         [menuItem setState:[self processSwapOnWindowOpenWithOpenerEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
314     else if (action == @selector(toggleUseUISideCompositing:))
315         [menuItem setState:[self useUISideCompositing] ? NSControlStateValueOn : NSControlStateValueOff];
316     else if (action == @selector(togglePerWindowWebProcessesDisabled:))
317         [menuItem setState:[self perWindowWebProcessesDisabled] ? NSControlStateValueOn : NSControlStateValueOff];
318     else if (action == @selector(toggleEnableSubPixelCSSOMMetrics:))
319         [menuItem setState:[self subPixelCSSOMMetricsEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
320     else if (action == @selector(toggleDebugOverlay:))
321         [menuItem setState:[self debugOverlayVisible:menuItem] ? NSControlStateValueOn : NSControlStateValueOff];
322
323 #if WK_API_ENABLED
324     if (menuItem.tag == ExperimentalFeatureTag) {
325         _WKExperimentalFeature *feature = menuItem.representedObject;
326         [menuItem setState:[defaultPreferences() _isEnabledForExperimentalFeature:feature] ? NSControlStateValueOn : NSControlStateValueOff];
327     }
328     if (menuItem.tag == InternalDebugFeatureTag) {
329         _WKInternalDebugFeature *feature = menuItem.representedObject;
330         [menuItem setState:[defaultPreferences() _isEnabledForInternalDebugFeature:feature] ? NSControlStateValueOn : NSControlStateValueOff];
331     }
332 #endif
333
334     return YES;
335 }
336
337 - (void)_toggleBooleanDefault:(NSString *)defaultName
338 {
339     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
340     [defaults setBool:![defaults boolForKey:defaultName] forKey:defaultName];
341
342     [(BrowserAppDelegate *)[[NSApplication sharedApplication] delegate] didChangeSettings];
343 }
344
345 - (void)toggleUseWebKit2ByDefault:(id)sender
346 {
347     [self _toggleBooleanDefault:UseWebKit2ByDefaultPreferenceKey];
348 }
349
350 - (BOOL)useWebKit2ByDefault
351 {
352     return [[NSUserDefaults standardUserDefaults] boolForKey:UseWebKit2ByDefaultPreferenceKey];
353 }
354
355 - (void)toggleCreateEditorByDefault:(id)sender
356 {
357     [self _toggleBooleanDefault:CreateEditorByDefaultPreferenceKey];
358 }
359
360 - (BOOL)createEditorByDefault
361 {
362     return [[NSUserDefaults standardUserDefaults] boolForKey:CreateEditorByDefaultPreferenceKey];
363 }
364
365 - (void)toggleUseTransparentWindows:(id)sender
366 {
367     [self _toggleBooleanDefault:UseTransparentWindowsPreferenceKey];
368 }
369
370 - (BOOL)useTransparentWindows
371 {
372     return [[NSUserDefaults standardUserDefaults] boolForKey:UseTransparentWindowsPreferenceKey];
373 }
374
375 - (void)toggleUsePaginatedMode:(id)sender
376 {
377     [self _toggleBooleanDefault:UsePaginatedModePreferenceKey];
378 }
379
380 - (BOOL)usePaginatedMode
381 {
382     return [[NSUserDefaults standardUserDefaults] boolForKey:UsePaginatedModePreferenceKey];
383 }
384
385 - (void)toggleUseUISideCompositing:(id)sender
386 {
387     [self _toggleBooleanDefault:UseRemoteLayerTreeDrawingAreaPreferenceKey];
388 }
389
390 - (BOOL)useUISideCompositing
391 {
392     return [[NSUserDefaults standardUserDefaults] boolForKey:UseRemoteLayerTreeDrawingAreaPreferenceKey];
393 }
394
395 - (void)togglePerWindowWebProcessesDisabled:(id)sender
396 {
397     NSAlert *alert = [[NSAlert alloc] init];
398     [alert setMessageText:self.perWindowWebProcessesDisabled ? @"Are you sure you want to switch to per-window web processes?" : @"Are you sure you want to switch to a single web process?"];
399     [alert setInformativeText:@"This requires quitting and relaunching MiniBrowser. I'll do the quitting. You will have to do the relaunching."];
400     [alert addButtonWithTitle:@"Switch and Quit"];
401     [alert addButtonWithTitle:@"Cancel"];
402
403     NSModalResponse response = [alert runModal];    
404     [alert release];
405
406     if (response != NSAlertFirstButtonReturn)
407         return;
408
409     [self _toggleBooleanDefault:PerWindowWebProcessesDisabledKey];
410     [NSApp terminate:self];
411 }
412
413 - (BOOL)perWindowWebProcessesDisabled
414 {
415     return [[NSUserDefaults standardUserDefaults] boolForKey:PerWindowWebProcessesDisabledKey];
416 }
417
418 - (void)toggleIncrementalRenderingSuppressed:(id)sender
419 {
420     [self _toggleBooleanDefault:IncrementalRenderingSuppressedPreferenceKey];
421 }
422
423 - (BOOL)incrementalRenderingSuppressed
424 {
425     return [[NSUserDefaults standardUserDefaults] boolForKey:IncrementalRenderingSuppressedPreferenceKey];
426 }
427
428 - (void)toggleShowLayerBorders:(id)sender
429 {
430     [self _toggleBooleanDefault:LayerBordersVisiblePreferenceKey];
431 }
432
433 - (BOOL)layerBordersVisible
434 {
435     return [[NSUserDefaults standardUserDefaults] boolForKey:LayerBordersVisiblePreferenceKey];
436 }
437
438 - (void)toggleSimpleLineLayoutEnabled:(id)sender
439 {
440     [self _toggleBooleanDefault:SimpleLineLayoutEnabledPreferenceKey];
441 }
442
443 - (BOOL)simpleLineLayoutEnabled
444 {
445     return [[NSUserDefaults standardUserDefaults] boolForKey:SimpleLineLayoutEnabledPreferenceKey];
446 }
447
448 - (void)toggleSimpleLineLayoutDebugBordersEnabled:(id)sender
449 {
450     [self _toggleBooleanDefault:SimpleLineLayoutDebugBordersEnabledPreferenceKey];
451 }
452
453 - (BOOL)simpleLineLayoutDebugBordersEnabled
454 {
455     return [[NSUserDefaults standardUserDefaults] boolForKey:SimpleLineLayoutDebugBordersEnabledPreferenceKey];
456 }
457
458 - (void)toggleAcceleratedDrawingEnabled:(id)sender
459 {
460     [self _toggleBooleanDefault:AcceleratedDrawingEnabledPreferenceKey];
461 }
462
463 - (BOOL)acceleratedDrawingEnabled
464 {
465     return [[NSUserDefaults standardUserDefaults] boolForKey:AcceleratedDrawingEnabledPreferenceKey];
466 }
467
468 - (void)toggleDisplayListDrawingEnabled:(id)sender
469 {
470     [self _toggleBooleanDefault:DisplayListDrawingEnabledPreferenceKey];
471 }
472
473 - (BOOL)displayListDrawingEnabled
474 {
475     return [[NSUserDefaults standardUserDefaults] boolForKey:DisplayListDrawingEnabledPreferenceKey];
476 }
477
478 - (void)toggleSubpixelAntialiasedLayerTextEnabled:(id)sender
479 {
480     [self _toggleBooleanDefault:SubpixelAntialiasedLayerTextEnabledPreferenceKey];
481 }
482
483 - (BOOL)subpixelAntialiasedLayerTextEnabled
484 {
485     return [[NSUserDefaults standardUserDefaults] boolForKey:SubpixelAntialiasedLayerTextEnabledPreferenceKey];
486 }
487
488 - (void)toggleReserveSpaceForBanners:(id)sender
489 {
490     [self _toggleBooleanDefault:ReserveSpaceForBannersPreferenceKey];
491 }
492
493 - (void)toggleShowTiledScrollingIndicator:(id)sender
494 {
495     [self _toggleBooleanDefault:TiledScrollingIndicatorVisiblePreferenceKey];
496 }
497
498 - (void)toggleShowResourceUsageOverlay:(id)sender
499 {
500     [self _toggleBooleanDefault:ResourceUsageOverlayVisiblePreferenceKey];
501 }
502
503 - (BOOL)loadsAllSiteIcons
504 {
505     return [[NSUserDefaults standardUserDefaults] boolForKey:LoadsAllSiteIconsKey];
506 }
507
508 - (void)toggleLoadsAllSiteIcons:(id)sender
509 {
510     [self _toggleBooleanDefault:LoadsAllSiteIconsKey];
511 }
512
513 - (BOOL)usesGameControllerFramework
514 {
515     return [[NSUserDefaults standardUserDefaults] boolForKey:UsesGameControllerFrameworkKey];
516 }
517
518 - (void)toggleUsesGameControllerFramework:(id)sender
519 {
520     [self _toggleBooleanDefault:UsesGameControllerFrameworkKey];
521 }
522
523 - (BOOL)networkCacheSpeculativeRevalidationDisabled
524 {
525     return [[NSUserDefaults standardUserDefaults] boolForKey:NetworkCacheSpeculativeRevalidationDisabledKey];
526 }
527
528 - (void)toggleNetworkCacheSpeculativeRevalidationDisabled:(id)sender
529 {
530     [self _toggleBooleanDefault:NetworkCacheSpeculativeRevalidationDisabledKey];
531 }
532
533 - (BOOL)processSwapOnNavigationEnabled
534 {
535     return [[NSUserDefaults standardUserDefaults] boolForKey:ProcessSwapOnNavigationKey];
536 }
537
538 - (void)toggleProcessSwapOnNavigation:(id)sender
539 {
540     [self _toggleBooleanDefault:ProcessSwapOnNavigationKey];
541 }
542
543 - (BOOL)processSwapOnWindowOpenWithOpenerEnabled
544 {
545     return [[NSUserDefaults standardUserDefaults] boolForKey:ProcessSwapOnWindowOpenWithOpenerKey];
546 }
547
548 - (void)toggleProcessSwapOnWindowOpenWithOpener:(id)sender
549 {
550     [self _toggleBooleanDefault:ProcessSwapOnWindowOpenWithOpenerKey];
551 }
552
553 - (BOOL)isSpaceReservedForBanners
554 {
555     return [[NSUserDefaults standardUserDefaults] boolForKey:ReserveSpaceForBannersPreferenceKey];
556 }
557
558 - (BOOL)tiledScrollingIndicatorVisible
559 {
560     return [[NSUserDefaults standardUserDefaults] boolForKey:TiledScrollingIndicatorVisiblePreferenceKey];
561 }
562
563 - (BOOL)resourceUsageOverlayVisible
564 {
565     return [[NSUserDefaults standardUserDefaults] boolForKey:ResourceUsageOverlayVisiblePreferenceKey];
566 }
567
568 - (void)toggleResourceLoadStatisticsEnabled:(id)sender
569 {
570     [self _toggleBooleanDefault:ResourceLoadStatisticsEnabledPreferenceKey];
571 }
572
573 - (BOOL)visualViewportEnabled
574 {
575     return [[NSUserDefaults standardUserDefaults] boolForKey:VisualViewportEnabledPreferenceKey];
576 }
577
578 - (void)toggleVisualViewportEnabled:(id)sender
579 {
580     [self _toggleBooleanDefault:VisualViewportEnabledPreferenceKey];
581 }
582
583 - (BOOL)resourceLoadStatisticsEnabled
584 {
585     return [[NSUserDefaults standardUserDefaults] boolForKey:ResourceLoadStatisticsEnabledPreferenceKey];
586 }
587
588 - (void)toggleLargeImageAsyncDecodingEnabled:(id)sender
589 {
590     [self _toggleBooleanDefault:LargeImageAsyncDecodingEnabledPreferenceKey];
591 }
592
593 - (BOOL)largeImageAsyncDecodingEnabled
594 {
595     return [[NSUserDefaults standardUserDefaults] boolForKey:LargeImageAsyncDecodingEnabledPreferenceKey];
596 }
597
598 - (void)toggleAnimatedImageAsyncDecodingEnabled:(id)sender
599 {
600     [self _toggleBooleanDefault:AnimatedImageAsyncDecodingEnabledPreferenceKey];
601 }
602
603 - (BOOL)animatedImageAsyncDecodingEnabled
604 {
605     return [[NSUserDefaults standardUserDefaults] boolForKey:AnimatedImageAsyncDecodingEnabledPreferenceKey];
606 }
607
608 - (void)toggleAppleColorFilterEnabled:(id)sender
609 {
610     [self _toggleBooleanDefault:AppleColorFilterEnabledPreferenceKey];
611 }
612
613 - (BOOL)appleColorFilterEnabled
614 {
615     return [[NSUserDefaults standardUserDefaults] boolForKey:AppleColorFilterEnabledPreferenceKey];
616 }
617
618 - (void)togglePunchOutWhiteBackgroundsInDarkMode:(id)sender
619 {
620     [self _toggleBooleanDefault:PunchOutWhiteBackgroundsInDarkModePreferenceKey];
621 }
622
623 - (BOOL)punchOutWhiteBackgroundsInDarkMode
624 {
625     return [[NSUserDefaults standardUserDefaults] boolForKey:PunchOutWhiteBackgroundsInDarkModePreferenceKey];
626 }
627
628 - (void)toggleUseSystemAppearance:(id)sender
629 {
630     [self _toggleBooleanDefault:UseSystemAppearancePreferenceKey];
631 }
632
633 - (BOOL)useSystemAppearance
634 {
635     return [[NSUserDefaults standardUserDefaults] boolForKey:UseSystemAppearancePreferenceKey];
636 }
637
638 - (void)toggleEnableSubPixelCSSOMMetrics:(id)sender
639 {
640     [self _toggleBooleanDefault:EnableSubPixelCSSOMMetricsPreferenceKey];
641 }
642
643 - (BOOL)subPixelCSSOMMetricsEnabled
644 {
645     return [[NSUserDefaults standardUserDefaults] boolForKey:EnableSubPixelCSSOMMetricsPreferenceKey];
646 }
647
648 - (BOOL)nonFastScrollableRegionOverlayVisible
649 {
650     return [[NSUserDefaults standardUserDefaults] boolForKey:NonFastScrollableRegionOverlayVisiblePreferenceKey];
651 }
652
653 - (BOOL)wheelEventHandlerRegionOverlayVisible
654 {
655     return [[NSUserDefaults standardUserDefaults] boolForKey:WheelEventHandlerRegionOverlayVisiblePreferenceKey];
656 }
657
658 - (NSString *)preferenceKeyForRegionOverlayTag:(NSUInteger)tag
659 {
660     switch (tag) {
661     case NonFastScrollableRegionOverlayTag:
662         return NonFastScrollableRegionOverlayVisiblePreferenceKey;
663
664     case WheelEventHandlerRegionOverlayTag:
665         return WheelEventHandlerRegionOverlayVisiblePreferenceKey;
666     }
667     return nil;
668 }
669
670 - (void)toggleDebugOverlay:(id)sender
671 {
672     NSString *preferenceKey = [self preferenceKeyForRegionOverlayTag:[sender tag]];
673     if (preferenceKey)
674         [self _toggleBooleanDefault:preferenceKey];
675 }
676
677 #if WK_API_ENABLED
678 - (void)toggleExperimentalFeature:(id)sender
679 {
680     _WKExperimentalFeature *feature = ((NSMenuItem *)sender).representedObject;
681     WKPreferences *preferences = defaultPreferences();
682
683     BOOL currentlyEnabled = [preferences _isEnabledForExperimentalFeature:feature];
684     [preferences _setEnabled:!currentlyEnabled forExperimentalFeature:feature];
685
686     [[NSUserDefaults standardUserDefaults] setBool:!currentlyEnabled forKey:feature.key];
687 }
688
689 - (void)toggleInternalDebugFeature:(id)sender
690 {
691     _WKInternalDebugFeature *feature = ((NSMenuItem *)sender).representedObject;
692     WKPreferences *preferences = defaultPreferences();
693
694     BOOL currentlyEnabled = [preferences _isEnabledForInternalDebugFeature:feature];
695     [preferences _setEnabled:!currentlyEnabled forInternalDebugFeature:feature];
696
697     [[NSUserDefaults standardUserDefaults] setBool:!currentlyEnabled forKey:feature.key];
698 }
699 #endif
700
701 - (BOOL)debugOverlayVisible:(NSMenuItem *)menuItem
702 {
703     NSString *preferenceKey = [self preferenceKeyForRegionOverlayTag:[menuItem tag]];
704     if (preferenceKey)
705         return [[NSUserDefaults standardUserDefaults] boolForKey:preferenceKey];
706
707     return NO;
708 }
709
710 - (NSString *)defaultURL
711 {
712     NSString *customDefaultURL = [[NSUserDefaults standardUserDefaults] stringForKey:DefaultURLPreferenceKey];
713     if (customDefaultURL)
714         return customDefaultURL;
715     return defaultURL;
716 }
717
718 - (void)setDefaultURLToCurrentURL:(id)sender
719 {
720     NSWindowController *windowController = [[NSApp keyWindow] windowController];
721     NSString *customDefaultURL = nil;
722
723     if ([windowController isKindOfClass:[BrowserWindowController class]])
724         customDefaultURL = [[(BrowserWindowController *)windowController currentURL] absoluteString];
725
726     if (customDefaultURL)
727         [[NSUserDefaults standardUserDefaults] setObject:customDefaultURL forKey:DefaultURLPreferenceKey];
728 }
729
730 @end