-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
firebase_core_web.dart
336 lines (288 loc) 路 10.6 KB
/
firebase_core_web.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// ignore_for_file: unsafe_html
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
part of firebase_core_web;
/// Defines a Firebase service by name.
class FirebaseWebService {
/// The name which matches the Firebase JS Web SDK postfix.
String name;
/// Naming of Firebase web products is different from Flutterfire plugins. This
/// property allows overriding of web naming to Flutterfire plugin naming.
String? override;
/// Function to call to ensure the Firebase Service is initalized.
/// Usually used to ensure that the Web SDK match the behavior
/// of native SDKs.
EnsurePluginInitialized ensurePluginInitialized;
/// Creates a new [FirebaseWebService].
FirebaseWebService._(
this.name, {
this.override,
this.ensurePluginInitialized,
});
}
typedef EnsurePluginInitialized = Future<void> Function()?;
/// The entry point for accessing Firebase.
///
/// You can get an instance by calling [FirebaseCore.instance].
class FirebaseCoreWeb extends FirebasePlatform {
static Map<String, FirebaseWebService> _services = {
'core': FirebaseWebService._('app', override: 'core'),
'app-check': FirebaseWebService._('app-check', override: 'app_check'),
'remote-config':
FirebaseWebService._('remote-config', override: 'remote_config'),
};
/// Internally registers a Firebase Service to be initialized.
static void registerService(
String service, [
EnsurePluginInitialized? ensurePluginInitialized,
]) {
_services.putIfAbsent(
service,
() => FirebaseWebService._(
service,
ensurePluginInitialized: ensurePluginInitialized,
),
);
}
/// Registers that [FirebaseCoreWeb] is the platform implementation.
static void registerWith(Registrar registrar) {
FirebasePlatform.instance = FirebaseCoreWeb();
}
/// Returns the Firebase JS SDK Version to use.
///
/// You can override the supported version by attaching a version string to
/// the window (window.flutterfire_web_sdk_version = 'x.x.x'). Do so at your
/// own risk as the version might be unsupported or untested against.
String get _firebaseSDKVersion {
return context['flutterfire_web_sdk_version'] ??
supportedFirebaseJsSdkVersion;
}
/// Returns a list of services which won't be automatically injected on
/// initilization. This is useful incases where you wish to manually include
/// the scripts (e.g. in countries where you must request the users permission
/// to include Analytics).
///
/// You can do this by attaching an array of services to the window, e.g:
///
/// window.flutterfire_ignore_scripts = ['analytics'];
///
/// You must ensure the Firebase script is injected before using the service.
List<String> get _ignoredServiceScripts {
try {
JsObject ignored =
JsObject.fromBrowserObject(context['flutterfire_ignore_scripts']);
if (ignored is Iterable) {
return (ignored as Iterable)
.map((e) => e.toString())
.toList(growable: false);
}
} catch (e) {
// Noop
}
return [];
}
/// Injects a `script` with a `src` dynamically into the head of the current
/// document.
Future<void> _injectSrcScript(String src, String windowVar) async {
ScriptElement script = ScriptElement();
script.type = 'text/javascript';
script.crossOrigin = 'anonymous';
script.text = '''
window.ff_trigger_$windowVar = async (callback) => {
callback(await import("$src"));
};
''';
assert(document.head != null);
document.head!.append(script);
Completer completer = Completer();
context.callMethod('ff_trigger_$windowVar', [
(module) {
context[windowVar] = module;
context.deleteProperty('ff_trigger_$windowVar');
completer.complete();
}
]);
await completer.future;
}
/// Initializes the Firebase JS SDKs by injecting them into the `head` of the
/// document when Firebase is initialized.
Future<void> _initializeCore() async {
// If Firebase is already available, core has already been initialized
// (or the user has added the scripts to their html file).
if (context['firebase_core'] != null) {
return;
}
String version = _firebaseSDKVersion;
List<String> ignored = _ignoredServiceScripts;
await Future.wait(
_services.values.map((service) {
if (ignored.contains(service.override ?? service.name)) {
return Future.value();
}
return _injectSrcScript(
'https://www.gstatic.com/firebasejs/$version/firebase-${service.name}.js',
'firebase_${service.override ?? service.name}',
);
}),
);
}
/// Returns all created [FirebaseAppPlatform] instances.
@override
List<FirebaseAppPlatform> get apps {
return guardNotInitialized(
() => firebase.apps.map(_createFromJsApp).toList(growable: false),
);
}
/// Initializes a new [FirebaseAppPlatform] instance by [name] and [options] and returns
/// the created app. This method should be called before any usage of FlutterFire plugins.
///
/// The default app instance cannot be initialized here and should be created
/// using the platform Firebase integration.
@override
Future<FirebaseAppPlatform> initializeApp({
String? name,
FirebaseOptions? options,
}) async {
await _initializeCore();
guardNotInitialized(() => firebase.SDK_VERSION);
assert(
() {
if (firebase.SDK_VERSION != supportedFirebaseJsSdkVersion) {
// ignore: avoid_print
print(
'''
WARNING: FlutterFire for Web is explicitly tested against Firebase JS SDK version "$supportedFirebaseJsSdkVersion"
but your currently specifying "${firebase.SDK_VERSION}" by either the imported Firebase JS SDKs in your web/index.html
file or by providing an override - this may lead to unexpected issues in your application. It is recommended that you change all of the versions of the
Firebase JS SDK version "$supportedFirebaseJsSdkVersion":
If you override the version manually:
change:
<script>window.flutterfire_web_sdk_version = '${firebase.SDK_VERSION}';</script>
to:
<script>window.flutterfire_web_sdk_version = '$supportedFirebaseJsSdkVersion';</script>
If you import the Firebase scripts in index.html, instead allow FlutterFire to manage this for you by removing
any Firebase scripts in your web/index.html file:
e.g. remove: <script src="https://www.gstatic.com/firebasejs/${firebase.SDK_VERSION}/firebase-app.js"></script>
''',
);
}
return true;
}(),
);
firebase.App? app;
if (name == null || name == defaultFirebaseAppName) {
bool defaultAppExists = false;
try {
app = firebase.app();
defaultAppExists = true;
} catch (e) {
// noop
}
if (defaultAppExists) {
if (options != null) {
// If there is a default app already and the user provided options do a soft
// check to see if options are roughly identical (so we don't unnecessarily
// throw on minor differences such as platform specific keys missing,
// e.g. hot reloads/restarts).
if (options.apiKey != app!.options.apiKey ||
options.databaseURL != app.options.databaseURL ||
options.storageBucket != app.options.storageBucket) {
// Options are different; throw.
throw duplicateApp(defaultFirebaseAppName);
}
}
} else {
assert(
options != null,
'FirebaseOptions cannot be null when creating the default app.',
);
// At this point, there is no default app so we need to create it with
// the users options.
app = firebase.initializeApp(
apiKey: options!.apiKey,
authDomain: options.authDomain,
databaseURL: options.databaseURL,
projectId: options.projectId,
storageBucket: options.storageBucket,
messagingSenderId: options.messagingSenderId,
appId: options.appId,
measurementId: options.measurementId,
);
}
}
// Ensure the user has provided options for secondary apps.
if (name != null && name != defaultFirebaseAppName) {
assert(
options != null,
'FirebaseOptions cannot be null when creating a secondary Firebase app.',
);
try {
app = firebase.initializeApp(
name: name,
apiKey: options!.apiKey,
authDomain: options.authDomain,
databaseURL: options.databaseURL,
projectId: options.projectId,
storageBucket: options.storageBucket,
messagingSenderId: options.messagingSenderId,
appId: options.appId,
measurementId: options.measurementId,
);
} catch (e) {
if (_getJSErrorCode(e) == 'app/duplicate-app') {
throw duplicateApp(name);
}
throw _catchJSError(e);
}
}
await Future.wait(
_services.values.map((service) {
final ensureInitializedFunction = service.ensurePluginInitialized;
if (ensureInitializedFunction == null) {
return Future.value();
}
return ensureInitializedFunction();
}),
);
return _createFromJsApp(app!);
}
/// Returns a [FirebaseAppPlatform] instance.
///
/// If no name is provided, the default app instance is returned.
/// Throws if the app does not exist.
@override
FirebaseAppPlatform app([String name = defaultFirebaseAppName]) {
firebase.App app;
try {
app = guardNotInitialized(() => firebase.app(name));
} catch (e) {
if (_getJSErrorCode(e) == 'app/no-app') {
throw noAppExists(name);
}
throw _catchJSError(e);
}
return _createFromJsApp(app);
}
}
/// Converts a Exception to a FirebaseAdminException.
Never _handleException(Object exception, StackTrace stackTrace) {
if (exception.toString().contains('of undefined')) {
throw coreNotInitialized();
}
Error.throwWithStackTrace(exception, stackTrace);
}
/// A generic guard wrapper for API calls to handle exceptions.
R guardNotInitialized<R>(R Function() cb) {
try {
final value = cb();
if (value is Future) {
return value.catchError(
_handleException,
) as R;
}
return value;
} catch (error, stackTrace) {
_handleException(error, stackTrace);
}
}