Repository navigation
Add the plugin registry and PrebidMobile.registerPlugin (plugin system, step 1) - #1054
Conversation
…m, step 1) Android counterpart of prebid/prebid-mobile-ios#1424, following Step 1 of the Android delivery plan (III.14) in "4 - Core Design - Android Implementation". - PrebidPluginInfo, PluginCapability<P> (RENDERER) and PluginRegistry<P> in org.prebid.mobile.api.plugin. - PrebidMobile.registerPlugin / unregisterPlugin / containsPlugin. - PrebidMobilePluginRegister stores renderers in the RENDERER registry. - The renderer registration methods on PrebidMobile and PrebidMobilePluginRegister are deprecated, as on iOS. - SdkInitializer registers PrebidRenderer only when it isn't registered. Behavior change: a renderer whose name is already registered is ignored with a warning instead of replacing the registered one. Renderers are listed in registration order, and the register is thread-safe. Tests that registered a fake PrebidRenderer over one left by another test now unregister it first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| if (onRegister != null) { | ||
| try { | ||
| onRegister.call(plugin); | ||
| } catch (RuntimeException exception) { |
There was a problem hiding this comment.
The rollback only covers RuntimeException. Kotlin has no checked exceptions, so a Kotlin plugin's onRegister can throw e.g. IOException, and a vendor SDK with a missing optional dependency throws NoClassDefFoundError, an Error. In both cases:
- the exception escapes
PrebidMobile.registerPlugin; - the registration stays reserved but never becomes active, so
containsPluginreturnsfalse; - the same identity can never register again ("already registered"), and the dead entry counts against capacity.
Nothing reaches this in this PR, because RENDERER has no callbacks. It becomes reachable in Step 2, when EXTENDED_IDS wires in ExtendedIdProvider::onRegister. The Javadoc already promises the rollback, so please fix it here: catch Exception, and roll back in a finally so an Error also removes the registration before it propagates. If you'd rather defer it, please track it as an explicit Step 2 item.
|
|
||
| try { | ||
| onUnregister.call(entry.plugin); | ||
| } catch (RuntimeException exception) { |
There was a problem hiding this comment.
Same issue: a Kotlin plugin's onUnregister can throw a checked exception, which escapes this catch (RuntimeException). From remove it escapes PrebidMobile.unregisterPlugin. From removeAll it stops the loop, so the remaining plugins never get onUnregister. Please catch Exception here.
| static void registerSdkRenderer() { | ||
| PrebidRenderer renderer = new PrebidRenderer(); | ||
|
|
||
| if (PrebidMobile.containsPlugin(renderer, PluginCapability.RENDERER)) { |
There was a problem hiding this comment.
This check is by name only. A renderer named PrebidRenderer that the app registered before init now silently stays as the default renderer; on master, init replaced it (last-wins). That contradicts spec III.10 and the PR description, which say an app renderer can no longer silently replace the SDK default. Please log a warning when the registered PrebidRenderer isn't the SDK's own PrebidRenderer class, or correct the claim in the description.
Review fixes for the plugin registry: - PluginRegistry.add catches Exception instead of RuntimeException, so a checked exception from Kotlin code is logged and rolled back too. The rollback runs in a finally block, so an Error (e.g. NoClassDefFoundError) also frees the identity and the capacity slot before it propagates. - onUnregister failures are caught as Exception, so a checked exception neither escapes unregisterPlugin nor stops removeAll. - SdkInitializer logs a warning when the renderer registered under the name PrebidRenderer is not the SDK's own class. It stays the default, where on master init replaced it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1054 made registerPlugin() ignore a renderer whose name is already registered. Two tests added by #1050 register a fake under the default renderer's name without unregistering it first, so the real renderer ran and the mock controller was never called. Their cleanup then removed the default renderer, so InterstitialAdUnitTest also failed in a full run. Both fail on master (18e416d). Unregister first, as the tests #1054 updated already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1054 made registerPlugin() ignore a renderer whose name is already registered. Two tests added by #1050 register a fake under the default renderer's name without unregistering it first, so the real renderer ran and the mock controller was never called. Their cleanup then removed the default renderer, so InterstitialAdUnitTest also failed in a full run. Both fail on master (18e416d). Unregister first, as the tests #1054 updated already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
#1054 made registerPlugin() ignore a renderer whose name is already registered. Two tests added by #1050 register a fake under the default renderer's name without unregistering it first, so the real renderer ran and the mock controller was never called. Their cleanup then removed the default renderer, so InterstitialAdUnitTest also failed in a full run. Both fail on master (18e416d). Unregister first, as the tests #1054 updated already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 4432c9c)
… it twice #1054 ignores a plugin renderer whose name is already registered, so the fake Prebid renderer that #1050's tests register was dropped, and InterstitialAdUnitTest and RewardedAdUnitTest failed on master. Unregister it first. Test-only, identical to the same change in #1055 and #1057, so whichever merges first makes the others' copy a no-op. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…line (#1055) * Always complete fetchDemand: report SDK-not-ready, add a request deadline Fixes #1053. - BidLoader.load() no longer returns silently while the SDK is not initialized: it reports the new SDK_NOT_INITIALIZED result (ResultCode and FetchDemandResult), and still schedules auto refresh. - Each bid request gets a deadline (connect + read timeout + 1 s). If it hasn't finished by then, it is cancelled and reported as TIMEOUT. A late result is dropped, so the listener is called once per request. - Redirected connections now get the same connect/read timeouts as the first one; without them they could block forever. - destroy() still cancels without calling the listener (now documented). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix the mediation invalid-ad-object test that passed without running The test passed a non-null delegate, so fetchDemand() never hit the INVALID_AD_OBJECT check. It went on to BidLoader.load(), which used to return silently while the SDK wasn't initialized, so the assertion in the listener never ran. Now that load() reports SDK_NOT_INITIALIZED, the listener runs and the test failed. Pass a null delegate as the test intends, and verify the listener is called. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Replace the default renderer in two #1050 tests broken by #1054 #1054 made registerPlugin() ignore a renderer whose name is already registered. Two tests added by #1050 register a fake under the default renderer's name without unregistering it first, so the real renderer ran and the mock controller was never called. Their cleanup then removed the default renderer, so InterstitialAdUnitTest also failed in a full run. Both fail on master (18e416d). Unregister first, as the tests #1054 updated already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Android counterpart of prebid/prebid-mobile-ios#1424, part of prebid/prebid-mobile-ios#461. This is Step 1 of the Android delivery plan (III.14) in 4 - Core Design - Android Implementation: the registry core, the unified registration functions, and renderers moved onto them. It branches from master and doesn't depend on #1024 (Step 2).
Section numbers below (III.x) refer to that doc.
What it adds
PrebidPluginInfo(public,org.prebid.mobile.api.plugin): plugin identity, a name and a version with value equality (III.5).PluginCapability<P>(public): what a plugin registers as. Only the SDK defines capabilities; this PR addsRENDERERonly (III.3).PrebidMobile.registerPlugin(plugin, capability),unregisterPlugin(plugin, capability)andcontainsPlugin(plugin, capability)(III.3, III.12). Java infersPfrom the capability, so a plugin of the wrong type doesn't compile, and there is no runtime cast. iOS checks the type at runtime instead.PluginRegistry<P>, a public class marked@RestrictTo(LIBRARY_GROUP)(III.4). A registry keeps registration order, ignores a second plugin with the same identity (the first one stays), supports a capacity, and calls the plugin's callbacks outside its lock. A plugin is active once itsonRegisterreturns. IfonRegisterthrows an exception, the registration is rolled back and the exception is logged. This includes checked exceptions, which Kotlin code can throw without declaring them. IfonRegisterthrows anError, such asNoClassDefFoundErrorfrom a missing optional dependency, the registration is rolled back too, and theErrorpropagates. An exception fromonUnregisteris logged and not propagated, and it doesn't stopremoveAll. A process-wide sequence orders plugins across capabilities.PrebidMobilePluginRegisterstores renderers inPluginCapability.RENDERER.getRegistry()instead of its ownHashMap(III.10). Renderers are still identified by name only. Selection (name +isSupportRenderingFor+ exact version, elsePrebidRenderer) and the event listener calls are unchanged.Where this differs from the doc, and why
PrebidMobile.registerPluginRenderer,unregisterPluginRendererandcontainsPluginRenderer, plusPrebidMobilePluginRegister.registerPlugin,unregisterPluginandcontainsPlugin(PrebidMobilePluginRenderer). On Android,containsPluginRendererandcontainsPlugin(…, RENDERER)both match by name, so the deprecation message needs no note about it. On iOS, the old method matched the instance.SdkInitializerregistered a newPrebidRendereron everyinit. Now that duplicates are ignored, a secondinitafter a failed one would log the duplicate warning. This is the same fix the iOS review asked for. If the renderer already registered under that name isn't the SDK's ownPrebidRendererclass,initkeeps it as the default renderer and logs a warning.PrebidPluginInfo.toString()(III.5). It is the name alone when the version is empty, so renderer log lines don't end in a slash ([renderer] Plugin VendorRenderer/ is not registered). Same as iOS.PluginCapability.getName()andtoString(). They read the name from the capability's registry, so a capability's name and the[name]prefix in its logs can't differ. Same as iOS; the doc's capability has no name.PluginRegistry.Entryhas getters instead of public fields (III.4), following thegetX()style of III.1. The registry also hasremoveAll(), which the@VisibleForTestingmethodPrebidMobilePluginRegister.unregisterAllPlugins()needs. It calls everyonUnregisterand keeps going if one of them throws.PrebidLoggeris app code. Registering and unregistering are logged at debug level.Kept as the doc asks:
removecallsonUnregisterright away, even whileonRegisteris still running. The iOS review dropped the deferred-unregister states for the same model.PrebidMobilemethods: register during SDK setup; unregistering is best-effort; registering or unregistering the same plugin concurrently, or from inside its own callbacks, is not supported.java.util.function,Optionalorjava.timein the API (III.6). The registry declares its ownIdentifyandLifecycleinterfaces.Behavior changes
init, a publisher renderer namedPrebidRenderercan no longer replace the SDK default. One registered beforeinitnow stays the default renderer, andinitlogs a warning; on master,initreplaced it. The doc (III.13) asks for this to be in the release notes.getRTBListOfRenderersForandext.prebid.sdk.rendererslist renderers in registration order. On master they followedHashMaporder.HashMap.With no plugin registered, the bid request is unchanged.
Doc questions (III.15)
@RestrictTovs. package placement: uses@RestrictTo(LIBRARY_GROUP), as the doc proposes.PrebidRendereris still registered first inSdkInitializer.init'stryblock, so start-up order doesn't change.identifyrun outside the lock. Renderers have no lifecycle callbacks.registerPlugin(plugin, capability), which matches the iOS Objective-C selectorregisterPlugin:as:. Nothing is released yet, so renaming is cheap.Not in this PR
EXTENDED_IDS, and droppingPrebidMobile.*ExtendedIdProvider).Tests
PluginRegistryTest(23) andPrebidPluginInfoTest(3) cover the registry: order, identity, capacity, callbacks that call back into the registry, and the sequence across registries. They also cover:onRegisterthrows an unchecked exception, a checked exception or anError, and that a failed registration doesn't take up capacity;onUnregisterthat throws an unchecked or a checked exception, fromremoveand fromremoveAll;PluginRegistrationTest(14) coversregisterPluginwithRENDERER: the duplicate-name warning, replacing a renderer by unregistering it first, unregistering by name, the deprecated methods going through the capability, registration order ingetRTBListOfRenderersForand in theext.prebid.sdk.renderersJSON, default and fallback renderer selection, event listeners, a renderer with anullname, andunregisterAllPlugins.SdkInitializerTest: a secondregisterSdkRenderer()keeps the first renderer and logs no warning. An app renderer registered under the namePrebidRendererstays the default, andregisterSdkRenderer()logs a warning naming its class.PrebidRendererover one that an earlier test class had left registered. They relied on replace-on-duplicate, so, as III.14 asks, their helpers now unregister the same name first:DisplayViewTest,MediationBannerViewTest,PluginRendererFactoryTest,InterstitialAdUnitTest,RewardedAdUnitTest, and the AdMob and MAX interstitial and rewarded tests.PrebidMobilePluginRegisterTestpasses unchanged.PrebidMobile.registerPlugin(…, PluginCapability.RENDERER).Results (local, JDK 21; CI uses JDK 17):
:PrebidMobile-core:testReleaseUnitTest: 1533 tests, 0 failures:PrebidMobile-admobAdapters:testReleaseUnitTest: 54 tests, 0 failures:PrebidMobile-maxAdapters:testReleaseUnitTest: 122 tests, 0 failuresPrebidInternalTestApp,PrebidDemoJavaandPrebidDemoKotlincompile.🤖 Generated with Claude Code