|
| 1 | +// |
| 2 | +// ACAccountStore+JavaScriptBridge.m |
| 3 | +// JavaScriptBridge |
| 4 | +// |
| 5 | +// Created by kishikawa katsumi on 2014/01/05. |
| 6 | +// Copyright (c) 2014 kishikawa katsumi. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "ACAccountStore+JavaScriptBridge.h" |
| 10 | +#import "JSBMessageForwarding.h" |
| 11 | + |
| 12 | +@implementation ACAccountStore (JavaScriptBridge) |
| 13 | + |
| 14 | +#pragma clang diagnostic push |
| 15 | +#pragma clang diagnostic ignored "-Wdeprecated-declarations" |
| 16 | + |
| 17 | +- (void)__saveAccount:(ACAccount *)account withCompletionHandler:(JSValue *)handlerFunction |
| 18 | +{ |
| 19 | + JSContext *context = [JSContext currentContext]; |
| 20 | + id currentSelf = context[@"self"]; |
| 21 | + |
| 22 | + ACAccountStoreSaveCompletionHandler completionHandler = NULL; |
| 23 | + if (!handlerFunction.isUndefined) { |
| 24 | + completionHandler = ^(BOOL success, NSError *error) { |
| 25 | + NSMutableArray *parameters = [[NSMutableArray alloc] init]; |
| 26 | + [parameters addObject:@(success)]; |
| 27 | + if (error) { |
| 28 | + [parameters addObject:error]; |
| 29 | + } else { |
| 30 | + [parameters addObject:[NSNull null]]; |
| 31 | + } |
| 32 | + |
| 33 | + dispatchFunction(currentSelf, handlerFunction, parameters); |
| 34 | + }; |
| 35 | + } |
| 36 | + |
| 37 | + [self saveAccount:account withCompletionHandler:completionHandler]; |
| 38 | +} |
| 39 | + |
| 40 | +- (void)__requestAccessToAccountsWithType:(ACAccountType *)accountType |
| 41 | + withCompletionHandler:(JSValue *)handlerFunction |
| 42 | +{ |
| 43 | + JSContext *context = [JSContext currentContext]; |
| 44 | + id currentSelf = context[@"self"]; |
| 45 | + |
| 46 | + ACAccountStoreRequestAccessCompletionHandler handler = NULL; |
| 47 | + if (!handlerFunction.isUndefined) { |
| 48 | + handler = ^(BOOL granted, NSError *error) { |
| 49 | + NSMutableArray *parameters = [[NSMutableArray alloc] init]; |
| 50 | + [parameters addObject:@(granted)]; |
| 51 | + if (error) { |
| 52 | + [parameters addObject:error]; |
| 53 | + } else { |
| 54 | + [parameters addObject:[NSNull null]]; |
| 55 | + } |
| 56 | + |
| 57 | + dispatchFunction(currentSelf, handlerFunction, parameters); |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + [self requestAccessToAccountsWithType:accountType withCompletionHandler:handler]; |
| 62 | +} |
| 63 | + |
| 64 | +- (void)__requestAccessToAccountsWithType:(ACAccountType *)accountType |
| 65 | + options:(NSDictionary *)options |
| 66 | + completion:(JSValue *)completionFunction |
| 67 | +{ |
| 68 | + JSContext *context = [JSContext currentContext]; |
| 69 | + id currentSelf = context[@"self"]; |
| 70 | + |
| 71 | + ACAccountStoreRequestAccessCompletionHandler completion = NULL; |
| 72 | + if (!completionFunction.isUndefined) { |
| 73 | + completion = ^(BOOL granted, NSError *error) { |
| 74 | + NSMutableArray *parameters = [[NSMutableArray alloc] init]; |
| 75 | + [parameters addObject:@(granted)]; |
| 76 | + if (error) { |
| 77 | + [parameters addObject:error]; |
| 78 | + } else { |
| 79 | + [parameters addObject:[NSNull null]]; |
| 80 | + } |
| 81 | + |
| 82 | + dispatchFunction(currentSelf, completionFunction, parameters); |
| 83 | + }; |
| 84 | + } |
| 85 | + |
| 86 | + [self requestAccessToAccountsWithType:accountType options:options completion:completion]; |
| 87 | +} |
| 88 | + |
| 89 | +- (void)__renewCredentialsForAccount:(ACAccount *)account completion:(JSValue *)handlerFunction |
| 90 | +{ |
| 91 | + JSContext *context = [JSContext currentContext]; |
| 92 | + id currentSelf = context[@"self"]; |
| 93 | + |
| 94 | + ACAccountStoreCredentialRenewalHandler completionHandler = NULL; |
| 95 | + if (!handlerFunction.isUndefined) { |
| 96 | + completionHandler = ^(ACAccountCredentialRenewResult renewResult, NSError *error) { |
| 97 | + NSMutableArray *parameters = [[NSMutableArray alloc] init]; |
| 98 | + [parameters addObject:@(renewResult)]; |
| 99 | + if (error) { |
| 100 | + [parameters addObject:error]; |
| 101 | + } else { |
| 102 | + [parameters addObject:[NSNull null]]; |
| 103 | + } |
| 104 | + |
| 105 | + dispatchFunction(currentSelf, handlerFunction, parameters); |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + [self renewCredentialsForAccount:account completion:completionHandler]; |
| 110 | +} |
| 111 | + |
| 112 | +- (void)__removeAccount:(ACAccount *)account withCompletionHandler:(JSValue *)handlerFunction |
| 113 | +{ |
| 114 | + JSContext *context = [JSContext currentContext]; |
| 115 | + id currentSelf = context[@"self"]; |
| 116 | + |
| 117 | + ACAccountStoreRemoveCompletionHandler completionHandler = NULL; |
| 118 | + if (!handlerFunction.isUndefined) { |
| 119 | + completionHandler = ^(BOOL success, NSError *error) { |
| 120 | + NSMutableArray *parameters = [[NSMutableArray alloc] init]; |
| 121 | + [parameters addObject:@(success)]; |
| 122 | + if (error) { |
| 123 | + [parameters addObject:error]; |
| 124 | + } else { |
| 125 | + [parameters addObject:[NSNull null]]; |
| 126 | + } |
| 127 | + |
| 128 | + dispatchFunction(currentSelf, handlerFunction, parameters); |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + [self removeAccount:account withCompletionHandler:completionHandler]; |
| 133 | +} |
| 134 | + |
| 135 | +#pragma clang diagnostic pop |
| 136 | + |
| 137 | +@end |
0 commit comments