Skip to content

LibraryVersion reporting #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions firebase-common/firebase-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies {
implementation "com.google.android.gms:play-services-basement:$playServicesVersion"
implementation "com.google.android.gms:play-services-tasks:$playServicesVersion"

api 'com.google.auto.value:auto-value-annotations:1.6'
compileOnly 'com.google.code.findbugs:jsr305:3.0.2'

testImplementation 'com.android.support.test:runner:1.0.2'
Expand All @@ -64,6 +65,8 @@ dependencies {
testImplementation "com.google.truth:truth:$googleTruthVersion"
testImplementation 'org.mockito:mockito-core:2.21.0'

annotationProcessor 'com.google.auto.value:auto-value:1.6'

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation "com.google.truth:truth:$googleTruthVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@
import com.google.firebase.components.InitTracker;
import com.google.firebase.components.TestComponentOne;
import com.google.firebase.components.TestComponentTwo;
import com.google.firebase.components.TestUserAgentDependentComponent;
import com.google.firebase.internal.InternalTokenResult;
import com.google.firebase.platforminfo.UserAgentPublisher;
import com.google.firebase.testing.FirebaseAppRule;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -65,10 +67,8 @@
// TODO(arondeak): uncomment lines when Firebase API targets are in integ.
@RunWith(AndroidJUnit4.class)
public class FirebaseAppTest {

protected static final String GOOGLE_APP_ID = "1:855246033427:android:6e48bff8253f3f6e6e";
protected static final String GOOGLE_API_KEY = "AIzaSyD3asb-2pEZVqMkmL6M9N6nHZRR_znhrh0";
private static final String APP_NAME = "myApp";

protected static final FirebaseOptions OPTIONS =
new FirebaseOptions.Builder()
Expand Down Expand Up @@ -118,6 +118,37 @@ public void testBackgroundStateChangeCallbacks() {
assertThat(backgroundState.get()).isFalse();
}

@Test
public void testInitializeApp_shouldPublishUserAgentPublisherThatReturnsPublishedVersions() {
String[] expectedUserAgent = {"firebase-common/16.0.5", "test-component/1.2.3"};
Context mockContext = createForwardingMockContext();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(mockContext);

TestUserAgentDependentComponent userAgentDependant =
firebaseApp.get(TestUserAgentDependentComponent.class);
UserAgentPublisher userAgentPublisher = userAgentDependant.getUserAgentPublisher();
String[] actualUserAgent = userAgentPublisher.getUserAgent().split(" ");
Arrays.sort(actualUserAgent);

assertThat(actualUserAgent).asList().contains("test-component/1.2.3");
}

@Test
public void testInitializeApp_shouldPublishVersionForFirebaseCommon() {
Context mockContext = createForwardingMockContext();
FirebaseApp firebaseApp = FirebaseApp.initializeApp(mockContext);

TestUserAgentDependentComponent userAgentDependant =
firebaseApp.get(TestUserAgentDependentComponent.class);
UserAgentPublisher userAgentPublisher = userAgentDependant.getUserAgentPublisher();
String[] actualUserAgent = userAgentPublisher.getUserAgent().split(" ");
Arrays.sort(actualUserAgent);

// After sorting the user agents are expected to be {"firebase-common/x.y.z",
// "test-component/1.2.3"}
assertThat(actualUserAgent[0]).contains("firebase-common");
}

@Test
public void testRemovedBackgroundStateChangeCallbacksDontFire() {
FirebaseApp firebaseApp = FirebaseApp.initializeApp(targetContext, OPTIONS, "myApp");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
import com.google.firebase.internal.DefaultIdTokenListenersCountChangedListener;
import com.google.firebase.internal.InternalTokenProvider;
import com.google.firebase.internal.InternalTokenResult;
import com.google.firebase.platforminfo.DefaultUserAgentPublisher;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -151,6 +153,8 @@ public class FirebaseApp {
@GuardedBy("LOCK")
static final Map<String, FirebaseApp> INSTANCES = new ArrayMap<>();

private static final String FIREBASE_COMMON = "firebase-common";

private final Context applicationContext;
private final String name;
private final FirebaseOptions options;
Expand Down Expand Up @@ -536,7 +540,9 @@ protected FirebaseApp(Context applicationContext, String name, FirebaseOptions o
registrars,
Component.of(applicationContext, Context.class),
Component.of(this, FirebaseApp.class),
Component.of(options, FirebaseOptions.class));
Component.of(options, FirebaseOptions.class),
LibraryVersionComponent.create(FIREBASE_COMMON, BuildConfig.VERSION_NAME),
DefaultUserAgentPublisher.component());
publisher = componentRuntime.get(Publisher.class);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2018 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.platforminfo;

import com.google.firebase.components.Component;
import com.google.firebase.components.Dependency;
import java.util.Iterator;
import java.util.Set;

/**
* Provides a user agent string that captures the SDKs and their corresponding versions.
*
* <p>Example user agent string: "firebase-common/16.1.1 firebase-firestore/16.1.2
* firebase-database/16.1.2"
*/
public class DefaultUserAgentPublisher implements UserAgentPublisher {
private final String javaSDKVersionUserAgent;
private final OutOfBandLibraryVersionRegistrar gamesSDKRegistrar;

DefaultUserAgentPublisher(
Set<LibraryVersion> libraryVersions, OutOfBandLibraryVersionRegistrar gamesSDKRegistrar) {
this.javaSDKVersionUserAgent = toUserAgent(libraryVersions);
this.gamesSDKRegistrar = gamesSDKRegistrar;
}

/**
* Returns the user agent string that is computed as follows 1. For our JavaSDKs, the string is
* computed in advance since the components framework guarantees that we receive all published
* versions. 2. For our GamesSDKs, the strings are recomputed each time since the registration of
* the versions happens out of band and we take the optimistic approach of recomputing each time.
*/
@Override
public String getUserAgent() {
if (gamesSDKRegistrar.getRegisteredVersions().isEmpty()) {
return javaSDKVersionUserAgent;
}

return javaSDKVersionUserAgent + ' ' + toUserAgent(gamesSDKRegistrar.getRegisteredVersions());
}

private static String toUserAgent(Set<LibraryVersion> tokens) {
StringBuilder sb = new StringBuilder();
Iterator<LibraryVersion> iterator = tokens.iterator();
while (iterator.hasNext()) {
LibraryVersion token = iterator.next();
sb.append(token.getLibraryName()).append('/').append(token.getVersion());
if (iterator.hasNext()) {
sb.append(' ');
}
}
return sb.toString();
}

/** Creates a component to codify a user agent string that captures SDK versions. */
public static Component<UserAgentPublisher> component() {
return Component.builder(UserAgentPublisher.class)
.add(Dependency.setOf(LibraryVersion.class))
.factory(
c ->
new DefaultUserAgentPublisher(
c.setOf(LibraryVersion.class), OutOfBandLibraryVersionRegistrar.getInstance()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2018 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.platforminfo;

import com.google.auto.value.AutoValue;
import javax.annotation.Nonnull;

/** The class is not public to ensure other components cannot depend on it. */
@AutoValue
abstract class LibraryVersion {
static LibraryVersion create(String name, String version) {
return new AutoValue_LibraryVersion(name, version);
}

@Nonnull
public abstract String getLibraryName();

@Nonnull
public abstract String getVersion();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2018 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.platforminfo;

import com.google.firebase.components.Component;

/** Factory to create a component that publishes the version of an SDK */
public class LibraryVersionComponent {
private LibraryVersionComponent() {}

/** Creates a component that publishes SDK versions */
public static Component<?> create(String sdkName, String version) {
return Component.intoSet(LibraryVersion.create(sdkName, version), LibraryVersion.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2018 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.platforminfo;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* In order to allow the C++ and Unity SDKs to publish their versions without the use of the
* components framework, we have a mechanism where the versions can be wired as out of band as side
* effects. See {@link OutOfBandLibraryVersionRegistrar#registerVersion(String, String)}
*
* <p>Java libraries should use {@link LibraryVersionComponent#create(String, String)} instead.
*/
public class OutOfBandLibraryVersionRegistrar {
private final Set<LibraryVersion> infos = new HashSet<>();
private static volatile OutOfBandLibraryVersionRegistrar INSTANCE;

OutOfBandLibraryVersionRegistrar() {}

/**
* Thread safe method to publish versions outside of the components mechanics.
*
* <p>It is the responsibility of the caller to register the version at app launch.
*/
public void registerVersion(String sdkName, String version) {
synchronized (infos) {
infos.add(LibraryVersion.create(sdkName, version));
}
}

/** Returns registered versions */
Set<LibraryVersion> getRegisteredVersions() {
synchronized (infos) {
return Collections.unmodifiableSet(infos);
}
}

/** Returns an instance of {@link OutOfBandLibraryVersionRegistrar} */
public static OutOfBandLibraryVersionRegistrar getInstance() {
OutOfBandLibraryVersionRegistrar localRef = INSTANCE;
if (localRef == null) {
synchronized (OutOfBandLibraryVersionRegistrar.class) {
localRef = INSTANCE;
if (localRef == null) {
INSTANCE = localRef = new OutOfBandLibraryVersionRegistrar();
}
}
}
return localRef;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2018 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.platforminfo;

/** Component that publishes a user agent string */
public interface UserAgentPublisher {
String getUserAgent();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 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.

/** @hide */
package com.google.firebase.platforminfo;
Loading