@@ -13,16 +13,30 @@ const controllerName = 'SubjectMetadataController';
13
13
14
14
type SubjectOrigin = string ;
15
15
16
+ /**
17
+ * The different kinds of subjects that MetaMask may interact with, including
18
+ * third parties and itself (e.g., when the background communicated with the UI).
19
+ */
20
+ export enum SubjectType {
21
+ Extension = 'extension' ,
22
+ Internal = 'internal' ,
23
+ Unknown = 'unknown' ,
24
+ Website = 'website' ,
25
+ Snap = 'snap' ,
26
+ }
27
+
16
28
export type SubjectMetadata = PermissionSubjectMetadata & {
17
29
[ key : string ] : Json ;
18
30
// TODO:TS4.4 make optional
19
31
name : string | null ;
32
+ subjectType : SubjectType | null ;
20
33
extensionId : string | null ;
21
34
iconUrl : string | null ;
22
35
} ;
23
36
24
37
type SubjectMetadataToAdd = PermissionSubjectMetadata & {
25
38
name ?: string | null ;
39
+ subjectType ?: SubjectType | null ;
26
40
extensionId ?: string | null ;
27
41
iconUrl ?: string | null ;
28
42
} & Record < string , Json > ;
@@ -44,7 +58,14 @@ export type GetSubjectMetadataState = {
44
58
handler : ( ) => SubjectMetadataControllerState ;
45
59
} ;
46
60
47
- export type SubjectMetadataControllerActions = GetSubjectMetadataState ;
61
+ export type GetSubjectMetadata = {
62
+ type : `${typeof controllerName } :getSubjectMetadata`;
63
+ handler : ( origin : SubjectOrigin ) => SubjectMetadata | undefined ;
64
+ } ;
65
+
66
+ export type SubjectMetadataControllerActions =
67
+ | GetSubjectMetadataState
68
+ | GetSubjectMetadata ;
48
69
49
70
export type SubjectMetadataStateChange = {
50
71
type : `${typeof controllerName } :stateChange`;
@@ -80,7 +101,7 @@ export class SubjectMetadataController extends BaseController<
80
101
> {
81
102
private subjectCacheLimit : number ;
82
103
83
- private subjectsWithoutPermissionsEcounteredSinceStartup : Set < string > ;
104
+ private subjectsWithoutPermissionsEncounteredSinceStartup : Set < string > ;
84
105
85
106
private subjectHasPermissions : GenericPermissionController [ 'hasPermissions' ] ;
86
107
@@ -110,15 +131,20 @@ export class SubjectMetadataController extends BaseController<
110
131
111
132
this . subjectHasPermissions = hasPermissions ;
112
133
this . subjectCacheLimit = subjectCacheLimit ;
113
- this . subjectsWithoutPermissionsEcounteredSinceStartup = new Set ( ) ;
134
+ this . subjectsWithoutPermissionsEncounteredSinceStartup = new Set ( ) ;
135
+
136
+ this . messagingSystem . registerActionHandler (
137
+ `${ this . name } :getSubjectMetadata` ,
138
+ this . getSubjectMetadata . bind ( this ) ,
139
+ ) ;
114
140
}
115
141
116
142
/**
117
143
* Clears the state of this controller. Also resets the cache of subjects
118
144
* encountered since startup, so as to not prematurely reach the cache limit.
119
145
*/
120
146
clearState ( ) : void {
121
- this . subjectsWithoutPermissionsEcounteredSinceStartup . clear ( ) ;
147
+ this . subjectsWithoutPermissionsEncounteredSinceStartup . clear ( ) ;
122
148
this . update ( ( _draftState ) => {
123
149
return { ...defaultState } ;
124
150
} ) ;
@@ -143,20 +169,22 @@ export class SubjectMetadataController extends BaseController<
143
169
extensionId : metadata . extensionId || null ,
144
170
iconUrl : metadata . iconUrl || null ,
145
171
name : metadata . name || null ,
172
+ subjectType : metadata . subjectType || null ,
146
173
} ;
147
174
148
175
let originToForget : string | null = null ;
149
176
// We only delete the oldest encountered subject from the cache, again to
150
177
// ensure that the user's experience isn't degraded by missing icons etc.
151
178
if (
152
- this . subjectsWithoutPermissionsEcounteredSinceStartup . size >=
179
+ this . subjectsWithoutPermissionsEncounteredSinceStartup . size >=
153
180
this . subjectCacheLimit
154
181
) {
155
- const cachedOrigin = this . subjectsWithoutPermissionsEcounteredSinceStartup
156
- . values ( )
157
- . next ( ) . value ;
182
+ const cachedOrigin =
183
+ this . subjectsWithoutPermissionsEncounteredSinceStartup
184
+ . values ( )
185
+ . next ( ) . value ;
158
186
159
- this . subjectsWithoutPermissionsEcounteredSinceStartup . delete (
187
+ this . subjectsWithoutPermissionsEncounteredSinceStartup . delete (
160
188
cachedOrigin ,
161
189
) ;
162
190
@@ -165,7 +193,7 @@ export class SubjectMetadataController extends BaseController<
165
193
}
166
194
}
167
195
168
- this . subjectsWithoutPermissionsEcounteredSinceStartup . add ( origin ) ;
196
+ this . subjectsWithoutPermissionsEncounteredSinceStartup . add ( origin ) ;
169
197
170
198
this . update ( ( draftState ) => {
171
199
// Typecast: ts(2589)
@@ -176,6 +204,16 @@ export class SubjectMetadataController extends BaseController<
176
204
} ) ;
177
205
}
178
206
207
+ /**
208
+ * Gets the subject metadata for the given origin, if any.
209
+ *
210
+ * @param origin - The origin for which to get the subject metadata.
211
+ * @returns The subject metadata, if any, or `undefined` otherwise.
212
+ */
213
+ getSubjectMetadata ( origin : SubjectOrigin ) : SubjectMetadata | undefined {
214
+ return this . state . subjectMetadata [ origin ] ;
215
+ }
216
+
179
217
/**
180
218
* Deletes all subjects without permissions from the controller's state.
181
219
*/
0 commit comments