diff --git a/firebase-installations/api.txt b/firebase-installations/api.txt index 1514498e651..4e676ce4849 100644 --- a/firebase-installations/api.txt +++ b/firebase-installations/api.txt @@ -25,45 +25,45 @@ package com.google.firebase.installations { package com.google.firebase.installations.local { - public class PersistedFid { - ctor public PersistedFid(@NonNull FirebaseApp); + public class PersistedInstallation { + ctor public PersistedInstallation(@NonNull FirebaseApp); method @NonNull public boolean clear(); - method @NonNull public boolean insertOrUpdatePersistedFidEntry(@NonNull com.google.firebase.installations.local.PersistedFidEntry); - method @NonNull public com.google.firebase.installations.local.PersistedFidEntry readPersistedFidEntryValue(); + method @NonNull public boolean insertOrUpdatePersistedInstallationEntry(@NonNull com.google.firebase.installations.local.PersistedInstallationEntry); + method @NonNull public com.google.firebase.installations.local.PersistedInstallationEntry readPersistedInstallationEntryValue(); } - public enum PersistedFid.RegistrationStatus { - enum_constant public static final com.google.firebase.installations.local.PersistedFid.RegistrationStatus NOT_GENERATED; - enum_constant public static final com.google.firebase.installations.local.PersistedFid.RegistrationStatus REGISTERED; - enum_constant public static final com.google.firebase.installations.local.PersistedFid.RegistrationStatus REGISTER_ERROR; - enum_constant public static final com.google.firebase.installations.local.PersistedFid.RegistrationStatus UNREGISTERED; + public enum PersistedInstallation.RegistrationStatus { + enum_constant public static final com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus NOT_GENERATED; + enum_constant public static final com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus REGISTERED; + enum_constant public static final com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus REGISTER_ERROR; + enum_constant public static final com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus UNREGISTERED; } - public abstract class PersistedFidEntry { - ctor public PersistedFidEntry(); - method @NonNull public static com.google.firebase.installations.local.PersistedFidEntry.Builder builder(); + public abstract class PersistedInstallationEntry { + ctor public PersistedInstallationEntry(); + method @NonNull public static com.google.firebase.installations.local.PersistedInstallationEntry.Builder builder(); method @Nullable public abstract String getAuthToken(); method public abstract long getExpiresInSecs(); method @Nullable public abstract String getFirebaseInstallationId(); method @Nullable public abstract String getRefreshToken(); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFid.RegistrationStatus getRegistrationStatus(); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus getRegistrationStatus(); method public abstract long getTokenCreationEpochInSecs(); method public boolean isErrored(); method public boolean isNotGenerated(); method public boolean isRegistered(); method public boolean isUnregistered(); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder toBuilder(); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder toBuilder(); } - public abstract static class PersistedFidEntry.Builder { - ctor public PersistedFidEntry.Builder(); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry build(); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder setAuthToken(@Nullable String); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder setExpiresInSecs(long); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder setFirebaseInstallationId(@NonNull String); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder setRefreshToken(@Nullable String); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder setRegistrationStatus(@NonNull com.google.firebase.installations.local.PersistedFid.RegistrationStatus); - method @NonNull public abstract com.google.firebase.installations.local.PersistedFidEntry.Builder setTokenCreationEpochInSecs(long); + public abstract static class PersistedInstallationEntry.Builder { + ctor public PersistedInstallationEntry.Builder(); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry build(); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder setAuthToken(@Nullable String); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder setExpiresInSecs(long); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder setFirebaseInstallationId(@NonNull String); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder setRefreshToken(@Nullable String); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder setRegistrationStatus(@NonNull com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus); + method @NonNull public abstract com.google.firebase.installations.local.PersistedInstallationEntry.Builder setTokenCreationEpochInSecs(long); } } @@ -72,30 +72,18 @@ package com.google.firebase.installations.remote { public class FirebaseInstallationServiceClient { ctor public FirebaseInstallationServiceClient(@NonNull Context); - method @NonNull public com.google.firebase.installations.remote.InstallationResponse createFirebaseInstallation(@NonNull String, @NonNull String, @NonNull String, @NonNull String) throws com.google.firebase.installations.remote.FirebaseInstallationServiceException; - method @NonNull public void deleteFirebaseInstallation(@NonNull String, @NonNull String, @NonNull String, @NonNull String) throws com.google.firebase.installations.remote.FirebaseInstallationServiceException; - method @NonNull public InstallationTokenResult generateAuthToken(@NonNull String, @NonNull String, @NonNull String, @NonNull String) throws com.google.firebase.installations.remote.FirebaseInstallationServiceException; - } - - public class FirebaseInstallationServiceException { - ctor public FirebaseInstallationServiceException(@NonNull com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status); - ctor public FirebaseInstallationServiceException(@NonNull String, @NonNull com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status); - ctor public FirebaseInstallationServiceException(@NonNull String, @NonNull com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status, @NonNull Throwable); - method @NonNull public com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status getStatus(); - } - - public enum FirebaseInstallationServiceException.Status { - enum_constant public static final com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status NETWORK_ERROR; - enum_constant public static final com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status SERVER_ERROR; - enum_constant public static final com.google.firebase.installations.remote.FirebaseInstallationServiceException.Status UNAUTHORIZED; + method @NonNull public com.google.firebase.installations.remote.InstallationResponse createFirebaseInstallation(@NonNull String, @NonNull String, @NonNull String, @NonNull String); + method @NonNull public void deleteFirebaseInstallation(@NonNull String, @NonNull String, @NonNull String, @NonNull String); + method @NonNull public InstallationTokenResult generateAuthToken(@NonNull String, @NonNull String, @NonNull String, @NonNull String); } public abstract class InstallationResponse { ctor public InstallationResponse(); method @NonNull public static com.google.firebase.installations.remote.InstallationResponse.Builder builder(); method @NonNull public abstract InstallationTokenResult getAuthToken(); - method @NonNull public abstract String getName(); + method @NonNull public abstract String getFid(); method @NonNull public abstract String getRefreshToken(); + method @NonNull public abstract String getUri(); method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse.Builder toBuilder(); } @@ -103,8 +91,9 @@ package com.google.firebase.installations.remote { ctor public InstallationResponse.Builder(); method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse build(); method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse.Builder setAuthToken(@NonNull InstallationTokenResult); - method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse.Builder setName(@NonNull String); + method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse.Builder setFid(@NonNull String); method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse.Builder setRefreshToken(@NonNull String); + method @NonNull public abstract com.google.firebase.installations.remote.InstallationResponse.Builder setUri(@NonNull String); } } diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java b/firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java index bd6ef7eb292..d64d34ffc72 100644 --- a/firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installations/FirebaseInstallationsInstrumentedTest.java @@ -15,7 +15,8 @@ package com.google.firebase.installations; import static com.google.common.truth.Truth.assertWithMessage; -import static com.google.firebase.installations.FisAndroidTestConstants.DEFAULT_PERSISTED_FID_ENTRY; +import static com.google.firebase.installations.FisAndroidTestConstants.DEFAULT_PERSISTED_INSTALLATION_ENTRY; +import static com.google.firebase.installations.FisAndroidTestConstants.INVALID_TEST_FID; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_API_KEY; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN; @@ -31,7 +32,7 @@ import static com.google.firebase.installations.FisAndroidTestConstants.TEST_REFRESH_TOKEN; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP_2; -import static com.google.firebase.installations.local.PersistedFidEntrySubject.assertThat; +import static com.google.firebase.installations.local.PersistedInstallationEntrySubject.assertThat; import static org.junit.Assert.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -48,12 +49,12 @@ import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseException; import com.google.firebase.FirebaseOptions; -import com.google.firebase.installations.local.PersistedFid; -import com.google.firebase.installations.local.PersistedFid.RegistrationStatus; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.installations.local.PersistedInstallation; +import com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus; +import com.google.firebase.installations.local.PersistedInstallationEntry; import com.google.firebase.installations.remote.FirebaseInstallationServiceClient; -import com.google.firebase.installations.remote.FirebaseInstallationServiceException; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; @@ -79,56 +80,66 @@ public class FirebaseInstallationsInstrumentedTest { private FirebaseApp firebaseApp; private ExecutorService executor; - private PersistedFid persistedFid; + private PersistedInstallation persistedInstallation; @Mock private FirebaseInstallationServiceClient backendClientReturnsOk; @Mock private FirebaseInstallationServiceClient backendClientReturnsError; - @Mock private PersistedFid persistedFidReturnsError; + @Mock private PersistedInstallation persistedInstallationReturnsError; @Mock private Utils mockUtils; - @Mock private PersistedFid mockPersistedFid; + @Mock private PersistedInstallation mockPersistedInstallation; @Mock private FirebaseInstallationServiceClient mockClient; - private static final PersistedFidEntry REGISTERED_FID_ENTRY = - PersistedFidEntry.builder() + private static final PersistedInstallationEntry REGISTERED_INSTALLATION_ENTRY = + PersistedInstallationEntry.builder() .setFirebaseInstallationId(TEST_FID_1) .setAuthToken(TEST_AUTH_TOKEN) .setRefreshToken(TEST_REFRESH_TOKEN) .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) .setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) - .setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.REGISTERED) .build(); - private static final PersistedFidEntry EXPIRED_AUTH_TOKEN_ENTRY = - PersistedFidEntry.builder() + private static final PersistedInstallationEntry EXPIRED_AUTH_TOKEN_ENTRY = + PersistedInstallationEntry.builder() .setFirebaseInstallationId(TEST_FID_1) .setAuthToken(TEST_AUTH_TOKEN) .setRefreshToken(TEST_REFRESH_TOKEN) .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) .setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP_2) - .setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.REGISTERED) .build(); - private static final PersistedFidEntry UNREGISTERED_FID_ENTRY = - PersistedFidEntry.builder() + private static final PersistedInstallationEntry UNREGISTERED_INSTALLATION_ENTRY = + PersistedInstallationEntry.builder() .setFirebaseInstallationId(TEST_FID_1) .setAuthToken("") .setRefreshToken("") .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) .setExpiresInSecs(0) - .setRegistrationStatus(PersistedFid.RegistrationStatus.UNREGISTERED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.UNREGISTERED) .build(); - private static final PersistedFidEntry UPDATED_AUTH_TOKEN_ENTRY = - PersistedFidEntry.builder() + private static final PersistedInstallationEntry INVALID_INSTALLATION_ENTRY = + PersistedInstallationEntry.builder() + .setFirebaseInstallationId(INVALID_TEST_FID) + .setAuthToken("") + .setRefreshToken("") + .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) + .setExpiresInSecs(0) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.UNREGISTERED) + .build(); + + private static final PersistedInstallationEntry UPDATED_AUTH_TOKEN_ENTRY = + PersistedInstallationEntry.builder() .setFirebaseInstallationId(TEST_FID_1) .setAuthToken(TEST_AUTH_TOKEN_2) .setRefreshToken(TEST_REFRESH_TOKEN) .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) .setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) - .setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.REGISTERED) .build(); @Before - public void setUp() throws FirebaseInstallationServiceException { + public void setUp() throws FirebaseException { MockitoAnnotations.initMocks(this); FirebaseApp.clearInstancesForTest(); executor = new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>()); @@ -140,7 +151,7 @@ public void setUp() throws FirebaseInstallationServiceException { .setProjectId(TEST_PROJECT_ID) .setApiKey(TEST_API_KEY) .build()); - persistedFid = new PersistedFid(firebaseApp); + persistedInstallation = new PersistedInstallation(firebaseApp); when(backendClientReturnsOk.createFirebaseInstallation( anyString(), anyString(), anyString(), anyString())) @@ -150,15 +161,14 @@ public void setUp() throws FirebaseInstallationServiceException { anyString(), anyString(), anyString(), anyString())) .thenReturn(TEST_INSTALLATION_TOKEN_RESULT); - when(persistedFidReturnsError.insertOrUpdatePersistedFidEntry(any())).thenReturn(false); - when(persistedFidReturnsError.readPersistedFidEntryValue()) - .thenReturn(DEFAULT_PERSISTED_FID_ENTRY); + when(persistedInstallationReturnsError.insertOrUpdatePersistedInstallationEntry(any())) + .thenReturn(false); + when(persistedInstallationReturnsError.readPersistedInstallationEntryValue()) + .thenReturn(DEFAULT_PERSISTED_INSTALLATION_ENTRY); when(backendClientReturnsError.createFirebaseInstallation( anyString(), anyString(), anyString(), anyString())) - .thenThrow( - new FirebaseInstallationServiceException( - "SDK Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)); + .thenThrow(new FirebaseException("SDK Error")); when(mockUtils.createRandomFid()).thenReturn(TEST_FID_1); when(mockUtils.currentTimeInSecs()).thenReturn(TEST_CREATION_TIMESTAMP_2); @@ -168,46 +178,49 @@ public void setUp() throws FirebaseInstallationServiceException { .when(backendClientReturnsOk) .deleteFirebaseInstallation(anyString(), anyString(), anyString(), anyString()); // Mocks server error on FIS deletion - doThrow( - new FirebaseInstallationServiceException( - "Server Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)) + doThrow(new FirebaseException("Server Error")) .when(backendClientReturnsError) .deleteFirebaseInstallation(anyString(), anyString(), anyString(), anyString()); } @After public void cleanUp() throws Exception { - persistedFid.clear(); + persistedInstallation.clear(); } @Test - public void testGetId_PersistedFidOk_BackendOk() throws Exception { - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + public void testGetId_PersistedInstallationOk_BackendOk() throws Exception { + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); // No exception, means success. assertWithMessage("getId Task failed.") .that(Tasks.await(firebaseInstallations.getId())) .isNotEmpty(); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); assertThat(entryValue).hasFid(TEST_FID_1); // Waiting for Task that registers FID on the FIS Servers executor.awaitTermination(500, TimeUnit.MILLISECONDS); - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasFid(TEST_FID_1); - assertThat(updatedFidEntry).hasRegistrationStatus(RegistrationStatus.REGISTERED); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasFid(TEST_FID_1); + assertThat(updatedInstallationEntry).hasRegistrationStatus(RegistrationStatus.REGISTERED); } @Test public void testGetId_multipleCalls_sameFIDReturned() throws Exception { - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); + when(backendClientReturnsOk.createFirebaseInstallation( + anyString(), anyString(), anyString(), anyString())) + .thenReturn(TEST_INSTALLATION_RESPONSE); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); // Call getId multiple times Task task1 = firebaseInstallations.getId(); @@ -224,35 +237,69 @@ public void testGetId_multipleCalls_sameFIDReturned() throws Exception { .isEqualTo(TEST_FID_1); verify(backendClientReturnsOk, times(1)) .createFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_APP_ID_1); - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasFid(TEST_FID_1); - assertThat(updatedFidEntry).hasRegistrationStatus(RegistrationStatus.REGISTERED); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasFid(TEST_FID_1); + assertThat(updatedInstallationEntry).hasRegistrationStatus(RegistrationStatus.REGISTERED); } @Test - public void testGetId_PersistedFidOk_BackendError() throws Exception { + public void testGetId_invalidFid_storesValidFidFromResponse() throws Exception { + // Update local storage with installation entry that has invalid fid. + persistedInstallation.insertOrUpdatePersistedInstallationEntry(INVALID_INSTALLATION_ENTRY); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsError, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); + + // No exception, means success. + assertWithMessage("getId Task failed.") + .that(Tasks.await(firebaseInstallations.getId())) + .isNotEmpty(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(entryValue).hasFid(INVALID_TEST_FID); + + // Waiting for Task that registers FID on the FIS Servers + executor.awaitTermination(500, TimeUnit.MILLISECONDS); + + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + // After FID registration is complete, installation entry is updated with valid fid. + assertThat(updatedInstallationEntry).hasFid(TEST_FID_1); + assertThat(updatedInstallationEntry).hasRegistrationStatus(RegistrationStatus.REGISTERED); + } + + @Test + public void testGetId_PersistedInstallationOk_BackendError() throws Exception { + FirebaseInstallations firebaseInstallations = + new FirebaseInstallations( + executor, firebaseApp, backendClientReturnsError, persistedInstallation, mockUtils); Tasks.await(firebaseInstallations.getId()); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); assertThat(entryValue).hasFid(TEST_FID_1); // Waiting for Task that registers FID on the FIS Servers executor.awaitTermination(500, TimeUnit.MILLISECONDS); - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasFid(TEST_FID_1); - assertThat(updatedFidEntry).hasRegistrationStatus(RegistrationStatus.REGISTER_ERROR); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasFid(TEST_FID_1); + assertThat(updatedInstallationEntry).hasRegistrationStatus(RegistrationStatus.REGISTER_ERROR); } @Test - public void testGetId_PersistedFidError_BackendOk() throws InterruptedException { + public void testGetId_PersistedInstallationError_BackendOk() throws InterruptedException { FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFidReturnsError, mockUtils); + executor, + firebaseApp, + backendClientReturnsOk, + persistedInstallationReturnsError, + mockUtils); // Expect exception try { @@ -277,29 +324,32 @@ public void testGetId_fidRegistrationUncheckedException_statusUpdated() throws E invocation -> { throw new InterruptedException(); }); - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = - new FirebaseInstallations(executor, firebaseApp, mockClient, persistedFid, mockUtils); + new FirebaseInstallations( + executor, firebaseApp, mockClient, persistedInstallation, mockUtils); Tasks.await(firebaseInstallations.getId()); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); assertThat(entryValue).hasFid(TEST_FID_1); // Waiting for Task that registers FID on the FIS Servers executor.awaitTermination(500, TimeUnit.MILLISECONDS); // Validate that registration status is REGISTER_ERROR - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasFid(TEST_FID_1); - assertThat(updatedFidEntry).hasRegistrationStatus(RegistrationStatus.REGISTER_ERROR); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasFid(TEST_FID_1); + assertThat(updatedInstallationEntry).hasRegistrationStatus(RegistrationStatus.REGISTER_ERROR); } @Test public void testGetId_expiredAuthTokenUncheckedException_statusUpdated() throws Exception { - // Update local storage with fid entry that has auth token expired. - persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY); + // Update local storage with installation entry that has auth token expired. + persistedInstallation.insertOrUpdatePersistedInstallationEntry(EXPIRED_AUTH_TOKEN_ENTRY); // Mocking unchecked exception on FIS generateAuthToken when(mockClient.generateAuthToken(anyString(), anyString(), anyString(), anyString())) .thenAnswer( @@ -309,45 +359,50 @@ public void testGetId_expiredAuthTokenUncheckedException_statusUpdated() throws when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true); FirebaseInstallations firebaseInstallations = - new FirebaseInstallations(executor, firebaseApp, mockClient, persistedFid, mockUtils); + new FirebaseInstallations( + executor, firebaseApp, mockClient, persistedInstallation, mockUtils); assertWithMessage("getId Task failed") .that(Tasks.await(firebaseInstallations.getId())) .isNotEmpty(); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); assertThat(entryValue).hasFid(TEST_FID_1); // Waiting for Task that generates auth token with the FIS Servers executor.awaitTermination(500, TimeUnit.MILLISECONDS); // Validate that registration status is REGISTER_ERROR - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasFid(TEST_FID_1); - assertThat(updatedFidEntry).hasRegistrationStatus(RegistrationStatus.REGISTER_ERROR); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasFid(TEST_FID_1); + assertThat(updatedInstallationEntry).hasRegistrationStatus(RegistrationStatus.REGISTER_ERROR); } @Test public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception { - // Update local storage with fid entry that has auth token expired. - persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY); + // Update local storage with installation entry that has auth token expired. + persistedInstallation.insertOrUpdatePersistedInstallationEntry(EXPIRED_AUTH_TOKEN_ENTRY); when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); assertWithMessage("getId Task failed") .that(Tasks.await(firebaseInstallations.getId())) .isNotEmpty(); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); assertThat(entryValue).hasFid(TEST_FID_1); // Waiting for Task that registers FID on the FIS Servers executor.awaitTermination(500, TimeUnit.MILLISECONDS); // Validate that Persisted FID has a refreshed auth token now - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasAuthToken(TEST_AUTH_TOKEN_2); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasAuthToken(TEST_AUTH_TOKEN_2); verify(backendClientReturnsOk, never()) .createFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_APP_ID_1); verify(backendClientReturnsOk, times(1)) @@ -356,23 +411,28 @@ public void testGetId_expiredAuthToken_refreshesAuthToken() throws Exception { @Test public void testGetAuthToken_fidDoesNotExist_successful() throws Exception { - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); Tasks.await(firebaseInstallations.getAuthToken(FirebaseInstallationsApi.DO_NOT_FORCE_REFRESH)); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); assertThat(entryValue).hasAuthToken(TEST_AUTH_TOKEN); } @Test - public void testGetAuthToken_PersistedFidError_failure() throws Exception { - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + public void testGetAuthToken_PersistedInstallationError_failure() throws Exception { + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFidReturnsError, mockUtils); + executor, + firebaseApp, + backendClientReturnsOk, + persistedInstallationReturnsError, + mockUtils); // Expect exception try { @@ -392,12 +452,13 @@ public void testGetAuthToken_PersistedFidError_failure() throws Exception { @Test public void testGetAuthToken_fidExists_successful() throws Exception { - when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY); - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + when(mockPersistedInstallation.readPersistedInstallationEntryValue()) + .thenReturn(REGISTERED_INSTALLATION_ENTRY); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, mockPersistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, mockPersistedInstallation, mockUtils); InstallationTokenResult installationTokenResult = Tasks.await( @@ -412,13 +473,13 @@ public void testGetAuthToken_fidExists_successful() throws Exception { @Test public void testGetAuthToken_expiredAuthToken_fetchedNewTokenFromFIS() throws Exception { - persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY); + persistedInstallation.insertOrUpdatePersistedInstallationEntry(EXPIRED_AUTH_TOKEN_ENTRY); when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true); when(mockUtils.isAuthTokenExpired(UPDATED_AUTH_TOKEN_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); InstallationTokenResult installationTokenResult = Tasks.await( @@ -433,14 +494,14 @@ public void testGetAuthToken_expiredAuthToken_fetchedNewTokenFromFIS() throws Ex @Test public void testGetAuthToken_unregisteredFid_fetchedNewTokenFromFIS() throws Exception { - // Update local storage with a unregistered fid entry to validate that getAuthToken calls getId - // to ensure FID registration and returns a valid auth token. - persistedFid.insertOrUpdatePersistedFidEntry(UNREGISTERED_FID_ENTRY); - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + // Update local storage with a unregistered installation entry to validate that getAuthToken + // calls getId to ensure FID registration and returns a valid auth token. + persistedInstallation.insertOrUpdatePersistedInstallationEntry(UNREGISTERED_INSTALLATION_ENTRY); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); InstallationTokenResult installationTokenResult = Tasks.await( @@ -455,17 +516,16 @@ public void testGetAuthToken_unregisteredFid_fetchedNewTokenFromFIS() throws Exc @Test public void testGetAuthToken_serverError_failure() throws Exception { - when(mockPersistedFid.readPersistedFidEntryValue()).thenReturn(REGISTERED_FID_ENTRY); + when(mockPersistedInstallation.readPersistedInstallationEntryValue()) + .thenReturn(REGISTERED_INSTALLATION_ENTRY); when(backendClientReturnsError.generateAuthToken( anyString(), anyString(), anyString(), anyString())) - .thenThrow( - new FirebaseInstallationServiceException( - "Server Error", FirebaseInstallationServiceException.Status.SERVER_ERROR)); - when(mockUtils.isAuthTokenExpired(REGISTERED_FID_ENTRY)).thenReturn(false); + .thenThrow(new FirebaseException("Server Error")); + when(mockUtils.isAuthTokenExpired(REGISTERED_INSTALLATION_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsError, mockPersistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsError, mockPersistedInstallation, mockUtils); // Expect exception try { @@ -488,13 +548,13 @@ public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce( // Update local storage with a EXPIRED_AUTH_TOKEN_ENTRY to validate the flow of multiple tasks // triggered simultaneously. Task2 waits for Task1 to complete. On task1 completion, task2 reads // the UPDATED_AUTH_TOKEN_FID_ENTRY generated by Task1. - persistedFid.insertOrUpdatePersistedFidEntry(EXPIRED_AUTH_TOKEN_ENTRY); + persistedInstallation.insertOrUpdatePersistedInstallationEntry(EXPIRED_AUTH_TOKEN_ENTRY); when(mockUtils.isAuthTokenExpired(EXPIRED_AUTH_TOKEN_ENTRY)).thenReturn(true); when(mockUtils.isAuthTokenExpired(UPDATED_AUTH_TOKEN_ENTRY)).thenReturn(false); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); // Call getAuthToken multiple times with DO_NOT_FORCE_REFRESH option Task task1 = @@ -516,7 +576,7 @@ public void testGetAuthToken_multipleCallsDoNotForceRefresh_fetchedNewTokenOnce( @Test public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() throws Exception { - persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY); + persistedInstallation.insertOrUpdatePersistedInstallationEntry(REGISTERED_INSTALLATION_ENTRY); // Use a mock ServiceClient for network calls with delay(500ms) to ensure first task is not // completed before the second task starts. Hence, we can test multiple calls to getAuthToken() // and verify one task waits for another task to complete. @@ -545,7 +605,7 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); // Call getAuthToken multiple times with FORCE_REFRESH option. Task task1 = @@ -563,38 +623,41 @@ public void testGetAuthToken_multipleCallsForceRefresh_fetchedNewTokenTwice() th .isEqualTo(TEST_AUTH_TOKEN_3); verify(backendClientReturnsOk, times(1)) .generateAuthToken(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); - PersistedFidEntry updatedFidEntry = persistedFid.readPersistedFidEntryValue(); - assertThat(updatedFidEntry).hasAuthToken(TEST_AUTH_TOKEN_3); + PersistedInstallationEntry updatedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(updatedInstallationEntry).hasAuthToken(TEST_AUTH_TOKEN_3); } @Test public void testDelete_registeredFID_successful() throws Exception { - // Update local storage with a registered fid entry - persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY); + // Update local storage with a registered installation entry + persistedInstallation.insertOrUpdatePersistedInstallationEntry(REGISTERED_INSTALLATION_ENTRY); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); Tasks.await(firebaseInstallations.delete()); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); - assertThat(entryValue).isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(entryValue).isEqualTo(DEFAULT_PERSISTED_INSTALLATION_ENTRY); verify(backendClientReturnsOk, times(1)) .deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); } @Test public void testDelete_unregisteredFID_successful() throws Exception { - // Update local storage with a unregistered fid entry - persistedFid.insertOrUpdatePersistedFidEntry(UNREGISTERED_FID_ENTRY); + // Update local storage with a unregistered installation entry + persistedInstallation.insertOrUpdatePersistedInstallationEntry(UNREGISTERED_INSTALLATION_ENTRY); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); Tasks.await(firebaseInstallations.delete()); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); - assertThat(entryValue).isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(entryValue).isEqualTo(DEFAULT_PERSISTED_INSTALLATION_ENTRY); verify(backendClientReturnsOk, never()) .deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); } @@ -603,23 +666,24 @@ public void testDelete_unregisteredFID_successful() throws Exception { public void testDelete_emptyPersistedFidEntry_successful() throws Exception { FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsOk, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsOk, persistedInstallation, mockUtils); Tasks.await(firebaseInstallations.delete()); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); - assertThat(entryValue).isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(entryValue).isEqualTo(DEFAULT_PERSISTED_INSTALLATION_ENTRY); verify(backendClientReturnsOk, never()) .deleteFirebaseInstallation(TEST_API_KEY, TEST_FID_1, TEST_PROJECT_ID, TEST_REFRESH_TOKEN); } @Test public void testDelete_serverError_failure() throws Exception { - // Update local storage with a registered fid entry - persistedFid.insertOrUpdatePersistedFidEntry(REGISTERED_FID_ENTRY); + // Update local storage with a registered installation entry + persistedInstallation.insertOrUpdatePersistedInstallationEntry(REGISTERED_INSTALLATION_ENTRY); FirebaseInstallations firebaseInstallations = new FirebaseInstallations( - executor, firebaseApp, backendClientReturnsError, persistedFid, mockUtils); + executor, firebaseApp, backendClientReturnsError, persistedInstallation, mockUtils); // Expect exception try { @@ -633,8 +697,9 @@ public void testDelete_serverError_failure() throws Exception { assertWithMessage("Exception status doesn't match") .that(((FirebaseInstallationsException) expected.getCause()).getStatus()) .isEqualTo(FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); - PersistedFidEntry entryValue = persistedFid.readPersistedFidEntryValue(); - assertThat(entryValue).isEqualTo(REGISTERED_FID_ENTRY); + PersistedInstallationEntry entryValue = + persistedInstallation.readPersistedInstallationEntryValue(); + assertThat(entryValue).isEqualTo(REGISTERED_INSTALLATION_ENTRY); } } } diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installations/FisAndroidTestConstants.java b/firebase-installations/src/androidTest/java/com/google/firebase/installations/FisAndroidTestConstants.java index 1728cb56554..d94cdcc214c 100644 --- a/firebase-installations/src/androidTest/java/com/google/firebase/installations/FisAndroidTestConstants.java +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installations/FisAndroidTestConstants.java @@ -14,11 +14,13 @@ package com.google.firebase.installations; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.installations.local.PersistedInstallationEntry; import com.google.firebase.installations.remote.InstallationResponse; public final class FisAndroidTestConstants { public static final String TEST_FID_1 = "cccccccccccccccccccccc"; + /** Invalid FID. */ + public static final String INVALID_TEST_FID = "invalid"; public static final String TEST_PROJECT_ID = "777777777777"; @@ -40,11 +42,12 @@ public final class FisAndroidTestConstants { public static final long TEST_CREATION_TIMESTAMP_1 = 2000L; public static final long TEST_CREATION_TIMESTAMP_2 = 2L; - public static final PersistedFidEntry DEFAULT_PERSISTED_FID_ENTRY = - PersistedFidEntry.builder().build(); + public static final PersistedInstallationEntry DEFAULT_PERSISTED_INSTALLATION_ENTRY = + PersistedInstallationEntry.builder().build(); public static final InstallationResponse TEST_INSTALLATION_RESPONSE = InstallationResponse.builder() - .setName("/projects/" + TEST_PROJECT_ID + "/installations/" + TEST_FID_1) + .setUri("/projects/" + TEST_PROJECT_ID + "/installations/" + TEST_FID_1) + .setFid(TEST_FID_1) .setRefreshToken(TEST_REFRESH_TOKEN) .setAuthToken( InstallationTokenResult.builder() diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidEntrySubject.java b/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedInstallationEntrySubject.java similarity index 65% rename from firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidEntrySubject.java rename to firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedInstallationEntrySubject.java index 48586508823..84c738ef42c 100644 --- a/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidEntrySubject.java +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedInstallationEntrySubject.java @@ -18,37 +18,40 @@ import com.google.common.truth.FailureMetadata; import com.google.common.truth.Subject; -import com.google.firebase.installations.local.PersistedFid.RegistrationStatus; +import com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus; import org.checkerframework.checker.nullness.compatqual.NullableDecl; -public final class PersistedFidEntrySubject extends Subject { +public final class PersistedInstallationEntrySubject extends Subject { // User-defined entry point - public static PersistedFidEntrySubject assertThat( - @NullableDecl PersistedFidEntry persistedFidEntry) { - return assertAbout(PERSISTED_FID_ENTRY_SUBJECT_FACTORY).that(persistedFidEntry); + public static PersistedInstallationEntrySubject assertThat( + @NullableDecl PersistedInstallationEntry persistedInstallationEntry) { + return assertAbout(PERSISTED_INSTALLATION_ENTRY_SUBJECT_FACTORY) + .that(persistedInstallationEntry); } // Static method for getting the subject factory (for use with assertAbout()) - public static Subject.Factory persistedFidEntry() { - return PERSISTED_FID_ENTRY_SUBJECT_FACTORY; + public static Subject.Factory + persistedInstallationEntry() { + return PERSISTED_INSTALLATION_ENTRY_SUBJECT_FACTORY; } - // Boiler-plate Subject.Factory for EmployeeSubject - private static final Subject.Factory - PERSISTED_FID_ENTRY_SUBJECT_FACTORY = PersistedFidEntrySubject::new; + // Boiler-plate Subject.Factory for PersistedInstallationEntrySubject + private static final Subject.Factory< + PersistedInstallationEntrySubject, PersistedInstallationEntry> + PERSISTED_INSTALLATION_ENTRY_SUBJECT_FACTORY = PersistedInstallationEntrySubject::new; - private final PersistedFidEntry actual; + private final PersistedInstallationEntry actual; /** * Constructor for use by subclasses. If you want to create an instance of this class itself, call - * {@link Subject#check(String, PersistedFidEntry ..) check(...)}{@code .that(actual)}. + * {@link Subject#check(String, PersistedInstallationEntry ..) check(...)}{@code .that(actual)}. * * @param metadata * @param actual */ - protected PersistedFidEntrySubject( - FailureMetadata metadata, @NullableDecl PersistedFidEntry actual) { + protected PersistedInstallationEntrySubject( + FailureMetadata metadata, @NullableDecl PersistedInstallationEntry actual) { super(metadata, actual); this.actual = actual; } diff --git a/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidTest.java b/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedInstallationTest.java similarity index 69% rename from firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidTest.java rename to firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedInstallationTest.java index f00b9447bdb..a27e1885924 100644 --- a/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedFidTest.java +++ b/firebase-installations/src/androidTest/java/com/google/firebase/installations/local/PersistedInstallationTest.java @@ -14,7 +14,7 @@ package com.google.firebase.installations.local; -import static com.google.firebase.installations.FisAndroidTestConstants.DEFAULT_PERSISTED_FID_ENTRY; +import static com.google.firebase.installations.FisAndroidTestConstants.DEFAULT_PERSISTED_INSTALLATION_ENTRY; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_1; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_APP_ID_2; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_AUTH_TOKEN; @@ -23,27 +23,27 @@ import static com.google.firebase.installations.FisAndroidTestConstants.TEST_FID_1; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_REFRESH_TOKEN; import static com.google.firebase.installations.FisAndroidTestConstants.TEST_TOKEN_EXPIRATION_TIMESTAMP; -import static com.google.firebase.installations.local.PersistedFidEntrySubject.assertThat; +import static com.google.firebase.installations.local.PersistedInstallationEntrySubject.assertThat; import static org.junit.Assert.assertTrue; import androidx.test.core.app.ApplicationProvider; import androidx.test.runner.AndroidJUnit4; import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseOptions; -import com.google.firebase.installations.local.PersistedFid.RegistrationStatus; +import com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -/** Instrumented tests for {@link PersistedFid} */ +/** Instrumented tests for {@link PersistedInstallation} */ @RunWith(AndroidJUnit4.class) -public class PersistedFidTest { +public class PersistedInstallationTest { private FirebaseApp firebaseApp0; private FirebaseApp firebaseApp1; - private PersistedFid persistedFid0; - private PersistedFid persistedFid1; + private PersistedInstallation persistedInstallation0; + private PersistedInstallation persistedInstallation1; @Before public void setUp() { @@ -57,36 +57,39 @@ public void setUp() { ApplicationProvider.getApplicationContext(), new FirebaseOptions.Builder().setApplicationId(TEST_APP_ID_2).build(), "firebase_app_1"); - persistedFid0 = new PersistedFid(firebaseApp0); - persistedFid1 = new PersistedFid(firebaseApp1); + persistedInstallation0 = new PersistedInstallation(firebaseApp0); + persistedInstallation1 = new PersistedInstallation(firebaseApp1); } @After public void cleanUp() throws Exception { - persistedFid0.clear(); - persistedFid1.clear(); + persistedInstallation0.clear(); + persistedInstallation1.clear(); } @Test - public void testReadPersistedFidEntry_Null() { - assertThat(persistedFid0.readPersistedFidEntryValue()).isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); - assertThat(persistedFid1.readPersistedFidEntryValue()).isEqualTo(DEFAULT_PERSISTED_FID_ENTRY); + public void testReadPersistedInstallationEntry_Null() { + assertThat(persistedInstallation0.readPersistedInstallationEntryValue()) + .isEqualTo(DEFAULT_PERSISTED_INSTALLATION_ENTRY); + assertThat(persistedInstallation1.readPersistedInstallationEntryValue()) + .isEqualTo(DEFAULT_PERSISTED_INSTALLATION_ENTRY); } @Test - public void testUpdateAndReadPersistedFidEntry_successful() throws Exception { - // Insert Persisted Fid Entry with Unregistered status in Shared Prefs + public void testUpdateAndReadPersistedInstallationEntry_successful() throws Exception { + // Insert Persisted Installation Entry with Unregistered status in Shared Prefs assertTrue( - persistedFid0.insertOrUpdatePersistedFidEntry( - PersistedFidEntry.builder() + persistedInstallation0.insertOrUpdatePersistedInstallationEntry( + PersistedInstallationEntry.builder() .setFirebaseInstallationId(TEST_FID_1) .setAuthToken(TEST_AUTH_TOKEN) .setRefreshToken(TEST_REFRESH_TOKEN) - .setRegistrationStatus(PersistedFid.RegistrationStatus.UNREGISTERED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.UNREGISTERED) .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_1) .setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) .build())); - PersistedFidEntry entryValue = persistedFid0.readPersistedFidEntryValue(); + PersistedInstallationEntry entryValue = + persistedInstallation0.readPersistedInstallationEntryValue(); // Validate insertion was successful assertThat(entryValue).hasFid(TEST_FID_1); @@ -98,16 +101,16 @@ public void testUpdateAndReadPersistedFidEntry_successful() throws Exception { // Update Persisted Fid Entry with Registered status in Shared Prefs assertTrue( - persistedFid0.insertOrUpdatePersistedFidEntry( - PersistedFidEntry.builder() + persistedInstallation0.insertOrUpdatePersistedInstallationEntry( + PersistedInstallationEntry.builder() .setFirebaseInstallationId(TEST_FID_1) .setAuthToken(TEST_AUTH_TOKEN) .setRefreshToken(TEST_REFRESH_TOKEN) - .setRegistrationStatus(PersistedFid.RegistrationStatus.REGISTERED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.REGISTERED) .setTokenCreationEpochInSecs(TEST_CREATION_TIMESTAMP_2) .setExpiresInSecs(TEST_TOKEN_EXPIRATION_TIMESTAMP) .build())); - entryValue = persistedFid0.readPersistedFidEntryValue(); + entryValue = persistedInstallation0.readPersistedInstallationEntryValue(); // Validate update was successful assertThat(entryValue).hasFid(TEST_FID_1); diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java index 7eebd24933e..709b78637a8 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java @@ -23,11 +23,11 @@ import com.google.android.gms.tasks.TaskCompletionSource; import com.google.android.gms.tasks.Tasks; import com.google.firebase.FirebaseApp; -import com.google.firebase.installations.local.PersistedFid; -import com.google.firebase.installations.local.PersistedFid.RegistrationStatus; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.FirebaseException; +import com.google.firebase.installations.local.PersistedInstallation; +import com.google.firebase.installations.local.PersistedInstallation.RegistrationStatus; +import com.google.firebase.installations.local.PersistedInstallationEntry; import com.google.firebase.installations.remote.FirebaseInstallationServiceClient; -import com.google.firebase.installations.remote.FirebaseInstallationServiceException; import com.google.firebase.installations.remote.InstallationResponse; import java.util.ArrayList; import java.util.Iterator; @@ -52,7 +52,7 @@ public class FirebaseInstallations implements FirebaseInstallationsApi { private final FirebaseApp firebaseApp; private final FirebaseInstallationServiceClient serviceClient; - private final PersistedFid persistedFid; + private final PersistedInstallation persistedInstallation; private final ExecutorService executor; private final Utils utils; private final Object lock = new Object(); @@ -69,7 +69,7 @@ public class FirebaseInstallations implements FirebaseInstallationsApi { new ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>()), firebaseApp, new FirebaseInstallationServiceClient(firebaseApp.getApplicationContext()), - new PersistedFid(firebaseApp), + new PersistedInstallation(firebaseApp), new Utils(DefaultClock.getInstance())); } @@ -77,12 +77,12 @@ public class FirebaseInstallations implements FirebaseInstallationsApi { ExecutorService executor, FirebaseApp firebaseApp, FirebaseInstallationServiceClient serviceClient, - PersistedFid persistedFid, + PersistedInstallation persistedInstallation, Utils utils) { this.firebaseApp = firebaseApp; this.serviceClient = serviceClient; this.executor = executor; - this.persistedFid = persistedFid; + this.persistedInstallation = persistedInstallation; this.utils = utils; } @@ -184,12 +184,13 @@ private Task addGetAuthTokenListener( return taskCompletionSource.getTask(); } - private void triggerOnStateReached(PersistedFidEntry persistedFidEntry) { + private void triggerOnStateReached(PersistedInstallationEntry persistedInstallationEntry) { synchronized (lock) { Iterator it = listeners.iterator(); while (it.hasNext()) { StateListener l = it.next(); - boolean doneListening = l.onStateReached(persistedFidEntry, shouldRefreshAuthToken); + boolean doneListening = + l.onStateReached(persistedInstallationEntry, shouldRefreshAuthToken); if (doneListening) { it.remove(); } @@ -197,12 +198,13 @@ private void triggerOnStateReached(PersistedFidEntry persistedFidEntry) { } } - private void triggerOnException(PersistedFidEntry persistedFidEntry, Exception exception) { + private void triggerOnException( + PersistedInstallationEntry persistedInstallationEntry, Exception exception) { synchronized (lock) { Iterator it = listeners.iterator(); while (it.hasNext()) { StateListener l = it.next(); - boolean doneListening = l.onException(persistedFidEntry, exception); + boolean doneListening = l.onException(persistedInstallationEntry, exception); if (doneListening) { it.remove(); } @@ -212,21 +214,22 @@ private void triggerOnException(PersistedFidEntry persistedFidEntry, Exception e private final void doRegistration() { try { - PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry persistedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); // New FID needs to be created - if (persistedFidEntry.isErrored() || persistedFidEntry.isNotGenerated()) { + if (persistedInstallationEntry.isErrored() || persistedInstallationEntry.isNotGenerated()) { String fid = utils.createRandomFid(); persistFid(fid); - persistedFidEntry = persistedFid.readPersistedFidEntryValue(); + persistedInstallationEntry = persistedInstallation.readPersistedInstallationEntryValue(); } - triggerOnStateReached(persistedFidEntry); + triggerOnStateReached(persistedInstallationEntry); // FID needs to be registered - if (persistedFidEntry.isUnregistered()) { - registerAndSaveFid(persistedFidEntry); - persistedFidEntry = persistedFid.readPersistedFidEntryValue(); + if (persistedInstallationEntry.isUnregistered()) { + registerAndSaveFid(persistedInstallationEntry); + persistedInstallationEntry = persistedInstallation.readPersistedInstallationEntryValue(); // Newly registered Fid will have valid auth token. No refresh required. synchronized (lock) { shouldRefreshAuthToken = false; @@ -236,7 +239,7 @@ private final void doRegistration() { // Don't notify the listeners at this point; we might as well make ure the auth token is up // to date before letting them know. - boolean needRefresh = utils.isAuthTokenExpired(persistedFidEntry); + boolean needRefresh = utils.isAuthTokenExpired(persistedInstallationEntry); if (!needRefresh) { synchronized (lock) { needRefresh = shouldRefreshAuthToken; @@ -245,30 +248,31 @@ private final void doRegistration() { // Refresh Auth token if needed if (needRefresh) { - fetchAuthTokenFromServer(persistedFidEntry); - persistedFidEntry = persistedFid.readPersistedFidEntryValue(); + fetchAuthTokenFromServer(persistedInstallationEntry); + persistedInstallationEntry = persistedInstallation.readPersistedInstallationEntryValue(); synchronized (lock) { shouldRefreshAuthToken = false; } } - triggerOnStateReached(persistedFidEntry); + triggerOnStateReached(persistedInstallationEntry); } catch (Exception e) { - PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue(); - PersistedFidEntry errorFidEntry = - persistedFidEntry + PersistedInstallationEntry persistedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); + PersistedInstallationEntry errorInstallationEntry = + persistedInstallationEntry .toBuilder() .setRegistrationStatus(RegistrationStatus.REGISTER_ERROR) .build(); - persistedFid.insertOrUpdatePersistedFidEntry(errorFidEntry); - triggerOnException(errorFidEntry, e); + persistedInstallation.insertOrUpdatePersistedInstallationEntry(errorInstallationEntry); + triggerOnException(errorInstallationEntry, e); } } private void persistFid(String fid) throws FirebaseInstallationsException { boolean firstUpdateCacheResult = - persistedFid.insertOrUpdatePersistedFidEntry( - PersistedFidEntry.builder() + persistedInstallation.insertOrUpdatePersistedInstallationEntry( + PersistedInstallationEntry.builder() .setFirebaseInstallationId(fid) .setRegistrationStatus(RegistrationStatus.UNREGISTERED) .build()); @@ -281,7 +285,7 @@ private void persistFid(String fid) throws FirebaseInstallationsException { } /** Registers the created Fid with FIS servers and update the shared prefs. */ - private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry) + private Void registerAndSaveFid(PersistedInstallationEntry persistedInstallationEntry) throws FirebaseInstallationsException { try { long creationTime = utils.currentTimeInSecs(); @@ -289,12 +293,12 @@ private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry) InstallationResponse installationResponse = serviceClient.createFirebaseInstallation( /*apiKey= */ firebaseApp.getOptions().getApiKey(), - /*fid= */ persistedFidEntry.getFirebaseInstallationId(), + /*fid= */ persistedInstallationEntry.getFirebaseInstallationId(), /*projectID= */ firebaseApp.getOptions().getProjectId(), /*appId= */ getApplicationId()); - persistedFid.insertOrUpdatePersistedFidEntry( - PersistedFidEntry.builder() - .setFirebaseInstallationId(persistedFidEntry.getFirebaseInstallationId()) + persistedInstallation.insertOrUpdatePersistedInstallationEntry( + PersistedInstallationEntry.builder() + .setFirebaseInstallationId(installationResponse.getFid()) .setRegistrationStatus(RegistrationStatus.REGISTERED) .setAuthToken(installationResponse.getAuthToken().getToken()) .setRefreshToken(installationResponse.getRefreshToken()) @@ -302,7 +306,7 @@ private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry) .setTokenCreationEpochInSecs(creationTime) .build()); - } catch (FirebaseInstallationServiceException exception) { + } catch (FirebaseException exception) { throw new FirebaseInstallationsException( exception.getMessage(), FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); } @@ -310,29 +314,29 @@ private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry) } /** Calls the FIS servers to generate an auth token for this Firebase installation. */ - private InstallationTokenResult fetchAuthTokenFromServer(PersistedFidEntry persistedFidEntry) - throws FirebaseInstallationsException { + private InstallationTokenResult fetchAuthTokenFromServer( + PersistedInstallationEntry persistedInstallationEntry) throws FirebaseInstallationsException { try { long creationTime = utils.currentTimeInSecs(); InstallationTokenResult tokenResult = serviceClient.generateAuthToken( /*apiKey= */ firebaseApp.getOptions().getApiKey(), - /*fid= */ persistedFidEntry.getFirebaseInstallationId(), + /*fid= */ persistedInstallationEntry.getFirebaseInstallationId(), /*projectID= */ firebaseApp.getOptions().getProjectId(), - /*refreshToken= */ persistedFidEntry.getRefreshToken()); + /*refreshToken= */ persistedInstallationEntry.getRefreshToken()); - persistedFid.insertOrUpdatePersistedFidEntry( - PersistedFidEntry.builder() - .setFirebaseInstallationId(persistedFidEntry.getFirebaseInstallationId()) + persistedInstallation.insertOrUpdatePersistedInstallationEntry( + PersistedInstallationEntry.builder() + .setFirebaseInstallationId(persistedInstallationEntry.getFirebaseInstallationId()) .setRegistrationStatus(RegistrationStatus.REGISTERED) .setAuthToken(tokenResult.getToken()) - .setRefreshToken(persistedFidEntry.getRefreshToken()) + .setRefreshToken(persistedInstallationEntry.getRefreshToken()) .setExpiresInSecs(tokenResult.getTokenExpirationTimestamp()) .setTokenCreationEpochInSecs(creationTime) .build()); return tokenResult; - } catch (FirebaseInstallationServiceException exception) { + } catch (FirebaseException exception) { throw new FirebaseInstallationsException( "Failed to generate auth token for a Firebase Installation.", FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); @@ -345,25 +349,26 @@ private InstallationTokenResult fetchAuthTokenFromServer(PersistedFidEntry persi */ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsException { - PersistedFidEntry persistedFidEntry = persistedFid.readPersistedFidEntryValue(); + PersistedInstallationEntry persistedInstallationEntry = + persistedInstallation.readPersistedInstallationEntryValue(); - if (persistedFidEntry.isRegistered()) { + if (persistedInstallationEntry.isRegistered()) { // Call the FIS servers to delete this Firebase Installation Id. try { serviceClient.deleteFirebaseInstallation( firebaseApp.getOptions().getApiKey(), - persistedFidEntry.getFirebaseInstallationId(), + persistedInstallationEntry.getFirebaseInstallationId(), firebaseApp.getOptions().getProjectId(), - persistedFidEntry.getRefreshToken()); + persistedInstallationEntry.getRefreshToken()); - } catch (FirebaseInstallationServiceException exception) { + } catch (FirebaseException exception) { throw new FirebaseInstallationsException( "Failed to delete a Firebase Installation.", FirebaseInstallationsException.Status.SDK_INTERNAL_ERROR); } } - persistedFid.clear(); + persistedInstallation.clear(); return null; } } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/GetAuthTokenListener.java b/firebase-installations/src/main/java/com/google/firebase/installations/GetAuthTokenListener.java index 008daadbe65..d0898aabd5a 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/GetAuthTokenListener.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/GetAuthTokenListener.java @@ -15,7 +15,7 @@ package com.google.firebase.installations; import com.google.android.gms.tasks.TaskCompletionSource; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.installations.local.PersistedInstallationEntry; class GetAuthTokenListener implements StateListener { private final Utils utils; @@ -29,16 +29,16 @@ public GetAuthTokenListener( @Override public boolean onStateReached( - PersistedFidEntry persistedFidEntry, boolean shouldRefreshAuthToken) { + PersistedInstallationEntry persistedInstallationEntry, boolean shouldRefreshAuthToken) { // AuthTokenListener state is reached when FID is registered and has a valid auth token - if (persistedFidEntry.isRegistered() - && !utils.isAuthTokenExpired(persistedFidEntry) + if (persistedInstallationEntry.isRegistered() + && !utils.isAuthTokenExpired(persistedInstallationEntry) && !shouldRefreshAuthToken) { resultTaskCompletionSource.setResult( InstallationTokenResult.builder() - .setToken(persistedFidEntry.getAuthToken()) - .setTokenExpirationTimestamp(persistedFidEntry.getExpiresInSecs()) - .setTokenCreationTimestamp(persistedFidEntry.getTokenCreationEpochInSecs()) + .setToken(persistedInstallationEntry.getAuthToken()) + .setTokenExpirationTimestamp(persistedInstallationEntry.getExpiresInSecs()) + .setTokenCreationTimestamp(persistedInstallationEntry.getTokenCreationEpochInSecs()) .build()); return true; } @@ -46,8 +46,9 @@ public boolean onStateReached( } @Override - public boolean onException(PersistedFidEntry persistedFidEntry, Exception exception) { - if (persistedFidEntry.isErrored()) { + public boolean onException( + PersistedInstallationEntry persistedInstallationEntry, Exception exception) { + if (persistedInstallationEntry.isErrored()) { resultTaskCompletionSource.trySetException(exception); return true; } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java b/firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java index 2e24b97cc07..38134cc6217 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/GetIdListener.java @@ -15,7 +15,7 @@ package com.google.firebase.installations; import com.google.android.gms.tasks.TaskCompletionSource; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.installations.local.PersistedInstallationEntry; class GetIdListener implements StateListener { final TaskCompletionSource taskCompletionSource; @@ -25,17 +25,19 @@ public GetIdListener(TaskCompletionSource taskCompletionSource) { } @Override - public boolean onStateReached(PersistedFidEntry persistedFidEntry, boolean unused) { - if (persistedFidEntry.isUnregistered() || persistedFidEntry.isRegistered()) { - taskCompletionSource.trySetResult(persistedFidEntry.getFirebaseInstallationId()); + public boolean onStateReached( + PersistedInstallationEntry persistedInstallationEntry, boolean unused) { + if (persistedInstallationEntry.isUnregistered() || persistedInstallationEntry.isRegistered()) { + taskCompletionSource.trySetResult(persistedInstallationEntry.getFirebaseInstallationId()); return true; } return false; } @Override - public boolean onException(PersistedFidEntry persistedFidEntry, Exception exception) { - if (persistedFidEntry.isErrored()) { + public boolean onException( + PersistedInstallationEntry persistedInstallationEntry, Exception exception) { + if (persistedInstallationEntry.isErrored()) { taskCompletionSource.trySetException(exception); return true; } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/StateListener.java b/firebase-installations/src/main/java/com/google/firebase/installations/StateListener.java index a9691f63d18..d9570835adf 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/StateListener.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/StateListener.java @@ -14,18 +14,19 @@ package com.google.firebase.installations; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.installations.local.PersistedInstallationEntry; interface StateListener { /** - * Returns {@code true} if the defined {@link PersistedFidEntry} state is reached, {@code false} - * otherwise. + * Returns {@code true} if the defined {@link PersistedInstallationEntry} state is reached, {@code + * false} otherwise. */ - boolean onStateReached(PersistedFidEntry persistedFidEntry, boolean shouldRefreshAuthToken); + boolean onStateReached( + PersistedInstallationEntry persistedInstallationEntry, boolean shouldRefreshAuthToken); /** * Returns {@code true} if an exception is thrown while registering a Firebase Installation, * {@code false} otherwise. */ - boolean onException(PersistedFidEntry persistedFidEntry, Exception exception); + boolean onException(PersistedInstallationEntry persistedInstallationEntry, Exception exception); } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/Utils.java b/firebase-installations/src/main/java/com/google/firebase/installations/Utils.java index 1a3477e9894..03fd022befd 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/Utils.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/Utils.java @@ -16,7 +16,7 @@ import androidx.annotation.NonNull; import com.google.android.gms.common.util.Clock; -import com.google.firebase.installations.local.PersistedFidEntry; +import com.google.firebase.installations.local.PersistedInstallationEntry; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.UUID; @@ -52,8 +52,9 @@ class Utils { * Checks if the FIS Auth token is expired or going to expire in next 1 hour {@link * #AUTH_TOKEN_EXPIRATION_BUFFER_IN_SECS}. */ - public boolean isAuthTokenExpired(PersistedFidEntry persistedFidEntry) { - return persistedFidEntry.getTokenCreationEpochInSecs() + persistedFidEntry.getExpiresInSecs() + public boolean isAuthTokenExpired(PersistedInstallationEntry persistedInstallationEntry) { + return persistedInstallationEntry.getTokenCreationEpochInSecs() + + persistedInstallationEntry.getExpiresInSecs() > currentTimeInSecs() + AUTH_TOKEN_EXPIRATION_BUFFER_IN_SECS; } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedFid.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallation.java similarity index 79% rename from firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedFid.java rename to firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallation.java index 808b277c467..aa47c16d8b1 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedFid.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallation.java @@ -26,36 +26,36 @@ * A layer that locally persists a few Firebase Installation attributes on top the Firebase * Installation API. */ -public class PersistedFid { +public class PersistedInstallation { // Registration Status of each persisted fid entry // NOTE: never change the ordinal of the enum values because the enum values are stored in shared // prefs as their ordinal numbers. public enum RegistrationStatus { /** - * {@link PersistedFidEntry} default registration status. Next state: UNREGISTERED - A new FID - * is created and persisted locally before registering with FIS servers. + * {@link PersistedInstallationEntry} default registration status. Next state: UNREGISTERED - A + * new FID is created and persisted locally before registering with FIS servers. */ NOT_GENERATED, /** - * {@link PersistedFidEntry} is not synced with FIS servers. Next state: REGISTERED - If FID - * registration is successful. REGISTER_ERROR - If FID registration or refresh auth token + * {@link PersistedInstallationEntry} is not synced with FIS servers. Next state: REGISTERED - + * If FID registration is successful. REGISTER_ERROR - If FID registration or refresh auth token * failed. */ UNREGISTERED, /** - * {@link PersistedFidEntry} is synced to FIS servers. Next state: REGISTER_ERROR - If FID - * registration or refresh auth token failed. + * {@link PersistedInstallationEntry} is synced to FIS servers. Next state: REGISTER_ERROR - If + * FID registration or refresh auth token failed. */ REGISTERED, /** - * {@link PersistedFidEntry} is in error state when an exception is thrown while syncing with - * FIS server. Next state: UNREGISTERED - A new FID is created and persisted locally before - * registering with FIS servers. + * {@link PersistedInstallationEntry} is in error state when an exception is thrown while + * syncing with FIS server. Next state: UNREGISTERED - A new FID is created and persisted + * locally before registering with FIS servers. */ REGISTER_ERROR, } - private static final String SHARED_PREFS_NAME = "PersistedFid"; + private static final String SHARED_PREFS_NAME = "PersistedInstallation"; private static final String FIREBASE_INSTALLATION_ID_KEY = "Fid"; private static final String AUTH_TOKEN_KEY = "AuthToken"; @@ -78,7 +78,7 @@ public enum RegistrationStatus { private final String persistenceKey; - public PersistedFid(@NonNull FirebaseApp firebaseApp) { + public PersistedInstallation(@NonNull FirebaseApp firebaseApp) { // Different FirebaseApp in the same Android application should have the same application // context and same dir path prefs = @@ -89,7 +89,7 @@ public PersistedFid(@NonNull FirebaseApp firebaseApp) { } @NonNull - public PersistedFidEntry readPersistedFidEntryValue() { + public PersistedInstallationEntry readPersistedInstallationEntryValue() { synchronized (prefs) { String fid = prefs.getString(getSharedPreferencesKey(FIREBASE_INSTALLATION_ID_KEY), null); int status = prefs.getInt(getSharedPreferencesKey(PERSISTED_STATUS_KEY), -1); @@ -100,9 +100,9 @@ public PersistedFidEntry readPersistedFidEntryValue() { long expiresIn = prefs.getLong(getSharedPreferencesKey(EXPIRES_IN_SECONDS_KEY), 0); if (fid == null || !(status >= 0 && status < RegistrationStatus.values().length)) { - return PersistedFidEntry.builder().build(); + return PersistedInstallationEntry.builder().build(); } - return PersistedFidEntry.builder() + return PersistedInstallationEntry.builder() .setFirebaseInstallationId(fid) .setRegistrationStatus(RegistrationStatus.values()[status]) .setAuthToken(authToken) @@ -114,7 +114,8 @@ public PersistedFidEntry readPersistedFidEntryValue() { } @NonNull - public boolean insertOrUpdatePersistedFidEntry(@NonNull PersistedFidEntry entryValue) { + public boolean insertOrUpdatePersistedInstallationEntry( + @NonNull PersistedInstallationEntry entryValue) { synchronized (prefs) { SharedPreferences.Editor editor = prefs.edit(); editor.putString( diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedFidEntry.java b/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallationEntry.java similarity index 63% rename from firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedFidEntry.java rename to firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallationEntry.java index 4019967cdb6..e7d66ae914f 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedFidEntry.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/local/PersistedInstallationEntry.java @@ -19,17 +19,17 @@ import com.google.auto.value.AutoValue; /** - * This class represents a persisted fid entry in {@link PersistedFid}, which contains a few - * Firebase Installation attributes and the persisted status of this entry. + * This class represents a persisted fid entry in {@link PersistedInstallation}, which contains a + * few Firebase Installation attributes and the persisted status of this entry. */ @AutoValue -public abstract class PersistedFidEntry { +public abstract class PersistedInstallationEntry { @Nullable public abstract String getFirebaseInstallationId(); @NonNull - public abstract PersistedFid.RegistrationStatus getRegistrationStatus(); + public abstract PersistedInstallation.RegistrationStatus getRegistrationStatus(); @Nullable public abstract String getAuthToken(); @@ -42,30 +42,30 @@ public abstract class PersistedFidEntry { public abstract long getTokenCreationEpochInSecs(); public boolean isRegistered() { - return getRegistrationStatus() == PersistedFid.RegistrationStatus.REGISTERED; + return getRegistrationStatus() == PersistedInstallation.RegistrationStatus.REGISTERED; } public boolean isErrored() { - return getRegistrationStatus() == PersistedFid.RegistrationStatus.REGISTER_ERROR; + return getRegistrationStatus() == PersistedInstallation.RegistrationStatus.REGISTER_ERROR; } public boolean isUnregistered() { - return getRegistrationStatus() == PersistedFid.RegistrationStatus.UNREGISTERED; + return getRegistrationStatus() == PersistedInstallation.RegistrationStatus.UNREGISTERED; } public boolean isNotGenerated() { - return getRegistrationStatus() == PersistedFid.RegistrationStatus.NOT_GENERATED; + return getRegistrationStatus() == PersistedInstallation.RegistrationStatus.NOT_GENERATED; } @NonNull public abstract Builder toBuilder(); - /** Returns a default Builder object to create an PersistedFidEntry object */ + /** Returns a default Builder object to create an PersistedInstallationEntry object */ @NonNull - public static PersistedFidEntry.Builder builder() { - return new AutoValue_PersistedFidEntry.Builder() + public static PersistedInstallationEntry.Builder builder() { + return new AutoValue_PersistedInstallationEntry.Builder() .setTokenCreationEpochInSecs(0) - .setRegistrationStatus(PersistedFid.RegistrationStatus.NOT_GENERATED) + .setRegistrationStatus(PersistedInstallation.RegistrationStatus.NOT_GENERATED) .setExpiresInSecs(0); } @@ -75,7 +75,8 @@ public abstract static class Builder { public abstract Builder setFirebaseInstallationId(@NonNull String value); @NonNull - public abstract Builder setRegistrationStatus(@NonNull PersistedFid.RegistrationStatus value); + public abstract Builder setRegistrationStatus( + @NonNull PersistedInstallation.RegistrationStatus value); @NonNull public abstract Builder setAuthToken(@Nullable String value); @@ -90,6 +91,6 @@ public abstract static class Builder { public abstract Builder setTokenCreationEpochInSecs(long value); @NonNull - public abstract PersistedFidEntry build(); + public abstract PersistedInstallationEntry build(); } } diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java b/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java index ddb43292471..6a1bafba6f5 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceClient.java @@ -25,7 +25,9 @@ import com.google.android.gms.common.util.AndroidUtilsLight; import com.google.android.gms.common.util.Hex; import com.google.android.gms.common.util.VisibleForTesting; +import com.google.firebase.FirebaseException; import com.google.firebase.installations.InstallationTokenResult; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; @@ -53,10 +55,8 @@ public class FirebaseInstallationServiceClient { private static final String CONTENT_ENCODING_HEADER_KEY = "Content-Encoding"; private static final String GZIP_CONTENT_ENCODING = "gzip"; - private static final String UNAUTHORIZED_ERROR_MESSAGE = - "The request did not have the required credentials."; private static final String INTERNAL_SERVER_ERROR_MESSAGE = "There was an internal server error."; - private static final String NETWORK_ERROR_MESSAGE = "The server returned an unexpected error:"; + private static final String NETWORK_ERROR_MESSAGE = "The server returned an unexpected error: %s"; private static final String X_ANDROID_PACKAGE_HEADER_KEY = "X-Android-Package"; private static final String X_ANDROID_CERT_HEADER_KEY = "X-Android-Cert"; @@ -65,6 +65,9 @@ public class FirebaseInstallationServiceClient { private static final Pattern EXPIRATION_TIMESTAMP_PATTERN = Pattern.compile("[0-9]+s"); + private static final int MAX_RETRIES = 1; + private static final Charset UTF_8 = Charset.forName("UTF-8"); + @VisibleForTesting static final String PARSING_EXPIRATION_TIME_ERROR_MESSAGE = "Invalid Expiration Timestamp."; @@ -86,9 +89,10 @@ public FirebaseInstallationServiceClient(@NonNull Context context) { @NonNull public InstallationResponse createFirebaseInstallation( @NonNull String apiKey, @NonNull String fid, @NonNull String projectID, @NonNull String appId) - throws FirebaseInstallationServiceException { + throws FirebaseException { String resourceName = String.format(CREATE_REQUEST_RESOURCE_NAME_FORMAT, projectID); try { + int retryCount = 0; URL url = new URL( String.format( @@ -97,46 +101,36 @@ public InstallationResponse createFirebaseInstallation( FIREBASE_INSTALLATIONS_API_VERSION, resourceName, apiKey)); + while (retryCount <= MAX_RETRIES) { + HttpsURLConnection httpsURLConnection = openHttpsURLConnection(url); + httpsURLConnection.setRequestMethod("POST"); + httpsURLConnection.setDoOutput(true); - HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); - httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); - httpsURLConnection.setDoOutput(true); - httpsURLConnection.setRequestMethod("POST"); - httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); - httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); - httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); - httpsURLConnection.addRequestProperty(X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName()); - httpsURLConnection.addRequestProperty( - X_ANDROID_CERT_HEADER_KEY, getFingerprintHashForPackage()); - - GZIPOutputStream gzipOutputStream = - new GZIPOutputStream(httpsURLConnection.getOutputStream()); - try { - gzipOutputStream.write( - buildCreateFirebaseInstallationRequestBody(fid, appId).toString().getBytes("UTF-8")); - } catch (JSONException e) { - throw new IllegalStateException(e); - } finally { - gzipOutputStream.close(); - } + GZIPOutputStream gzipOutputStream = + new GZIPOutputStream(httpsURLConnection.getOutputStream()); + try { + gzipOutputStream.write( + buildCreateFirebaseInstallationRequestBody(fid, appId).toString().getBytes("UTF-8")); + } catch (JSONException e) { + throw new IllegalStateException(e); + } finally { + gzipOutputStream.close(); + } - int httpResponseCode = httpsURLConnection.getResponseCode(); - switch (httpResponseCode) { - case 200: - return readCreateResponse(httpsURLConnection); - case 401: - throw new FirebaseInstallationServiceException( - UNAUTHORIZED_ERROR_MESSAGE, FirebaseInstallationServiceException.Status.UNAUTHORIZED); - default: - throw new FirebaseInstallationServiceException( - INTERNAL_SERVER_ERROR_MESSAGE, - FirebaseInstallationServiceException.Status.SERVER_ERROR); + int httpResponseCode = httpsURLConnection.getResponseCode(); + switch (httpResponseCode) { + case 200: + return readCreateResponse(httpsURLConnection); + case 500: + retryCount++; + break; + default: + throw new FirebaseException(readErrorResponse(httpsURLConnection)); + } } + throw new FirebaseException(INTERNAL_SERVER_ERROR_MESSAGE); } catch (IOException e) { - throw new FirebaseInstallationServiceException( - NETWORK_ERROR_MESSAGE + e.getMessage(), - FirebaseInstallationServiceException.Status.NETWORK_ERROR); + throw new FirebaseException(String.format(NETWORK_ERROR_MESSAGE, e.getMessage())); } } @@ -163,7 +157,7 @@ public void deleteFirebaseInstallation( @NonNull String fid, @NonNull String projectID, @NonNull String refreshToken) - throws FirebaseInstallationServiceException { + throws FirebaseException { String resourceName = String.format(DELETE_REQUEST_RESOURCE_NAME_FORMAT, projectID, fid); try { URL url = @@ -175,31 +169,19 @@ public void deleteFirebaseInstallation( resourceName, apiKey)); - HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); - httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); - httpsURLConnection.setDoOutput(true); + HttpsURLConnection httpsURLConnection = openHttpsURLConnection(url); httpsURLConnection.setRequestMethod("DELETE"); httpsURLConnection.addRequestProperty("Authorization", "FIS_v2 " + refreshToken); - httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); - httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); int httpResponseCode = httpsURLConnection.getResponseCode(); switch (httpResponseCode) { case 200: return; - case 401: - throw new FirebaseInstallationServiceException( - UNAUTHORIZED_ERROR_MESSAGE, FirebaseInstallationServiceException.Status.UNAUTHORIZED); default: - throw new FirebaseInstallationServiceException( - INTERNAL_SERVER_ERROR_MESSAGE, - FirebaseInstallationServiceException.Status.SERVER_ERROR); + throw new FirebaseException(readErrorResponse(httpsURLConnection)); } } catch (IOException e) { - throw new FirebaseInstallationServiceException( - NETWORK_ERROR_MESSAGE + e.getMessage(), - FirebaseInstallationServiceException.Status.NETWORK_ERROR); + throw new FirebaseException(String.format(NETWORK_ERROR_MESSAGE, e.getMessage())); } } @@ -218,10 +200,11 @@ public InstallationTokenResult generateAuthToken( @NonNull String fid, @NonNull String projectID, @NonNull String refreshToken) - throws FirebaseInstallationServiceException { + throws FirebaseException { String resourceName = String.format(GENERATE_AUTH_TOKEN_REQUEST_RESOURCE_NAME_FORMAT, projectID, fid); try { + int retryCount = 0; URL url = new URL( String.format( @@ -230,46 +213,53 @@ public InstallationTokenResult generateAuthToken( FIREBASE_INSTALLATIONS_API_VERSION, resourceName, apiKey)); + while (retryCount <= MAX_RETRIES) { + HttpsURLConnection httpsURLConnection = openHttpsURLConnection(url); + httpsURLConnection.setRequestMethod("POST"); + httpsURLConnection.addRequestProperty("Authorization", "FIS_v2 " + refreshToken); - HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); - httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); - httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); - httpsURLConnection.setDoOutput(true); - httpsURLConnection.setRequestMethod("POST"); - httpsURLConnection.addRequestProperty("Authorization", "FIS_v2 " + refreshToken); - httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); - httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); - httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); - - int httpResponseCode = httpsURLConnection.getResponseCode(); - switch (httpResponseCode) { - case 200: - return readGenerateAuthTokenResponse(httpsURLConnection); - case 401: - throw new FirebaseInstallationServiceException( - UNAUTHORIZED_ERROR_MESSAGE, FirebaseInstallationServiceException.Status.UNAUTHORIZED); - default: - throw new FirebaseInstallationServiceException( - INTERNAL_SERVER_ERROR_MESSAGE, - FirebaseInstallationServiceException.Status.SERVER_ERROR); + int httpResponseCode = httpsURLConnection.getResponseCode(); + switch (httpResponseCode) { + case 200: + return readGenerateAuthTokenResponse(httpsURLConnection); + case 500: + retryCount++; + break; + default: + throw new FirebaseException(readErrorResponse(httpsURLConnection)); + } } + throw new FirebaseException(INTERNAL_SERVER_ERROR_MESSAGE); } catch (IOException e) { - throw new FirebaseInstallationServiceException( - NETWORK_ERROR_MESSAGE + e.getMessage(), - FirebaseInstallationServiceException.Status.NETWORK_ERROR); + throw new FirebaseException(String.format(NETWORK_ERROR_MESSAGE, e.getMessage())); } } + + private HttpsURLConnection openHttpsURLConnection(URL url) throws IOException { + HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); + httpsURLConnection.setConnectTimeout(NETWORK_TIMEOUT_MILLIS); + httpsURLConnection.setReadTimeout(NETWORK_TIMEOUT_MILLIS); + httpsURLConnection.addRequestProperty(CONTENT_TYPE_HEADER_KEY, JSON_CONTENT_TYPE); + httpsURLConnection.addRequestProperty(ACCEPT_HEADER_KEY, JSON_CONTENT_TYPE); + httpsURLConnection.addRequestProperty(CONTENT_ENCODING_HEADER_KEY, GZIP_CONTENT_ENCODING); + httpsURLConnection.addRequestProperty(X_ANDROID_PACKAGE_HEADER_KEY, context.getPackageName()); + httpsURLConnection.addRequestProperty( + X_ANDROID_CERT_HEADER_KEY, getFingerprintHashForPackage()); + return httpsURLConnection; + } + // Read the response from the createFirebaseInstallation API. private InstallationResponse readCreateResponse(HttpsURLConnection conn) throws IOException { - JsonReader reader = - new JsonReader(new InputStreamReader(conn.getInputStream(), Charset.defaultCharset())); + JsonReader reader = new JsonReader(new InputStreamReader(conn.getInputStream(), UTF_8)); InstallationTokenResult.Builder installationTokenResult = InstallationTokenResult.builder(); InstallationResponse.Builder builder = InstallationResponse.builder(); reader.beginObject(); while (reader.hasNext()) { String name = reader.nextName(); if (name.equals("name")) { - builder.setName(reader.nextString()); + builder.setUri(reader.nextString()); + } else if (name.equals("fid")) { + builder.setFid(reader.nextString()); } else if (name.equals("refreshToken")) { builder.setRefreshToken(reader.nextString()); } else if (name.equals("authToken")) { @@ -299,8 +289,7 @@ private InstallationResponse readCreateResponse(HttpsURLConnection conn) throws // Read the response from the generateAuthToken FirebaseInstallation API. private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection conn) throws IOException { - JsonReader reader = - new JsonReader(new InputStreamReader(conn.getInputStream(), Charset.defaultCharset())); + JsonReader reader = new JsonReader(new InputStreamReader(conn.getInputStream(), UTF_8)); InstallationTokenResult.Builder builder = InstallationTokenResult.builder(); reader.beginObject(); while (reader.hasNext()) { @@ -318,6 +307,18 @@ private InstallationTokenResult readGenerateAuthTokenResponse(HttpsURLConnection return builder.build(); } + // Read the error message from the response. + private String readErrorResponse(HttpsURLConnection conn) throws IOException { + BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getErrorStream(), UTF_8)); + StringBuilder response = new StringBuilder(); + for (String input = reader.readLine(); input != null; input = reader.readLine()) { + response.append(input).append('\n'); + } + return String.format( + "The server responded with an error. HTTP response: [%d %s %s]", + conn.getResponseCode(), conn.getResponseMessage(), response); + } + /** Gets the Android package's SHA-1 fingerprint. */ private String getFingerprintHashForPackage() { byte[] hash; diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceException.java b/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceException.java deleted file mode 100644 index b9be3727d4d..00000000000 --- a/firebase-installations/src/main/java/com/google/firebase/installations/remote/FirebaseInstallationServiceException.java +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2019 Google LLC -// -// 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. - -package com.google.firebase.installations.remote; - -import androidx.annotation.NonNull; -import com.google.firebase.FirebaseException; - -/** The class for all Exceptions thrown by {@link FirebaseInstallationServiceClient}. */ -public class FirebaseInstallationServiceException extends FirebaseException { - public enum Status { - SERVER_ERROR, - - NETWORK_ERROR, - - UNAUTHORIZED - } - - @NonNull private final Status status; - - public FirebaseInstallationServiceException(@NonNull Status status) { - this.status = status; - } - - public FirebaseInstallationServiceException(@NonNull String message, @NonNull Status status) { - super(message); - this.status = status; - } - - public FirebaseInstallationServiceException( - @NonNull String message, @NonNull Status status, @NonNull Throwable cause) { - super(message, cause); - this.status = status; - } - - /** - * Gets the status status for the operation that failed. - * - * @return the status for the FirebaseInstallationServiceException - */ - @NonNull - public Status getStatus() { - return status; - } -} diff --git a/firebase-installations/src/main/java/com/google/firebase/installations/remote/InstallationResponse.java b/firebase-installations/src/main/java/com/google/firebase/installations/remote/InstallationResponse.java index 2022a498fe1..c0c04a33be9 100644 --- a/firebase-installations/src/main/java/com/google/firebase/installations/remote/InstallationResponse.java +++ b/firebase-installations/src/main/java/com/google/firebase/installations/remote/InstallationResponse.java @@ -22,7 +22,10 @@ public abstract class InstallationResponse { @NonNull - public abstract String getName(); + public abstract String getUri(); + + @NonNull + public abstract String getFid(); @NonNull public abstract String getRefreshToken(); @@ -42,7 +45,10 @@ public static InstallationResponse.Builder builder() { @AutoValue.Builder public abstract static class Builder { @NonNull - public abstract Builder setName(@NonNull String value); + public abstract Builder setUri(@NonNull String value); + + @NonNull + public abstract Builder setFid(@NonNull String value); @NonNull public abstract Builder setRefreshToken(@NonNull String value);