• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# AutoFillRequest (系统接口)
2
3当AutoFillExtensionAbility触发回调函数时,提供给开发者的页面数据和回调接口。
4
5> **说明:**
6>
7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 本模块接口均为系统接口。
9> 本模块接口仅可在Stage模型下使用。
10
11## 导入模块
12
13```ts
14import autoFillManager from '@ohos.app.ability.autoFillManager';
15```
16
17## FillRequest
18
19自动填充信息。
20
21**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
22
23| 名称        | 类型                 | 必填 | 说明                                                         |
24| ----------- | -------------------- | ---- | ------------------------------------------------------------ |
25| type        | [AutoFillType](js-apis-inner-application-autoFillType-sys.md)       | 是   | 自动填充类型。          |
26| viewData    | [ViewData](js-apis-inner-application-viewData-sys.md)               | 是   | 页面数据。              |
27
28## SaveRequest
29
30自动保存请求信息。
31
32**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
33
34| 名称        | 类型                 | 必填 | 说明                                                         |
35| ----------- | -------------------- | ---- | ------------------------------------------------------------ |
36| viewData    | [ViewData](js-apis-inner-application-viewData-sys.md)               | 是   | 页面数据。              |
37
38## FillResponse
39
40自动填充响应。
41
42**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
43
44| 名称        | 类型                 | 必填 | 说明                                                         |
45| ----------- | -------------------- | ---- | ------------------------------------------------------------ |
46| viewData    | [ViewData](js-apis-inner-application-viewData-sys.md)               | 是   | 页面数据。              |
47
48## FillRequestCallback
49
50自动填充或者生成密码时的回调对象,可以通过此回调通知客户端成功或者失败。
51
52### FillRequestCallback.onSuccess
53
54onSuccess(response: FillResponse): void
55
56通知自动填充请求已成功完成。
57
58**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
59
60**参数:**
61
62| 参数名 | 类型 | 必填 | 说明 |
63| -------- | -------- | -------- | ------------------------------ |
64| response | [FillResponse](../apis/#fillresponse)  | 是 | 自动填充响应信息。 |
65
66**错误码:**
67
68| 错误码ID | 错误信息 |
69| ------- | -------------------------------- |
70| 16000050 | Internal error. |
71
72**示例:**
73
74  ```ts
75  // MyAutoFillExtensionAbility.ts
76  import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility';
77  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
78  import autoFillManager from '@ohos.app.ability.autoFillManager';
79  import hilog from '@ohos.hilog';
80
81  class MyAutoFillExtensionAbility extends AutoFillExtensionAbility {
82    onFillRequest(session: UIExtensionContentSession,
83                  request: autoFillManager.FillRequest,
84                  callback: autoFillManager.FillRequestCallback) {
85      hilog.info(0x0000, 'testTag', '%{public}s', 'autofill onFillRequest');
86      try {
87        let storageData: Record<string, string | autoFillManager.FillRequestCallback | autoFillManager.ViewData> = {
88          'fillCallback': callback,
89          'message': 'AutoFill Page',
90          'viewData': request.viewData,
91        }
92        let storage_fill = new LocalStorage(storageData);
93        if (session) {
94          session.loadContent('pages/AutoFillPage', storage_fill);
95        } else {
96          hilog.error(0x0000, 'testTag', '%{public}s', 'session is null');
97        }
98      } catch (err) {
99        hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content');
100      }
101    }
102  }
103  ```
104
105  ```ts
106  // AutoFillPage.ets
107  import autoFillManager from '@ohos.app.ability.autoFillManager';
108  import Base from '@ohos.base';
109  import hilog from '@ohos.hilog';
110
111  let storage: LocalStorage = LocalStorage.getShared();
112  let fillCallback: autoFillManager.FillRequestCallback | undefined =
113    storage.get<autoFillManager.FillRequestCallback>('fillCallback');
114  let viewData: autoFillManager.ViewData | undefined = storage.get<autoFillManager.ViewData>('viewData');
115
116  @Entry
117  @Component
118  struct AutoFillPage {
119
120    build() {
121      Row() {
122        Column() {
123          Text('AutoFill Page')
124            .fontSize(50)
125            .fontWeight(FontWeight.Bold)
126        }
127
128        Button('onSuccess')
129          .onClick(() => {
130            if (viewData) {
131              viewData.pageNodeInfos[0].value = 'user1';
132              viewData.pageNodeInfos[1].value = 'user1 password';
133              viewData.pageNodeInfos[2].value = 'user1 generate new password';
134              hilog.info(0x0000, 'testTag', 'autofill success with viewData: %{public}s', JSON.stringify(viewData));
135              try {
136                fillCallback?.onSuccess({ viewData: viewData });
137              } catch (error) {
138                console.error(`catch error, code: ${(error as Base.BusinessError).code},
139                  message: ${(error as Base.BusinessError).message}`);
140              }
141            }
142          })
143          .width('100%')
144      }
145      .height('100%')
146    }
147  }
148  ```
149
150### FillRequestCallback.onFailure
151
152onFailure(): void
153
154通知自动填充请求已失败。
155
156**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
157
158**错误码:**
159
160| 错误码ID | 错误信息 |
161| ------- | -------------------------------- |
162| 16000050 | Internal error. |
163
164**示例:**
165
166  ```ts
167  // MyAutoFillExtensionAbility.ts
168  import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility';
169  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
170  import autoFillManager from '@ohos.app.ability.autoFillManager';
171  import hilog from '@ohos.hilog';
172
173  class MyAutoFillExtensionAbility extends AutoFillExtensionAbility {
174    onFillRequest(session : UIExtensionContentSession,
175                  request : autoFillManager.FillRequest,
176                  callback : autoFillManager.FillRequestCallback) {
177      hilog.info(0x0000, 'testTag', '%{public}s', 'autofill onFillRequest');
178      try {
179        let storageData: Record<string, string | autoFillManager.FillRequestCallback | autoFillManager.ViewData> = {
180          'fillCallback': callback,
181          'message': 'AutoFill Page',
182          'viewData': request.viewData,
183        }
184        let storage_fill = new LocalStorage(storageData);
185        if (session) {
186          session.loadContent('pages/AutoFill Page', storage_fill);
187        } else {
188          hilog.error(0x0000, 'testTag', '%{public}s', 'session is null');
189        }
190      } catch (err) {
191        hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content');
192      }
193    }
194  }
195  ```
196
197  ```ts
198  // AutoFillPage.ets
199  import autoFillManager from '@ohos.app.ability.autoFillManager';
200  import Base from '@ohos.base';
201  import hilog from '@ohos.hilog';
202
203  let storage: LocalStorage = LocalStorage.getShared();
204  let fillCallback: autoFillManager.FillRequestCallback | undefined =
205    storage.get<autoFillManager.FillRequestCallback>('fillCallback');
206
207  @Entry
208  @Component
209  struct AutoFillPage {
210
211    build() {
212      Row() {
213        Column() {
214          Text('AutoFill Page')
215            .fontSize(50)
216            .fontWeight(FontWeight.Bold)
217        }
218        Button('onFailure')
219          .onClick(() => {
220            hilog.info(0x0000, 'testTag', 'autofill failure');
221            try {
222              fillCallback?.onFailure();
223            } catch (error) {
224              console.error(`catch error, code: ${(error as Base.BusinessError).code},
225                message: ${(error as Base.BusinessError).message}`);
226            }
227          })
228        .width('100%')
229      }
230      .height('100%')
231    }
232  }
233  ```
234
235### FillRequestCallback.onCancel
236
237onCancel(): void
238
239通知自动填充已被取消。
240
241**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
242
243**错误码:**
244
245| 错误码ID | 错误信息 |
246| ------- | -------------------------------- |
247| 16000050 | Internal error. |
248
249**示例:**
250
251  ```ts
252  // MyAutoFillExtensionAbility.ts
253  import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility';
254  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
255  import autoFillManager from '@ohos.app.ability.autoFillManager';
256  import hilog from '@ohos.hilog';
257
258  class MyAutoFillExtensionAbility extends AutoFillExtensionAbility {
259    onFillRequest(session: UIExtensionContentSession,
260                  request: autoFillManager.FillRequest,
261                  callback: autoFillManager.FillRequestCallback) {
262      hilog.info(0x0000, 'testTag', '%{public}s', 'autofill onFillRequest');
263      try {
264        let storageData: Record<string, string | autoFillManager.FillRequestCallback | autoFillManager.ViewData> = {
265          'fillCallback': callback,
266          'message': 'AutoFill Page',
267          'viewData': request.viewData,
268        }
269        let storage_fill = new LocalStorage(storageData);
270        if (session) {
271          session.loadContent('pages/AutoFillPage', storage_fill);
272        } else {
273          hilog.error(0x0000, 'testTag', '%{public}s', 'session is null');
274        }
275      } catch (err) {
276        hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content');
277      }
278    }
279  }
280  ```
281
282  ```ts
283  // AutoFillPage.ets
284  import autoFillManager from '@ohos.app.ability.autoFillManager';
285  import Base from '@ohos.base';
286  import hilog from '@ohos.hilog';
287
288  let storage: LocalStorage = LocalStorage.getShared();
289  let fillCallback: autoFillManager.FillRequestCallback | undefined =
290    storage.get<autoFillManager.FillRequestCallback>('fillCallback');
291
292  @Entry
293  @Component
294  struct AutoFillPage {
295
296    build() {
297      Row() {
298        Column() {
299          Text('Hello World')
300            .fontSize(50)
301            .fontWeight(FontWeight.Bold)
302        }
303
304        Button('onCancel')
305          .onClick(() => {
306            hilog.info(0x0000, 'testTag', 'autofill cancel');
307            try {
308              fillCallback?.onCancel();
309            } catch (error) {
310              console.error(`catch error, code: ${(error as Base.BusinessError).code},
311                message: ${(error as Base.BusinessError).message}`);
312            }
313          })
314          .width('100%')
315      }
316      .height('100%')
317    }
318  }
319  ```
320
321## SaveRequestCallback
322
323自动保存或者手动保存请求回调。
324
325### SaveRequestCallback.onSuccess
326
327onSuccess(): void
328
329通知保存请求已成功处理。
330
331**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
332
333**错误码:**
334
335| 错误码ID | 错误信息 |
336| ------- | -------------------------------- |
337| 16000050 | Internal error. |
338
339**示例:**
340
341  ```ts
342  // MyAutoFillExtensionAbility.ts
343  import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility';
344  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
345  import autoFillManager from '@ohos.app.ability.autoFillManager';
346  import hilog from '@ohos.hilog';
347
348  class MyAutoFillExtensionAbility extends AutoFillExtensionAbility {
349    onSaveRequest(session: UIExtensionContentSession,
350                  request: autoFillManager.SaveRequest,
351                  callback: autoFillManager.SaveRequestCallback) {
352      hilog.info(0x0000, 'testTag', '%{public}s', 'onSaveRequest');
353      try {
354        let storageData: Record<string, string | autoFillManager.SaveRequestCallback | autoFillManager.ViewData> = {
355          'message': 'AutoFill Page',
356          'saveCallback': callback,
357          'viewData': request.viewData
358        }
359        let storage_save = new LocalStorage(storageData);
360        if (session) {
361          session.loadContent('pages/SavePage', storage_save);
362        } else {
363          hilog.error(0x0000, 'testTag', '%{public}s', 'session is null');
364        }
365      } catch (err) {
366        hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content');
367      }
368    }
369  }
370  ```
371
372  ```ts
373  // SavePage.ets
374  import autoFillManager from '@ohos.app.ability.autoFillManager';
375  import Base from '@ohos.base';
376  import hilog from '@ohos.hilog';
377
378  let storage: LocalStorage = LocalStorage.getShared();
379  let saveCallback: autoFillManager.SaveRequestCallback | undefined =
380    storage.get<autoFillManager.SaveRequestCallback>('saveCallback');
381
382  @Entry
383  @Component
384  struct SavePage {
385
386    build() {
387      Row() {
388        Column() {
389          Text('SavePage')
390            .fontSize(50)
391            .fontWeight(FontWeight.Bold)
392        }
393
394        Button('onSuccess')
395          .onClick(() => {
396            hilog.info(0x0000, 'testTag', 'autosave success');
397            try {
398              saveCallback?.onSuccess();
399            } catch (error) {
400              console.error(`catch error, code: ${(error as Base.BusinessError).code},
401                message: ${(error as Base.BusinessError).message}`);
402            }
403          })
404          .width('100%')
405      }
406      .height('100%')
407    }
408  }
409  ```
410
411### SaveRequestCallback.onFailure
412
413onFailure(): void
414
415通知保存请求处理失败。
416
417**系统能力**:SystemCapability.Ability.AbilityRuntime.AbilityCore
418
419**错误码:**
420
421| 错误码ID | 错误信息 |
422| ------- | -------------------------------- |
423| 16000050 | Internal error. |
424
425**示例:**
426
427  ```ts
428  // MyAutoFillExtensionAbility.ts
429  import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility';
430  import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession';
431  import autoFillManager from '@ohos.app.ability.autoFillManager';
432  import hilog from '@ohos.hilog';
433
434  class MyAutoFillExtensionAbility extends AutoFillExtensionAbility {
435    onSaveRequest(session: UIExtensionContentSession,
436                  request: autoFillManager.SaveRequest,
437                  callback: autoFillManager.SaveRequestCallback) {
438      hilog.info(0x0000, 'testTag', '%{public}s', 'onSaveRequest');
439      try {
440        let storageData: Record<string, string | autoFillManager.SaveRequestCallback | autoFillManager.ViewData> = {
441          'message': 'AutoFill Page',
442          'saveCallback': callback,
443          'viewData': request.viewData
444        }
445        let storage_save = new LocalStorage(storageData);
446        if (session) {
447          session.loadContent('pages/SavePage', storage_save);
448        } else {
449          hilog.error(0x0000, 'testTag', '%{public}s', 'session is null');
450        }
451      } catch (err) {
452        hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content');
453      }
454    }
455  }
456  ```
457
458  ```ts
459  // SavePage.ets
460  import autoFillManager from '@ohos.app.ability.autoFillManager';
461  import Base from '@ohos.base';
462  import hilog from '@ohos.hilog';
463
464  let storage: LocalStorage = LocalStorage.getShared();
465  let saveCallback: autoFillManager.FillRequestCallback | undefined =
466    storage.get<autoFillManager.SaveRequestCallback>('saveCallback');
467
468  @Entry
469  @Component
470  struct SavePage {
471    build() {
472      Row() {
473        Column() {
474          Text('Save Page')
475            .fontSize(50)
476            .fontWeight(FontWeight.Bold)
477        }
478
479        Button('onFailure')
480          .onClick(() => {
481            hilog.info(0x0000, 'testTag', 'autofill failure');
482            try {
483              saveCallback?.onFailure();
484            } catch (error) {
485              console.error(`catch error, code: ${(error as Base.BusinessError).code},
486                message: ${(error as Base.BusinessError).message}`);
487            }
488          })
489          .width('100%')
490      }
491      .height('100%')
492    }
493  }
494  ```
495