Repository navigation
Expand file tree
/
Copy pathModuleConfiguration.java
More file actions
1379 lines (1226 loc) · 59.4 KB
/
Copy pathModuleConfiguration.java
File metadata and controls
1379 lines (1226 loc) · 59.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ly.count.sdk.java.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import ly.count.sdk.java.Config;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Resolves, stores and serves the SDK behavior settings, the server side configuration served by
* {@code /o/sdk?method=sc}.
* <p>
* Every setting starts from the developer configuration and is overridden by the settings provided
* at init (only while nothing is stored), by the stored settings and by every later server response.
* Each accepted response replaces the stored settings as a whole, so a setting the server stops
* sending goes back to the developer configuration, or to the SDK default when it sets none.
* {@link SDKCore} runs {@link #init(InternalConfig)}, which does the resolve, before it starts the
* global timer and builds the other modules; {@link #initFinished(InternalConfig)} fetches the
* settings and starts the refresh timer. In backend mode the module stays inert and serves the
* developer configuration.
* <p>
* Resolved values are published through volatile fields and immutable snapshots, so a reader on any
* thread sees consistent values while a response is applied on the thread that delivered it.
*/
public class ModuleConfiguration extends ModuleBase implements ConfigurationProvider {
static final String keyRTimestamp = "t";
static final String keyRVersion = "v";
static final String keyRConfig = "c";
static final String keyRLogGathering = "lg";
static final String keyRConnectionTest = "ct";
//top level key, a sibling of 'c', read from a live response and never stored
static final String keyRGzip = "gzip";
static final String keyRTracking = "tracking";
static final String keyRNetworking = "networking";
static final String keyRReqQueueSize = "rqs";
static final String keyREventQueueSize = "eqs";
static final String keyRLogging = "log";
static final String keyRSessionUpdateInterval = "sui";
static final String keyRSessionTracking = "st";
static final String keyRViewTracking = "vt";
static final String keyRLocationTracking = "lt";
static final String keyRRefreshContentZone = "rcz";
static final String keyRLimitKeyLength = "lkl";
static final String keyRLimitValueSize = "lvs";
static final String keyRLimitSegValues = "lsv";
static final String keyRLimitBreadcrumb = "lbc";
static final String keyRLimitTraceLine = "ltlpt";
static final String keyRLimitTraceLength = "ltl";
static final String keyRCustomEventTracking = "cet";
static final String keyREnterContentZone = "ecz";
static final String keyRContentZoneInterval = "czi";
static final String keyRConsentRequired = "cr";
static final String keyRDropOldRequestTime = "dort";
static final String keyRCrashReporting = "crt";
static final String keyRAutomaticSessionTracking = "ast";
static final String keyRAutomaticViewTracking = "avt";
static final String keyRAutomaticCrashReporting = "acr";
static final String keyRServerConfigUpdateInterval = "scui";
static final String keyRBackoffMechanism = "bom";
static final String keyRBOMAcceptedTimeout = "bom_at";
static final String keyRBOMRQPercentage = "bom_rqp";
static final String keyRBOMRequestAge = "bom_ra";
static final String keyRBOMDuration = "bom_d";
static final String keyRUserPropertyCacheLimit = "upcl";
static final String keyREventBlacklist = "eb";
static final String keyRUserPropertyBlacklist = "upb";
static final String keyRSegmentationBlacklist = "sb";
static final String keyREventSegmentationBlacklist = "esb";
static final String keyREventWhitelist = "ew";
static final String keyRUserPropertyWhitelist = "upw";
static final String keyRSegmentationWhitelist = "sw";
static final String keyREventSegmentationWhitelist = "esw";
static final String keyRJourneyTriggerEvents = "jte";
static final String keyRJourneyTriggerViews = "jtv";
static final String keyLGEnabled = "e";
static final String keyLGId = "i";
static final String keyLGLevels = "l";
static final String keyLGBatchSize = "b";
static final String logGatheringAllLevels = "ewidv";
static final int logGatheringDefaultBatchSize = 100;
static final int logGatheringMinBatchSize = 10;
//the log buffer ceiling, so also the largest batch the server may ask for: a bigger batch would never fill
static final int logGatheringMaxBufferedLines = 500;
static final int DEFAULT_SERVER_CONFIG_UPDATE_INTERVAL_HOURS = 4;
static final int DEFAULT_BOM_ACCEPTED_TIMEOUT_SECONDS = 10;
static final double DEFAULT_BOM_RQ_PERCENTAGE = 0.5;
static final int DEFAULT_BOM_REQUEST_AGE_HOURS = 24;
static final int DEFAULT_BOM_DURATION_SECONDS = 60;
static final int MIN_CONTENT_ZONE_INTERVAL = 16;
static final int NO_LIMIT = Integer.MAX_VALUE;
//the settings of the last accepted configuration; guarded by this
JSONObject latestRetrievedConfiguration = null;
//what each setting goes back to when the stored settings do not carry it, read from the developer configuration at init
private ConfigurationProvider developerValues = null;
//guarded by this
CountlyTimer serverConfigUpdateTimer = null;
private boolean serverConfigRequestsDisabled = false;
private volatile boolean running = false;
private final AtomicBoolean requestCompressionEnabled = new AtomicBoolean(false);
final AtomicReference<String> gzipAcceptingServerUrl = new AtomicReference<>(null);
final AtomicBoolean requestCompressionBlocked = new AtomicBoolean(false);
/**
* How many times the device ID changed without merge, written under this module's lock, so a
* response requested before such a change can tell its gather was armed for the previous user.
*/
private volatile int deviceIdChangesWithoutMerge = 0;
volatile ConnectionTestListener connectionTestListener = null;
//resolved values; the constant seeds are here, the ones read from the developer configuration are in seedFromDeveloperConfig
volatile boolean currentVTracking = true;
volatile boolean currentVNetworking = true;
volatile boolean currentVSessionTracking = true;
volatile boolean currentVViewTracking = true;
volatile boolean currentVCustomEventTracking = true;
volatile boolean currentVContentZone = false;
volatile boolean currentVCrashReporting = true;
volatile boolean currentVAutomaticSessionTracking = false;
volatile boolean currentVAutomaticViewTracking = false;
volatile boolean currentVAutomaticCrashReporting = true;
volatile boolean currentVLocationTracking = true;
volatile boolean currentVRefreshContentZone = true;
volatile boolean currentVBackoffMechanism = true;
volatile boolean currentVLoggingEnabled = false;
volatile boolean currentVRequiresConsent = false;
volatile int currentVServerConfigUpdateInterval = DEFAULT_SERVER_CONFIG_UPDATE_INTERVAL_HOURS;
volatile int currentVRequestQueueMaxSize = 1000;
volatile boolean currentVRequestQueueMaxSizeFromBehaviorSettings = false;
volatile int currentVEventQueueSizeThreshold = 10;
volatile int currentVSessionUpdateInterval = 60;
volatile int currentVMaxKeyLength = NO_LIMIT;
volatile int currentVMaxValueSize = NO_LIMIT;
volatile int currentVMaxSegmentationValues = NO_LIMIT;
volatile int currentVMaxBreadcrumbCount = 100;
volatile int currentVMaxStackTraceLinesPerThread = NO_LIMIT;
volatile int currentVMaxStackTraceLineLength = NO_LIMIT;
volatile int currentVUserPropertyCacheLimit = NO_LIMIT;
volatile int currentVBOMAcceptedTimeoutSeconds = DEFAULT_BOM_ACCEPTED_TIMEOUT_SECONDS;
volatile double currentVBOMRQPercentage = DEFAULT_BOM_RQ_PERCENTAGE;
volatile int currentVBOMRequestAge = DEFAULT_BOM_REQUEST_AGE_HOURS;
volatile int currentVBOMDuration = DEFAULT_BOM_DURATION_SECONDS;
volatile int currentVDropAgeHours = 0;
volatile int currentVZoneTimerInterval = ConfigContent.DEFAULT_ZONE_TIMER_INTERVAL;
volatile FilterList<Set<String>> currentVEventFilterList = FilterList.NO_NAMES;
volatile FilterList<Set<String>> currentVUserPropertyFilterList = FilterList.NO_NAMES;
volatile FilterList<Set<String>> currentVSegmentationFilterList = FilterList.NO_NAMES;
volatile FilterList<Map<String, Set<String>>> currentVEventSegmentationFilterList = FilterList.NO_NAMES_PER_EVENT;
volatile Set<String> currentVJourneyTriggerEvents = Collections.emptySet();
volatile Set<String> currentVJourneyTriggerViews = Collections.emptySet();
volatile LogGatheringDirective currentVLogGathering = LogGatheringDirective.UNDECIDED;
/**
* Runs the connection test that a live settings response asks for with its {@code ct} flag.
*/
interface ConnectionTestListener {
/**
* Called on the thread that delivered the response, after the settings in it were applied.
*
* @param fetchLatencyMs how long the settings request took, from sending it to its response, in milliseconds
*/
void onConnectionTestRequested(long fetchLatencyMs);
}
/**
* One decision of the log gathering directive, published as a whole so a reader never sees the
* state of one directive next to the gather id of another.
*/
static final class LogGatheringDirective {
static final LogGatheringDirective UNDECIDED = new LogGatheringDirective(LogGatheringState.UNDECIDED, null, logGatheringAllLevels, logGatheringDefaultBatchSize);
static final LogGatheringDirective NOT_GATHERING = new LogGatheringDirective(LogGatheringState.NOT_GATHERING, null, logGatheringAllLevels, logGatheringDefaultBatchSize);
final LogGatheringState state;
final String gatherId;
final String levels;
final int batchSize;
/**
* Holds one decision.
*
* @param state what was decided
* @param gatherId the gather id, {@code null} unless gathering
* @param levels the level characters to gather
* @param batchSize how many lines make a batch
*/
LogGatheringDirective(@Nonnull LogGatheringState state, @Nullable String gatherId, @Nonnull String levels, int batchSize) {
this.state = state;
this.gatherId = gatherId;
this.levels = levels;
this.batchSize = batchSize;
}
}
/**
* Builds a provider that serves the developer configuration alone, for code that reads the
* settings while no configuration module is registered on that configuration.
*
* @param config the developer configuration to serve
* @return a provider that never changes
*/
@Nonnull
static ConfigurationProvider developerDefaults(@Nonnull InternalConfig config) {
ModuleConfiguration defaults = new ModuleConfiguration();
defaults.seedFromDeveloperConfig(config);
return defaults;
}
/**
* Seeds every setting from the developer configuration and resolves the provided and the stored
* settings over it, then registers this module as the provider of the configuration. In backend
* mode nothing is loaded and the developer configuration stays in effect.
*
* @param config configuration of the SDK being initialized
*/
@Override
public void init(InternalConfig config) {
super.init(config);
boolean backendMode = config.isBackendModeEnabled();
serverConfigRequestsDisabled = backendMode || config.isSdkBehaviorSettingsRequestsDisabled();
requestCompressionEnabled.set(config.isRequestCompressionEnabled());
L.d("[ModuleConfiguration] init, backend mode:[" + backendMode + "], server config requests disabled:[" + serverConfigRequestsDisabled + "]");
seedFromDeveloperConfig(config);
developerValues = developerDefaults(config);
if (backendMode) {
L.d("[ModuleConfiguration] init, backend mode is enabled, SDK behavior settings are not applied");
} else {
loadConfigFromStorage(config.getSdkBehaviorSettings());
}
updateConfigVariables(config, null, true);
running = true;
config.configProvider = this;
}
/**
* Fetches the settings from the server and starts the refresh timer, unless settings requests
* are disabled or backend mode is on.
*
* @param config configuration of the running SDK
*/
@Override
public void initFinished(@Nonnull InternalConfig config) {
L.d("[ModuleConfiguration] initFinished");
if (serverConfigRequestsDisabled) {
return;
}
fetchConfigFromServer(config);
startServerConfigUpdateTimer();
}
/**
* Stops the refresh timer; a response that arrives afterwards is ignored.
*
* @param config configuration of the SDK being stopped
* @param clear whether the SDK clears its data, which removes the stored settings with it
*/
@Override
public void stop(InternalConfig config, boolean clear) {
super.stop(config, clear);
running = false;
stopServerConfigUpdateTimer();
}
/**
* Sets the resolved values that come from the developer configuration.
*
* @param config the developer configuration
*/
private void seedFromDeveloperConfig(@Nonnull InternalConfig config) {
currentVLoggingEnabled = config.getLoggingLevel() != null && config.getLoggingLevel() != Config.LoggingLevel.OFF;
currentVRequiresConsent = config.requiresConsent();
currentVAutomaticCrashReporting = config.isUnhandledCrashReportingEnabled();
currentVBackoffMechanism = config.isBackoffMechanismEnabled();
currentVRequestQueueMaxSize = config.getRequestQueueMaxSize();
currentVEventQueueSizeThreshold = config.getEventsBufferSize();
currentVSessionUpdateInterval = config.getSendUpdateEachSeconds();
currentVMaxBreadcrumbCount = config.getMaxBreadcrumbCount();
currentVMaxKeyLength = config.getMaxKeyLength();
currentVZoneTimerInterval = config.content.zoneTimerInterval;
}
/**
* Loads the stored settings, or the provided ones when nothing is stored yet, which stores them.
*
* @param sdkBehaviorSettings the settings the developer provided, {@code null} for none
*/
void loadConfigFromStorage(@Nullable String sdkBehaviorSettings) {
String sConfig = internalConfig.storageProvider.getServerConfig();
if (Utils.isEmptyOrNull(sConfig) && !Utils.isEmptyOrNull(sdkBehaviorSettings)) {
L.d("[ModuleConfiguration] loadConfigFromStorage, nothing is stored, using the provided settings");
sConfig = sdkBehaviorSettings;
}
L.v("[ModuleConfiguration] loadConfigFromStorage, [" + sConfig + "]");
if (Utils.isEmptyOrNull(sConfig)) {
L.d("[ModuleConfiguration] loadConfigFromStorage, no configuration is stored or provided");
return;
}
try {
saveAndStoreDownloadedConfig(new JSONObject(sConfig));
} catch (JSONException e) {
L.w("[ModuleConfiguration] loadConfigFromStorage, failed to parse, " + e);
}
}
/**
* Requires {@code v}, {@code t} and an object {@code c}, then removes the settings this SDK does
* not support. Other top level keys are allowed; they are never stored.
*
* @param config a configuration as the server sends it, its settings object is changed in place
* @return whether the configuration can be stored
*/
boolean validateServerConfig(@Nonnull JSONObject config) {
L.v("[ModuleConfiguration] validateServerConfig");
if (!config.has(keyRVersion)) {
L.w("[ModuleConfiguration] validateServerConfig, retrieved configuration has no 'v' field, it is ignored");
return false;
}
if (!config.has(keyRTimestamp)) {
L.w("[ModuleConfiguration] validateServerConfig, retrieved configuration has no 't' field, it is ignored");
return false;
}
if (!config.has(keyRConfig)) {
L.w("[ModuleConfiguration] validateServerConfig, retrieved configuration has no 'c' field, it is ignored");
return false;
}
JSONObject newInner = config.optJSONObject(keyRConfig);
if (newInner == null) {
L.w("[ModuleConfiguration] validateServerConfig, retrieved configuration has a 'c' that is not an object, it is ignored");
return false;
}
removeUnsupportedKeys(newInner);
return true;
}
/**
* Removes every setting whose key is unknown or whose value has the wrong type or range.
*
* @param newInner the settings object, changed in place
*/
private void removeUnsupportedKeys(@Nonnull JSONObject newInner) {
Iterator<String> keys = newInner.keys();
while (keys.hasNext()) {
String key = keys.next();
Object value = newInner.opt(key);
boolean isValid;
switch (key) {
case keyRNetworking:
case keyRTracking:
case keyRSessionTracking:
case keyRCrashReporting:
case keyRAutomaticSessionTracking:
case keyRAutomaticViewTracking:
case keyRAutomaticCrashReporting:
case keyRViewTracking:
case keyRCustomEventTracking:
case keyRLocationTracking:
case keyREnterContentZone:
case keyRRefreshContentZone:
case keyRBackoffMechanism:
case keyRLogging:
case keyRConsentRequired:
isValid = value instanceof Boolean;
break;
case keyRServerConfigUpdateInterval:
case keyRBOMAcceptedTimeout:
case keyRBOMRequestAge:
case keyRBOMDuration:
case keyRReqQueueSize:
case keyREventQueueSize:
case keyRSessionUpdateInterval:
case keyRLimitKeyLength:
case keyRLimitValueSize:
case keyRLimitSegValues:
case keyRLimitBreadcrumb:
case keyRLimitTraceLine:
case keyRLimitTraceLength:
case keyRUserPropertyCacheLimit:
isValid = isIntAtLeast(value, 1);
break;
case keyRDropOldRequestTime:
isValid = isIntAtLeast(value, 0);
break;
case keyRContentZoneInterval:
isValid = isIntAtLeast(value, MIN_CONTENT_ZONE_INTERVAL);
break;
case keyRBOMRQPercentage:
isValid = isFraction(value);
break;
case keyREventBlacklist:
case keyRSegmentationBlacklist:
case keyRUserPropertyBlacklist:
case keyREventWhitelist:
case keyRSegmentationWhitelist:
case keyRUserPropertyWhitelist:
case keyRJourneyTriggerEvents:
case keyRJourneyTriggerViews:
isValid = value instanceof JSONArray;
break;
case keyREventSegmentationBlacklist:
case keyREventSegmentationWhitelist:
isValid = value instanceof JSONObject;
break;
default:
L.w("[ModuleConfiguration] removeUnsupportedKeys, unknown key [" + key + "], removing it, value: [" + value + "]");
keys.remove();
continue;
}
if (!isValid) {
L.w("[ModuleConfiguration] removeUnsupportedKeys, invalid value for key [" + key + "], removing it, value: [" + value + "]");
keys.remove();
}
}
}
/**
* Whether a setting value is an integer of at least the given value.
*
* @param value the value as parsed from JSON
* @param minimum the smallest valid value
* @return {@code true} for a valid value
*/
private static boolean isIntAtLeast(@Nullable Object value, int minimum) {
return value instanceof Integer && (Integer) value >= minimum;
}
/**
* Whether a setting value is a number strictly between 0 and 1. Decimals are parsed as
* {@link java.math.BigDecimal}, so any {@link Number} is accepted.
*
* @param value the value as parsed from JSON
* @return {@code true} for a valid value
*/
private static boolean isFraction(@Nullable Object value) {
if (!(value instanceof Number)) {
return false;
}
double fraction = ((Number) value).doubleValue();
return fraction > 0.0 && fraction < 1.0;
}
/**
* Validates a configuration and replaces the stored settings with its settings, so a setting it
* does not carry is no longer stored. Only {@code v}, {@code t} and {@code c} are stored, so the
* log gathering directive and the connection test flag never are. An invalid configuration
* leaves the stored settings as they are.
*
* @param config a configuration as the server sends it
*/
synchronized void saveAndStoreDownloadedConfig(@Nonnull JSONObject config) {
L.v("[ModuleConfiguration] saveAndStoreDownloadedConfig");
if (!validateServerConfig(config)) {
L.w("[ModuleConfiguration] saveAndStoreDownloadedConfig, retrieved configuration is not valid, ignoring it");
return;
}
List<String> notStoredTopLevelKeys = new ArrayList<>();
for (String key : config.keySet()) {
if (!keyRVersion.equals(key) && !keyRTimestamp.equals(key) && !keyRConfig.equals(key)) {
notStoredTopLevelKeys.add(key);
}
}
if (!notStoredTopLevelKeys.isEmpty()) {
L.d("[ModuleConfiguration] saveAndStoreDownloadedConfig, top level keys that are not stored: " + notStoredTopLevelKeys);
}
//validation already removed every value that is not a usable setting, so the settings object is kept as it is
latestRetrievedConfiguration = config.optJSONObject(keyRConfig);
internalConfig.storageProvider.setServerConfig(new JSONObject(config, keyRVersion, keyRTimestamp, keyRConfig).toString());
}
/**
* Applies the stored settings to the resolved values, decides the log gathering directive of a
* live response requested since the last device ID change without merge and has the logger apply
* it, and restarts the refresh timer when its interval changed. A response requested before that
* change leaves the directive as it is, as its gather was armed for the previous user.
*
* @param config configuration of the running SDK
* @param serverResponse the live response being applied, {@code null} at init, where only stored or provided settings exist
* @param forCurrentDeviceId whether the response was requested since the last device ID change without merge
* @return whether a resolved value changed
*/
synchronized boolean updateConfigVariables(@Nonnull InternalConfig config, @Nullable JSONObject serverResponse, boolean forCurrentDeviceId) {
L.v("[ModuleConfiguration] updateConfigVariables, from server response:[" + (serverResponse != null) + "]");
if (forCurrentDeviceId) {
//read off the live response itself, so it applies even when its settings object was rejected
readLogGatheringDirective(serverResponse);
L.applyLogGatheringDirective(serverResponse == null ? "init" : "server response");
} else {
L.d("[ModuleConfiguration] updateConfigVariables, the response was requested for a previous device ID, its log gathering directive is ignored");
}
if (latestRetrievedConfiguration == null) {
return false;
}
StringBuilder sb = new StringBuilder();
boolean previousRequiresConsent = currentVRequiresConsent;
ConfigurationProvider developer = developerValues;
currentVNetworking = extractBoolean(keyRNetworking, sb, currentVNetworking, developer.getNetworkingEnabled());
currentVTracking = extractBoolean(keyRTracking, sb, currentVTracking, developer.getTrackingEnabled());
currentVSessionTracking = extractBoolean(keyRSessionTracking, sb, currentVSessionTracking, developer.getSessionTrackingEnabled());
currentVCrashReporting = extractBoolean(keyRCrashReporting, sb, currentVCrashReporting, developer.getCrashReportingEnabled());
currentVAutomaticSessionTracking = extractBoolean(keyRAutomaticSessionTracking, sb, currentVAutomaticSessionTracking, developer.getAutomaticSessionTrackingEnabled());
currentVAutomaticViewTracking = extractBoolean(keyRAutomaticViewTracking, sb, currentVAutomaticViewTracking, developer.getAutomaticViewTrackingEnabled());
currentVAutomaticCrashReporting = extractBoolean(keyRAutomaticCrashReporting, sb, currentVAutomaticCrashReporting, developer.getAutomaticCrashReportingEnabled());
currentVViewTracking = extractBoolean(keyRViewTracking, sb, currentVViewTracking, developer.getViewTrackingEnabled());
currentVCustomEventTracking = extractBoolean(keyRCustomEventTracking, sb, currentVCustomEventTracking, developer.getCustomEventTrackingEnabled());
currentVLocationTracking = extractBoolean(keyRLocationTracking, sb, currentVLocationTracking, developer.getLocationTrackingEnabled());
currentVContentZone = extractBoolean(keyREnterContentZone, sb, currentVContentZone, developer.getContentZoneEnabled());
currentVRefreshContentZone = extractBoolean(keyRRefreshContentZone, sb, currentVRefreshContentZone, developer.getRefreshContentZoneEnabled());
currentVBackoffMechanism = extractBoolean(keyRBackoffMechanism, sb, currentVBackoffMechanism, developer.getBOMEnabled());
currentVLoggingEnabled = extractBoolean(keyRLogging, sb, currentVLoggingEnabled, developer.getLoggingEnabled());
//the settings can turn the consent requirement on, never off when the developer turned it on
currentVRequiresConsent = developer.getConsentRequired() || extractBoolean(keyRConsentRequired, sb, currentVRequiresConsent, false);
currentVServerConfigUpdateInterval = extractInt(keyRServerConfigUpdateInterval, sb, currentVServerConfigUpdateInterval, developer.getServerConfigUpdateInterval());
currentVRequestQueueMaxSize = extractInt(keyRReqQueueSize, sb, currentVRequestQueueMaxSize, developer.getRequestQueueMaxSize());
currentVEventQueueSizeThreshold = extractInt(keyREventQueueSize, sb, currentVEventQueueSizeThreshold, developer.getEventQueueSizeThreshold());
currentVSessionUpdateInterval = extractInt(keyRSessionUpdateInterval, sb, currentVSessionUpdateInterval, developer.getSessionUpdateInterval());
currentVMaxKeyLength = extractInt(keyRLimitKeyLength, sb, currentVMaxKeyLength, developer.getMaxKeyLength());
currentVMaxValueSize = extractInt(keyRLimitValueSize, sb, currentVMaxValueSize, developer.getMaxValueSize());
currentVMaxSegmentationValues = extractInt(keyRLimitSegValues, sb, currentVMaxSegmentationValues, developer.getMaxSegmentationValues());
currentVMaxBreadcrumbCount = extractInt(keyRLimitBreadcrumb, sb, currentVMaxBreadcrumbCount, developer.getMaxBreadcrumbCount());
currentVMaxStackTraceLinesPerThread = extractInt(keyRLimitTraceLine, sb, currentVMaxStackTraceLinesPerThread, developer.getMaxStackTraceLinesPerThread());
currentVMaxStackTraceLineLength = extractInt(keyRLimitTraceLength, sb, currentVMaxStackTraceLineLength, developer.getMaxStackTraceLineLength());
currentVUserPropertyCacheLimit = extractInt(keyRUserPropertyCacheLimit, sb, currentVUserPropertyCacheLimit, developer.getUserPropertyCacheLimit());
currentVBOMAcceptedTimeoutSeconds = extractInt(keyRBOMAcceptedTimeout, sb, currentVBOMAcceptedTimeoutSeconds, developer.getBOMAcceptedTimeoutSeconds());
currentVBOMRQPercentage = extractFraction(keyRBOMRQPercentage, sb, currentVBOMRQPercentage, developer.getBOMRQPercentage());
currentVBOMRequestAge = extractInt(keyRBOMRequestAge, sb, currentVBOMRequestAge, developer.getBOMRequestAge());
currentVBOMDuration = extractInt(keyRBOMDuration, sb, currentVBOMDuration, developer.getBOMDuration());
currentVDropAgeHours = extractInt(keyRDropOldRequestTime, sb, currentVDropAgeHours, developer.getRequestDropAgeHours());
currentVZoneTimerInterval = extractInt(keyRContentZoneInterval, sb, currentVZoneTimerInterval, developer.getContentZoneTimerInterval());
currentVRequestQueueMaxSizeFromBehaviorSettings = latestRetrievedConfiguration.has(keyRReqQueueSize);
updateListingFilters();
if (serverResponse != null && previousRequiresConsent != currentVRequiresConsent) {
L.d("[ModuleConfiguration] updateConfigVariables, consent requirement changed to [" + currentVRequiresConsent + "], it applies on the next init, the running SDK keeps [" + config.requiresConsent() + "]");
}
if (serverConfigUpdateTimer != null && serverConfigUpdateTimer.getTimerDelaySeconds() != serverConfigUpdateIntervalSeconds()) {
startServerConfigUpdateTimer();
}
String updatedValues = sb.toString();
if (updatedValues.isEmpty()) {
return false;
}
L.i("[ModuleConfiguration] updateConfigVariables, SDK configuration has changed, new values: [" + updatedValues + "]");
return true;
}
/**
* Resolves a switch from the stored settings. Validation keeps only usable values, so a stored
* value is used as it is.
*
* @param key the settings key
* @param changedValues where a change is described
* @param currentValue the value in effect
* @param fallbackValue the developer's value, or the SDK default when the developer sets none
* @return the stored value, or the fallback value when none is stored
*/
private boolean extractBoolean(@Nonnull String key, @Nonnull StringBuilder changedValues, boolean currentValue, boolean fallbackValue) {
Object value = latestRetrievedConfiguration.opt(key);
return noteChange(key, changedValues, currentValue, value instanceof Boolean ? (Boolean) value : fallbackValue);
}
/**
* Resolves an integer from the stored settings. Validation keeps only usable values, so a
* stored value is used as it is.
*
* @param key the settings key
* @param changedValues where a change is described
* @param currentValue the value in effect
* @param fallbackValue the developer's value, or the SDK default when the developer sets none
* @return the stored value, or the fallback value when none is stored
*/
private int extractInt(@Nonnull String key, @Nonnull StringBuilder changedValues, int currentValue, int fallbackValue) {
Object value = latestRetrievedConfiguration.opt(key);
return noteChange(key, changedValues, currentValue, value instanceof Integer ? (Integer) value : fallbackValue);
}
/**
* Resolves a fraction from the stored settings. Validation keeps only usable values, so a
* stored value is used as it is.
*
* @param key the settings key
* @param changedValues where a change is described
* @param currentValue the value in effect
* @param fallbackValue the SDK default
* @return the stored value, or the fallback value when none is stored
*/
private double extractFraction(@Nonnull String key, @Nonnull StringBuilder changedValues, double currentValue, double fallbackValue) {
Object value = latestRetrievedConfiguration.opt(key);
return noteChange(key, changedValues, currentValue, value instanceof Number ? ((Number) value).doubleValue() : fallbackValue);
}
/**
* Describes a resolved value that differs from the value in effect.
*
* @param key the settings key
* @param changedValues where a change is described
* @param currentValue the value in effect
* @param resolvedValue the value just resolved
* @param <T> the type of the value
* @return the resolved value
*/
private static <T> T noteChange(@Nonnull String key, @Nonnull StringBuilder changedValues, @Nonnull T currentValue, @Nonnull T resolvedValue) {
if (!resolvedValue.equals(currentValue)) {
changedValues.append(key).append(":[").append(resolvedValue).append("], ");
}
return resolvedValue;
}
/**
* Rebuilds the listing filters and the journey trigger sets from the stored settings and
* publishes each as a new immutable snapshot. A filter the stored settings do not hold is an
* empty blacklist, which allows everything.
*/
private void updateListingFilters() {
currentVEventFilterList = namesFilterOf(keyREventBlacklist, keyREventWhitelist);
currentVUserPropertyFilterList = namesFilterOf(keyRUserPropertyBlacklist, keyRUserPropertyWhitelist);
currentVSegmentationFilterList = namesFilterOf(keyRSegmentationBlacklist, keyRSegmentationWhitelist);
currentVEventSegmentationFilterList = namesPerEventFilterOf(keyREventSegmentationBlacklist, keyREventSegmentationWhitelist);
currentVJourneyTriggerEvents = Collections.unmodifiableSet(namesOf(latestRetrievedConfiguration.optJSONArray(keyRJourneyTriggerEvents)));
currentVJourneyTriggerViews = Collections.unmodifiableSet(namesOf(latestRetrievedConfiguration.optJSONArray(keyRJourneyTriggerViews)));
L.d("[ModuleConfiguration] updateListingFilters, event filter:[" + currentVEventFilterList
+ "], user property filter:[" + currentVUserPropertyFilterList
+ "], segmentation filter:[" + currentVSegmentationFilterList
+ "], event segmentation filter:[" + currentVEventSegmentationFilterList
+ "], journey trigger events:" + currentVJourneyTriggerEvents
+ ", journey trigger views:" + currentVJourneyTriggerViews);
}
/**
* Builds a names filter from the stored settings, the blacklist winning when both are stored.
*
* @param blacklistKey the settings key of the blacklist
* @param whitelistKey the settings key of the whitelist
* @return the filter
*/
@Nonnull
private FilterList<Set<String>> namesFilterOf(@Nonnull String blacklistKey, @Nonnull String whitelistKey) {
JSONArray blacklist = latestRetrievedConfiguration.optJSONArray(blacklistKey);
if (blacklist != null) {
return FilterList.ofNames(namesOf(blacklist), false);
}
JSONArray whitelist = latestRetrievedConfiguration.optJSONArray(whitelistKey);
if (whitelist != null) {
return FilterList.ofNames(namesOf(whitelist), true);
}
return FilterList.NO_NAMES;
}
/**
* Builds a names per event filter from the stored settings, the blacklist winning when both are stored.
*
* @param blacklistKey the settings key of the blacklist
* @param whitelistKey the settings key of the whitelist
* @return the filter
*/
@Nonnull
private FilterList<Map<String, Set<String>>> namesPerEventFilterOf(@Nonnull String blacklistKey, @Nonnull String whitelistKey) {
JSONObject blacklist = latestRetrievedConfiguration.optJSONObject(blacklistKey);
if (blacklist != null) {
return FilterList.ofNamesPerEvent(namesPerEventOf(blacklist), false);
}
JSONObject whitelist = latestRetrievedConfiguration.optJSONObject(whitelistKey);
if (whitelist != null) {
return FilterList.ofNamesPerEvent(namesPerEventOf(whitelist), true);
}
return FilterList.NO_NAMES_PER_EVENT;
}
/**
* Reads the names of a JSON array, skipping nulls and turning other values into strings.
*
* @param jsonArray the array, {@code null} for none
* @return the names in array order
*/
@Nonnull
private static Set<String> namesOf(@Nullable JSONArray jsonArray) {
Set<String> names = new LinkedHashSet<>();
if (jsonArray == null) {
return names;
}
for (int i = 0; i < jsonArray.length(); i++) {
String item = jsonArray.optString(i, null);
if (item != null) {
names.add(item);
}
}
return names;
}
/**
* Reads the names of each event key of a JSON object, skipping keys whose value is not an array.
*
* @param jsonObject the object of arrays
* @return the names of each event key
*/
@Nonnull
private static Map<String, Set<String>> namesPerEventOf(@Nonnull JSONObject jsonObject) {
Map<String, Set<String>> namesPerEvent = new LinkedHashMap<>();
for (String key : jsonObject.keySet()) {
JSONArray jsonArray = jsonObject.optJSONArray(key);
if (jsonArray != null) {
namesPerEvent.put(key, namesOf(jsonArray));
}
}
return namesPerEvent;
}
/**
* Decides the log gathering state from the top level {@code lg} directive, shaped
* {@code {"e":false}} or {@code {"e":true,"i":id,"l":"ewidv","b":100}}. Only a live response
* decides: without one the state stays as it is, while disabled settings requests, a response
* without a usable directive and a directive with {@code e} not {@code true} decide off.
*
* @param serverResponse the live response, {@code null} at init
*/
private synchronized void readLogGatheringDirective(@Nullable JSONObject serverResponse) {
if (serverConfigRequestsDisabled) {
//without settings requests no directive can ever arrive, so nothing can ever be gathered
L.d("[ModuleConfiguration] readLogGatheringDirective, SDK behavior settings requests are disabled, log gathering can never be armed");
setLogGatheringOff();
return;
}
if (serverResponse == null) {
L.d("[ModuleConfiguration] readLogGatheringDirective, not a server response, log gathering stays undecided");
return;
}
JSONObject directive = serverResponse.optJSONObject(keyRLogGathering);
if (directive == null) {
L.d("[ModuleConfiguration] readLogGatheringDirective, server response had no usable '" + keyRLogGathering + "', log gathering is off");
setLogGatheringOff();
return;
}
if (!Boolean.TRUE.equals(directive.opt(keyLGEnabled))) {
L.d("[ModuleConfiguration] readLogGatheringDirective, directive says log gathering is off");
setLogGatheringOff();
return;
}
Object gatherIdRaw = directive.opt(keyLGId);
String gatherId = gatherIdRaw instanceof String ? ((String) gatherIdRaw).trim() : "";
if (gatherId.isEmpty()) {
//without the id the server rejects every uploaded batch
L.d("[ModuleConfiguration] readLogGatheringDirective, directive enables log gathering but carries no usable '" + keyLGId + "', log gathering is off");
setLogGatheringOff();
return;
}
LogGatheringDirective gathering = new LogGatheringDirective(LogGatheringState.GATHERING, gatherId,
sanitizeLogGatheringLevels(directive.opt(keyLGLevels)), sanitizeLogGatheringBatchSize(directive.opt(keyLGBatchSize)));
currentVLogGathering = gathering;
L.d("[ModuleConfiguration] readLogGatheringDirective, log gathering is on, id:[" + gathering.gatherId + "], levels:[" + gathering.levels + "], batch size:[" + gathering.batchSize + "]");
}
/**
* Decides against log gathering when nothing decided yet, for a run where no response can arrive,
* and has the logger apply the decision.
*
* @param reason why no response can arrive
*/
private synchronized void decideLogGatheringOffIfUndecided(@Nonnull String reason) {
if (currentVLogGathering.state == LogGatheringState.UNDECIDED) {
L.d("[ModuleConfiguration] decideLogGatheringOffIfUndecided, " + reason + ", log gathering is off");
setLogGatheringOff();
L.applyLogGatheringDirective(reason);
}
}
/**
* Publishes the decision against log gathering, which also drops any previous gather id.
*/
private void setLogGatheringOff() {
currentVLogGathering = LogGatheringDirective.NOT_GATHERING;
}
/**
* Stops log gathering before the device ID changes without merge: decides the directive off,
* then has the logger queue what it holds under the current device ID and stop, so nothing the
* next user does is gathered until a live response for the new device ID arms a gather.
*/
void stopLogGatheringForDeviceIdChange() {
synchronized (this) {
deviceIdChangesWithoutMerge++;
setLogGatheringOff();
}
L.stopLogGatheringForDeviceIdChange();
}
/**
* Keeps only the level characters this SDK knows, lower cased, in the order the server sent them
* and without duplicates. Falls back to every level when nothing usable is left.
*
* @param levelsRaw the {@code l} value of the directive
* @return the level characters to gather
*/
@Nonnull
private String sanitizeLogGatheringLevels(@Nullable Object levelsRaw) {
if (!(levelsRaw instanceof String)) {
return logGatheringAllLevels;
}
String levels = (String) levelsRaw;
StringBuilder filtered = new StringBuilder();
for (int i = 0; i < levels.length(); i++) {
char level = Character.toLowerCase(levels.charAt(i));
if (logGatheringAllLevels.indexOf(level) > -1 && filtered.indexOf(String.valueOf(level)) < 0) {
filtered.append(level);
}
}
if (filtered.length() == 0) {
L.d("[ModuleConfiguration] sanitizeLogGatheringLevels, no usable level in [" + levels + "], falling back to [" + logGatheringAllLevels + "]");
return logGatheringAllLevels;
}
return filtered.toString();
}
/**
* Clamps the batch size into [{@value #logGatheringMinBatchSize}, {@value #logGatheringMaxBufferedLines}],
* falling back to {@value #logGatheringDefaultBatchSize} when it is missing or not a number.
*
* @param batchSizeRaw the {@code b} value of the directive
* @return the batch size
*/
private static int sanitizeLogGatheringBatchSize(@Nullable Object batchSizeRaw) {
if (!(batchSizeRaw instanceof Number)) {
return logGatheringDefaultBatchSize;
}
int batchSize = ((Number) batchSizeRaw).intValue();
return Math.min(logGatheringMaxBufferedLines, Math.max(logGatheringMinBatchSize, batchSize));
}
/**
* Requests the settings from the server and applies the response. Skipped when settings
* requests are disabled, in backend mode and while there is no device ID. The request is sent
* even while the settings forbid networking, as the next response is the only way to allow it again.
*
* @param config configuration of the running SDK
*/
void fetchConfigFromServer(@Nonnull InternalConfig config) {
L.v("[ModuleConfiguration] fetchConfigFromServer");
if (!running || serverConfigRequestsDisabled) {
L.v("[ModuleConfiguration] fetchConfigFromServer, fetching the configuration is aborted, server config requests are disabled or the SDK is stopped");
return;
}
if (config.getDeviceId() == null) {
L.d("[ModuleConfiguration] fetchConfigFromServer, fetching the configuration is aborted, there is no device ID yet");
decideLogGatheringOffIfUndecided("no device ID, no server response this run");
return;
}
if (config.sdk == null || config.sdk.networking == null) {
L.w("[ModuleConfiguration] fetchConfigFromServer, fetching the configuration is aborted, networking is not available");
decideLogGatheringOffIfUndecided("networking is not available, no server response this run");
return;
}
try {
Params params = ModuleRequests.prepareRequiredParams(config).add("method", "sc");
if (requestCompressionEnabled.get()) {
params.add("gzip", "1");
}
String requestData = params.toString();
Transport transport = config.sdk.networking.getTransport();
final long fetchStartNs = System.nanoTime();
final int requestedAfterDeviceIdChanges = deviceIdChangesWithoutMerge;
final String requestedAppKey = config.getServerAppKey();
final String requestedServerUrl = config.getServerURL().toString();
config.immediateRequestGenerator.createImmediateRequestMaker().doWork(requestData, "/o/sdk?", transport, false, true,
response -> onServerConfigResponse(config, response, fetchStartNs, requestedAfterDeviceIdChanges, requestedAppKey, requestedServerUrl), L);
} catch (Exception e) {
L.e("[ModuleConfiguration] fetchConfigFromServer, failed to request the configuration, [" + e + "]");
decideLogGatheringOffIfUndecided("the server config request failed");
}
}
/**
* Handles the response of a settings request: strips the connection test flag, stores and
* applies the settings, and tells the SDK when a resolved value changed.
*
* @param config configuration of the running SDK
* @param response the response, {@code null} when the request failed
* @param fetchStartNs {@link System#nanoTime()} just before the request was made
* @param requestedAfterDeviceIdChanges how many device ID changes without merge happened before the request was made
* @param requestedAppKey the app key the request was made with
* @param requestedServerUrl the server URL the request was made to
*/
private void onServerConfigResponse(@Nonnull InternalConfig config, @Nullable JSONObject response, long fetchStartNs, int requestedAfterDeviceIdChanges,
@Nullable String requestedAppKey, @Nonnull String requestedServerUrl) {
if (!running) {
L.d("[ModuleConfiguration] onServerConfigResponse, the SDK was stopped before the response arrived, ignoring it");
return;
}
if (response == null) {
L.w("[ModuleConfiguration] onServerConfigResponse, not possible to retrieve configuration data, probably due to lack of connection to the server");
decideLogGatheringOffIfUndecided("server config fetch failed");
return;
}
if (Utils.isNotEqual(requestedAppKey, config.getServerAppKey()) || !requestedServerUrl.equals(config.getServerURL().toString())) {
L.d("[ModuleConfiguration] onServerConfigResponse, the app key or the server URL changed after the request was made, ignoring its response");
return;
}
L.d("[ModuleConfiguration] onServerConfigResponse, retrieved configuration response: [" + response + "]");
long fetchLatencyMs = (System.nanoTime() - fetchStartNs) / 1_000_000L;
boolean connectionTestRequested = extractConnectionTestFlag(response);
boolean gzipAccepted = extractGzipFlag(response);
if (requestCompressionEnabled.get()) {
gzipAcceptingServerUrl.set(gzipAccepted ? requestedServerUrl : null);
if (gzipAccepted && requestCompressionBlocked.compareAndSet(true, false)) {
L.i("[ModuleConfiguration] onServerConfigResponse, a behavior settings answer says that the server reads compressed bodies, requests are compressed again");
}
L.d("[ModuleConfiguration] onServerConfigResponse, the server " + (gzipAccepted ? "reads" : "does not say it reads") + " compressed request bodies");
}
boolean changed;
synchronized (this) {
//checked again under the lock stop() takes, so a response racing stop() never writes the store
if (!running) {
L.d("[ModuleConfiguration] onServerConfigResponse, the SDK was stopped while the response was read, ignoring it");
return;
}
saveAndStoreDownloadedConfig(response);
changed = updateConfigVariables(config, response, requestedAfterDeviceIdChanges == deviceIdChangesWithoutMerge);
}
if (changed && config.sdk != null) {
config.sdk.onSdkConfigurationChanged(config);
}
if (connectionTestRequested) {
notifyConnectionTestRequested(fetchLatencyMs);
}
}
/**
* Reads and removes the {@code gzip} flag of a live response, so it is never stored: only an
* answer that carries {@code true} says that the server reads compressed request bodies.
*
* @param serverConfigResponse the live response, changed in place
* @return {@code true} only when the response had {@code "gzip": true}
*/
static boolean extractGzipFlag(@Nullable JSONObject serverConfigResponse) {
if (serverConfigResponse == null || !serverConfigResponse.has(keyRGzip)) {
return false;