1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
22 #include "CyteKit/UCPlatform.h"
24 #include <sys/mount.h>
25 #include <sys/sysctl.h>
27 #include <IOKit/IOKitLib.h>
28 #include <objc/runtime.h>
30 #include "CyteKit/CyteObject.h"
31 #include "CyteKit/WebViewController.h"
32 #include "CyteKit/countByEnumeratingWithState.h"
33 #include "CyteKit/extern.h"
35 #include "iPhonePrivate.h"
36 #include <Menes/ObjectHandle.h>
38 @implementation NSDictionary (Cydia)
39 - (id) invokeUndefinedMethodFromWebScript:(NSString *)name withArguments:(NSArray *)arguments {
41 else if ([name isEqualToString:@"get"])
42 return [self objectForKey:[arguments objectAtIndex:0]];
43 else if ([name isEqualToString:@"keys"])
44 return [self allKeys];
48 static NSString *CYHex(NSData *data, bool reverse = false) {
52 size_t length([data length]);
53 uint8_t bytes[length];
54 [data getBytes:bytes];
56 char string[length * 2 + 1];
57 for (size_t i(0); i != length; ++i)
58 sprintf(string + i * 2, "%.2x", bytes[reverse ? length - i - 1 : i]);
60 return [NSString stringWithUTF8String:string];
63 static NSObject *CYIOGetValue(const char *path, NSString *property) {
64 io_registry_entry_t entry(IORegistryEntryFromPath(kIOMasterPortDefault, path));
65 if (entry == MACH_PORT_NULL)
68 CFTypeRef value(IORegistryEntryCreateCFProperty(entry, (CFStringRef) property, kCFAllocatorDefault, 0));
69 IOObjectRelease(entry);
73 return [(id) value autorelease];
76 @implementation CyteObject {
77 _H<CyteWebViewController> indirect_;
80 + (BOOL) isKeyExcludedFromWebScript:(const char *)name {
84 - (id) initWithDelegate:(CyteWebViewController *)indirect {
85 if ((self = [super init]) != nil) {
91 - (NSArray *) attributeKeys {
92 return [NSArray arrayWithObjects:
96 @"coreFoundationVersionNumber",
106 - (unsigned) bittage {
108 #elif defined(__x86_64__)
110 #elif defined(__arm64__)
112 #elif defined(__arm__)
119 - (NSString *) bbsnum {
120 return (id) CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false) ?: [NSNull null];
123 - (NSString *) build {
124 return [NSString stringWithUTF8String:System_];
127 - (NSString *) coreFoundationVersionNumber {
128 return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber];
131 - (NSString *) ecid {
132 return (id) [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString] ?: [NSNull null];
135 - (NSString *) firmware {
136 return [[UIDevice currentDevice] systemVersion];
139 - (NSString *) hostname {
140 return [[UIDevice currentDevice] name];
143 - (NSString *) idiom {
144 return IsWildcat_ ? @"ipad" : @"iphone";
147 - (NSString *) model {
148 return [NSString stringWithUTF8String:Machine_];
151 - (NSString *) serial {
152 return (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber");
156 + (NSString *) webScriptNameForSelector:(SEL)selector {
158 else if (selector == @selector(addInternalRedirect::))
159 return @"addInternalRedirect";
160 else if (selector == @selector(close))
162 else if (selector == @selector(stringWithFormat:arguments:))
164 else if (selector == @selector(getIORegistryEntry::))
165 return @"getIORegistryEntry";
166 else if (selector == @selector(getKernelNumber:))
167 return @"getKernelNumber";
168 else if (selector == @selector(getKernelString:))
169 return @"getKernelString";
170 else if (selector == @selector(getLocaleIdentifier))
171 return @"getLocaleIdentifier";
172 else if (selector == @selector(getPreferredLanguages))
173 return @"getPreferredLanguages";
174 else if (selector == @selector(isReachable:))
175 return @"isReachable";
176 else if (selector == @selector(localizedStringForKey:value:table:))
178 else if (selector == @selector(popViewController:))
179 return @"popViewController";
180 else if (selector == @selector(registerFrame:))
181 return @"registerFrame";
182 else if (selector == @selector(removeButton))
183 return @"removeButton";
184 else if (selector == @selector(scrollToBottom:))
185 return @"scrollToBottom";
186 else if (selector == @selector(setAllowsNavigationAction:))
187 return @"setAllowsNavigationAction";
188 else if (selector == @selector(setBadgeValue:))
189 return @"setBadgeValue";
190 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
191 return @"setButtonImage";
192 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
193 return @"setButtonTitle";
194 else if (selector == @selector(setHidesBackButton:))
195 return @"setHidesBackButton";
196 else if (selector == @selector(setHidesNavigationBar:))
197 return @"setHidesNavigationBar";
198 else if (selector == @selector(setNavigationBarStyle:))
199 return @"setNavigationBarStyle";
200 else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:))
201 return @"setNavigationBarTintColor";
202 else if (selector == @selector(setPasteboardString:))
203 return @"setPasteboardString";
204 else if (selector == @selector(setPasteboardURL:))
205 return @"setPasteboardURL";
206 else if (selector == @selector(setScrollAlwaysBounceVertical:))
207 return @"setScrollAlwaysBounceVertical";
208 else if (selector == @selector(setScrollIndicatorStyle:))
209 return @"setScrollIndicatorStyle";
210 else if (selector == @selector(setViewportWidth:))
211 return @"setViewportWidth";
212 else if (selector == @selector(statfs:))
214 else if (selector == @selector(supports:))
216 else if (selector == @selector(unload))
222 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
223 return [self webScriptNameForSelector:selector] == nil;
226 - (void) addInternalRedirect:(NSString *)from :(NSString *)to {
227 [CyteWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO];
231 [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO];
234 - (NSString *) getKernelString:(NSString *)name {
235 const char *string([name UTF8String]);
238 if (sysctlbyname(string, NULL, &size, NULL, 0) == -1)
239 return (id) [NSNull null];
241 char value[size + 1];
242 if (sysctlbyname(string, value, &size, NULL, 0) == -1)
243 return (id) [NSNull null];
245 // XXX: just in case you request something ludicrous
248 return [NSString stringWithCString:value];
251 - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry {
252 NSObject *value(CYIOGetValue([path UTF8String], entry));
255 if ([value isKindOfClass:[NSData class]])
256 value = CYHex((NSData *) value);
261 - (NSString *) getLocaleIdentifier {
262 _H<const __CFLocale> locale(CFLocaleCopyCurrent(), true);
263 return locale == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(locale);
266 - (NSArray *) getPreferredLanguages {
267 return [NSLocale preferredLanguages];
270 - (NSNumber *) isReachable:(NSString *)name {
271 return [NSNumber numberWithBool:CyteIsReachable([name UTF8String])];
274 - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table {
275 if (reinterpret_cast<id>(value) == [WebUndefined undefined])
277 if (reinterpret_cast<id>(table) == [WebUndefined undefined])
279 return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table];
282 - (void) popViewController:(NSNumber *)value {
283 if (value == (id) [WebUndefined undefined])
284 value = [NSNumber numberWithBool:YES];
285 [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO];
288 - (void) registerFrame:(DOMHTMLIFrameElement *)iframe {
289 WebFrame *frame([iframe contentFrame]);
290 [indirect_ registerFrame:frame];
293 - (void) removeButton {
294 [indirect_ removeButton];
297 - (void) scrollToBottom:(NSNumber *)animated {
298 [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO];
301 - (void) setAllowsNavigationAction:(NSString *)value {
302 [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO];
305 - (void) setBadgeValue:(id)value {
306 [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO];
309 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
310 [indirect_ setButtonImage:button withStyle:style toFunction:function];
313 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
314 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
317 - (void) setHidesBackButton:(NSString *)value {
318 [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO];
321 - (void) setHidesNavigationBar:(NSString *)value {
322 [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO];
325 - (void) setNavigationBarStyle:(NSString *)value {
326 [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO];
329 - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha {
330 float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]);
331 UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]);
332 [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO];
335 - (void) setPasteboardString:(NSString *)value {
336 [[objc_getClass("UIPasteboard") generalPasteboard] setString:value];
339 - (void) setPasteboardURL:(NSString *)value {
340 [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]];
343 - (void) setScrollAlwaysBounceVertical:(NSNumber *)value {
344 [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO];
347 - (void) setScrollIndicatorStyle:(NSString *)style {
348 [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO];
351 - (void) setViewportWidth:(float)width {
352 [indirect_ setViewportWidthOnMainThread:width];
355 - (NSArray *) statfs:(NSString *)path {
358 if (path == nil || statfs([path UTF8String], &stat) == -1)
361 return [NSArray arrayWithObjects:
362 [NSNumber numberWithUnsignedLong:stat.f_bsize],
363 [NSNumber numberWithUnsignedLong:stat.f_blocks],
364 [NSNumber numberWithUnsignedLong:stat.f_bfree],
368 - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments {
369 //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]);
370 unsigned count([arguments count]);
372 for (unsigned i(0); i != count; ++i)
373 values[i] = [arguments objectAtIndex:i];
374 return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
377 - (BOOL) supports:(NSString *)feature {
378 return [feature isEqualToString:@"window.open"];
382 [[indirect_ rootViewController] performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO];