Permalink
Browse files

Add document.title WKWebView communication mechanism option.

Observed on the native side via KVO.
  • Loading branch information...
mihaip committed Apr 14, 2015
1 parent 111094a commit b7ad1910ca9453e899b7d9c356fedd0559e2885f
@@ -24,10 +24,11 @@
WKLocationHashInOut = 13,
WKAlert = 14,
WKPrompt = 15,
WKTitle = 16,
// Not actual full mechanisms, but just ways of measuring the native -> web function call time.
UIWebViewExecuteJs = 16,
WKWebViewExecuteJs = 17,
UIWebViewExecuteJs = 17,
WKWebViewExecuteJs = 18,
kNumMechanisms
};
@@ -102,6 +103,7 @@ -(void)loadView {
_wkWebView = [[WKWebView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_uiWebView.frame) + 10, width - 30, 56) configuration:configuration];
_wkWebView.navigationDelegate = self;
_wkWebView.UIDelegate = self;
[_wkWebView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
[self.view addSubview:_wkWebView];
bottomWebView = _wkWebView;
}
@@ -183,7 +185,7 @@ -(void)startIteration {
// Cookie changes don't seem to trigger delegate methods on iOS 8. Since it's a slower mechanism,
// it's not work investigating.
[self endIteration:0];
} else if (mechanism == WKMessageHandler || mechanism == WKLocationHash || mechanism == WKLocationReplaceHash || mechanism == WKLocationHashInOut || mechanism == WKAlert || mechanism == WKPrompt) {
} else if (mechanism == WKMessageHandler || mechanism == WKLocationHash || mechanism == WKLocationReplaceHash || mechanism == WKLocationHashInOut || mechanism == WKAlert || mechanism == WKPrompt || mechanism == WKTitle) {
if (_wkWebView) {
if (mechanism == WKLocationHashInOut) {
NSString *pingParams = [NSString stringWithFormat:@"{\"mechanism\": %d, \"startTime\": \"%qu\"}", mechanism, start];
@@ -263,6 +265,7 @@ -(void)showBenchmarkResults {
case WKLocationHashInOut: name = @"location.hash i/o"; break;
case WKAlert: name = @"window.alert() "; break;
case WKPrompt: name = @"window.prompt() "; break;
case WKTitle: name = @"document.title "; break;
case UIWebViewExecuteJs: name = @"UIWebView "; [results appendString:@"\nJS Execution\n"]; break;
case WKWebViewExecuteJs: name = @"WKWebView "; break;
@@ -360,8 +363,19 @@ -(void)userContentController:(WKUserContentController *)userContentController di
[self endIteration:end - start.longLongValue];
}
@end
#pragma mark KVO
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (object == _wkWebView && [keyPath isEqualToString:@"title"]) {
NSString *title = change[NSKeyValueChangeNewKey];
NSRange pongRange = [title rangeOfString:@"pong:"];
if (pongRange.location == 0) {
[self handlePongRequest:[title substringFromIndex:pongRange.location + pongRange.length]];
}
}
}
@end
@implementation PongUrlProtocol
@@ -9,7 +9,7 @@ var Mechanism = {
XhrAsync: 7,
CookieChange: 8,
JavaScriptCore: 9,
UIWebViewExecuteJs: 16
UIWebViewExecuteJs: 17
};
// The link does not need to be appended to the document, that avoids triggering
@@ -5,7 +5,8 @@ var Mechanism = {
WKLocationHashInOut: 13,
WKAlert: 14,
WKPrompt: 15,
WKWebViewExecuteJs: 17
WKTitle: 16,
WKWebViewExecuteJs: 18
};
var pingCount = 0;
@@ -28,6 +29,9 @@ function ping(mechanism, startTime) {
case Mechanism.WKPrompt:
window.prompt("pong:" + startTime);
break;
case Mechanism.WKTitle:
document.title = "pong:" + startTime;
break;
case Mechanism.WKWebViewExecuteJs:
return startTime;
break;

0 comments on commit b7ad191

Please sign in to comment.