Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
Merge pull request #41 from vito1188/APTT27
Browse files Browse the repository at this point in the history
[APTT-27] WKWebView / UIWebView - Send HTTP/S network request status information to backend.
  • Loading branch information
vito1188 committed May 14, 2018
2 parents 7685bc8 + 54dd085 commit 174bd34
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ xcode_scheme: Tests
osx_image: xcode9.1

before_install:
# FIXME:remove 2 lines below when xcodeproj is fixed https://github.com/CocoaPods/Xcodeproj/issues/572
- gem uninstall --force -x -i /Users/travis/.rvm/gems/ruby-2.4.2@global xcodeproj
- gem install -q -N -f --no-update-sources xcodeproj:1.5.7
- gem update fastlane --no-ri --no-rdoc --no-document
- pod repo update

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ void _handleChangedState(NSURLSessionTask *task, NSURLSessionTaskState state)
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
statusCode = httpResponse.statusCode;
}
[_RPTTrackingManager.sharedInstance.tracker end:trackingIdentifier statusCode:statusCode];
_RPTTracker *tracker = _RPTTrackingManager.sharedInstance.tracker;
[tracker updateStatusCode:statusCode trackingIdentifier:trackingIdentifier];
[tracker end:trackingIdentifier];
}
}
}
Expand Down
18 changes: 7 additions & 11 deletions RPerformanceTracking/Private/_RPTClassManipulator+UIWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ static void endTrackingWithUIWebView(UIWebView *webView)

if (trackingIdentifier)
{
// Update status code
NSCachedURLResponse *urlResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)urlResponse.response;
if (httpResponse.URL)
{
[_RPTTrackingManager.sharedInstance.tracker updateStatusCode:httpResponse.statusCode trackingIdentifier:trackingIdentifier];
}
[manager.tracker end:trackingIdentifier];
}
objc_setAssociatedObject(webView, _RPT_UIWEBVIEW_TRACKINGIDENTIFIER, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
Expand Down Expand Up @@ -141,17 +148,6 @@ + (void)_swizzleUIWebViewDelegate:(id<UIWebViewDelegate>)delegate
((void(*)(id, SEL, id))originalImp)(selfRef, selector, webView);
}

NSCachedURLResponse *urlResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:webView.request];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)urlResponse.response;
if (httpResponse.URL)
{
uint_fast64_t trackingIdentifier = [objc_getAssociatedObject(webView, _RPT_UIWEBVIEW_TRACKINGIDENTIFIER) unsignedLongLongValue];
if (trackingIdentifier)
{
[_RPTTrackingManager.sharedInstance.tracker end:trackingIdentifier statusCode:httpResponse.statusCode];
}
}

endTrackingWithUIWebView(webView);
};
[self swizzleSelector:@selector(webViewDidFinishLoad:)
Expand Down
14 changes: 12 additions & 2 deletions RPerformanceTracking/Private/_RPTClassManipulator+WKWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ static void endTrackingWithWKWebView(WKWebView *webView)
objc_setAssociatedObject(webView, _RPT_WKWEBVIEW_TRACKINGIDENTIFIER, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static void updateStatusCodeForWebView(NSInteger statusCode, WKWebView *webView)
{
[_RPTTrackingManager.sharedInstance.tracker prolongMetric];

uint_fast64_t trackingIdentifier = [objc_getAssociatedObject(webView, _RPT_WKWEBVIEW_TRACKINGIDENTIFIER) unsignedLongLongValue];
if (trackingIdentifier)
{
[_RPTTrackingManager.sharedInstance.tracker updateStatusCode:statusCode trackingIdentifier:trackingIdentifier];
}
}

@implementation _RPTClassManipulator (WKWebView)
+ (void)load
{
Expand Down Expand Up @@ -253,8 +264,7 @@ + (void)_swizzleWKWebViewNavDelegate:(id<WKNavigationDelegate>)delegate
NSHTTPURLResponse *response = (NSHTTPURLResponse *)navigationResponse.response;
if (response.URL && [response.URL.absoluteString isEqualToString:webView.URL.absoluteString])
{
uint_fast64_t trackingIdentifier = [objc_getAssociatedObject(webView, _RPT_WKWEBVIEW_TRACKINGIDENTIFIER) unsignedLongLongValue];
[_RPTTrackingManager.sharedInstance.tracker end:trackingIdentifier statusCode:response.statusCode];
updateStatusCodeForWebView(response.statusCode, webView);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion RPerformanceTracking/Private/_RPTTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RPT_EXPORT @interface _RPTTracker : NSObject
- (uint_fast64_t)startCustom:(NSString *)custom;
- (uint_fast64_t)addDevice:(NSString *)name start:(NSTimeInterval)startTime end:(NSTimeInterval)endTime;
- (void)end:(uint_fast64_t)trackingIdentifier;
- (void)end:(uint_fast64_t)trackingIdentifier statusCode:(NSInteger)statusCode;
- (void)updateStatusCode:(NSInteger)statusCode trackingIdentifier:(uint_fast64_t)trackingIdentifier;

- (instancetype)initWithRingBuffer:(_RPTRingBuffer *)ringBuffer currentMetric:(_RPTMetric *)currentMetric NS_DESIGNATED_INITIALIZER;
@end
Expand Down
9 changes: 6 additions & 3 deletions RPerformanceTracking/Private/_RPTTracker.m
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,19 @@ - (uint_fast64_t)addDevice:(NSString *)name start:(NSTimeInterval)startTime end:

- (void)end:(uint_fast64_t)trackingIdentifier
{
[self end:trackingIdentifier statusCode:0];
if (trackingIdentifier)
{
_RPTMeasurement *measurement = [_ringBuffer measurementWithTrackingIdentifier:trackingIdentifier];
measurement.endTime = [NSDate.date timeIntervalSince1970];
}
}

- (void)end:(uint_fast64_t)trackingIdentifier statusCode:(NSInteger)statusCode
- (void)updateStatusCode:(NSInteger)statusCode trackingIdentifier:(uint_fast64_t)trackingIdentifier
{
if (trackingIdentifier)
{
_RPTMeasurement *measurement = [_ringBuffer measurementWithTrackingIdentifier:trackingIdentifier];
measurement.statusCode = statusCode;
measurement.endTime = [NSDate.date timeIntervalSince1970];
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/NetworkSessionHookTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,8 @@ - (void)assertThatTrackerEndsRequestForSessionTask:(NSURLSessionTask *)dataTask
uint_fast64_t ti = [objc_getAssociatedObject(dataTask, @selector(_rpt_sessionTask_trackingIdentifier)) unsignedLongLongValue];
XCTAssertNotEqual(ti, 0);

OCMVerify([self.trackerMock end:ti statusCode:statusCode]);
OCMVerify([self.trackerMock updateStatusCode:statusCode trackingIdentifier:ti]);
OCMVerify([self.trackerMock end:ti]);

_RPTMeasurement *measurement = [_trackingManager.ringBuffer measurementWithTrackingIdentifier:ti];
XCTAssertNotNil(measurement);
Expand Down

0 comments on commit 174bd34

Please sign in to comment.