Skip to content

Commit d911224

Browse files
committed
Add appCheck registration
1 parent a7eec5a commit d911224

5 files changed

Lines changed: 39 additions & 1 deletion

File tree

packages/firebase_ai/firebase_ai/lib/src/firebase_ai.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class FirebaseAI extends FirebasePluginPlatform {
6868
bool? useLimitedUseAppCheckTokens,
6969
}) {
7070
app ??= Firebase.app();
71+
appCheck ??= app.getService<FirebaseAppCheck>();
7172
var instanceKey = '${app.name}::vertexai::$location';
7273

7374
if (_cachedInstances.containsKey(instanceKey)) {
@@ -100,6 +101,7 @@ class FirebaseAI extends FirebasePluginPlatform {
100101
bool? useLimitedUseAppCheckTokens,
101102
}) {
102103
app ??= Firebase.app();
104+
appCheck ??= app.getService<FirebaseAppCheck>();
103105
var instanceKey = '${app.name}::googleai';
104106

105107
if (_cachedInstances.containsKey(instanceKey)) {

packages/firebase_ai/firebase_ai/test/firebase_vertexai_test.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ void main() {
9696
expect(vertexAIAppCheck.useLimitedUseAppCheckTokens, true);
9797
});
9898

99+
test('Instance creation with auto-injected AppCheck', () {
100+
final vertexAI = FirebaseAI.vertexAI(app: customApp);
101+
102+
expect(vertexAI.app, equals(customApp));
103+
expect(vertexAI.appCheck, equals(customAppCheck));
104+
});
105+
99106
test('generativeModel creation with Grounding tools', () {
100107
final ai = FirebaseAI.googleAI();
101108

packages/firebase_app_check/firebase_app_check/lib/src/firebase_app_check.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ class FirebaseAppCheck extends FirebasePluginPlatform {
4141
/// Returns an instance using a specified [FirebaseApp].
4242
static FirebaseAppCheck instanceFor({required FirebaseApp app}) {
4343
return _firebaseAppCheckInstances.putIfAbsent(app.name, () {
44-
return FirebaseAppCheck._(app: app);
44+
final instance = FirebaseAppCheck._(app: app);
45+
app.registerService<FirebaseAppCheck>(instance);
46+
return instance;
4547
});
4648
}
4749

packages/firebase_core/firebase_core/lib/src/firebase_app.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@ class FirebaseApp {
7171

7272
@override
7373
String toString() => '$FirebaseApp($name)';
74+
75+
static final Map<String, Map<Type, dynamic>> _registries = {};
76+
77+
/// Registers a service instance for this app.
78+
void registerService<T>(T service) {
79+
final registry = _registries.putIfAbsent(name, () => {});
80+
registry[T] = service;
81+
}
82+
83+
/// Returns a registered service instance for this app.
84+
T? getService<T>() {
85+
final registry = _registries[name];
86+
if (registry != null) {
87+
return registry[T] as T?;
88+
}
89+
return null;
90+
}
7491
}

packages/firebase_core/firebase_core/test/firebase_core_test.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ void main() {
6464
mock.app(testAppName),
6565
]);
6666
});
67+
68+
test('.registerService() and .getService()', () {
69+
FirebaseApp app = Firebase.app(testAppName);
70+
71+
const testService = 'testServiceInstance';
72+
app.registerService<String>(testService);
73+
74+
expect(app.getService<String>(), testService);
75+
expect(app.getService<int>(), isNull);
76+
});
6777
});
6878

6979
test('.initializeApp() with demoProjectId', () async {

0 commit comments

Comments
 (0)