From c2d5a588c6e3f2a6b5c8e88aa3defbd0dcacac5a Mon Sep 17 00:00:00 2001 From: YoloMao Date: Thu, 25 Apr 2024 10:28:17 +0800 Subject: [PATCH 01/18] feat: add preflight before events request --- .../Event/GrowingEventManager.m | 4 + GrowingTrackerCore/Manager/GrowingSession.m | 3 + .../Network/GrowingNetworkPreflight.h | 32 ++++++ .../Network/GrowingNetworkPreflight.m | 108 ++++++++++++++++++ .../Request/Adapter/GrowingRequestAdapter.h | 4 + .../Request/Adapter/GrowingRequestAdapter.m | 36 ++++++ .../Network/Request/GrowingPreflightRequest.h | 28 +++++ .../Network/Request/GrowingPreflightRequest.m | 63 ++++++++++ .../Public/GrowingRequestProtocol.h | 1 + 9 files changed, 279 insertions(+) create mode 100644 GrowingTrackerCore/Network/GrowingNetworkPreflight.h create mode 100644 GrowingTrackerCore/Network/GrowingNetworkPreflight.m create mode 100644 GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h create mode 100644 GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 06b07796e..ac5d92ae1 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -34,6 +34,7 @@ #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" #import "GrowingTrackerCore/Utils/GrowingDeviceInfo.h" +#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" static const NSUInteger kGrowingMaxDBCacheSize = 100; // default: write to DB as soon as there are 100 events static const NSUInteger kGrowingMaxBatchSize = 500; // default: send no more than 500 events in every batch @@ -228,6 +229,9 @@ - (void)sendEventsInstantWithChannel:(GrowingEventChannel *)channel { // 非安全 发送日志 - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { + if (![GrowingNetworkPreflight isSucceed]) { + return; + } if (channel.isUploading) { return; } diff --git a/GrowingTrackerCore/Manager/GrowingSession.m b/GrowingTrackerCore/Manager/GrowingSession.m index a86f361a5..e4a7e58da 100644 --- a/GrowingTrackerCore/Manager/GrowingSession.m +++ b/GrowingTrackerCore/Manager/GrowingSession.m @@ -31,6 +31,7 @@ #import "GrowingTrackerCore/Utils/GrowingInternalMacros.h" #import "GrowingULAppLifecycle.h" #import "GrowingULTimeUtil.h" +#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" @interface GrowingSession () @@ -84,6 +85,8 @@ + (instancetype)currentSession { } - (void)generateVisit { + [GrowingNetworkPreflight sendPreflight]; + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; if (!trackConfiguration.dataCollectionEnabled) { return; diff --git a/GrowingTrackerCore/Network/GrowingNetworkPreflight.h b/GrowingTrackerCore/Network/GrowingNetworkPreflight.h new file mode 100644 index 000000000..f95a71403 --- /dev/null +++ b/GrowingTrackerCore/Network/GrowingNetworkPreflight.h @@ -0,0 +1,32 @@ +// +// GrowingNetworkPreflight.h +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/24. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface GrowingNetworkPreflight : NSObject + ++ (BOOL)isSucceed; + ++ (void)sendPreflight; + +@end + +NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m b/GrowingTrackerCore/Network/GrowingNetworkPreflight.m new file mode 100644 index 000000000..42b8b1ac0 --- /dev/null +++ b/GrowingTrackerCore/Network/GrowingNetworkPreflight.m @@ -0,0 +1,108 @@ +// +// GrowingNetworkPreflight.m +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/24. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" +#import "GrowingTrackerCore/Public/GrowingEventNetworkService.h" +#import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" +#import "GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h" +#import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" +#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" + +typedef NS_ENUM(NSUInteger, GrowingNetworkPreflightStatus) { + GrowingNWPreflightStatusNotDetermined, + GrowingNWPreflightStatusAuthorized, + GrowingNWPreflightStatusDenied, + GrowingNWPreflightStatusWaitingForResponse, +}; + +static NSTimeInterval const kGrowingPreflightMaxTime = 300; + +@interface GrowingNetworkPreflight () + +@property (nonatomic, assign) GrowingNetworkPreflightStatus status; +@property (nonatomic, assign) NSTimeInterval nextPreflightTime; + +@end + +@implementation GrowingNetworkPreflight + +#pragma mark - Initialize + ++ (instancetype)sharedInstance { + static id instance = nil; + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + instance = [[self alloc] init]; + }); + return instance; +} + +#pragma mark - Public Methods + ++ (BOOL)isSucceed { + GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; + return preflight.status == GrowingNWPreflightStatusAuthorized; +} + ++ (void)sendPreflight { + [GrowingDispatchManager dispatchInGrowingThread:^{ + NSTimeInterval dataUploadInterval = GrowingConfigurationManager.sharedInstance.trackConfiguration.dataUploadInterval; + dataUploadInterval = MAX(dataUploadInterval, 5); + + GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; + preflight.nextPreflightTime = dataUploadInterval; + if (preflight.status != GrowingNWPreflightStatusWaitingForResponse) { + [preflight sendPreflight]; + } + }]; +} + +#pragma mark - Private Methods + +- (void)sendPreflight { + self.status = GrowingNWPreflightStatusWaitingForResponse; + + id service = + [[GrowingServiceManager sharedInstance] createService:@protocol(GrowingEventNetworkService)]; + if (!service) { + return; + } + + NSObject *preflight = [[GrowingPreflightRequest alloc] init]; + [service sendRequest:preflight + completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) { + self.status = GrowingNWPreflightStatusAuthorized; + } else { + self.status = GrowingNWPreflightStatusDenied; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.nextPreflightTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [GrowingDispatchManager dispatchInGrowingThread:^{ + [self sendPreflight]; + }]; + }); + + self.nextPreflightTime = MIN(self.nextPreflightTime * 2, kGrowingPreflightMaxTime); + } + }]; + }]; +} + +@end diff --git a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h index b0a971806..56257c900 100644 --- a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h +++ b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h @@ -30,4 +30,8 @@ NS_ASSUME_NONNULL_BEGIN @end +@interface GrowingPreflightRequestHeaderAdapter : NSObject + +@end + NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m index f21b0d2f5..3c9756148 100644 --- a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m +++ b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m @@ -78,6 +78,10 @@ - (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { case GrowingHTTPMethodDELETE: httpMethod = @"DELETE"; break; + + case GrowingHTTPMethodOPTIONS: + httpMethod = @"OPTIONS"; + break; default: break; @@ -92,3 +96,35 @@ - (NSUInteger)priority { } @end + +#pragma mark GrowingPreflightRequestHeaderAdapter + +@interface GrowingPreflightRequestHeaderAdapter () + +@property (nonatomic, weak) id request; + +@end + +@implementation GrowingPreflightRequestHeaderAdapter + ++ (instancetype)adapterWithRequest:(id)request { + GrowingPreflightRequestHeaderAdapter *adapter = [[self alloc] init]; + adapter.request = request; + return adapter; +} + +- (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { + NSMutableURLRequest *needAdaptReq = request; + [needAdaptReq setValue:@"POST" forHTTPHeaderField:@"Access-Control-Request-Method"]; + [needAdaptReq setValue:@"Accept, Content-Type, X-Timestamp, X-Crypt-Codec, X-Compress-Codec" forHTTPHeaderField:@"Access-Control-Request-Headers"]; + NSURL *url = [self.request absoluteURL]; + NSString *origin = [NSString stringWithFormat:@"%@://%@", url.scheme, url.host]; + [needAdaptReq setValue:origin forHTTPHeaderField:@"Origin"]; + return needAdaptReq; +} + +- (NSUInteger)priority { + return 0; +} + +@end diff --git a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h b/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h new file mode 100644 index 000000000..a30bd50ff --- /dev/null +++ b/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h @@ -0,0 +1,28 @@ +// +// GrowingPreflightRequest.h +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/24. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "GrowingRequestProtocol.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface GrowingPreflightRequest : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m b/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m new file mode 100644 index 000000000..aa444e99c --- /dev/null +++ b/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m @@ -0,0 +1,63 @@ +// +// GrowingPreflightRequest.m +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/24. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h" +#import "GrowingTrackerCore/Helpers/GrowingHelpers.h" +#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" +#import "GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h" +#import "GrowingULTimeUtil.h" + +@implementation GrowingPreflightRequest + +- (GrowingHTTPMethod)method { + return GrowingHTTPMethodOPTIONS; +} + +- (NSURL *)absoluteURL { + NSString *baseUrl = GrowingConfigurationManager.sharedInstance.trackConfiguration.dataCollectionServerHost; + if (!baseUrl.length) { + return nil; + } + + NSString *absoluteURLString = [baseUrl growingHelper_absoluteURLStringWithPath:self.path andQuery:self.query]; + return [NSURL URLWithString:absoluteURLString]; +} + +- (NSString *)path { + NSString *accountId = [GrowingConfigurationManager sharedInstance].trackConfiguration.accountId; + NSString *path = [NSString stringWithFormat:@"v3/projects/%@/collect", accountId]; + return path; +} + +- (NSArray> *)adapters { + GrowingPreflightRequestHeaderAdapter *basicHeaderAdapter = [GrowingPreflightRequestHeaderAdapter adapterWithRequest:self]; + GrowingRequestMethodAdapter *methodAdapter = [GrowingRequestMethodAdapter adapterWithRequest:self]; + return @[basicHeaderAdapter, methodAdapter]; +} + +- (NSDictionary *)query { + NSString *stm = [NSString stringWithFormat:@"%llu", [GrowingULTimeUtil currentTimeMillis]]; + return @{@"stm": stm}; +} + +- (NSTimeInterval)timeoutInSeconds { + return 30.0f; +} + +@end diff --git a/GrowingTrackerCore/Public/GrowingRequestProtocol.h b/GrowingTrackerCore/Public/GrowingRequestProtocol.h index 0ccf38e08..11355ac78 100644 --- a/GrowingTrackerCore/Public/GrowingRequestProtocol.h +++ b/GrowingTrackerCore/Public/GrowingRequestProtocol.h @@ -24,6 +24,7 @@ typedef NS_ENUM(NSUInteger, GrowingHTTPMethod) { GrowingHTTPMethodPOST, GrowingHTTPMethodPUT, GrowingHTTPMethodDELETE, + GrowingHTTPMethodOPTIONS, }; @protocol GrowingRequestProtocol; From 58545d276961f007c07854aa359e97e89c38ddc4 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Thu, 25 Apr 2024 10:47:47 +0800 Subject: [PATCH 02/18] fix: cellular data limit change to 20MB --- .../GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m | 4 ++-- GrowingTrackerCore/GrowingTrackConfiguration.m | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m index 740a00c6c..991812ce1 100644 --- a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m +++ b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m @@ -114,7 +114,7 @@ - (void)testDefaultConfiguration_Autotracker { GrowingAutotrackConfiguration *configuration = (GrowingAutotrackConfiguration *)GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); - XCTAssertEqual(configuration.cellularDataLimit, 10); + XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); XCTAssertEqual(configuration.dataCollectionEnabled, YES); @@ -190,7 +190,7 @@ - (void)testDefaultConfiguration_Tracker { GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); - XCTAssertEqual(configuration.cellularDataLimit, 10); + XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); XCTAssertEqual(configuration.dataCollectionEnabled, YES); diff --git a/GrowingTrackerCore/GrowingTrackConfiguration.m b/GrowingTrackerCore/GrowingTrackConfiguration.m index 597d6d7a7..836a1d0b1 100644 --- a/GrowingTrackerCore/GrowingTrackConfiguration.m +++ b/GrowingTrackerCore/GrowingTrackConfiguration.m @@ -50,7 +50,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId { _dataSourceId = nil; _debugEnabled = NO; - _cellularDataLimit = 10; + _cellularDataLimit = 20; _dataUploadInterval = 15; _sessionInterval = 30; _dataCollectionEnabled = YES; From 77d0f4b9d2df6d3882ff37e1ae9648ece30d3ad8 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Thu, 25 Apr 2024 10:49:55 +0800 Subject: [PATCH 03/18] feat: add requestPreflight config --- .../GrowingAnalyticsStartTests.m | 6 ++++++ GrowingTrackerCore/GrowingTrackConfiguration.m | 2 ++ GrowingTrackerCore/Network/GrowingNetworkPreflight.m | 12 +++++++++++- .../Public/GrowingTrackConfiguration.h | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m index 991812ce1..572fb0373 100644 --- a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m +++ b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m @@ -114,6 +114,7 @@ - (void)testDefaultConfiguration_Autotracker { GrowingAutotrackConfiguration *configuration = (GrowingAutotrackConfiguration *)GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); + XCTAssertEqual(configuration.requestPreflight, YES); XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); @@ -135,6 +136,7 @@ - (void)testDefaultConfiguration_Autotracker { - (void)testSetConfiguration_Autotracker { GrowingAutotrackConfiguration *config = [GrowingAutotrackConfiguration configurationWithAccountId:@"test"]; config.debugEnabled = YES; + config.requestPreflight = NO; config.cellularDataLimit = 5; config.dataUploadInterval = 10; config.sessionInterval = 10; @@ -165,6 +167,7 @@ - (void)testSetConfiguration_Autotracker { GrowingAutotrackConfiguration *configuration = (GrowingAutotrackConfiguration *)GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, YES); + XCTAssertEqual(configuration.requestPreflight, NO); XCTAssertEqual(configuration.cellularDataLimit, 5); XCTAssertEqual(configuration.dataUploadInterval, 10); XCTAssertEqual(configuration.sessionInterval, 10); @@ -190,6 +193,7 @@ - (void)testDefaultConfiguration_Tracker { GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); + XCTAssertEqual(configuration.requestPreflight, YES); XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); @@ -210,6 +214,7 @@ - (void)testDefaultConfiguration_Tracker { - (void)testSetConfiguration_Tracker { GrowingTrackConfiguration *config = [GrowingTrackConfiguration configurationWithAccountId:@"test"]; config.debugEnabled = YES; + config.requestPreflight = NO; config.cellularDataLimit = 5; config.dataUploadInterval = 10; config.sessionInterval = 10; @@ -238,6 +243,7 @@ - (void)testSetConfiguration_Tracker { GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, YES); + XCTAssertEqual(configuration.requestPreflight, NO); XCTAssertEqual(configuration.cellularDataLimit, 5); XCTAssertEqual(configuration.dataUploadInterval, 10); XCTAssertEqual(configuration.sessionInterval, 10); diff --git a/GrowingTrackerCore/GrowingTrackConfiguration.m b/GrowingTrackerCore/GrowingTrackConfiguration.m index 836a1d0b1..6e931c196 100644 --- a/GrowingTrackerCore/GrowingTrackConfiguration.m +++ b/GrowingTrackerCore/GrowingTrackConfiguration.m @@ -50,6 +50,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId { _dataSourceId = nil; _debugEnabled = NO; + _requestPreflight = YES; _cellularDataLimit = 20; _dataUploadInterval = 15; _sessionInterval = 30; @@ -96,6 +97,7 @@ - (id)copyWithZone:(NSZone *)zone { configuration->_accountId = [_accountId copy]; configuration->_dataSourceId = [_dataSourceId copy]; configuration->_debugEnabled = _debugEnabled; + configuration->_requestPreflight = _requestPreflight; configuration->_cellularDataLimit = _cellularDataLimit; configuration->_dataUploadInterval = _dataUploadInterval; configuration->_sessionInterval = _sessionInterval; diff --git a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m b/GrowingTrackerCore/Network/GrowingNetworkPreflight.m index 42b8b1ac0..2977964c9 100644 --- a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m +++ b/GrowingTrackerCore/Network/GrowingNetworkPreflight.m @@ -56,13 +56,23 @@ + (instancetype)sharedInstance { #pragma mark - Public Methods + (BOOL)isSucceed { + BOOL requestPreflight = GrowingConfigurationManager.sharedInstance.trackConfiguration.requestPreflight; + if (!requestPreflight) { + return YES; + } GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; return preflight.status == GrowingNWPreflightStatusAuthorized; } + (void)sendPreflight { [GrowingDispatchManager dispatchInGrowingThread:^{ - NSTimeInterval dataUploadInterval = GrowingConfigurationManager.sharedInstance.trackConfiguration.dataUploadInterval; + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; + BOOL requestPreflight = trackConfiguration.requestPreflight; + if (!requestPreflight) { + return; + } + + NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; diff --git a/GrowingTrackerCore/Public/GrowingTrackConfiguration.h b/GrowingTrackerCore/Public/GrowingTrackConfiguration.h index 3044e29e6..957cbb32e 100644 --- a/GrowingTrackerCore/Public/GrowingTrackConfiguration.h +++ b/GrowingTrackerCore/Public/GrowingTrackConfiguration.h @@ -29,6 +29,7 @@ FOUNDATION_EXPORT NSString *const kGrowingDefaultDataCollectionServerHost; @property (nonatomic, copy, readonly) NSString *accountId; @property (nonatomic, copy) NSString *dataSourceId; @property (nonatomic, assign) BOOL debugEnabled; +@property (nonatomic, assign) BOOL requestPreflight; @property (nonatomic, assign) NSUInteger cellularDataLimit; @property (nonatomic, assign) NSTimeInterval dataUploadInterval; @property (nonatomic, assign) NSTimeInterval sessionInterval; From 81e4c495d07b0b557c9171d337d56da5ce742c39 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Thu, 25 Apr 2024 10:50:04 +0800 Subject: [PATCH 04/18] fix: response status code 413 logic --- GrowingTrackerCore/Event/GrowingEventManager.m | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index ac5d92ae1..927c2bf37 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -347,7 +347,7 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { channel.isUploading = NO; }]; } - if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) { + if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { [GrowingDispatchManager dispatchInGrowingThread:^{ if (isViaCellular) { if ([eventRequest respondsToSelector:@selector(outsize)]) { @@ -355,10 +355,12 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { } } - for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: - request:channel:)]) { - [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; + if (httpResponse.statusCode != 413) { + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: + request:channel:)]) { + [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; + } } } From 19903259611167797b4366f50e48a6f470115d5c Mon Sep 17 00:00:00 2001 From: GIOSDK Date: Thu, 25 Apr 2024 02:55:01 +0000 Subject: [PATCH 05/18] style: code format --- .../Event/GrowingEventManager.m | 79 ++++++++++--------- GrowingTrackerCore/Manager/GrowingSession.m | 4 +- .../Network/GrowingNetworkPreflight.m | 51 ++++++------ .../Request/Adapter/GrowingRequestAdapter.m | 5 +- .../Network/Request/GrowingPreflightRequest.m | 3 +- 5 files changed, 74 insertions(+), 68 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 927c2bf37..e2d99a03d 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -25,6 +25,7 @@ #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" #import "GrowingTrackerCore/Manager/GrowingSession.h" #import "GrowingTrackerCore/Network/GrowingNetworkInterfaceManager.h" +#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" #import "GrowingTrackerCore/Network/Request/GrowingEventRequest.h" #import "GrowingTrackerCore/Public/GrowingBaseEvent.h" #import "GrowingTrackerCore/Public/GrowingEventFilter.h" @@ -34,7 +35,6 @@ #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" #import "GrowingTrackerCore/Utils/GrowingDeviceInfo.h" -#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" static const NSUInteger kGrowingMaxDBCacheSize = 100; // default: write to DB as soon as there are 100 events static const NSUInteger kGrowingMaxBatchSize = 500; // default: send no more than 500 events in every batch @@ -340,44 +340,45 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { channel.isUploading = YES; NSObject *eventRequest = [[GrowingEventRequest alloc] initWithEvents:rawEvents]; - [service sendRequest:eventRequest - completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { - if (error) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - channel.isUploading = NO; - }]; - } - if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - if (isViaCellular) { - if ([eventRequest respondsToSelector:@selector(outsize)]) { - self.uploadEventSize += eventRequest.outsize; - } - } - - if (httpResponse.statusCode != 413) { - for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: - request:channel:)]) { - [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; - } - } - } - - [self removeEvents_unsafe:events forChannel:channel]; - channel.isUploading = NO; - - // 如果剩余数量 大于单包数量 则直接发送 - if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { - [self sendEventsInstantWithChannel:channel]; - } - }]; - } else { - [GrowingDispatchManager dispatchInGrowingThread:^{ - channel.isUploading = NO; - }]; - } - }]; + [service + sendRequest:eventRequest + completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { + if (error) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + channel.isUploading = NO; + }]; + } + if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + if (isViaCellular) { + if ([eventRequest respondsToSelector:@selector(outsize)]) { + self.uploadEventSize += eventRequest.outsize; + } + } + + if (httpResponse.statusCode != 413) { + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: + request:channel:)]) { + [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; + } + } + } + + [self removeEvents_unsafe:events forChannel:channel]; + channel.isUploading = NO; + + // 如果剩余数量 大于单包数量 则直接发送 + if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { + [self sendEventsInstantWithChannel:channel]; + } + }]; + } else { + [GrowingDispatchManager dispatchInGrowingThread:^{ + channel.isUploading = NO; + }]; + } + }]; } #pragma mark Event Persist diff --git a/GrowingTrackerCore/Manager/GrowingSession.m b/GrowingTrackerCore/Manager/GrowingSession.m index e4a7e58da..474f52cdd 100644 --- a/GrowingTrackerCore/Manager/GrowingSession.m +++ b/GrowingTrackerCore/Manager/GrowingSession.m @@ -23,6 +23,7 @@ #import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h" #import "GrowingTrackerCore/Helpers/GrowingHelpers.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" +#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" #import "GrowingTrackerCore/Public/GrowingTrackConfiguration.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogMacros.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" @@ -31,7 +32,6 @@ #import "GrowingTrackerCore/Utils/GrowingInternalMacros.h" #import "GrowingULAppLifecycle.h" #import "GrowingULTimeUtil.h" -#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" @interface GrowingSession () @@ -86,7 +86,7 @@ + (instancetype)currentSession { - (void)generateVisit { [GrowingNetworkPreflight sendPreflight]; - + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; if (!trackConfiguration.dataCollectionEnabled) { return; diff --git a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m b/GrowingTrackerCore/Network/GrowingNetworkPreflight.m index 2977964c9..71e90d651 100644 --- a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m +++ b/GrowingTrackerCore/Network/GrowingNetworkPreflight.m @@ -18,11 +18,11 @@ // limitations under the License. #import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" +#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" +#import "GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h" #import "GrowingTrackerCore/Public/GrowingEventNetworkService.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" -#import "GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" -#import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" typedef NS_ENUM(NSUInteger, GrowingNetworkPreflightStatus) { GrowingNWPreflightStatusNotDetermined, @@ -71,10 +71,10 @@ + (void)sendPreflight { if (!requestPreflight) { return; } - + NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); - + GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; preflight.nextPreflightTime = dataUploadInterval; if (preflight.status != GrowingNWPreflightStatusWaitingForResponse) { @@ -87,32 +87,35 @@ + (void)sendPreflight { - (void)sendPreflight { self.status = GrowingNWPreflightStatusWaitingForResponse; - + id service = [[GrowingServiceManager sharedInstance] createService:@protocol(GrowingEventNetworkService)]; if (!service) { return; } - + NSObject *preflight = [[GrowingPreflightRequest alloc] init]; - [service sendRequest:preflight - completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) { - self.status = GrowingNWPreflightStatusAuthorized; - } else { - self.status = GrowingNWPreflightStatusDenied; - - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.nextPreflightTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [GrowingDispatchManager dispatchInGrowingThread:^{ - [self sendPreflight]; - }]; - }); - - self.nextPreflightTime = MIN(self.nextPreflightTime * 2, kGrowingPreflightMaxTime); - } - }]; - }]; + [service + sendRequest:preflight + completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) { + self.status = GrowingNWPreflightStatusAuthorized; + } else { + self.status = GrowingNWPreflightStatusDenied; + + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.nextPreflightTime * NSEC_PER_SEC)), + dispatch_get_main_queue(), + ^{ + [GrowingDispatchManager dispatchInGrowingThread:^{ + [self sendPreflight]; + }]; + }); + + self.nextPreflightTime = MIN(self.nextPreflightTime * 2, kGrowingPreflightMaxTime); + } + }]; + }]; } @end diff --git a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m index 3c9756148..f089d0523 100644 --- a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m +++ b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m @@ -78,7 +78,7 @@ - (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { case GrowingHTTPMethodDELETE: httpMethod = @"DELETE"; break; - + case GrowingHTTPMethodOPTIONS: httpMethod = @"OPTIONS"; break; @@ -116,7 +116,8 @@ + (instancetype)adapterWithRequest:(id)request { - (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { NSMutableURLRequest *needAdaptReq = request; [needAdaptReq setValue:@"POST" forHTTPHeaderField:@"Access-Control-Request-Method"]; - [needAdaptReq setValue:@"Accept, Content-Type, X-Timestamp, X-Crypt-Codec, X-Compress-Codec" forHTTPHeaderField:@"Access-Control-Request-Headers"]; + [needAdaptReq setValue:@"Accept, Content-Type, X-Timestamp, X-Crypt-Codec, X-Compress-Codec" + forHTTPHeaderField:@"Access-Control-Request-Headers"]; NSURL *url = [self.request absoluteURL]; NSString *origin = [NSString stringWithFormat:@"%@://%@", url.scheme, url.host]; [needAdaptReq setValue:origin forHTTPHeaderField:@"Origin"]; diff --git a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m b/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m index aa444e99c..9c64a5d2b 100644 --- a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m +++ b/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m @@ -46,7 +46,8 @@ - (NSString *)path { } - (NSArray> *)adapters { - GrowingPreflightRequestHeaderAdapter *basicHeaderAdapter = [GrowingPreflightRequestHeaderAdapter adapterWithRequest:self]; + GrowingPreflightRequestHeaderAdapter *basicHeaderAdapter = + [GrowingPreflightRequestHeaderAdapter adapterWithRequest:self]; GrowingRequestMethodAdapter *methodAdapter = [GrowingRequestMethodAdapter adapterWithRequest:self]; return @[basicHeaderAdapter, methodAdapter]; } From 6eee064455fb2aa0dcd0b0f1e10b27ad352d744e Mon Sep 17 00:00:00 2001 From: YoloMao Date: Thu, 25 Apr 2024 13:48:42 +0800 Subject: [PATCH 06/18] ci: update unit-testing --- .../GrowingAnalyticsStartTests/GrowingAnalyticsStartTests2.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests2.m b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests2.m index f196b4d46..b0bb5814e 100644 --- a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests2.m +++ b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests2.m @@ -50,7 +50,7 @@ - (void)testDefaultConfiguration_Autotracker { GrowingAutotrackConfiguration *configuration = (GrowingAutotrackConfiguration *)GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); - XCTAssertEqual(configuration.cellularDataLimit, 10); + XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); XCTAssertEqual(configuration.dataCollectionEnabled, YES); @@ -72,7 +72,7 @@ - (void)testDefaultConfiguration_Tracker { GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); - XCTAssertEqual(configuration.cellularDataLimit, 10); + XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); XCTAssertEqual(configuration.dataCollectionEnabled, YES); From bce0b4cdb40fe0ade2982fe831f7076ea8f84b40 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Fri, 26 Apr 2024 11:04:15 +0800 Subject: [PATCH 07/18] fix: response status code 403 logic --- GrowingTrackerCore/Event/GrowingEventManager.m | 3 +++ 1 file changed, 3 insertions(+) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index e2d99a03d..267ba210a 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -375,6 +375,9 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { }]; } else { [GrowingDispatchManager dispatchInGrowingThread:^{ + if (httpResponse.statusCode == 403) { + [GrowingNetworkPreflight sendPreflight]; + } channel.isUploading = NO; }]; } From 33e1ffcbab487b4b8baf8c2f0bbbf5ab054329cf Mon Sep 17 00:00:00 2001 From: YoloMao Date: Fri, 26 Apr 2024 14:38:58 +0800 Subject: [PATCH 08/18] fix: events request logic --- .../Event/GrowingEventManager.m | 77 +++++++++---------- 1 file changed, 35 insertions(+), 42 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 267ba210a..03375d1cd 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -340,48 +340,41 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { channel.isUploading = YES; NSObject *eventRequest = [[GrowingEventRequest alloc] initWithEvents:rawEvents]; - [service - sendRequest:eventRequest - completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { - if (error) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - channel.isUploading = NO; - }]; - } - if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - if (isViaCellular) { - if ([eventRequest respondsToSelector:@selector(outsize)]) { - self.uploadEventSize += eventRequest.outsize; - } - } - - if (httpResponse.statusCode != 413) { - for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: - request:channel:)]) { - [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; - } - } - } - - [self removeEvents_unsafe:events forChannel:channel]; - channel.isUploading = NO; - - // 如果剩余数量 大于单包数量 则直接发送 - if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { - [self sendEventsInstantWithChannel:channel]; - } - }]; - } else { - [GrowingDispatchManager dispatchInGrowingThread:^{ - if (httpResponse.statusCode == 403) { - [GrowingNetworkPreflight sendPreflight]; - } - channel.isUploading = NO; - }]; - } - }]; + [service sendRequest:eventRequest completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + channel.isUploading = NO; + + if (error) { + return; + } + + if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { + if (isViaCellular) { + if ([eventRequest respondsToSelector:@selector(outsize)]) { + self.uploadEventSize += eventRequest.outsize; + } + } + + if (httpResponse.statusCode != 413) { + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: + request:channel:)]) { + [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; + } + } + } + + [self removeEvents_unsafe:events forChannel:channel]; + + // 如果剩余数量 大于单包数量 则直接发送 + if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { + [self sendEventsInstantWithChannel:channel]; + } + } else if (httpResponse.statusCode == 403) { + [GrowingNetworkPreflight sendPreflight]; + } + }]; + }]; } #pragma mark Event Persist From 65e0d60501b65a8bef67f12cb5b364444c0fc074 Mon Sep 17 00:00:00 2001 From: GIOSDK Date: Fri, 26 Apr 2024 06:39:24 +0000 Subject: [PATCH 09/18] style: code format --- .../Event/GrowingEventManager.m | 74 ++++++++++--------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 03375d1cd..b7b0e919f 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -340,41 +340,45 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { channel.isUploading = YES; NSObject *eventRequest = [[GrowingEventRequest alloc] initWithEvents:rawEvents]; - [service sendRequest:eventRequest completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { - [GrowingDispatchManager dispatchInGrowingThread:^{ - channel.isUploading = NO; - - if (error) { - return; - } - - if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || httpResponse.statusCode == 413) { - if (isViaCellular) { - if ([eventRequest respondsToSelector:@selector(outsize)]) { - self.uploadEventSize += eventRequest.outsize; - } - } - - if (httpResponse.statusCode != 413) { - for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: - request:channel:)]) { - [obj growingEventManagerEventsDidSend:events request:eventRequest channel:channel]; - } - } - } - - [self removeEvents_unsafe:events forChannel:channel]; - - // 如果剩余数量 大于单包数量 则直接发送 - if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { - [self sendEventsInstantWithChannel:channel]; - } - } else if (httpResponse.statusCode == 403) { - [GrowingNetworkPreflight sendPreflight]; - } - }]; - }]; + [service sendRequest:eventRequest + completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { + [GrowingDispatchManager dispatchInGrowingThread:^{ + channel.isUploading = NO; + + if (error) { + return; + } + + if ((httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) || + httpResponse.statusCode == 413) { + if (isViaCellular) { + if ([eventRequest respondsToSelector:@selector(outsize)]) { + self.uploadEventSize += eventRequest.outsize; + } + } + + if (httpResponse.statusCode != 413) { + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: + request:channel:)]) { + [obj growingEventManagerEventsDidSend:events + request:eventRequest + channel:channel]; + } + } + } + + [self removeEvents_unsafe:events forChannel:channel]; + + // 如果剩余数量 大于单包数量 则直接发送 + if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { + [self sendEventsInstantWithChannel:channel]; + } + } else if (httpResponse.statusCode == 403) { + [GrowingNetworkPreflight sendPreflight]; + } + }]; + }]; } #pragma mark Event Persist From bd3c3fa4711dc4463c2de0a02a5167e9193fb401 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Mon, 29 Apr 2024 19:00:27 +0800 Subject: [PATCH 10/18] feat: WIP preflight module --- Example/Example.xcodeproj/project.pbxproj | 24 +++--- .../GrowingAnalyticsStartTests.m | 6 -- GrowingAnalytics.podspec | 8 ++ .../Event/GrowingEventManager.m | 2 +- .../GrowingTrackConfiguration.m | 15 +++- GrowingTrackerCore/Manager/GrowingSession.m | 2 +- .../Request/Adapter/GrowingRequestAdapter.h | 4 - .../Request/Adapter/GrowingRequestAdapter.m | 32 -------- .../Public/GrowingTrackConfiguration.h | 1 - .../GrowingNetworkPreflight+Private.h | 34 ++++++++ .../Preflight}/GrowingNetworkPreflight.m | 81 +++++++++++++------ .../Public/GrowingNetworkPreflight.h | 37 +++++++++ .../Request/GrowingPFEventRequestAdapter.h | 11 +-- .../Request/GrowingPFEventRequestAdapter.m | 45 +++++++++++ .../Preflight/Request/GrowingPFRequest.h | 4 +- .../Preflight/Request/GrowingPFRequest.m | 11 +-- .../Request/GrowingPFRequestHeaderAdapter.h | 29 +++++++ .../Request/GrowingPFRequestHeaderAdapter.m | 51 ++++++++++++ 18 files changed, 299 insertions(+), 98 deletions(-) create mode 100644 Modules/Preflight/GrowingNetworkPreflight+Private.h rename {GrowingTrackerCore/Network => Modules/Preflight}/GrowingNetworkPreflight.m (58%) create mode 100644 Modules/Preflight/Public/GrowingNetworkPreflight.h rename GrowingTrackerCore/Network/GrowingNetworkPreflight.h => Modules/Preflight/Request/GrowingPFEventRequestAdapter.h (80%) create mode 100644 Modules/Preflight/Request/GrowingPFEventRequestAdapter.m rename GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h => Modules/Preflight/Request/GrowingPFRequest.h (88%) rename GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m => Modules/Preflight/Request/GrowingPFRequest.m (86%) create mode 100644 Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h create mode 100644 Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.m diff --git a/Example/Example.xcodeproj/project.pbxproj b/Example/Example.xcodeproj/project.pbxproj index 4544a8324..912337ee7 100644 --- a/Example/Example.xcodeproj/project.pbxproj +++ b/Example/Example.xcodeproj/project.pbxproj @@ -2332,7 +2332,7 @@ inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Example/Pods-Example-frameworks.sh", "${BUILT_PRODUCTS_DIR}/GrowingAPM/GrowingAPM.framework", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-18cf2943/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-232e7e5e/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingToolsKit/GrowingToolsKit.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", @@ -2361,7 +2361,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-GrowingAnalyticsStartTests/Pods-GrowingAnalyticsStartTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-88384491/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-4f84af90/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", ); @@ -2559,7 +2559,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Example-watchOS Watch App/Pods-Example-watchOS Watch App-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-3edd9a5d/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-403bfd64/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-TrackerCore-watchOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-watchOS/Protobuf.framework", ); @@ -2582,7 +2582,7 @@ inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-ExampleiOS13/Pods-ExampleiOS13-frameworks.sh", "${BUILT_PRODUCTS_DIR}/GrowingAPM/GrowingAPM.framework", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-8f52182e/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-0ddd49d8/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingToolsKit/GrowingToolsKit.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", @@ -2608,7 +2608,7 @@ inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Example-SwiftUI/Pods-Example-SwiftUI-frameworks.sh", "${BUILT_PRODUCTS_DIR}/GrowingAPM-Core/GrowingAPM.framework", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-dd4d243e/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-d1292d27/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", ); @@ -2653,7 +2653,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-GrowingAnalyticsTests/Pods-GrowingAnalyticsTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-b35945a7/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-5371fadd/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", ); @@ -2675,7 +2675,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-ABTestingTests/Pods-ABTestingTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-cfbe515d/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-fa1c1980/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", "${BUILT_PRODUCTS_DIR}/OHHTTPStubs/OHHTTPStubs.framework", @@ -2699,7 +2699,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Example-tvOS/Pods-Example-tvOS-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-67f52605/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-6ab1300e/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-tvOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-tvOS/Protobuf.framework", ); @@ -2765,7 +2765,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-HostApplicationTests/Pods-HostApplicationTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-d5b2e835/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-a2e10ca9/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", "${BUILT_PRODUCTS_DIR}/KIF/KIF.framework", @@ -2855,7 +2855,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-ProtobufTests/Pods-ProtobufTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-d5b2e835/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-a2e10ca9/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", ); @@ -2877,7 +2877,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-Example-macOS/Pods-Example-macOS-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-9b78b9c4/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-8df63dd3/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-TrackerCore-macOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-macOS/Protobuf.framework", ); @@ -2899,7 +2899,7 @@ ); inputPaths = ( "${PODS_ROOT}/Target Support Files/Pods-AdvertisingTests/Pods-AdvertisingTests-frameworks.sh", - "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-8f52182e/GrowingAnalytics.framework", + "${BUILT_PRODUCTS_DIR}/GrowingAnalytics-0ddd49d8/GrowingAnalytics.framework", "${BUILT_PRODUCTS_DIR}/GrowingUtils-AutotrackerCore-TrackerCore-iOS/GrowingUtils.framework", "${BUILT_PRODUCTS_DIR}/Protobuf-iOS/Protobuf.framework", ); diff --git a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m index 572fb0373..991812ce1 100644 --- a/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m +++ b/Example/GrowingAnalyticsTests/GrowingAnalyticsStartTests/GrowingAnalyticsStartTests.m @@ -114,7 +114,6 @@ - (void)testDefaultConfiguration_Autotracker { GrowingAutotrackConfiguration *configuration = (GrowingAutotrackConfiguration *)GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); - XCTAssertEqual(configuration.requestPreflight, YES); XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); @@ -136,7 +135,6 @@ - (void)testDefaultConfiguration_Autotracker { - (void)testSetConfiguration_Autotracker { GrowingAutotrackConfiguration *config = [GrowingAutotrackConfiguration configurationWithAccountId:@"test"]; config.debugEnabled = YES; - config.requestPreflight = NO; config.cellularDataLimit = 5; config.dataUploadInterval = 10; config.sessionInterval = 10; @@ -167,7 +165,6 @@ - (void)testSetConfiguration_Autotracker { GrowingAutotrackConfiguration *configuration = (GrowingAutotrackConfiguration *)GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, YES); - XCTAssertEqual(configuration.requestPreflight, NO); XCTAssertEqual(configuration.cellularDataLimit, 5); XCTAssertEqual(configuration.dataUploadInterval, 10); XCTAssertEqual(configuration.sessionInterval, 10); @@ -193,7 +190,6 @@ - (void)testDefaultConfiguration_Tracker { GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, NO); - XCTAssertEqual(configuration.requestPreflight, YES); XCTAssertEqual(configuration.cellularDataLimit, 20); XCTAssertEqual(configuration.dataUploadInterval, 15); XCTAssertEqual(configuration.sessionInterval, 30); @@ -214,7 +210,6 @@ - (void)testDefaultConfiguration_Tracker { - (void)testSetConfiguration_Tracker { GrowingTrackConfiguration *config = [GrowingTrackConfiguration configurationWithAccountId:@"test"]; config.debugEnabled = YES; - config.requestPreflight = NO; config.cellularDataLimit = 5; config.dataUploadInterval = 10; config.sessionInterval = 10; @@ -243,7 +238,6 @@ - (void)testSetConfiguration_Tracker { GrowingTrackConfiguration *configuration = GrowingConfigurationManager.sharedInstance.trackConfiguration; XCTAssertEqual(configuration.debugEnabled, YES); - XCTAssertEqual(configuration.requestPreflight, NO); XCTAssertEqual(configuration.cellularDataLimit, 5); XCTAssertEqual(configuration.dataUploadInterval, 10); XCTAssertEqual(configuration.sessionInterval, 10); diff --git a/GrowingAnalytics.podspec b/GrowingAnalytics.podspec index ae2fd503d..4dbe178c4 100644 --- a/GrowingAnalytics.podspec +++ b/GrowingAnalytics.podspec @@ -30,6 +30,7 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和 autotracker.ios.dependency 'GrowingAnalytics/Hybrid', s.version.to_s autotracker.ios.dependency 'GrowingAnalytics/MobileDebugger', s.version.to_s autotracker.ios.dependency 'GrowingAnalytics/WebCircle', s.version.to_s + autotracker.dependency 'GrowingAnalytics/Preflight', s.version.to_s autotracker.dependency 'GrowingAnalytics/DefaultServices', s.version.to_s end @@ -40,6 +41,7 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和 # Modules tracker.ios.dependency 'GrowingAnalytics/MobileDebugger', s.version.to_s + tracker.dependency 'GrowingAnalytics/Preflight', s.version.to_s tracker.dependency 'GrowingAnalytics/DefaultServices', s.version.to_s end @@ -205,6 +207,12 @@ GrowingAnalytics具备自动采集基本的用户行为事件,比如访问和 ab.dependency 'GrowingAnalytics/TrackerCore', s.version.to_s end + s.subspec 'Preflight' do |preflight| + preflight.source_files = 'Modules/Preflight/**/*{.h,.m,.c,.cpp,.mm}' + preflight.public_header_files = 'Modules/Preflight/Public/*.h' + preflight.dependency 'GrowingAnalytics/TrackerCore', s.version.to_s + end + # 使用flutter无埋点插件时,将自动导入该库,正常情况下请勿手动导入 s.subspec 'Flutter' do |flutter| flutter.source_files = 'Modules/Flutter/**/*{.h,.m,.c,.cpp,.mm}' diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index b7b0e919f..39e808a85 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -25,7 +25,7 @@ #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" #import "GrowingTrackerCore/Manager/GrowingSession.h" #import "GrowingTrackerCore/Network/GrowingNetworkInterfaceManager.h" -#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" +#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" #import "GrowingTrackerCore/Network/Request/GrowingEventRequest.h" #import "GrowingTrackerCore/Public/GrowingBaseEvent.h" #import "GrowingTrackerCore/Public/GrowingEventFilter.h" diff --git a/GrowingTrackerCore/GrowingTrackConfiguration.m b/GrowingTrackerCore/GrowingTrackConfiguration.m index 6e931c196..7014ddbe5 100644 --- a/GrowingTrackerCore/GrowingTrackConfiguration.m +++ b/GrowingTrackerCore/GrowingTrackConfiguration.m @@ -21,6 +21,7 @@ #import "GrowingTrackerCore/Manager/GrowingSession.h" NSString *const kGrowingDefaultDataCollectionServerHost = @"https://napi.growingio.com"; +NSString *const kGrowingDefaultMinorDataCollectionServerHost = @"https://minor.napi.growingio.com"; NSString *const kGrowingDefaultDeepLinkHost = @"https://link.growingio.com"; NSString *const kGrowingDefaultABTestingServerHost = @"https://ab.growingio.com"; @@ -39,6 +40,10 @@ @interface GrowingTrackConfiguration () @property (nonatomic, copy) NSString *abTestingServerHost; @property (nonatomic, assign) NSUInteger abTestingRequestInterval; +// Preflight +@property (nonatomic, assign) BOOL requestPreflight; +@property (nonatomic, copy) NSString *minorDataCollectionServerHost; + @end @implementation GrowingTrackConfiguration @@ -50,7 +55,6 @@ - (instancetype)initWithAccountId:(NSString *)accountId { _dataSourceId = nil; _debugEnabled = NO; - _requestPreflight = YES; _cellularDataLimit = 20; _dataUploadInterval = 15; _sessionInterval = 30; @@ -79,6 +83,10 @@ - (instancetype)initWithAccountId:(NSString *)accountId { // ABTesting _abTestingServerHost = kGrowingDefaultABTestingServerHost; _abTestingRequestInterval = 5; + + // Preflight + _requestPreflight = NO; + _minorDataCollectionServerHost = kGrowingDefaultMinorDataCollectionServerHost; } return self; @@ -97,7 +105,6 @@ - (id)copyWithZone:(NSZone *)zone { configuration->_accountId = [_accountId copy]; configuration->_dataSourceId = [_dataSourceId copy]; configuration->_debugEnabled = _debugEnabled; - configuration->_requestPreflight = _requestPreflight; configuration->_cellularDataLimit = _cellularDataLimit; configuration->_dataUploadInterval = _dataUploadInterval; configuration->_sessionInterval = _sessionInterval; @@ -126,6 +133,10 @@ - (id)copyWithZone:(NSZone *)zone { // ABTesting configuration->_abTestingServerHost = [_abTestingServerHost copy]; configuration->_abTestingRequestInterval = _abTestingRequestInterval; + + // Preflight + configuration->_requestPreflight = _requestPreflight; + configuration->_minorDataCollectionServerHost = [_minorDataCollectionServerHost copy]; return configuration; } diff --git a/GrowingTrackerCore/Manager/GrowingSession.m b/GrowingTrackerCore/Manager/GrowingSession.m index 474f52cdd..99c874f0f 100644 --- a/GrowingTrackerCore/Manager/GrowingSession.m +++ b/GrowingTrackerCore/Manager/GrowingSession.m @@ -23,7 +23,7 @@ #import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h" #import "GrowingTrackerCore/Helpers/GrowingHelpers.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" -#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" +#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" #import "GrowingTrackerCore/Public/GrowingTrackConfiguration.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogMacros.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" diff --git a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h index 56257c900..b0a971806 100644 --- a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h +++ b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h @@ -30,8 +30,4 @@ NS_ASSUME_NONNULL_BEGIN @end -@interface GrowingPreflightRequestHeaderAdapter : NSObject - -@end - NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m index f089d0523..437af260a 100644 --- a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m +++ b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m @@ -97,35 +97,3 @@ - (NSUInteger)priority { @end -#pragma mark GrowingPreflightRequestHeaderAdapter - -@interface GrowingPreflightRequestHeaderAdapter () - -@property (nonatomic, weak) id request; - -@end - -@implementation GrowingPreflightRequestHeaderAdapter - -+ (instancetype)adapterWithRequest:(id)request { - GrowingPreflightRequestHeaderAdapter *adapter = [[self alloc] init]; - adapter.request = request; - return adapter; -} - -- (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { - NSMutableURLRequest *needAdaptReq = request; - [needAdaptReq setValue:@"POST" forHTTPHeaderField:@"Access-Control-Request-Method"]; - [needAdaptReq setValue:@"Accept, Content-Type, X-Timestamp, X-Crypt-Codec, X-Compress-Codec" - forHTTPHeaderField:@"Access-Control-Request-Headers"]; - NSURL *url = [self.request absoluteURL]; - NSString *origin = [NSString stringWithFormat:@"%@://%@", url.scheme, url.host]; - [needAdaptReq setValue:origin forHTTPHeaderField:@"Origin"]; - return needAdaptReq; -} - -- (NSUInteger)priority { - return 0; -} - -@end diff --git a/GrowingTrackerCore/Public/GrowingTrackConfiguration.h b/GrowingTrackerCore/Public/GrowingTrackConfiguration.h index 957cbb32e..3044e29e6 100644 --- a/GrowingTrackerCore/Public/GrowingTrackConfiguration.h +++ b/GrowingTrackerCore/Public/GrowingTrackConfiguration.h @@ -29,7 +29,6 @@ FOUNDATION_EXPORT NSString *const kGrowingDefaultDataCollectionServerHost; @property (nonatomic, copy, readonly) NSString *accountId; @property (nonatomic, copy) NSString *dataSourceId; @property (nonatomic, assign) BOOL debugEnabled; -@property (nonatomic, assign) BOOL requestPreflight; @property (nonatomic, assign) NSUInteger cellularDataLimit; @property (nonatomic, assign) NSTimeInterval dataUploadInterval; @property (nonatomic, assign) NSTimeInterval sessionInterval; diff --git a/Modules/Preflight/GrowingNetworkPreflight+Private.h b/Modules/Preflight/GrowingNetworkPreflight+Private.h new file mode 100644 index 000000000..1a81fafa0 --- /dev/null +++ b/Modules/Preflight/GrowingNetworkPreflight+Private.h @@ -0,0 +1,34 @@ +// +// GrowingNetworkPreflight+Private.h +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/29. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "GrowingNetworkPreflight.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface GrowingNetworkPreflight (Private) + ++ (BOOL)isSucceed; + ++ (NSString *)dataCollectionServerHost; + ++ (void)sendPreflight; + +@end + +NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m similarity index 58% rename from GrowingTrackerCore/Network/GrowingNetworkPreflight.m rename to Modules/Preflight/GrowingNetworkPreflight.m index 71e90d651..760b33af1 100644 --- a/GrowingTrackerCore/Network/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -17,18 +17,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "GrowingTrackerCore/Network/GrowingNetworkPreflight.h" +#import "Modules/Preflight/GrowingNetworkPreflight+Private.h" +#import "Modules/Preflight/Request/GrowingPFRequest.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" -#import "GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h" #import "GrowingTrackerCore/Public/GrowingEventNetworkService.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" +#import "GrowingTrackerCore/Network/Request/Adapter/GrowingEventRequestAdapters.h" +#import "Modules/Preflight/Request/GrowingPFEventRequestAdapter.h" + +GrowingMod(GrowingNetworkPreflight) typedef NS_ENUM(NSUInteger, GrowingNetworkPreflightStatus) { - GrowingNWPreflightStatusNotDetermined, - GrowingNWPreflightStatusAuthorized, - GrowingNWPreflightStatusDenied, - GrowingNWPreflightStatusWaitingForResponse, + GrowingNWPreflightStatusNotDetermined, // 待预检 + GrowingNWPreflightStatusWaitingForResponse, // 预检中 + GrowingNWPreflightStatusAuthorized, // 预检成功 + GrowingNWPreflightStatusDenied, // 预检失败 + GrowingNWPreflightStatusClosed, // 预检关闭 }; static NSTimeInterval const kGrowingPreflightMaxTime = 300; @@ -38,11 +43,18 @@ @interface GrowingNetworkPreflight () @property (nonatomic, assign) GrowingNetworkPreflightStatus status; @property (nonatomic, assign) NSTimeInterval nextPreflightTime; +@property (nonatomic, assign) NSTimeInterval minPreflightTime; +@property (nonatomic, copy) NSString *dataCollectionServerHost; + @end @implementation GrowingNetworkPreflight -#pragma mark - Initialize +#pragma mark - GrowingModuleProtocol + ++ (BOOL)singleton { + return YES; +} + (instancetype)sharedInstance { static id instance = nil; @@ -53,31 +65,47 @@ + (instancetype)sharedInstance { return instance; } +- (void)growingModInit:(GrowingContext *)context { + [GrowingEventRequestAdapters.sharedInstance addAdapter:GrowingPFEventRequestAdapter.class]; + + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; + NSString *dataCollectionServerHost = trackConfiguration.dataCollectionServerHost; + self.dataCollectionServerHost = dataCollectionServerHost; + if (![dataCollectionServerHost isEqualToString:kGrowingDefaultDataCollectionServerHost]) { + // 私有部署 + BOOL requestPreflight = trackConfiguration.requestPreflight; + if (!requestPreflight) { + // 预检功能关闭 + self.status = GrowingNWPreflightStatusClosed; + } + } + + NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; + dataUploadInterval = MAX(dataUploadInterval, 5); + self.minPreflightTime = dataUploadInterval; +} + #pragma mark - Public Methods + (BOOL)isSucceed { - BOOL requestPreflight = GrowingConfigurationManager.sharedInstance.trackConfiguration.requestPreflight; - if (!requestPreflight) { - return YES; - } GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; - return preflight.status == GrowingNWPreflightStatusAuthorized; + return preflight.status > GrowingNWPreflightStatusWaitingForResponse; +} + ++ (NSString *)dataCollectionServerHost { + // call when preflight isSucceed + GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; + return preflight.dataCollectionServerHost; } + (void)sendPreflight { [GrowingDispatchManager dispatchInGrowingThread:^{ GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; - BOOL requestPreflight = trackConfiguration.requestPreflight; - if (!requestPreflight) { - return; - } - - NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; - dataUploadInterval = MAX(dataUploadInterval, 5); - GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; - preflight.nextPreflightTime = dataUploadInterval; - if (preflight.status != GrowingNWPreflightStatusWaitingForResponse) { + preflight.nextPreflightTime = preflight.minPreflightTime; + preflight.dataCollectionServerHost = trackConfiguration.dataCollectionServerHost; + if (preflight.status != GrowingNWPreflightStatusWaitingForResponse || + preflight.status != GrowingNWPreflightStatusClosed) { [preflight sendPreflight]; } }]; @@ -94,16 +122,19 @@ - (void)sendPreflight { return; } - NSObject *preflight = [[GrowingPreflightRequest alloc] init]; + NSObject *preflight = [[GrowingPFRequest alloc] init]; [service sendRequest:preflight completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { [GrowingDispatchManager dispatchInGrowingThread:^{ if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 400) { self.status = GrowingNWPreflightStatusAuthorized; - } else { + } else if (httpResponse.statusCode == 403) { self.status = GrowingNWPreflightStatusDenied; - + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; + self.dataCollectionServerHost = trackConfiguration.minorDataCollectionServerHost; + } else { + self.status = GrowingNWPreflightStatusWaitingForResponse; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.nextPreflightTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ diff --git a/Modules/Preflight/Public/GrowingNetworkPreflight.h b/Modules/Preflight/Public/GrowingNetworkPreflight.h new file mode 100644 index 000000000..4092ce88f --- /dev/null +++ b/Modules/Preflight/Public/GrowingNetworkPreflight.h @@ -0,0 +1,37 @@ +// +// GrowingNetworkPreflight.h +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/24. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import +#import "GrowingModuleProtocol.h" +#import "GrowingTrackConfiguration.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface GrowingNetworkPreflight : NSObject + +@end + +@interface GrowingTrackConfiguration (Preflight) + +@property (nonatomic, assign) BOOL requestPreflight; +@property (nonatomic, copy) NSString *minorDataCollectionServerHost; + +@end + +NS_ASSUME_NONNULL_END diff --git a/GrowingTrackerCore/Network/GrowingNetworkPreflight.h b/Modules/Preflight/Request/GrowingPFEventRequestAdapter.h similarity index 80% rename from GrowingTrackerCore/Network/GrowingNetworkPreflight.h rename to Modules/Preflight/Request/GrowingPFEventRequestAdapter.h index f95a71403..1c2d3aa86 100644 --- a/GrowingTrackerCore/Network/GrowingNetworkPreflight.h +++ b/Modules/Preflight/Request/GrowingPFEventRequestAdapter.h @@ -1,8 +1,8 @@ // -// GrowingNetworkPreflight.h +// GrowingPFEventRequestAdapter.h // GrowingAnalytics // -// Created by YoloMao on 2024/4/24. +// Created by YoloMao on 2024/4/29. // Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -18,14 +18,11 @@ // limitations under the License. #import +#import "GrowingRequestProtocol.h" NS_ASSUME_NONNULL_BEGIN -@interface GrowingNetworkPreflight : NSObject - -+ (BOOL)isSucceed; - -+ (void)sendPreflight; +@interface GrowingPFEventRequestAdapter : NSObject @end diff --git a/Modules/Preflight/Request/GrowingPFEventRequestAdapter.m b/Modules/Preflight/Request/GrowingPFEventRequestAdapter.m new file mode 100644 index 000000000..835a41ace --- /dev/null +++ b/Modules/Preflight/Request/GrowingPFEventRequestAdapter.m @@ -0,0 +1,45 @@ +// +// GrowingPFEventRequestAdapter.m +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/29. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "Modules/Preflight/Request/GrowingPFEventRequestAdapter.h" +#import "Modules/Preflight/GrowingNetworkPreflight+Private.h" + +@implementation GrowingPFEventRequestAdapter + ++ (instancetype)adapterWithRequest:(id)request { + GrowingPFEventRequestAdapter *adapter = [[self alloc] init]; + return adapter; +} + +- (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { + NSMutableURLRequest *needAdaptReq = request; + NSString *serverHost = [GrowingNetworkPreflight dataCollectionServerHost]; + NSURL *url = [NSURL URLWithString:serverHost]; + NSURLComponents *components1 = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO]; + NSURLComponents *components2 = [NSURLComponents componentsWithURL:needAdaptReq.URL resolvingAgainstBaseURL:NO]; + components2.host = components1.host; + needAdaptReq.URL = components2.URL; + return needAdaptReq; +} + +- (NSUInteger)priority { + return 0; +} + +@end diff --git a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h b/Modules/Preflight/Request/GrowingPFRequest.h similarity index 88% rename from GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h rename to Modules/Preflight/Request/GrowingPFRequest.h index a30bd50ff..d2eb04547 100644 --- a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h +++ b/Modules/Preflight/Request/GrowingPFRequest.h @@ -1,5 +1,5 @@ // -// GrowingPreflightRequest.h +// GrowingPFRequest.h // GrowingAnalytics // // Created by YoloMao on 2024/4/24. @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN -@interface GrowingPreflightRequest : NSObject +@interface GrowingPFRequest : NSObject @end diff --git a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m b/Modules/Preflight/Request/GrowingPFRequest.m similarity index 86% rename from GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m rename to Modules/Preflight/Request/GrowingPFRequest.m index 9c64a5d2b..c54b6e0cd 100644 --- a/GrowingTrackerCore/Network/Request/GrowingPreflightRequest.m +++ b/Modules/Preflight/Request/GrowingPFRequest.m @@ -1,5 +1,5 @@ // -// GrowingPreflightRequest.m +// GrowingPFRequest.m // GrowingAnalytics // // Created by YoloMao on 2024/4/24. @@ -17,13 +17,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "GrowingTrackerCore/Network/Request/GrowingPreflightRequest.h" +#import "Modules/Preflight/Request/GrowingPFRequest.h" +#import "Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h" #import "GrowingTrackerCore/Helpers/GrowingHelpers.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" #import "GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h" #import "GrowingULTimeUtil.h" -@implementation GrowingPreflightRequest +@implementation GrowingPFRequest - (GrowingHTTPMethod)method { return GrowingHTTPMethodOPTIONS; @@ -46,8 +47,8 @@ - (NSString *)path { } - (NSArray> *)adapters { - GrowingPreflightRequestHeaderAdapter *basicHeaderAdapter = - [GrowingPreflightRequestHeaderAdapter adapterWithRequest:self]; + GrowingPFRequestHeaderAdapter *basicHeaderAdapter = + [GrowingPFRequestHeaderAdapter adapterWithRequest:self]; GrowingRequestMethodAdapter *methodAdapter = [GrowingRequestMethodAdapter adapterWithRequest:self]; return @[basicHeaderAdapter, methodAdapter]; } diff --git a/Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h b/Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h new file mode 100644 index 000000000..22e3d621d --- /dev/null +++ b/Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h @@ -0,0 +1,29 @@ +// +// GrowingPFRequestHeaderAdapter.h +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/29. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import +#import "GrowingRequestProtocol.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface GrowingPFRequestHeaderAdapter : NSObject + +@end + +NS_ASSUME_NONNULL_END diff --git a/Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.m b/Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.m new file mode 100644 index 000000000..e928753dc --- /dev/null +++ b/Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.m @@ -0,0 +1,51 @@ +// +// GrowingPFRequestHeaderAdapter.m +// GrowingAnalytics +// +// Created by YoloMao on 2024/4/29. +// Copyright (C) 2024 Beijing Yishu Technology Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h" + +@interface GrowingPFRequestHeaderAdapter () + +@property (nonatomic, weak) id request; + +@end + +@implementation GrowingPFRequestHeaderAdapter + ++ (instancetype)adapterWithRequest:(id)request { + GrowingPFRequestHeaderAdapter *adapter = [[self alloc] init]; + adapter.request = request; + return adapter; +} + +- (NSMutableURLRequest *)adaptedURLRequest:(NSMutableURLRequest *)request { + NSMutableURLRequest *needAdaptReq = request; + [needAdaptReq setValue:@"POST" forHTTPHeaderField:@"Access-Control-Request-Method"]; + [needAdaptReq setValue:@"Accept, Content-Type, X-Timestamp, X-Crypt-Codec, X-Compress-Codec" + forHTTPHeaderField:@"Access-Control-Request-Headers"]; + NSURL *url = [self.request absoluteURL]; + NSString *origin = [NSString stringWithFormat:@"%@://%@", url.scheme, url.host]; + [needAdaptReq setValue:origin forHTTPHeaderField:@"Origin"]; + return needAdaptReq; +} + +- (NSUInteger)priority { + return 0; +} + +@end From 0b749c5a350846c7fe9e0ec95f209960473f0d3a Mon Sep 17 00:00:00 2001 From: GIOSDK Date: Mon, 29 Apr 2024 11:00:50 +0000 Subject: [PATCH 11/18] style: code format --- .../Event/GrowingEventManager.m | 2 +- .../GrowingTrackConfiguration.m | 4 ++-- GrowingTrackerCore/Manager/GrowingSession.m | 2 +- .../Request/Adapter/GrowingRequestAdapter.m | 1 - Modules/Preflight/GrowingNetworkPreflight.m | 21 ++++++++++--------- Modules/Preflight/Request/GrowingPFRequest.m | 5 ++--- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 39e808a85..2f0ea3203 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -25,7 +25,6 @@ #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" #import "GrowingTrackerCore/Manager/GrowingSession.h" #import "GrowingTrackerCore/Network/GrowingNetworkInterfaceManager.h" -#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" #import "GrowingTrackerCore/Network/Request/GrowingEventRequest.h" #import "GrowingTrackerCore/Public/GrowingBaseEvent.h" #import "GrowingTrackerCore/Public/GrowingEventFilter.h" @@ -35,6 +34,7 @@ #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" #import "GrowingTrackerCore/Utils/GrowingDeviceInfo.h" +#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" static const NSUInteger kGrowingMaxDBCacheSize = 100; // default: write to DB as soon as there are 100 events static const NSUInteger kGrowingMaxBatchSize = 500; // default: send no more than 500 events in every batch diff --git a/GrowingTrackerCore/GrowingTrackConfiguration.m b/GrowingTrackerCore/GrowingTrackConfiguration.m index 7014ddbe5..d91c25ba6 100644 --- a/GrowingTrackerCore/GrowingTrackConfiguration.m +++ b/GrowingTrackerCore/GrowingTrackConfiguration.m @@ -83,7 +83,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId { // ABTesting _abTestingServerHost = kGrowingDefaultABTestingServerHost; _abTestingRequestInterval = 5; - + // Preflight _requestPreflight = NO; _minorDataCollectionServerHost = kGrowingDefaultMinorDataCollectionServerHost; @@ -133,7 +133,7 @@ - (id)copyWithZone:(NSZone *)zone { // ABTesting configuration->_abTestingServerHost = [_abTestingServerHost copy]; configuration->_abTestingRequestInterval = _abTestingRequestInterval; - + // Preflight configuration->_requestPreflight = _requestPreflight; configuration->_minorDataCollectionServerHost = [_minorDataCollectionServerHost copy]; diff --git a/GrowingTrackerCore/Manager/GrowingSession.m b/GrowingTrackerCore/Manager/GrowingSession.m index 99c874f0f..530628999 100644 --- a/GrowingTrackerCore/Manager/GrowingSession.m +++ b/GrowingTrackerCore/Manager/GrowingSession.m @@ -23,7 +23,6 @@ #import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h" #import "GrowingTrackerCore/Helpers/GrowingHelpers.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" -#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" #import "GrowingTrackerCore/Public/GrowingTrackConfiguration.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogMacros.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" @@ -32,6 +31,7 @@ #import "GrowingTrackerCore/Utils/GrowingInternalMacros.h" #import "GrowingULAppLifecycle.h" #import "GrowingULTimeUtil.h" +#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" @interface GrowingSession () diff --git a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m index 437af260a..873f9263f 100644 --- a/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m +++ b/GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.m @@ -96,4 +96,3 @@ - (NSUInteger)priority { } @end - diff --git a/Modules/Preflight/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m index 760b33af1..9c505af5e 100644 --- a/Modules/Preflight/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -17,23 +17,23 @@ // See the License for the specific language governing permissions and // limitations under the License. -#import "Modules/Preflight/GrowingNetworkPreflight+Private.h" -#import "Modules/Preflight/Request/GrowingPFRequest.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" +#import "GrowingTrackerCore/Network/Request/Adapter/GrowingEventRequestAdapters.h" #import "GrowingTrackerCore/Public/GrowingEventNetworkService.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" -#import "GrowingTrackerCore/Network/Request/Adapter/GrowingEventRequestAdapters.h" +#import "Modules/Preflight/GrowingNetworkPreflight+Private.h" #import "Modules/Preflight/Request/GrowingPFEventRequestAdapter.h" +#import "Modules/Preflight/Request/GrowingPFRequest.h" GrowingMod(GrowingNetworkPreflight) typedef NS_ENUM(NSUInteger, GrowingNetworkPreflightStatus) { - GrowingNWPreflightStatusNotDetermined, // 待预检 - GrowingNWPreflightStatusWaitingForResponse, // 预检中 - GrowingNWPreflightStatusAuthorized, // 预检成功 - GrowingNWPreflightStatusDenied, // 预检失败 - GrowingNWPreflightStatusClosed, // 预检关闭 + GrowingNWPreflightStatusNotDetermined, // 待预检 + GrowingNWPreflightStatusWaitingForResponse, // 预检中 + GrowingNWPreflightStatusAuthorized, // 预检成功 + GrowingNWPreflightStatusDenied, // 预检失败 + GrowingNWPreflightStatusClosed, // 预检关闭 }; static NSTimeInterval const kGrowingPreflightMaxTime = 300; @@ -79,7 +79,7 @@ - (void)growingModInit:(GrowingContext *)context { self.status = GrowingNWPreflightStatusClosed; } } - + NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); self.minPreflightTime = dataUploadInterval; @@ -131,7 +131,8 @@ - (void)sendPreflight { self.status = GrowingNWPreflightStatusAuthorized; } else if (httpResponse.statusCode == 403) { self.status = GrowingNWPreflightStatusDenied; - GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; + GrowingTrackConfiguration *trackConfiguration = + GrowingConfigurationManager.sharedInstance.trackConfiguration; self.dataCollectionServerHost = trackConfiguration.minorDataCollectionServerHost; } else { self.status = GrowingNWPreflightStatusWaitingForResponse; diff --git a/Modules/Preflight/Request/GrowingPFRequest.m b/Modules/Preflight/Request/GrowingPFRequest.m index c54b6e0cd..d4213590c 100644 --- a/Modules/Preflight/Request/GrowingPFRequest.m +++ b/Modules/Preflight/Request/GrowingPFRequest.m @@ -18,11 +18,11 @@ // limitations under the License. #import "Modules/Preflight/Request/GrowingPFRequest.h" -#import "Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h" #import "GrowingTrackerCore/Helpers/GrowingHelpers.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" #import "GrowingTrackerCore/Network/Request/Adapter/GrowingRequestAdapter.h" #import "GrowingULTimeUtil.h" +#import "Modules/Preflight/Request/GrowingPFRequestHeaderAdapter.h" @implementation GrowingPFRequest @@ -47,8 +47,7 @@ - (NSString *)path { } - (NSArray> *)adapters { - GrowingPFRequestHeaderAdapter *basicHeaderAdapter = - [GrowingPFRequestHeaderAdapter adapterWithRequest:self]; + GrowingPFRequestHeaderAdapter *basicHeaderAdapter = [GrowingPFRequestHeaderAdapter adapterWithRequest:self]; GrowingRequestMethodAdapter *methodAdapter = [GrowingRequestMethodAdapter adapterWithRequest:self]; return @[basicHeaderAdapter, methodAdapter]; } From 11ac767af7b256c693c08ef182a75fc0f3afb7cd Mon Sep 17 00:00:00 2001 From: YoloMao Date: Tue, 30 Apr 2024 14:16:04 +0800 Subject: [PATCH 12/18] fix: rename minorServerHost to alternateServerHost --- GrowingTrackerCore/GrowingTrackConfiguration.m | 8 ++++---- Modules/Preflight/GrowingNetworkPreflight.m | 2 +- Modules/Preflight/Public/GrowingNetworkPreflight.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/GrowingTrackerCore/GrowingTrackConfiguration.m b/GrowingTrackerCore/GrowingTrackConfiguration.m index d91c25ba6..39a881915 100644 --- a/GrowingTrackerCore/GrowingTrackConfiguration.m +++ b/GrowingTrackerCore/GrowingTrackConfiguration.m @@ -21,7 +21,7 @@ #import "GrowingTrackerCore/Manager/GrowingSession.h" NSString *const kGrowingDefaultDataCollectionServerHost = @"https://napi.growingio.com"; -NSString *const kGrowingDefaultMinorDataCollectionServerHost = @"https://minor.napi.growingio.com"; +NSString *const kGrowingDefaultAlternateDataCollectionServerHost = @"https://alternate.napi.growingio.com"; NSString *const kGrowingDefaultDeepLinkHost = @"https://link.growingio.com"; NSString *const kGrowingDefaultABTestingServerHost = @"https://ab.growingio.com"; @@ -42,7 +42,7 @@ @interface GrowingTrackConfiguration () // Preflight @property (nonatomic, assign) BOOL requestPreflight; -@property (nonatomic, copy) NSString *minorDataCollectionServerHost; +@property (nonatomic, copy) NSString *alternateDataCollectionServerHost; @end @@ -86,7 +86,7 @@ - (instancetype)initWithAccountId:(NSString *)accountId { // Preflight _requestPreflight = NO; - _minorDataCollectionServerHost = kGrowingDefaultMinorDataCollectionServerHost; + _alternateDataCollectionServerHost = kGrowingDefaultAlternateDataCollectionServerHost; } return self; @@ -136,7 +136,7 @@ - (id)copyWithZone:(NSZone *)zone { // Preflight configuration->_requestPreflight = _requestPreflight; - configuration->_minorDataCollectionServerHost = [_minorDataCollectionServerHost copy]; + configuration->_alternateDataCollectionServerHost = [_alternateDataCollectionServerHost copy]; return configuration; } diff --git a/Modules/Preflight/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m index 9c505af5e..563262f89 100644 --- a/Modules/Preflight/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -133,7 +133,7 @@ - (void)sendPreflight { self.status = GrowingNWPreflightStatusDenied; GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; - self.dataCollectionServerHost = trackConfiguration.minorDataCollectionServerHost; + self.dataCollectionServerHost = trackConfiguration.alternateDataCollectionServerHost; } else { self.status = GrowingNWPreflightStatusWaitingForResponse; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.nextPreflightTime * NSEC_PER_SEC)), diff --git a/Modules/Preflight/Public/GrowingNetworkPreflight.h b/Modules/Preflight/Public/GrowingNetworkPreflight.h index 4092ce88f..c8e42ffb9 100644 --- a/Modules/Preflight/Public/GrowingNetworkPreflight.h +++ b/Modules/Preflight/Public/GrowingNetworkPreflight.h @@ -30,7 +30,7 @@ NS_ASSUME_NONNULL_BEGIN @interface GrowingTrackConfiguration (Preflight) @property (nonatomic, assign) BOOL requestPreflight; -@property (nonatomic, copy) NSString *minorDataCollectionServerHost; +@property (nonatomic, copy) NSString *alternateDataCollectionServerHost; @end From cc7cc59fca1b3591b3f7f12d13a264d6925ecc48 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Tue, 30 Apr 2024 16:08:06 +0800 Subject: [PATCH 13/18] feat: preflight module --- .../Core/GrowingModuleManager.m | 2 + .../Event/GrowingEventManager.h | 19 +++++++-- .../Event/GrowingEventManager.m | 36 +++++++++-------- GrowingTrackerCore/Manager/GrowingSession.m | 16 +++++--- .../Public/GrowingModuleManager.h | 6 ++- .../AppleSearchAds/GrowingAsaFetcher.m | 12 ++++-- .../GrowingDebuggerEventQueue.m | 12 ++++-- .../GrowingNetworkPreflight+Private.h | 4 -- Modules/Preflight/GrowingNetworkPreflight.m | 39 +++++++++++++++---- 9 files changed, 102 insertions(+), 44 deletions(-) diff --git a/GrowingTrackerCore/Core/GrowingModuleManager.m b/GrowingTrackerCore/Core/GrowingModuleManager.m index b36bf14e6..60119abc1 100644 --- a/GrowingTrackerCore/Core/GrowingModuleManager.m +++ b/GrowingTrackerCore/Core/GrowingModuleManager.m @@ -27,6 +27,7 @@ static NSString *kInitSelector = @"growingModInit:"; static NSString *kSetDataCollectionEnabledSelector = @"growingModSetDataCollectionEnabled:"; +static NSString *kRefreshSessionSelector = @"growingModRefreshSession:"; @interface GrowingModuleManager () @@ -191,6 +192,7 @@ - (NSMutableArray *)modules { _selectorByEvent = @{ @(GrowingMInitEvent): kInitSelector, @(GrowingMSetDataCollectionEnabledEvent): kSetDataCollectionEnabledSelector, + @(GrowingMRefreshSessionEvent): kRefreshSessionSelector, }; } return _selectorByEvent; diff --git a/GrowingTrackerCore/Event/GrowingEventManager.h b/GrowingTrackerCore/Event/GrowingEventManager.h index 36135ae00..ab9cb5f41 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.h +++ b/GrowingTrackerCore/Event/GrowingEventManager.h @@ -46,16 +46,27 @@ /// @param event 当前事件 - (void)growingEventManagerEventDidWrite:(GrowingBaseEvent *_Nullable)event; +/// 是否开始发送事件 +/// @param channel 当前发送通道 +- (BOOL)growingEventManagerEventShouldBeginSending:(GrowingEventChannel *_Nonnull)channel; + /// 即将发送事件 /// @param events 发送的事件 +/// @param channel 当前发送通道 - (NSArray *_Nonnull)growingEventManagerEventsWillSend:(NSArray> *_Nonnull)events channel:(GrowingEventChannel *_Nonnull)channel; -/// 事件发送完毕 +/// 事件发送请求完毕 /// @param events 发送的事件 -- (void)growingEventManagerEventsDidSend:(NSArray> *_Nonnull)events - request:(id _Nonnull)request - channel:(GrowingEventChannel *_Nonnull)channel; +/// @param request 当前请求 +/// @param channel 当前发送通道 +/// @param httpResponse 当前请求响应 +/// @param error 当前请求错误 +- (void)growingEventManagerEventsSendingCompletion:(NSArray> *_Nonnull)events + request:(id _Nonnull)request + channel:(GrowingEventChannel *_Nonnull)channel + httpResponse:(NSHTTPURLResponse *_Nonnull)httpResponse + error:(NSError *_Nonnull)error; @end diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 2f0ea3203..84376010b 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -34,7 +34,6 @@ #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" #import "GrowingTrackerCore/Utils/GrowingDeviceInfo.h" -#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" static const NSUInteger kGrowingMaxDBCacheSize = 100; // default: write to DB as soon as there are 100 events static const NSUInteger kGrowingMaxBatchSize = 500; // default: send no more than 500 events in every batch @@ -229,9 +228,17 @@ - (void)sendEventsInstantWithChannel:(GrowingEventChannel *)channel { // 非安全 发送日志 - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { - if (![GrowingNetworkPreflight isSucceed]) { + BOOL shouldBeginSending = YES; + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventShouldBeginSending:)]) { + shouldBeginSending = [obj growingEventManagerEventShouldBeginSending:channel]; + } + } + + if (!shouldBeginSending) { return; } + if (channel.isUploading) { return; } @@ -343,8 +350,18 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { [service sendRequest:eventRequest completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { [GrowingDispatchManager dispatchInGrowingThread:^{ + for (NSObject *obj in self.allInterceptor) { + if ([obj respondsToSelector:@selector(growingEventManagerEventsSendingCompletion:request:channel:httpResponse:error:)]) { + [obj growingEventManagerEventsSendingCompletion:events + request:eventRequest + channel:channel + httpResponse:httpResponse + error:error]; + } + } + channel.isUploading = NO; - + if (error) { return; } @@ -357,25 +374,12 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { } } - if (httpResponse.statusCode != 413) { - for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsDidSend: - request:channel:)]) { - [obj growingEventManagerEventsDidSend:events - request:eventRequest - channel:channel]; - } - } - } - [self removeEvents_unsafe:events forChannel:channel]; // 如果剩余数量 大于单包数量 则直接发送 if (channel.db.countOfEvents >= kGrowingMaxBatchSize) { [self sendEventsInstantWithChannel:channel]; } - } else if (httpResponse.statusCode == 403) { - [GrowingNetworkPreflight sendPreflight]; } }]; }]; diff --git a/GrowingTrackerCore/Manager/GrowingSession.m b/GrowingTrackerCore/Manager/GrowingSession.m index 530628999..56761446c 100644 --- a/GrowingTrackerCore/Manager/GrowingSession.m +++ b/GrowingTrackerCore/Manager/GrowingSession.m @@ -31,10 +31,11 @@ #import "GrowingTrackerCore/Utils/GrowingInternalMacros.h" #import "GrowingULAppLifecycle.h" #import "GrowingULTimeUtil.h" -#import "Modules/Preflight/Public/GrowingNetworkPreflight.h" +#import "GrowingTrackerCore/Public/GrowingModuleManager.h" @interface GrowingSession () +@property (nonatomic, assign, readwrite, getter=isSentVisitAfterRefreshSessionId) BOOL sentVisitAfterRefreshSessionId; @property (nonatomic, copy, readwrite) NSString *sessionId; @property (nonatomic, copy, readwrite) NSString *loginUserId; @property (nonatomic, copy, readwrite) NSString *loginUserKey; @@ -85,19 +86,22 @@ + (instancetype)currentSession { } - (void)generateVisit { - [GrowingNetworkPreflight sendPreflight]; - GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; if (!trackConfiguration.dataCollectionEnabled) { return; } - _sentVisitAfterRefreshSessionId = YES; + self.sentVisitAfterRefreshSessionId = YES; [GrowingEventGenerator generateVisitEvent]; } - (void)refreshSessionId { - _sessionId = NSUUID.UUID.UUIDString; - _sentVisitAfterRefreshSessionId = NO; + self.sessionId = NSUUID.UUID.UUIDString; + self.sentVisitAfterRefreshSessionId = NO; + + [[GrowingModuleManager sharedInstance] triggerEvent:GrowingMRefreshSessionEvent + withCustomParam:@{ + @"sessionId": self.sessionId.copy + }]; } // iOS 11系统上面VC的viewDidAppear生命周期会早于AppDelegate的applicationDidBecomeActive,这样会造成Page事件早于Visit事件 diff --git a/GrowingTrackerCore/Public/GrowingModuleManager.h b/GrowingTrackerCore/Public/GrowingModuleManager.h index e7fc65c19..0844ff238 100644 --- a/GrowingTrackerCore/Public/GrowingModuleManager.h +++ b/GrowingTrackerCore/Public/GrowingModuleManager.h @@ -19,7 +19,11 @@ #import -typedef NS_ENUM(NSInteger, GrowingModuleEventType) { GrowingMInitEvent, GrowingMSetDataCollectionEnabledEvent }; +typedef NS_ENUM(NSInteger, GrowingModuleEventType) { + GrowingMInitEvent, + GrowingMSetDataCollectionEnabledEvent, + GrowingMRefreshSessionEvent, +}; NS_ASSUME_NONNULL_BEGIN diff --git a/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m b/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m index 158e8235b..f10ce141e 100644 --- a/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m +++ b/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m @@ -126,9 +126,15 @@ - (NSArray *)growingEventManagerEventsWillSend:(NSArray> *)events - request:(id)request - channel:(GrowingEventChannel *)channel { +- (void)growingEventManagerEventsSendingCompletion:(NSArray> *)events + request:(id)request + channel:(GrowingEventChannel *)channel + httpResponse:(NSHTTPURLResponse *)httpResponse + error:(NSError *)error { + if (httpResponse.statusCode < 200 || httpResponse.statusCode >= 400) { + return; + } + for (id event in events) { if ([event.eventType isEqualToString:GrowingEventTypeActivate]) { id jsonObject = event.toJSONObject; diff --git a/Modules/MobileDebugger/GrowingDebuggerEventQueue.m b/Modules/MobileDebugger/GrowingDebuggerEventQueue.m index e6b134077..22ec3bfea 100644 --- a/Modules/MobileDebugger/GrowingDebuggerEventQueue.m +++ b/Modules/MobileDebugger/GrowingDebuggerEventQueue.m @@ -83,9 +83,15 @@ - (void)enqueue:(id)anObject { GROWING_UNLOCK(lock); } -- (void)growingEventManagerEventsDidSend:(NSArray> *)events - request:(id)request - channel:(GrowingEventChannel *)channel { +- (void)growingEventManagerEventsSendingCompletion:(NSArray> *)events + request:(id)request + channel:(GrowingEventChannel *)channel + httpResponse:(NSHTTPURLResponse *)httpResponse + error:(NSError *)error { + if (httpResponse.statusCode < 200 || httpResponse.statusCode >= 400) { + return; + } + if (events && request && request.absoluteURL) { NSString *url = request.absoluteURL.absoluteString.copy; for (id event in events) { diff --git a/Modules/Preflight/GrowingNetworkPreflight+Private.h b/Modules/Preflight/GrowingNetworkPreflight+Private.h index 1a81fafa0..eb4ec014d 100644 --- a/Modules/Preflight/GrowingNetworkPreflight+Private.h +++ b/Modules/Preflight/GrowingNetworkPreflight+Private.h @@ -23,12 +23,8 @@ NS_ASSUME_NONNULL_BEGIN @interface GrowingNetworkPreflight (Private) -+ (BOOL)isSucceed; - + (NSString *)dataCollectionServerHost; -+ (void)sendPreflight; - @end NS_ASSUME_NONNULL_END diff --git a/Modules/Preflight/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m index 563262f89..0dc6f7791 100644 --- a/Modules/Preflight/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -22,6 +22,7 @@ #import "GrowingTrackerCore/Public/GrowingEventNetworkService.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" +#import "GrowingTrackerCore/Event/GrowingEventManager.h" #import "Modules/Preflight/GrowingNetworkPreflight+Private.h" #import "Modules/Preflight/Request/GrowingPFEventRequestAdapter.h" #import "Modules/Preflight/Request/GrowingPFRequest.h" @@ -38,7 +39,7 @@ typedef NS_ENUM(NSUInteger, GrowingNetworkPreflightStatus) { static NSTimeInterval const kGrowingPreflightMaxTime = 300; -@interface GrowingNetworkPreflight () +@interface GrowingNetworkPreflight () @property (nonatomic, assign) GrowingNetworkPreflightStatus status; @property (nonatomic, assign) NSTimeInterval nextPreflightTime; @@ -66,6 +67,8 @@ + (instancetype)sharedInstance { } - (void)growingModInit:(GrowingContext *)context { + [[GrowingEventManager sharedInstance] addInterceptor:self]; + [GrowingEventRequestAdapters.sharedInstance addAdapter:GrowingPFEventRequestAdapter.class]; GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; @@ -83,21 +86,45 @@ - (void)growingModInit:(GrowingContext *)context { NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); self.minPreflightTime = dataUploadInterval; + + [GrowingNetworkPreflight sendPreflight]; } -#pragma mark - Public Methods +- (void)growingModRefreshSession:(GrowingContext *)context { + [GrowingNetworkPreflight sendPreflight]; +} -+ (BOOL)isSucceed { - GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; - return preflight.status > GrowingNWPreflightStatusWaitingForResponse; +#pragma mark - GrowingEventInterceptor + +- (BOOL)growingEventManagerEventShouldBeginSending:(GrowingEventChannel *)channel { + return [GrowingNetworkPreflight isSucceed]; } +- (void)growingEventManagerEventsSendingCompletion:(NSArray> *)events + request:(id)request + channel:(GrowingEventChannel *)channel + httpResponse:(NSHTTPURLResponse *)httpResponse + error:(NSError *)error { + if (httpResponse.statusCode == 403) { + [GrowingNetworkPreflight sendPreflight]; + } +} + +#pragma mark - Public Methods + + (NSString *)dataCollectionServerHost { // call when preflight isSucceed GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; return preflight.dataCollectionServerHost; } +#pragma mark - Private Methods + ++ (BOOL)isSucceed { + GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; + return preflight.status > GrowingNWPreflightStatusWaitingForResponse; +} + + (void)sendPreflight { [GrowingDispatchManager dispatchInGrowingThread:^{ GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; @@ -111,8 +138,6 @@ + (void)sendPreflight { }]; } -#pragma mark - Private Methods - - (void)sendPreflight { self.status = GrowingNWPreflightStatusWaitingForResponse; From 7b67729366f6b8787b6bfb535b90da3031e6758e Mon Sep 17 00:00:00 2001 From: GIOSDK Date: Tue, 30 Apr 2024 08:11:50 +0000 Subject: [PATCH 14/18] style: code format --- GrowingTrackerCore/Event/GrowingEventManager.m | 11 ++++++----- GrowingTrackerCore/Manager/GrowingSession.m | 8 +++----- .../Advertising/AppleSearchAds/GrowingAsaFetcher.m | 4 ++-- Modules/MobileDebugger/GrowingDebuggerEventQueue.m | 2 +- Modules/Preflight/GrowingNetworkPreflight.m | 6 +++--- 5 files changed, 15 insertions(+), 16 deletions(-) diff --git a/GrowingTrackerCore/Event/GrowingEventManager.m b/GrowingTrackerCore/Event/GrowingEventManager.m index 84376010b..2ecb8197e 100644 --- a/GrowingTrackerCore/Event/GrowingEventManager.m +++ b/GrowingTrackerCore/Event/GrowingEventManager.m @@ -234,11 +234,11 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { shouldBeginSending = [obj growingEventManagerEventShouldBeginSending:channel]; } } - + if (!shouldBeginSending) { return; } - + if (channel.isUploading) { return; } @@ -351,7 +351,8 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { completion:^(NSHTTPURLResponse *_Nonnull httpResponse, NSData *_Nonnull data, NSError *_Nonnull error) { [GrowingDispatchManager dispatchInGrowingThread:^{ for (NSObject *obj in self.allInterceptor) { - if ([obj respondsToSelector:@selector(growingEventManagerEventsSendingCompletion:request:channel:httpResponse:error:)]) { + if ([obj respondsToSelector:@selector + (growingEventManagerEventsSendingCompletion:request:channel:httpResponse:error:)]) { [obj growingEventManagerEventsSendingCompletion:events request:eventRequest channel:channel @@ -359,9 +360,9 @@ - (void)sendEventsOfChannel_unsafe:(GrowingEventChannel *)channel { error:error]; } } - + channel.isUploading = NO; - + if (error) { return; } diff --git a/GrowingTrackerCore/Manager/GrowingSession.m b/GrowingTrackerCore/Manager/GrowingSession.m index 56761446c..d2ee9ac28 100644 --- a/GrowingTrackerCore/Manager/GrowingSession.m +++ b/GrowingTrackerCore/Manager/GrowingSession.m @@ -23,6 +23,7 @@ #import "GrowingTrackerCore/Event/Tools/GrowingPersistenceDataProvider.h" #import "GrowingTrackerCore/Helpers/GrowingHelpers.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" +#import "GrowingTrackerCore/Public/GrowingModuleManager.h" #import "GrowingTrackerCore/Public/GrowingTrackConfiguration.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogMacros.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" @@ -31,7 +32,6 @@ #import "GrowingTrackerCore/Utils/GrowingInternalMacros.h" #import "GrowingULAppLifecycle.h" #import "GrowingULTimeUtil.h" -#import "GrowingTrackerCore/Public/GrowingModuleManager.h" @interface GrowingSession () @@ -97,11 +97,9 @@ - (void)generateVisit { - (void)refreshSessionId { self.sessionId = NSUUID.UUID.UUIDString; self.sentVisitAfterRefreshSessionId = NO; - + [[GrowingModuleManager sharedInstance] triggerEvent:GrowingMRefreshSessionEvent - withCustomParam:@{ - @"sessionId": self.sessionId.copy - }]; + withCustomParam:@{@"sessionId": self.sessionId.copy}]; } // iOS 11系统上面VC的viewDidAppear生命周期会早于AppDelegate的applicationDidBecomeActive,这样会造成Page事件早于Visit事件 diff --git a/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m b/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m index f10ce141e..b7694cb2a 100644 --- a/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m +++ b/Modules/Advertising/AppleSearchAds/GrowingAsaFetcher.m @@ -126,7 +126,7 @@ - (NSArray *)growingEventManagerEventsWillSend:(NSArray> *)events +- (void)growingEventManagerEventsSendingCompletion:(NSArray> *)events request:(id)request channel:(GrowingEventChannel *)channel httpResponse:(NSHTTPURLResponse *)httpResponse @@ -134,7 +134,7 @@ - (void)growingEventManagerEventsSendingCompletion:(NSArray= 400) { return; } - + for (id event in events) { if ([event.eventType isEqualToString:GrowingEventTypeActivate]) { id jsonObject = event.toJSONObject; diff --git a/Modules/MobileDebugger/GrowingDebuggerEventQueue.m b/Modules/MobileDebugger/GrowingDebuggerEventQueue.m index 22ec3bfea..32e268d8c 100644 --- a/Modules/MobileDebugger/GrowingDebuggerEventQueue.m +++ b/Modules/MobileDebugger/GrowingDebuggerEventQueue.m @@ -91,7 +91,7 @@ - (void)growingEventManagerEventsSendingCompletion:(NSArray= 400) { return; } - + if (events && request && request.absoluteURL) { NSString *url = request.absoluteURL.absoluteString.copy; for (id event in events) { diff --git a/Modules/Preflight/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m index 0dc6f7791..7f85d16ca 100644 --- a/Modules/Preflight/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -17,12 +17,12 @@ // See the License for the specific language governing permissions and // limitations under the License. +#import "GrowingTrackerCore/Event/GrowingEventManager.h" #import "GrowingTrackerCore/Manager/GrowingConfigurationManager.h" #import "GrowingTrackerCore/Network/Request/Adapter/GrowingEventRequestAdapters.h" #import "GrowingTrackerCore/Public/GrowingEventNetworkService.h" #import "GrowingTrackerCore/Thirdparty/Logger/GrowingLogger.h" #import "GrowingTrackerCore/Thread/GrowingDispatchManager.h" -#import "GrowingTrackerCore/Event/GrowingEventManager.h" #import "Modules/Preflight/GrowingNetworkPreflight+Private.h" #import "Modules/Preflight/Request/GrowingPFEventRequestAdapter.h" #import "Modules/Preflight/Request/GrowingPFRequest.h" @@ -68,7 +68,7 @@ + (instancetype)sharedInstance { - (void)growingModInit:(GrowingContext *)context { [[GrowingEventManager sharedInstance] addInterceptor:self]; - + [GrowingEventRequestAdapters.sharedInstance addAdapter:GrowingPFEventRequestAdapter.class]; GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; @@ -86,7 +86,7 @@ - (void)growingModInit:(GrowingContext *)context { NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); self.minPreflightTime = dataUploadInterval; - + [GrowingNetworkPreflight sendPreflight]; } From 61eefc361e2637f03111f87241944abec86a143e Mon Sep 17 00:00:00 2001 From: YoloMao Date: Tue, 30 Apr 2024 16:25:38 +0800 Subject: [PATCH 15/18] chore: update Package.swift --- Package.swift | 12 ++++++++++++ Package@swift-5.9.swift | 12 ++++++++++++ 2 files changed, 24 insertions(+) diff --git a/Package.swift b/Package.swift index 24d76cd14..ab9063ba9 100644 --- a/Package.swift +++ b/Package.swift @@ -85,6 +85,7 @@ let package = Package( .Module.ads, .Module.apm, .Module.abTesting, + .Module.preflight, // MARK: - Services @@ -121,6 +122,7 @@ extension Target { .Module.hybrid, .Module.mobileDebugger, .Module.webCircle, + .Module.preflight, ], path: .Path.autotracker) @@ -129,6 +131,7 @@ extension Target { .tracker_objc, .Module.coreServices, .Module.mobileDebugger, + .Module.preflight, ], path: .Path.tracker) @@ -244,6 +247,12 @@ extension Target { path: .Path.abTesting, publicHeadersPath: .Path.publicHeaders, cSettings: [.hspFor(.Path.abTesting)]) + + static let preflight = target(name: .preflight, + dependencies: [.Core.trackerCore], + path: .Path.preflight, + publicHeadersPath: .Path.publicHeaders, + cSettings: [.hspFor(.Path.preflight)]) } enum Service { @@ -324,6 +333,7 @@ extension Target.Dependency { static let mobileDebugger = byName(name: .mobileDebugger, condition: .when(platforms: [.iOS])) static let webCircle = byName(name: .webCircle, condition: .when(platforms: [.iOS])) static let hybrid = byName(name: .hybrid, condition: .when(platforms: [.iOS, .macCatalyst])) + static let preflight = byName(name: .preflight) } enum Service { @@ -375,6 +385,7 @@ extension String { static let ads = "GrowingModule_Ads" static let apm = "GrowingModule_APM" static let abTesting = "GrowingModule_ABTesting" + static let preflight = "GrowingModule_Preflight" // Services static let database = "GrowingService_Database" @@ -410,6 +421,7 @@ extension String { static let ads = "Modules/Advertising" static let apm = "Modules/APM" static let abTesting = "Modules/ABTesting" + static let preflight = "Modules/Preflight" static let coreServices = "Modules/DefaultServices" // Services diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index d2b593c06..232d1a88b 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -86,6 +86,7 @@ let package = Package( .Module.ads, .Module.apm, .Module.abTesting, + .Module.preflight, // MARK: - Services @@ -122,6 +123,7 @@ extension Target { .Module.hybrid, .Module.mobileDebugger, .Module.webCircle, + .Module.preflight, ], path: .Path.autotracker) @@ -130,6 +132,7 @@ extension Target { .tracker_objc, .Module.coreServices, .Module.mobileDebugger, + .Module.preflight, ], path: .Path.tracker) @@ -245,6 +248,12 @@ extension Target { path: .Path.abTesting, publicHeadersPath: .Path.publicHeaders, cSettings: [.hspFor(.Path.abTesting)]) + + static let preflight = target(name: .preflight, + dependencies: [.Core.trackerCore], + path: .Path.preflight, + publicHeadersPath: .Path.publicHeaders, + cSettings: [.hspFor(.Path.preflight)]) } enum Service { @@ -325,6 +334,7 @@ extension Target.Dependency { static let mobileDebugger = byName(name: .mobileDebugger, condition: .when(platforms: [.iOS])) static let webCircle = byName(name: .webCircle, condition: .when(platforms: [.iOS])) static let hybrid = byName(name: .hybrid, condition: .when(platforms: [.iOS, .macCatalyst])) + static let preflight = byName(name: .preflight) } enum Service { @@ -376,6 +386,7 @@ extension String { static let ads = "GrowingModule_Ads" static let apm = "GrowingModule_APM" static let abTesting = "GrowingModule_ABTesting" + static let preflight = "GrowingModule_Preflight" // Services static let database = "GrowingService_Database" @@ -411,6 +422,7 @@ extension String { static let ads = "Modules/Advertising" static let apm = "Modules/APM" static let abTesting = "Modules/ABTesting" + static let preflight = "Modules/Preflight" static let coreServices = "Modules/DefaultServices" // Services From 6484ae8f05bb6996ba7238af8da32528ba2333ba Mon Sep 17 00:00:00 2001 From: GIOSDK Date: Tue, 30 Apr 2024 08:28:52 +0000 Subject: [PATCH 16/18] style: code format --- Package.swift | 2 +- Package@swift-5.9.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index ab9063ba9..b21aef8f4 100644 --- a/Package.swift +++ b/Package.swift @@ -247,7 +247,7 @@ extension Target { path: .Path.abTesting, publicHeadersPath: .Path.publicHeaders, cSettings: [.hspFor(.Path.abTesting)]) - + static let preflight = target(name: .preflight, dependencies: [.Core.trackerCore], path: .Path.preflight, diff --git a/Package@swift-5.9.swift b/Package@swift-5.9.swift index 232d1a88b..828abc840 100644 --- a/Package@swift-5.9.swift +++ b/Package@swift-5.9.swift @@ -248,7 +248,7 @@ extension Target { path: .Path.abTesting, publicHeadersPath: .Path.publicHeaders, cSettings: [.hspFor(.Path.abTesting)]) - + static let preflight = target(name: .preflight, dependencies: [.Core.trackerCore], path: .Path.preflight, From 77382f66f5876ed06fe0b197b756649e5865c461 Mon Sep 17 00:00:00 2001 From: YoloMao Date: Mon, 27 May 2024 15:04:25 +0800 Subject: [PATCH 17/18] fix: options request with disable/enable dataCollect --- Modules/Preflight/GrowingNetworkPreflight.m | 37 ++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/Modules/Preflight/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m index 7f85d16ca..ebcb482c0 100644 --- a/Modules/Preflight/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -68,9 +68,9 @@ + (instancetype)sharedInstance { - (void)growingModInit:(GrowingContext *)context { [[GrowingEventManager sharedInstance] addInterceptor:self]; - + [GrowingEventRequestAdapters.sharedInstance addAdapter:GrowingPFEventRequestAdapter.class]; - + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; NSString *dataCollectionServerHost = trackConfiguration.dataCollectionServerHost; self.dataCollectionServerHost = dataCollectionServerHost; @@ -82,11 +82,12 @@ - (void)growingModInit:(GrowingContext *)context { self.status = GrowingNWPreflightStatusClosed; } } - + NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); self.minPreflightTime = dataUploadInterval; - + self.nextPreflightTime = dataUploadInterval; + [GrowingNetworkPreflight sendPreflight]; } @@ -94,6 +95,10 @@ - (void)growingModRefreshSession:(GrowingContext *)context { [GrowingNetworkPreflight sendPreflight]; } +- (void)growingModSetDataCollectionEnabled:(GrowingContext *)context { + [GrowingNetworkPreflight sendPreflightIfNeeded]; +} + #pragma mark - GrowingEventInterceptor - (BOOL)growingEventManagerEventShouldBeginSending:(GrowingEventChannel *)channel { @@ -131,13 +136,23 @@ + (void)sendPreflight { GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; preflight.nextPreflightTime = preflight.minPreflightTime; preflight.dataCollectionServerHost = trackConfiguration.dataCollectionServerHost; - if (preflight.status != GrowingNWPreflightStatusWaitingForResponse || - preflight.status != GrowingNWPreflightStatusClosed) { + if (preflight.status == GrowingNWPreflightStatusNotDetermined || + preflight.status == GrowingNWPreflightStatusAuthorized || + preflight.status == GrowingNWPreflightStatusDenied) { + if (!trackConfiguration.dataCollectionEnabled) { + preflight.status = GrowingNWPreflightStatusNotDetermined; + return; + } [preflight sendPreflight]; } }]; } ++ (void)sendPreflightIfNeeded { + GrowingNetworkPreflight *preflight = [GrowingNetworkPreflight sharedInstance]; + [preflight sendPreflightIfNeeded]; +} + - (void)sendPreflight { self.status = GrowingNWPreflightStatusWaitingForResponse; @@ -160,12 +175,12 @@ - (void)sendPreflight { GrowingConfigurationManager.sharedInstance.trackConfiguration; self.dataCollectionServerHost = trackConfiguration.alternateDataCollectionServerHost; } else { - self.status = GrowingNWPreflightStatusWaitingForResponse; + self.status = GrowingNWPreflightStatusNotDetermined; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.nextPreflightTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [GrowingDispatchManager dispatchInGrowingThread:^{ - [self sendPreflight]; + [self sendPreflightIfNeeded]; }]; }); @@ -175,4 +190,10 @@ - (void)sendPreflight { }]; } +- (void)sendPreflightIfNeeded { + if (self.status == GrowingNWPreflightStatusNotDetermined) { + [self sendPreflight]; + } +} + @end From eaa26ecdf06978b2f15a4eee099c7b38b6957c70 Mon Sep 17 00:00:00 2001 From: GIOSDK Date: Mon, 27 May 2024 07:04:48 +0000 Subject: [PATCH 18/18] style: code format --- Modules/Preflight/GrowingNetworkPreflight.m | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Modules/Preflight/GrowingNetworkPreflight.m b/Modules/Preflight/GrowingNetworkPreflight.m index ebcb482c0..f6d682563 100644 --- a/Modules/Preflight/GrowingNetworkPreflight.m +++ b/Modules/Preflight/GrowingNetworkPreflight.m @@ -68,9 +68,9 @@ + (instancetype)sharedInstance { - (void)growingModInit:(GrowingContext *)context { [[GrowingEventManager sharedInstance] addInterceptor:self]; - + [GrowingEventRequestAdapters.sharedInstance addAdapter:GrowingPFEventRequestAdapter.class]; - + GrowingTrackConfiguration *trackConfiguration = GrowingConfigurationManager.sharedInstance.trackConfiguration; NSString *dataCollectionServerHost = trackConfiguration.dataCollectionServerHost; self.dataCollectionServerHost = dataCollectionServerHost; @@ -82,12 +82,12 @@ - (void)growingModInit:(GrowingContext *)context { self.status = GrowingNWPreflightStatusClosed; } } - + NSTimeInterval dataUploadInterval = trackConfiguration.dataUploadInterval; dataUploadInterval = MAX(dataUploadInterval, 5); self.minPreflightTime = dataUploadInterval; self.nextPreflightTime = dataUploadInterval; - + [GrowingNetworkPreflight sendPreflight]; }