Commit adfd4308 authored by Lily Houghton's avatar Lily Houghton Committed by Commit Bot

[Cronet] Add CronetMetricsDelegate and add/remove methods to Cronet

This adds a CronetMetricsDelegate interface, as well as addMetricsDelegate
and removeMetricsDelegate methods, which are used to register/unregister
metrics callbacks.

Currently the callback is never actually called; this is simply defining the
interface and registration methods.

Bug: 702796
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I0a3933bf623265d675e3a10ef5ed25149c89e101
Reviewed-on: https://chromium-review.googlesource.com/748819
Commit-Queue: Lily Houghton <lilyhoughton@chromium.org>
Reviewed-by: default avatarAndrei Kapishnikov <kapishnikov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#515456}
parent 75eeaf3b
......@@ -33,6 +33,17 @@ FOUNDATION_EXPORT GRPC_SUPPORT_EXPORT NSString* const CRNInvalidArgumentKey;
// be handled.
typedef BOOL (^RequestFilterBlock)(NSURLRequest* request);
// A delegate that a client can pass to Cronet to collect metrics for a
// session task. The delegate can be added and removed by calling
// [Cronet addMetricsDelegate:] and [Cronet removeMetricsDelegate:] methods
// respectively.
@protocol CronetMetricsDelegate <NSObject>
// Is invoked by Cronet when the metrics for the task are ready.
- (void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task
didFinishCollectingMetrics:(NSURLSessionTaskMetrics*)metrics
NS_AVAILABLE_IOS(10.0);
@end
// Interface for installing Cronet.
// TODO(gcasto): Should this macro be separate from the one defined in
// bidirectional_stream_c.h?
......@@ -195,4 +206,19 @@ GRPC_SUPPORT_EXPORT
// This method only has any effect before |start| is called.
+ (void)enableTestCertVerifierForTesting;
// Registers a CronetMetricsDelegate callback. The client is responsible for
// creating an object which implements CronetMetricsDelegate, and passing
// that as a |delegate|. Returns |YES| if the delegate is added successfully,
// and |NO| if it was not (because there is already an identical delegate
// registered).
+ (BOOL)addMetricsDelegate:(id<CronetMetricsDelegate>)delegate
NS_AVAILABLE_IOS(10.0);
// Unregisters a CronetMetricsDelegate callback. |delegate| should be a
// pointer to the delegate which is to be removed. It returns |YES| if the
// delegate is successfully removed and |NO| if the delegate is not
// contained in the set of delegates (and therefore cannot be removed).
+ (BOOL)removeMetricsDelegate:(id<CronetMetricsDelegate>)delegate
NS_AVAILABLE_IOS(10.0);
@end
......@@ -64,6 +64,7 @@ BOOL gEnableTestCertVerifierForTesting;
std::unique_ptr<net::CertVerifier> gMockCertVerifier;
NSString* gAcceptLanguages;
BOOL gEnablePKPBypassForLocalTrustAnchors;
NSMutableSet<id<CronetMetricsDelegate>>* gMetricsDelegates;
// CertVerifier, which allows any certificates for testing.
class TestCertVerifier : public net::CertVerifier {
......@@ -510,6 +511,27 @@ class CronetHttpProtocolHandlerDelegate
gMockCertVerifier.reset(nullptr);
gAcceptLanguages = nil;
gEnablePKPBypassForLocalTrustAnchors = YES;
gMetricsDelegates = [NSMutableSet set];
}
+ (BOOL)addMetricsDelegate:(id<CronetMetricsDelegate>)delegate {
@synchronized(gMetricsDelegates) {
if ([gMetricsDelegates containsObject:delegate]) {
return NO;
}
[gMetricsDelegates addObject:delegate];
return YES;
}
}
+ (BOOL)removeMetricsDelegate:(id<CronetMetricsDelegate>)delegate {
@synchronized(gMetricsDelegates) {
if ([gMetricsDelegates containsObject:delegate]) {
[gMetricsDelegates removeObject:delegate];
return YES;
}
return NO;
}
}
@end
......@@ -6,11 +6,26 @@
#include "components/cronet/ios/cronet_metrics.h"
#include "components/cronet/ios/test/cronet_test_base.h"
#include "testing/gtest_mac.h"
@interface TestMetricsDelegate : NSObject<CronetMetricsDelegate>
@end
@implementation TestMetricsDelegate
- (void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task
didFinishCollectingMetrics:(NSURLSessionTaskMetrics*)metrics
NS_AVAILABLE_IOS(10.0) {
// This is never actually called, so its definition currently doesn't matter.
}
@end
namespace cronet {
class CronetMetricsTest : public CronetTestBase {
protected:
CronetMetricsTest() {}
~CronetMetricsTest() override {}
};
TEST_F(CronetMetricsTest, Setters) {
......@@ -72,4 +87,15 @@ TEST_F(CronetMetricsTest, Setters) {
}
}
TEST_F(CronetMetricsTest, Delegate) {
if (@available(iOS 10, *)) {
TestMetricsDelegate* metrics_delegate =
[[TestMetricsDelegate alloc] init];
EXPECT_TRUE([Cronet addMetricsDelegate:metrics_delegate]);
EXPECT_FALSE([Cronet addMetricsDelegate:metrics_delegate]);
EXPECT_TRUE([Cronet removeMetricsDelegate:metrics_delegate]);
EXPECT_FALSE([Cronet removeMetricsDelegate:metrics_delegate]);
}
}
} // namespace cronet
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment