This repository was archived by the owner on Sep 14, 2023. It is now read-only.
forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfoundation.dart
More file actions
319 lines (268 loc) · 10.7 KB
/
foundation.dart
File metadata and controls
319 lines (268 loc) · 10.7 KB
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
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TODO(a14n): remove this import once Flutter 3.1 or later reaches stable (including flutter/flutter#104231)
// ignore: unnecessary_import
import 'dart:typed_data';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import '../common/instance_manager.dart';
import 'foundation_api_impls.dart';
/// The values that can be returned in a change map.
///
/// Wraps [NSKeyValueObservingOptions](https://developer.apple.com/documentation/foundation/nskeyvalueobservingoptions?language=objc).
enum NSKeyValueObservingOptions {
/// Indicates that the change map should provide the new attribute value, if applicable.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvalueobservingoptions/nskeyvalueobservingoptionnew?language=objc.
newValue,
/// Indicates that the change map should contain the old attribute value, if applicable.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvalueobservingoptions/nskeyvalueobservingoptionold?language=objc.
oldValue,
/// Indicates a notification should be sent to the observer immediately.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvalueobservingoptions/nskeyvalueobservingoptioninitial?language=objc.
initialValue,
/// Whether separate notifications should be sent to the observer before and after each change.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvalueobservingoptions/nskeyvalueobservingoptionprior?language=objc.
priorNotification,
}
/// The kinds of changes that can be observed.
///
/// Wraps [NSKeyValueChange](https://developer.apple.com/documentation/foundation/nskeyvaluechange?language=objc).
enum NSKeyValueChange {
/// Indicates that the value of the observed key path was set to a new value.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechange/nskeyvaluechangesetting?language=objc.
setting,
/// Indicates that an object has been inserted into the to-many relationship that is being observed.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechange/nskeyvaluechangeinsertion?language=objc.
insertion,
/// Indicates that an object has been removed from the to-many relationship that is being observed.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechange/nskeyvaluechangeremoval?language=objc.
removal,
/// Indicates that an object has been replaced in the to-many relationship that is being observed.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechange/nskeyvaluechangereplacement?language=objc.
replacement,
}
/// The keys that can appear in the change map.
///
/// Wraps [NSKeyValueChangeKey](https://developer.apple.com/documentation/foundation/nskeyvaluechangekey?language=objc).
enum NSKeyValueChangeKey {
/// Indicates changes made in a collection.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechangeindexeskey?language=objc.
indexes,
/// Indicates what sort of change has occurred.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechangekindkey?language=objc.
kind,
/// Indicates the new value for the attribute.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechangenewkey?language=objc.
newValue,
/// Indicates a notification is sent prior to a change.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechangenotificationispriorkey?language=objc.
notificationIsPrior,
/// Indicates the value of this key is the value before the attribute was changed.
///
/// See https://developer.apple.com/documentation/foundation/nskeyvaluechangeoldkey?language=objc.
oldValue,
}
/// The supported keys in a cookie attributes dictionary.
///
/// Wraps [NSHTTPCookiePropertyKey](https://developer.apple.com/documentation/foundation/nshttpcookiepropertykey).
enum NSHttpCookiePropertyKey {
/// A String object containing the comment for the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiecomment.
comment,
/// A String object containing the comment URL for the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiecommenturl.
commentUrl,
/// A String object stating whether the cookie should be discarded at the end of the session.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiediscard.
discard,
/// A String object specifying the expiration date for the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiedomain.
domain,
/// A String object specifying the expiration date for the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookieexpires.
expires,
/// A String object containing an integer value stating how long in seconds the cookie should be kept, at most.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiemaximumage.
maximumAge,
/// A String object containing the name of the cookie (required).
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiename.
name,
/// A String object containing the URL that set this cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookieoriginurl.
originUrl,
/// A String object containing the path for the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiepath.
path,
/// A String object containing comma-separated integer values specifying the ports for the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookieport.
port,
/// A String indicating the same-site policy for the cookie.
///
/// This is only supported on iOS version 13+. This value will be ignored on
/// versions < 13.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiesamesitepolicy.
sameSitePolicy,
/// A String object indicating that the cookie should be transmitted only over secure channels.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookiesecure.
secure,
/// A String object containing the value of the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookievalue.
value,
/// A String object that specifies the version of the cookie.
///
/// See https://developer.apple.com/documentation/foundation/nshttpcookieversion.
version,
}
/// A URL load request that is independent of protocol or URL scheme.
///
/// Wraps [NSUrlRequest](https://developer.apple.com/documentation/foundation/nsurlrequest?language=objc).
@immutable
class NSUrlRequest {
/// Constructs an [NSUrlRequest].
const NSUrlRequest({
required this.url,
this.httpMethod,
this.httpBody,
this.allHttpHeaderFields = const <String, String>{},
});
/// The URL being requested.
final String url;
/// The HTTP request method.
///
/// The default HTTP method is “GET”.
final String? httpMethod;
/// Data sent as the message body of a request, as in an HTTP POST request.
final Uint8List? httpBody;
/// All of the HTTP header fields for a request.
final Map<String, String> allHttpHeaderFields;
}
/// Information about an error condition.
///
/// Wraps [NSError](https://developer.apple.com/documentation/foundation/nserror?language=objc).
@immutable
class NSError {
/// Constructs an [NSError].
const NSError({
required this.code,
required this.domain,
required this.localizedDescription,
});
/// The error code.
///
/// Note that errors are domain-specific.
final int code;
/// A string containing the error domain.
final String domain;
/// A string containing the localized description of the error.
final String localizedDescription;
}
/// A representation of an HTTP cookie.
///
/// Wraps [NSHTTPCookie](https://developer.apple.com/documentation/foundation/nshttpcookie).
@immutable
class NSHttpCookie {
/// Initializes an HTTP cookie object using the provided properties.
const NSHttpCookie.withProperties(this.properties);
/// Properties of the new cookie object.
final Map<NSHttpCookiePropertyKey, Object> properties;
}
/// The root class of most Objective-C class hierarchies.
@immutable
class NSObject with Copyable {
// TODO(bparrishMines): Change constructor name to `detached`.
/// Constructs a [NSObject] without creating the associated
/// Objective-C object.
///
/// This should only be used by subclasses created by this library or to
/// create copies.
NSObject({
this.observeValue,
BinaryMessenger? binaryMessenger,
InstanceManager? instanceManager,
}) : _api = NSObjectHostApiImpl(
binaryMessenger: binaryMessenger,
instanceManager: instanceManager,
) {
// Ensures FlutterApis for the Foundation library are set up.
FoundationFlutterApis.instance.ensureSetUp();
}
/// Global instance of [InstanceManager].
static final InstanceManager globalInstanceManager =
InstanceManager(onWeakReferenceRemoved: (int instanceId) {
NSObjectHostApiImpl().dispose(instanceId);
});
final NSObjectHostApiImpl _api;
/// Informs the observing object when the value at the specified key path has changed.
final void Function(
String keyPath,
NSObject object,
Map<NSKeyValueChangeKey, Object?> change,
)? observeValue;
/// Registers the observer object to receive KVO notifications.
Future<void> addObserver(
NSObject observer, {
required String keyPath,
required Set<NSKeyValueObservingOptions> options,
}) {
assert(options.isNotEmpty);
return _api.addObserverForInstances(
this,
observer,
keyPath,
options,
);
}
/// Stops the observer object from receiving change notifications for the property.
Future<void> removeObserver(NSObject observer, {required String keyPath}) {
return _api.removeObserverForInstances(this, observer, keyPath);
}
/// Release the reference to the Objective-C object.
static void dispose(NSObject instance) {
instance._api.instanceManager.removeWeakReference(instance);
}
@override
Copyable copy() {
return NSObject(
observeValue: observeValue,
binaryMessenger: _api.binaryMessenger,
instanceManager: _api.instanceManager,
);
}
@override
int get hashCode {
return Object.hash(_api, _api.instanceManager.getIdentifier(this));
}
@override
bool operator ==(Object other) {
return other is NSObject &&
_api == other._api &&
_api.instanceManager.getIdentifier(this) ==
other._api.instanceManager.getIdentifier(other);
}
}