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(__arm64__)
110 #elif defined(__arm__)
117 - (NSString *) bbsnum {
118 return (id) CYHex((NSData *) CYIOGetValue("IOService:/AppleARMPE/baseband", @"snum"), false) ?: [NSNull null];
121 - (NSString *) build {
122 return [NSString stringWithUTF8String:System_];
125 - (NSString *) coreFoundationVersionNumber {
126 return [NSString stringWithFormat:@"%.2f", kCFCoreFoundationVersionNumber];
129 - (NSString *) ecid {
130 return (id) [CYHex((NSData *) CYIOGetValue("IODeviceTree:/chosen", @"unique-chip-id"), true) uppercaseString] ?: [NSNull null];
133 - (NSString *) firmware {
134 return [[UIDevice currentDevice] systemVersion];
137 - (NSString *) hostname {
138 return [[UIDevice currentDevice] name];
141 - (NSString *) idiom {
142 return IsWildcat_ ? @"ipad" : @"iphone";
145 - (NSString *) model {
146 return [NSString stringWithUTF8String:Machine_];
149 - (NSString *) serial {
150 return (NSString *) CYIOGetValue("IOService:/", @"IOPlatformSerialNumber");
154 + (NSString *) webScriptNameForSelector:(SEL)selector {
156 else if (selector == @selector(addInternalRedirect::))
157 return @"addInternalRedirect";
158 else if (selector == @selector(close))
160 else if (selector == @selector(stringWithFormat:arguments:))
162 else if (selector == @selector(getIORegistryEntry::))
163 return @"getIORegistryEntry";
164 else if (selector == @selector(getKernelNumber:))
165 return @"getKernelNumber";
166 else if (selector == @selector(getKernelString:))
167 return @"getKernelString";
168 else if (selector == @selector(getLocaleIdentifier))
169 return @"getLocaleIdentifier";
170 else if (selector == @selector(getPreferredLanguages))
171 return @"getPreferredLanguages";
172 else if (selector == @selector(isReachable:))
173 return @"isReachable";
174 else if (selector == @selector(localizedStringForKey:value:table:))
176 else if (selector == @selector(popViewController:))
177 return @"popViewController";
178 else if (selector == @selector(registerFrame:))
179 return @"registerFrame";
180 else if (selector == @selector(removeButton))
181 return @"removeButton";
182 else if (selector == @selector(scrollToBottom:))
183 return @"scrollToBottom";
184 else if (selector == @selector(setAllowsNavigationAction:))
185 return @"setAllowsNavigationAction";
186 else if (selector == @selector(setBadgeValue:))
187 return @"setBadgeValue";
188 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
189 return @"setButtonImage";
190 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
191 return @"setButtonTitle";
192 else if (selector == @selector(setHidesBackButton:))
193 return @"setHidesBackButton";
194 else if (selector == @selector(setHidesNavigationBar:))
195 return @"setHidesNavigationBar";
196 else if (selector == @selector(setNavigationBarStyle:))
197 return @"setNavigationBarStyle";
198 else if (selector == @selector(setNavigationBarTintRed:green:blue:alpha:))
199 return @"setNavigationBarTintColor";
200 else if (selector == @selector(setPasteboardString:))
201 return @"setPasteboardString";
202 else if (selector == @selector(setPasteboardURL:))
203 return @"setPasteboardURL";
204 else if (selector == @selector(setScrollAlwaysBounceVertical:))
205 return @"setScrollAlwaysBounceVertical";
206 else if (selector == @selector(setScrollIndicatorStyle:))
207 return @"setScrollIndicatorStyle";
208 else if (selector == @selector(setViewportWidth:))
209 return @"setViewportWidth";
210 else if (selector == @selector(statfs:))
212 else if (selector == @selector(supports:))
214 else if (selector == @selector(unload))
220 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
221 return [self webScriptNameForSelector:selector] == nil;
224 - (void) addInternalRedirect:(NSString *)from :(NSString *)to {
225 [CyteWebViewController performSelectorOnMainThread:@selector(addDiversion:) withObject:[[[Diversion alloc] initWithFrom:from to:to] autorelease] waitUntilDone:NO];
229 [indirect_ performSelectorOnMainThread:@selector(close) withObject:nil waitUntilDone:NO];
232 - (NSString *) getKernelString:(NSString *)name {
233 const char *string([name UTF8String]);
236 if (sysctlbyname(string, NULL, &size, NULL, 0) == -1)
237 return (id) [NSNull null];
239 char value[size + 1];
240 if (sysctlbyname(string, value, &size, NULL, 0) == -1)
241 return (id) [NSNull null];
243 // XXX: just in case you request something ludicrous
246 return [NSString stringWithCString:value];
249 - (NSObject *) getIORegistryEntry:(NSString *)path :(NSString *)entry {
250 NSObject *value(CYIOGetValue([path UTF8String], entry));
253 if ([value isKindOfClass:[NSData class]])
254 value = CYHex((NSData *) value);
259 - (NSString *) getLocaleIdentifier {
260 _H<const __CFLocale> locale(CFLocaleCopyCurrent(), true);
261 return locale == NULL ? (NSString *) [NSNull null] : (NSString *) CFLocaleGetIdentifier(locale);
264 - (NSArray *) getPreferredLanguages {
265 return [NSLocale preferredLanguages];
268 - (NSNumber *) isReachable:(NSString *)name {
269 return [NSNumber numberWithBool:CyteIsReachable([name UTF8String])];
272 - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table {
273 if (reinterpret_cast<id>(value) == [WebUndefined undefined])
275 if (reinterpret_cast<id>(table) == [WebUndefined undefined])
277 return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table];
280 - (void) popViewController:(NSNumber *)value {
281 if (value == (id) [WebUndefined undefined])
282 value = [NSNumber numberWithBool:YES];
283 [indirect_ performSelectorOnMainThread:@selector(popViewControllerWithNumber:) withObject:value waitUntilDone:NO];
286 - (void) registerFrame:(DOMHTMLIFrameElement *)iframe {
287 WebFrame *frame([iframe contentFrame]);
288 [indirect_ registerFrame:frame];
291 - (void) removeButton {
292 [indirect_ removeButton];
295 - (void) scrollToBottom:(NSNumber *)animated {
296 [indirect_ performSelectorOnMainThread:@selector(scrollToBottomAnimated:) withObject:animated waitUntilDone:NO];
299 - (void) setAllowsNavigationAction:(NSString *)value {
300 [indirect_ performSelectorOnMainThread:@selector(setAllowsNavigationActionByNumber:) withObject:value waitUntilDone:NO];
303 - (void) setBadgeValue:(id)value {
304 [indirect_ performSelectorOnMainThread:@selector(setBadgeValue:) withObject:value waitUntilDone:NO];
307 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
308 [indirect_ setButtonImage:button withStyle:style toFunction:function];
311 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
312 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
315 - (void) setHidesBackButton:(NSString *)value {
316 [indirect_ performSelectorOnMainThread:@selector(setHidesBackButtonByNumber:) withObject:value waitUntilDone:NO];
319 - (void) setHidesNavigationBar:(NSString *)value {
320 [indirect_ performSelectorOnMainThread:@selector(setHidesNavigationBarByNumber:) withObject:value waitUntilDone:NO];
323 - (void) setNavigationBarStyle:(NSString *)value {
324 [indirect_ performSelectorOnMainThread:@selector(setNavigationBarStyle:) withObject:value waitUntilDone:NO];
327 - (void) setNavigationBarTintRed:(NSNumber *)red green:(NSNumber *)green blue:(NSNumber *)blue alpha:(NSNumber *)alpha {
328 float opacity(alpha == (id) [WebUndefined undefined] ? 1 : [alpha floatValue]);
329 UIColor *color([UIColor colorWithRed:[red floatValue] green:[green floatValue] blue:[blue floatValue] alpha:opacity]);
330 [indirect_ performSelectorOnMainThread:@selector(setNavigationBarTintColor:) withObject:color waitUntilDone:NO];
333 - (void) setPasteboardString:(NSString *)value {
334 [[objc_getClass("UIPasteboard") generalPasteboard] setString:value];
337 - (void) setPasteboardURL:(NSString *)value {
338 [[objc_getClass("UIPasteboard") generalPasteboard] setURL:[NSURL URLWithString:value]];
341 - (void) setScrollAlwaysBounceVertical:(NSNumber *)value {
342 [indirect_ performSelectorOnMainThread:@selector(setScrollAlwaysBounceVerticalNumber:) withObject:value waitUntilDone:NO];
345 - (void) setScrollIndicatorStyle:(NSString *)style {
346 [indirect_ performSelectorOnMainThread:@selector(setScrollIndicatorStyleWithName:) withObject:style waitUntilDone:NO];
349 - (void) setViewportWidth:(float)width {
350 [indirect_ setViewportWidthOnMainThread:width];
353 - (NSArray *) statfs:(NSString *)path {
356 if (path == nil || statfs([path UTF8String], &stat) == -1)
359 return [NSArray arrayWithObjects:
360 [NSNumber numberWithUnsignedLong:stat.f_bsize],
361 [NSNumber numberWithUnsignedLong:stat.f_blocks],
362 [NSNumber numberWithUnsignedLong:stat.f_bfree],
366 - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments {
367 //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]);
368 unsigned count([arguments count]);
370 for (unsigned i(0); i != count; ++i)
371 values[i] = [arguments objectAtIndex:i];
372 return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
375 - (BOOL) supports:(NSString *)feature {
376 return [feature isEqualToString:@"window.open"];
380 [[indirect_ rootViewController] performSelectorOnMainThread:@selector(unloadData) withObject:nil waitUntilDone:NO];