From 4c427cf7cc5f20ffd0585cbe87e23a29130112de Mon Sep 17 00:00:00 2001 From: Jan-Erik Rediger Date: Tue, 5 May 2020 16:03:12 +0200 Subject: [PATCH] Remove service-glean's hard-dependency on httpurlconnection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We still test with it, so we keep it around. ⚠️ **BREAKING CHANGE** ⚠️ Users will need to supply a configuration with a ping uploader to Glean now. A default lib-fetch-httpurlconnection implementation can be used like this: ```kotlin import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient; import mozilla.components.service.glean.config.Configuration; import mozilla.components.service.glean.net.ConceptFetchHttpUploader; val config = Configuration(httpClient = ConceptFetchHttpUploader(lazy { HttpURLConnectionClient() })) Glean.initialize(context, true, config) ``` For Java this becomes: ```java import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient; import mozilla.components.service.glean.config.Configuration; import mozilla.components.service.glean.net.ConceptFetchHttpUploader; ConceptFetchHttpUploader httpClient = ConceptFetchHttpUploader.fromClient(new HttpURLConnectionClient()); Configuration config = new Configuration(httpClient); Glean.INSTANCE.initialize(context, true, config); ``` Co-authored-by: Alessio Placitelli --- components/service/glean/build.gradle | 6 ++++-- .../mozilla/components/service/glean/Glean.kt | 3 +-- .../service/glean/config/Configuration.kt | 17 +++++++++-------- .../glean/net/ConceptFetchHttpUploader.kt | 14 +++++++++++++- .../service/glean/GleanFromJavaTest.java | 9 +++++++-- docs/changelog.md | 7 +++++++ 6 files changed, 41 insertions(+), 15 deletions(-) diff --git a/components/service/glean/build.gradle b/components/service/glean/build.gradle index 147ee04c3f2..30f897e7937 100644 --- a/components/service/glean/build.gradle +++ b/components/service/glean/build.gradle @@ -50,10 +50,11 @@ dependencies { api GLEAN_LIBRARY + // So consumers can set a HTTP client. + api project(':concept-fetch') + implementation project(':support-ktx') implementation project(':support-base') - implementation project(':concept-fetch') - implementation project(':lib-fetch-httpurlconnection') implementation project(':support-utils') testImplementation Dependencies.androidx_test_core @@ -65,6 +66,7 @@ dependencies { testImplementation Dependencies.androidx_work_testing testImplementation project(':support-test') + testImplementation project(':lib-fetch-httpurlconnection') testImplementation project(':lib-fetch-okhttp') testImplementation GLEAN_LIBRARY_FORUNITTESTS diff --git a/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt b/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt index 983b44354ef..477a486aefc 100644 --- a/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt +++ b/components/service/glean/src/main/java/mozilla/components/service/glean/Glean.kt @@ -33,12 +33,11 @@ object Glean { * @param uploadEnabled A [Boolean] that determines the initial state of the uploader * @param configuration A Glean [Configuration] object with global settings. */ - @JvmOverloads @MainThread fun initialize( applicationContext: Context, uploadEnabled: Boolean, - configuration: Configuration = Configuration() + configuration: Configuration ) { GleanCore.initialize( applicationContext = applicationContext, diff --git a/components/service/glean/src/main/java/mozilla/components/service/glean/config/Configuration.kt b/components/service/glean/src/main/java/mozilla/components/service/glean/config/Configuration.kt index 21a5fdc4805..5f632f03efd 100644 --- a/components/service/glean/src/main/java/mozilla/components/service/glean/config/Configuration.kt +++ b/components/service/glean/src/main/java/mozilla/components/service/glean/config/Configuration.kt @@ -4,26 +4,27 @@ package mozilla.components.service.glean.config -import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient -import mozilla.components.service.glean.net.ConceptFetchHttpUploader import mozilla.telemetry.glean.net.PingUploader import mozilla.telemetry.glean.config.Configuration as GleanCoreConfiguration /** * The Configuration class describes how to configure the Glean. * - * @property serverEndpoint the server pings are sent to. Please note that this is + * @property httpClient The HTTP client implementation to use for uploading pings. + * If you don't provide your own networking stack with an HTTP client to use, + * you can fall back to a simple implementation on top of `java.net` using + * `ConceptFetchHttpUploader(lazy { HttpURLConnectionClient() as Client })` + * @property serverEndpoint (optional) the server pings are sent to. Please note that this is * is only meant to be changed for tests. - * @property channel the release channel the application is on, if known. This will be + * @property channel (optional )the release channel the application is on, if known. This will be * sent along with all the pings, in the `client_info` section. - * @property maxEvents the number of events to store before the events ping is sent - * @property httpClient The HTTP client implementation to use for uploading pings. + * @property maxEvents (optional) the number of events to store before the events ping is sent */ data class Configuration @JvmOverloads constructor ( + val httpClient: PingUploader, val serverEndpoint: String = DEFAULT_TELEMETRY_ENDPOINT, val channel: String? = null, - val maxEvents: Int? = null, - val httpClient: PingUploader = ConceptFetchHttpUploader(lazy { HttpURLConnectionClient() }) + val maxEvents: Int? = null ) { // The following is required to support calling our API from Java. companion object { diff --git a/components/service/glean/src/main/java/mozilla/components/service/glean/net/ConceptFetchHttpUploader.kt b/components/service/glean/src/main/java/mozilla/components/service/glean/net/ConceptFetchHttpUploader.kt index 01e119e5d66..f59c061ed1e 100644 --- a/components/service/glean/src/main/java/mozilla/components/service/glean/net/ConceptFetchHttpUploader.kt +++ b/components/service/glean/src/main/java/mozilla/components/service/glean/net/ConceptFetchHttpUploader.kt @@ -14,10 +14,12 @@ import mozilla.components.concept.fetch.isClientError import mozilla.components.concept.fetch.isSuccess import mozilla.components.support.base.log.logger.Logger import mozilla.telemetry.glean.net.HeadersList -import mozilla.telemetry.glean.net.PingUploader +import mozilla.telemetry.glean.net.PingUploader as CorePingUploader import java.io.IOException import java.util.concurrent.TimeUnit +typealias PingUploader = CorePingUploader + /** * A simple ping Uploader, which implements a "send once" policy, never * storing or attempting to send the ping again. This uses Android Component's @@ -33,6 +35,16 @@ class ConceptFetchHttpUploader( const val DEFAULT_CONNECTION_TIMEOUT = 10000L // The timeout, in milliseconds, to use when reading from the server. const val DEFAULT_READ_TIMEOUT = 30000L + + /** + * Export a constructor that is usable from Java. + * + * This looses the lazyness of creating the `client`. + */ + @JvmStatic + fun fromClient(client: Client): ConceptFetchHttpUploader { + return ConceptFetchHttpUploader(lazy { client }) + } } /** diff --git a/components/service/glean/src/test/java/mozilla/components/service/glean/GleanFromJavaTest.java b/components/service/glean/src/test/java/mozilla/components/service/glean/GleanFromJavaTest.java index e8d28ef0817..cfd3b2267f7 100644 --- a/components/service/glean/src/test/java/mozilla/components/service/glean/GleanFromJavaTest.java +++ b/components/service/glean/src/test/java/mozilla/components/service/glean/GleanFromJavaTest.java @@ -16,7 +16,9 @@ import java.util.HashMap; import java.util.Map; +import mozilla.components.lib.fetch.httpurlconnection.HttpURLConnectionClient; import mozilla.components.service.glean.config.Configuration; +import mozilla.components.service.glean.net.ConceptFetchHttpUploader; @RunWith(RobolectricTestRunner.class) public class GleanFromJavaTest { @@ -28,15 +30,18 @@ public class GleanFromJavaTest { public void testInitGleanWithDefaults() { Context context = ApplicationProvider.getApplicationContext(); WorkManagerTestInitHelper.initializeTestWorkManager(context); - Glean.INSTANCE.initialize(context, true); + ConceptFetchHttpUploader httpClient = ConceptFetchHttpUploader.fromClient(new HttpURLConnectionClient()); + Configuration config = new Configuration(httpClient); + Glean.INSTANCE.initialize(context, true, config); } @Test public void testInitGleanWithConfiguration() { Context context = ApplicationProvider.getApplicationContext(); WorkManagerTestInitHelper.initializeTestWorkManager(context); + ConceptFetchHttpUploader httpClient = ConceptFetchHttpUploader.fromClient(new HttpURLConnectionClient()); Configuration config = - new Configuration(Configuration.DEFAULT_TELEMETRY_ENDPOINT, "test-channel"); + new Configuration(httpClient, Configuration.DEFAULT_TELEMETRY_ENDPOINT, "test-channel"); Glean.INSTANCE.initialize(context, true, config); } diff --git a/docs/changelog.md b/docs/changelog.md index 7337fc3cf42..e7300d8251a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -27,6 +27,13 @@ permalink: /changelog/ * **browser-tabstray** * Added optional `itemDecoration` DividerItemDecoration parameter to `BrowserTabsTray` constructor to allow the clients to add their own dividers. This is used to ensure setting divider item decoration after setAdapter() is called. +* **service-glean** + * ⚠️ **This is a breaking change**: Glean's configuration now requires explicitly setting an http client. Users need to pass one at construction. + A default `lib-fetch-httpurlconnection` implementation is available. + Add it to the configuration object like this: + `val config = Configuration(httpClient = ConceptFetchHttpUploader(lazy { HttpURLConnectionClient() }))`. + See [PR #6875](https://github.com/mozilla-mobile/android-components/pull/6875) for details and full code examples. + # 40.0.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v39.0.0...v40.0.0)