aboutsummaryrefslogtreecommitdiffhomepage
path: root/example/ios/iOS UI Test/iOS UI Test/MCOCIDURLProtocol.mm
blob: 6a6d0a03062be2ac8ddbbaf48f11ada83c3acea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
//  MCTCIDURLProtocol.m
//  testUI
//
//  Created by DINH Viêt Hoà on 1/22/13.
//  Copyright (c) 2013 MailCore. All rights reserved.
//

#import "MCOCIDURLProtocol.h"

#import "MCOMessageView.h"

#define MCOCIDURLProtocolDownloadedNotification @"MCOCIDURLProtocolDownloadedNotification"

@implementation MCOCIDURLProtocol

+ (void) registerProtocol
{
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
		[NSURLProtocol registerClass:[MCOCIDURLProtocol class]];
	});
}

+ (BOOL) canInitWithRequest:(NSURLRequest *)theRequest
{
    if ([self isCID:[theRequest URL]])
        return YES;
    if ([self isXMailcoreImage:[theRequest URL]])
        return YES;
    return NO;
}

+ (BOOL) isCID:(NSURL *)url
{
	NSString *theScheme = [url scheme];
	if ([theScheme caseInsensitiveCompare:@"cid"] == NSOrderedSame)
        return YES;
    return NO;
}

+ (BOOL) isXMailcoreImage:(NSURL *)url
{
	NSString *theScheme = [url scheme];
	if ([theScheme caseInsensitiveCompare:@"x-mailcore-image"] == NSOrderedSame)
        return YES;
    return NO;
}

+ (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request
{
    return request;
}

- (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id < NSURLProtocolClient >)client
{
    self = [super initWithRequest:request cachedResponse:cachedResponse client:client];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_downloaded:) name:MCOCIDURLProtocolDownloadedNotification object:nil];
    return self;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (NSString *) _partUniqueID
{
    return [NSURLProtocol propertyForKey:@"PartUniqueID" inRequest:[self request]];
}

- (NSData *) _data
{
    return [NSURLProtocol propertyForKey:@"Data" inRequest:[self request]];
}

- (MCOAbstractMessage *) _message
{
    return (MCOAbstractMessage *) [NSURLProtocol propertyForKey:@"Message" inRequest:[self request]];
}

- (void) startLoading
{
    //NSLog(@"waiting for %p %@", self, [self _partUniqueID]);
    if ([self _data] != NULL) {
        [[self class] partDownloadedMessage:[self _message] partUniqueID:[self _partUniqueID] data:[self _data]];
    }
}

- (void) _downloaded:(NSNotification *)notification
{
    NSDictionary * userInfo = [notification userInfo];
    
    //NSLog(@"downloaded?");
    NSString * notifPartID = [userInfo objectForKey:@"PartUniqueID"];
    MCOAbstractMessage * notifMessage = [userInfo objectForKey:@"Message"];
    if (notifMessage != [self _message]) {
        return;
    }
    if (![[self _partUniqueID] isEqualToString:notifPartID]) {
        return;
    }
    
    //NSLog(@"downloaded2 %p %@", self, notifPartID);
    
    NSData * data = [userInfo objectForKey:@"Data"];
    NSURLResponse * response = [[NSURLResponse alloc] initWithURL:[[self request] URL] MIMEType:@"application/data"
                                            expectedContentLength:[data length] textEncodingName:nil];
    [[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    [[self client] URLProtocol:self didLoadData:data];
    [[self client] URLProtocolDidFinishLoading:self];
}

- (void) stopLoading
{
}

+ (void) startLoadingWithMessage:(MCOAbstractMessage *)message
                    partUniqueID:(NSString *)partUniqueID
                            data:(NSData *)data
                         request:(NSMutableURLRequest *)request
{
    [NSURLProtocol setProperty:message
                        forKey:@"Message" inRequest:request];
    if (data != NULL) {
        [NSURLProtocol setProperty:data forKey:@"Data" inRequest:request];
    }
    [NSURLProtocol setProperty:partUniqueID forKey:@"PartUniqueID" inRequest:request];
}

+ (void) partDownloadedMessage:(MCOAbstractMessage *)message
                  partUniqueID:(NSString *)partUniqueID
                          data:(NSData *)data
{
    NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] init];
    [userInfo setObject:message forKey:@"Message"];
    [userInfo setObject:partUniqueID forKey:@"PartUniqueID"];
    if (data != NULL) {
        [userInfo setObject:data forKey:@"Data"];
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:MCOCIDURLProtocolDownloadedNotification object:nil userInfo:userInfo];
    //NSLog(@"downloaded %p %@", self, partUniqueID);
}

@end