Skip to content

Commit

Permalink
Merge pull request #47 from emdobrin/MOB-17068
Browse files Browse the repository at this point in the history
[MOB-17068] Use NamedCollection from Core ServiceProvider
  • Loading branch information
cacheung authored Nov 29, 2022
2 parents e20e666 + addf7ba commit 8a6fa05
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 319 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import com.adobe.marketing.mobile.ExtensionErrorCallback;
import com.adobe.marketing.mobile.LoggingMode;
import com.adobe.marketing.mobile.MobileCore;
import com.adobe.marketing.mobile.services.NamedCollection;
import com.adobe.marketing.mobile.services.ServiceProvider;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -50,6 +52,16 @@ class ConsentExtension extends Extension {
* @param extensionApi {@link ExtensionApi} instance
*/
protected ConsentExtension(final ExtensionApi extensionApi) {
this(extensionApi, null);
}

/**
* Constructor used for testing.
* @param extensionApi {@link ExtensionApi} instance
* @param namedCollection {@link NamedCollection} instance from {@link ServiceProvider}
* @see ConsentExtension(ExtensionApi)
*/
protected ConsentExtension(final ExtensionApi extensionApi, final NamedCollection namedCollection) {
super(extensionApi);
ExtensionErrorCallback<ExtensionError> listenerErrorCallback = new ExtensionErrorCallback<ExtensionError>() {
@Override
Expand Down Expand Up @@ -94,7 +106,15 @@ public void error(final ExtensionError extensionError) {
ListenerEventHubBoot.class,
listenerErrorCallback
);
consentManager = new ConsentManager();
consentManager =
new ConsentManager(
namedCollection == null
? ServiceProvider
.getInstance()
.getDataStoreService()
.getNamedCollection(ConsentConstants.DataStoreKey.DATASTORE_NAME)
: namedCollection
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,33 @@

package com.adobe.marketing.mobile.edge.consent;

import com.adobe.marketing.mobile.LoggingMode;
import com.adobe.marketing.mobile.MobileCore;
import com.adobe.marketing.mobile.services.NamedCollection;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;

final class ConsentManager {

private static final String LOG_SOURCE = "ConsentManager";
private final NamedCollection namedCollection;
private Consents userOptedConsents; // holds on to consents that are updated using PublicAPI or from Edge Consent Response
private Consents defaultConsents; // holds on to default consents obtained from configuration response

/**
* Constructor.
* <p>
* Initializes the {@link #userOptedConsents} from data in persistence.
* Constructor - initializes the {@link #userOptedConsents} from data in persistence.
*
* @param namedCollection used for reading/writing consent preferences to persistence
*/
ConsentManager() {
userOptedConsents = ConsentStorageService.loadConsentsFromPersistence();
ConsentManager(final NamedCollection namedCollection) {
this.namedCollection = namedCollection;
userOptedConsents = loadConsentsFromPersistence();

// Initiate update consent with empty consent object if nothing is loaded from persistence
if (userOptedConsents == null) {
userOptedConsents = new Consents(new HashMap<String, Object>());
userOptedConsents = new Consents(new HashMap<>());
}
}

Expand All @@ -40,7 +49,7 @@ final class ConsentManager {
void mergeAndPersist(final Consents newConsents) {
// merge and persist
userOptedConsents.merge(newConsents);
ConsentStorageService.saveConsentsToPersistence(userOptedConsents);
saveConsentsToPersistence(userOptedConsents);
}

/**
Expand Down Expand Up @@ -79,4 +88,84 @@ Consents getCurrentConsents() {

return currentConsents;
}

/**
* Loads the requested consents from persistence.
* The jsonString from persistence is serialized into {@link Consents} object and returned.
*
* @return {@link Consent} the previously persisted consents. Returns null if there was any
* {@link JSONException} while serializing JSONString to {@code Consents} object.
*/
private Consents loadConsentsFromPersistence() {
if (namedCollection == null) {
MobileCore.log(
LoggingMode.WARNING,
ConsentConstants.LOG_TAG,
String.format(
"%s - loadConsentsFromPersistence failed due to unexpected null namedCollection.",
LOG_SOURCE
)
);
return null;
}

final String jsonString = namedCollection.getString(ConsentConstants.DataStoreKey.CONSENT_PREFERENCES, null);

if (jsonString == null) {
MobileCore.log(
LoggingMode.VERBOSE,
ConsentConstants.LOG_TAG,
String.format(
"%s - No previous consents were stored in persistence. Current consent is null",
LOG_SOURCE
)
);
return null;
}

try {
final JSONObject jsonObject = new JSONObject(jsonString);
final Map<String, Object> consentMap = Utility.toMap(jsonObject);
return new Consents(consentMap);
} catch (JSONException exception) {
MobileCore.log(
LoggingMode.DEBUG,
ConsentConstants.LOG_TAG,
String.format(
"%s - Serialization error while reading consent jsonString from persistence. Unable to load saved consents from persistence.",
LOG_SOURCE
)
);
return null;
}
}

/**
* Call this method to save the consents to persistence.
* The consents are converted to jsonString and stored into persistence.
*
* @param consents the consents that needs to be persisted under key {@link ConsentConstants.DataStoreKey#CONSENT_PREFERENCES}
*/
private void saveConsentsToPersistence(final Consents consents) {
if (namedCollection == null) {
MobileCore.log(
LoggingMode.WARNING,
ConsentConstants.LOG_TAG,
String.format(
"%s - saveConsentsToPersistence failed due to unexpected null namedCollection.",
LOG_SOURCE
)
);
return;
}

if (consents.isEmpty()) {
namedCollection.remove(ConsentConstants.DataStoreKey.CONSENT_PREFERENCES);
return;
}

final JSONObject jsonObject = new JSONObject(consents.asXDMMap());
final String jsonString = jsonObject.toString();
namedCollection.setString(ConsentConstants.DataStoreKey.CONSENT_PREFERENCES, jsonString);
}
}

This file was deleted.

Loading

0 comments on commit 8a6fa05

Please sign in to comment.