blob: e0d9a807692fe114bfda2f613b761c8c3e1709b2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2013, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#import "GAEChannelClient.h"
29
30#import "RTCPeerConnectionFactory.h"
31
32@interface GAEChannelClient ()
33
34@property(nonatomic, assign) id<GAEMessageHandler> delegate;
35@property(nonatomic, strong) UIWebView *webView;
36
37@end
38
39@implementation GAEChannelClient
40
fischman@webrtc.org9ca93a82013-10-29 00:14:15 +000041@synthesize delegate = _delegate;
42@synthesize webView = _webView;
43
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044- (id)initWithToken:(NSString *)token delegate:(id<GAEMessageHandler>)delegate {
45 self = [super init];
46 if (self) {
47 _webView = [[UIWebView alloc] init];
48 _webView.delegate = self;
49 _delegate = delegate;
50 NSString *htmlPath =
51 [[NSBundle mainBundle] pathForResource:@"ios_channel" ofType:@"html"];
52 NSURL *htmlUrl = [NSURL fileURLWithPath:htmlPath];
53 NSString *path = [NSString stringWithFormat:@"%@?token=%@",
54 [htmlUrl absoluteString],
55 token];
56
57 [_webView
58 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:path]]];
59 }
60 return self;
61}
62
63- (void)dealloc {
64 _webView.delegate = nil;
65 [_webView stopLoading];
66}
67
68#pragma mark - UIWebViewDelegate method
69
70- (BOOL)webView:(UIWebView *)webView
71 shouldStartLoadWithRequest:(NSURLRequest *)request
72 navigationType:(UIWebViewNavigationType)navigationType {
73 NSString *scheme = [request.URL scheme];
74 if ([scheme compare:@"js-frame"] != NSOrderedSame) {
75 return YES;
76 }
77 NSString *resourceSpecifier = [request.URL resourceSpecifier];
78 NSRange range = [resourceSpecifier rangeOfString:@":"];
79 NSString *method;
80 NSString *message;
81 if (range.length == 0 && range.location == NSNotFound) {
82 method = resourceSpecifier;
83 } else {
84 method = [resourceSpecifier substringToIndex:range.location];
85 message = [resourceSpecifier substringFromIndex:range.location + 1];
86 }
87 dispatch_async(dispatch_get_main_queue(), ^(void) {
88 if ([method compare:@"onopen"] == NSOrderedSame) {
89 [self.delegate onOpen];
90 } else if ([method compare:@"onmessage"] == NSOrderedSame) {
91 [self.delegate onMessage:message];
92 } else if ([method compare:@"onclose"] == NSOrderedSame) {
93 [self.delegate onClose];
94 } else if ([method compare:@"onerror"] == NSOrderedSame) {
95 // TODO(hughv): Get error.
96 int code = -1;
97 NSString *description = message;
98 [self.delegate onError:code withDescription:description];
99 } else {
100 NSAssert(NO, @"Invalid message sent from UIWebView: %@",
101 resourceSpecifier);
102 }
103 });
104 return YES;
105}
106
107@end