WTR is not successfully changing the NSUserDefaults
[WebKit-https.git] / Tools / WebKitTestRunner / InjectedBundle / mac / InjectedBundleMac.mm
1 /*
2  * Copyright (C) 2011 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 "InjectedBundle.h"
27
28 #import <AppKit/AppKit.h>
29 #import <Foundation/Foundation.h>
30 #import <objc/objc-runtime.h>
31
32 namespace WTR {
33
34 static NSSet *allowedFontFamilySet()
35 {
36     static NSSet *fontFamiliySet = [[NSSet setWithObjects:
37         @"Ahem",
38         @"Al Bayan",
39         @"American Typewriter",
40         @"Andale Mono",
41         @"Apple Braille",
42         @"Apple Color Emoji",
43         @"Apple Chancery",
44         @"Apple Garamond BT",
45         @"Apple LiGothic",
46         @"Apple LiSung",
47         @"Apple Symbols",
48         @"AppleGothic",
49         @"AppleMyungjo",
50         @"Arial Black",
51         @"Arial Hebrew",
52         @"Arial Narrow",
53         @"Arial Rounded MT Bold",
54         @"Arial Unicode MS",
55         @"Arial",
56         @"Ayuthaya",
57         @"Baghdad",
58         @"Baskerville",
59         @"BiauKai",
60         @"Big Caslon",
61         @"Brush Script MT",
62         @"Chalkboard",
63         @"Chalkduster",
64         @"Charcoal CY",
65         @"Cochin",
66         @"ColorBits",
67         @"Comic Sans MS",
68         @"Copperplate",
69         @"Corsiva Hebrew",
70         @"Courier New",
71         @"Courier",
72         @"DecoType Naskh",
73         @"Devanagari MT",
74         @"Didot",
75         @"Euphemia UCAS",
76         @"Futura",
77         @"GB18030 Bitmap",
78         @"Geeza Pro",
79         @"Geneva CY",
80         @"Geneva",
81         @"Georgia",
82         @"Gill Sans",
83         @"Gujarati MT",
84         @"GungSeo",
85         @"Gurmukhi MT",
86         @"HeadLineA",
87         @"Hei",
88         @"Heiti SC",
89         @"Heiti TC",
90         @"Helvetica CY",
91         @"Helvetica Neue",
92         @"Helvetica",
93         @"Herculanum",
94         @"Hiragino Kaku Gothic Pro",
95         @"Hiragino Kaku Gothic ProN",
96         @"Hiragino Kaku Gothic Std",
97         @"Hiragino Kaku Gothic StdN",
98         @"Hiragino Maru Gothic Pro",
99         @"Hiragino Maru Gothic ProN",
100         @"Hiragino Mincho Pro",
101         @"Hiragino Mincho ProN",
102         @"Hiragino Sans GB",
103         @"Hoefler Text",
104         @"Impact",
105         @"InaiMathi",
106         @"Kai",
107         @"Kailasa",
108         @"Kokonor",
109         @"Krungthep",
110         @"KufiStandardGK",
111         @"LiHei Pro",
112         @"LiSong Pro",
113         @"Lucida Grande",
114         @"Marker Felt",
115         @"Menlo",
116         @"Microsoft Sans Serif",
117         @"Monaco",
118         @"Mshtakan",
119         @"Nadeem",
120         @"New Peninim MT",
121         @"Optima",
122         @"Osaka",
123         @"Papyrus",
124         @"PCMyungjo",
125         @"PilGi",
126         @"Plantagenet Cherokee",
127         @"Raanana",
128         @"Sathu",
129         @"Silom",
130         @"Skia",
131         @"STFangsong",
132         @"STHeiti",
133         @"STKaiti",
134         @"STSong",
135         @"Symbol",
136         @"Tahoma",
137         @"Thonburi",
138         @"Times New Roman",
139         @"Times",
140         @"Trebuchet MS",
141         @"Verdana",
142         @"Webdings",
143         @"WebKit WeightWatcher",
144         @"Wingdings 2",
145         @"Wingdings 3",
146         @"Wingdings",
147         @"Zapf Dingbats",
148         @"Zapfino",
149         nil] retain];
150     
151     return fontFamiliySet;
152 }
153
154 static IMP appKitAvailableFontFamiliesIMP;
155 static IMP appKitAvailableFontsIMP;
156
157 static NSArray *drt_NSFontManager_availableFontFamilies(id self, SEL _cmd)
158 {
159     static NSArray *availableFontFamilies;
160     if (availableFontFamilies)
161         return availableFontFamilies;
162     
163     NSArray *availableFamilies = appKitAvailableFontFamiliesIMP(self, _cmd);
164
165     NSMutableSet *prunedFamiliesSet = [NSMutableSet setWithArray:availableFamilies];
166     [prunedFamiliesSet intersectSet:allowedFontFamilySet()];
167
168     availableFontFamilies = [[prunedFamiliesSet allObjects] retain];
169     return availableFontFamilies;
170 }
171
172 static NSArray *drt_NSFontManager_availableFonts(id self, SEL _cmd)
173 {
174     static NSArray *availableFonts;
175     if (availableFonts)
176         return availableFonts;
177     
178     NSSet *allowedFamilies = allowedFontFamilySet();
179     NSMutableArray *availableFontList = [[NSMutableArray alloc] initWithCapacity:[allowedFamilies count] * 2];
180     for (NSString *fontFamily in allowedFontFamilySet()) {
181         NSArray* fontsForFamily = [[NSFontManager sharedFontManager] availableMembersOfFontFamily:fontFamily];
182         for (NSArray* fontInfo in fontsForFamily) {
183             // Font name is the first entry in the array.
184             [availableFontList addObject:[fontInfo objectAtIndex:0]];
185         }
186     }
187     
188     availableFonts = availableFontList;
189     return availableFonts;
190 }
191
192 static void swizzleNSFontManagerMethods()
193 {
194     Method availableFontFamiliesMethod = class_getInstanceMethod(objc_getClass("NSFontManager"), @selector(availableFontFamilies));
195     ASSERT(availableFontFamiliesMethod);
196     if (!availableFontFamiliesMethod) {
197         NSLog(@"Failed to swizzle the \"availableFontFamilies\" method on NSFontManager");
198         return;
199     }
200     
201     appKitAvailableFontFamiliesIMP = method_setImplementation(availableFontFamiliesMethod, (IMP)drt_NSFontManager_availableFontFamilies);
202
203     Method availableFontsMethod = class_getInstanceMethod(objc_getClass("NSFontManager"), @selector(availableFonts));
204     ASSERT(availableFontsMethod);
205     if (!availableFontsMethod) {
206         NSLog(@"Failed to swizzle the \"availableFonts\" method on NSFontManager");
207         return;
208     }
209     
210     appKitAvailableFontsIMP = method_setImplementation(availableFontsMethod, (IMP)drt_NSFontManager_availableFonts);
211 }
212
213 void InjectedBundle::platformInitialize(WKTypeRef)
214 {
215     NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
216         [NSNumber numberWithInteger:4],   @"AppleAntiAliasingThreshold",
217         [NSNumber numberWithInteger:0],   @"AppleFontSmoothing",
218         [NSNumber numberWithBool:NO],     @"AppleScrollAnimationEnabled",
219         [NSNumber numberWithBool:NO],     @"NSOverlayScrollersEnabled",
220         @"Always",                        @"AppleShowScrollBars",
221         nil];
222
223     [[NSUserDefaults standardUserDefaults] setVolatileDomain:dict forName:NSArgumentDomain];    
224     
225     swizzleNSFontManagerMethods();
226 }
227
228 } // namespace WTR