1e41f4b71Sopenharmony_ci# @ohos.app.ability.AutoFillExtensionAbility (AutoFillExtensionAbility) (System API) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe AutoFillExtensionAbility module, inherited from [ExtensionAbility](js-apis-app-ability-extensionAbility.md), provides APIs for automatically filling in and saving accounts and passwords. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> The initial APIs of this module are supported since API version 11. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> 9e41f4b71Sopenharmony_ci> The APIs of this module can be used only in the stage model. 10e41f4b71Sopenharmony_ci> 11e41f4b71Sopenharmony_ci> The APIs provided by this module are system APIs. 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci## Modules to Import 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci```ts 16e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility } from '@kit.AbilityKit'; 17e41f4b71Sopenharmony_ci``` 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci## Properties 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci**Parameters** 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci| Name| Type| Readable| Writable| Description| 26e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | -------- | 27e41f4b71Sopenharmony_ci| context | [AutoFillExtensionContext](js-apis-inner-application-autoFillExtensionContext-sys.md) | Yes| No| Context of the AutoFillExtensionAbility. This context is inherited from **ExtensionContext**.| 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onCreate 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_cionCreate(): void 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ciCalled when an AutoFillExtensionAbility is created. 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci**Example** 39e41f4b71Sopenharmony_ci 40e41f4b71Sopenharmony_ci```ts 41e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility } from '@kit.AbilityKit'; 42e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 45e41f4b71Sopenharmony_ci onCreate() { 46e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onCreate'); 47e41f4b71Sopenharmony_ci } 48e41f4b71Sopenharmony_ci} 49e41f4b71Sopenharmony_ci``` 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onFillRequest 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_cionFillRequest(session: UIExtensionContentSession, request: FillRequest, callback: FillRequestCallback): void 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ciCalled when an auto-fill request is initiated or a password is generated. 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci**Parameters** 60e41f4b71Sopenharmony_ci 61e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 62e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 63e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the AutoFillExtensionAbility.| 64e41f4b71Sopenharmony_ci| request | [FillRequest](js-apis-inner-application-autoFillRequest-sys.md#fillrequest) | Yes| Data to be automatically filled in.| 65e41f4b71Sopenharmony_ci| callback | [FillRequestCallback](js-apis-inner-application-autoFillRequest-sys.md#fillrequestcallback) | Yes| Callback used for the auto-fill request.| 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci**Example** 68e41f4b71Sopenharmony_ci 69e41f4b71Sopenharmony_ci```ts 70e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility, UIExtensionContentSession, autoFillManager, common } from '@kit.AbilityKit'; 71e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 72e41f4b71Sopenharmony_ci 73e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 74e41f4b71Sopenharmony_ci onFillRequest(session: UIExtensionContentSession, 75e41f4b71Sopenharmony_ci request: autoFillManager.FillRequest, 76e41f4b71Sopenharmony_ci callback: autoFillManager.FillRequestCallback) { 77e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'autofill onFillRequest'); 78e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'fill requestCallback: %{public}s', JSON.stringify(callback)); 79e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'get request viewData: ', JSON.stringify(request.viewData)); 80e41f4b71Sopenharmony_ci try { 81e41f4b71Sopenharmony_ci let localStorageData: Record<string, UIExtensionContentSession | string | autoFillManager.FillRequestCallback | 82e41f4b71Sopenharmony_ci autoFillManager.ViewData | common.AutoFillExtensionContext> = { 83e41f4b71Sopenharmony_ci 'session': session, 84e41f4b71Sopenharmony_ci 'message': 'AutoFill Page', 85e41f4b71Sopenharmony_ci 'fillCallback': callback, 86e41f4b71Sopenharmony_ci 'viewData': request.viewData, 87e41f4b71Sopenharmony_ci 'context': this.context, 88e41f4b71Sopenharmony_ci }; 89e41f4b71Sopenharmony_ci let storage_fill = new LocalStorage(localStorageData); 90e41f4b71Sopenharmony_ci if (session) { 91e41f4b71Sopenharmony_ci session.loadContent('pages/SelectorList', storage_fill); 92e41f4b71Sopenharmony_ci } else { 93e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', '%{public}s', 'session is null'); 94e41f4b71Sopenharmony_ci } 95e41f4b71Sopenharmony_ci } catch (err) { 96e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content'); 97e41f4b71Sopenharmony_ci } 98e41f4b71Sopenharmony_ci } 99e41f4b71Sopenharmony_ci} 100e41f4b71Sopenharmony_ci``` 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onSaveRequest 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_cionSaveRequest(session: UIExtensionContentSession, request: SaveRequest, callback: SaveRequestCallback): void 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ciCalled when automatic or manual saving is initiated. 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci**Parameters** 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 113e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 114e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the AutoFillExtensionAbility.| 115e41f4b71Sopenharmony_ci| request | [SaveRequest](js-apis-inner-application-autoFillRequest-sys.md#saverequest) | Yes| Data to be saved.| 116e41f4b71Sopenharmony_ci| callback | [SaveRequestCallback](js-apis-inner-application-autoFillRequest-sys.md#saverequestcallback) | Yes| Callback used for the saving request.| 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Example** 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci```ts 121e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility, UIExtensionContentSession, autoFillManager, common } from '@kit.AbilityKit'; 122e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 123e41f4b71Sopenharmony_ci 124e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 125e41f4b71Sopenharmony_ci onSaveRequest(session : UIExtensionContentSession, 126e41f4b71Sopenharmony_ci request : autoFillManager.SaveRequest, 127e41f4b71Sopenharmony_ci callback : autoFillManager.SaveRequestCallback) { 128e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onSaveRequest'); 129e41f4b71Sopenharmony_ci try { 130e41f4b71Sopenharmony_ci let localStorageData: Record<string, UIExtensionContentSession | string | autoFillManager.SaveRequestCallback | 131e41f4b71Sopenharmony_ci autoFillManager.ViewData | common.AutoFillExtensionContext> = { 132e41f4b71Sopenharmony_ci 'session': session, 133e41f4b71Sopenharmony_ci 'message': 'AutoFill Page', 134e41f4b71Sopenharmony_ci 'fillCallback': callback, 135e41f4b71Sopenharmony_ci 'viewData': request.viewData, 136e41f4b71Sopenharmony_ci 'context': this.context, 137e41f4b71Sopenharmony_ci }; 138e41f4b71Sopenharmony_ci let storage_save = new LocalStorage(localStorageData); 139e41f4b71Sopenharmony_ci if (session) { 140e41f4b71Sopenharmony_ci session.loadContent('pages/SavePage', storage_save); 141e41f4b71Sopenharmony_ci } else { 142e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', '%{public}s', 'session is null'); 143e41f4b71Sopenharmony_ci } 144e41f4b71Sopenharmony_ci } catch (err) { 145e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content'); 146e41f4b71Sopenharmony_ci } 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci} 149e41f4b71Sopenharmony_ci``` 150e41f4b71Sopenharmony_ci 151e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onUpdateRequest<sup>12+</sup> 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_cionUpdateRequest(request: UpdateRequest): void 154e41f4b71Sopenharmony_ci 155e41f4b71Sopenharmony_ciCalled when an update request is received. 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci**Parameters** 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 162e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 163e41f4b71Sopenharmony_ci| request | [UpdateRequest](js-apis-inner-application-autoFillRequest-sys.md#updaterequest12) | Yes| Update request.| 164e41f4b71Sopenharmony_ci 165e41f4b71Sopenharmony_ci**Example** 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci```ts 168e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility, autoFillManager } from '@kit.AbilityKit'; 169e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 170e41f4b71Sopenharmony_ci 171e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 172e41f4b71Sopenharmony_ci onUpdateRequest(request: autoFillManager.UpdateRequest) { 173e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'on update request, view data is: %{public}s', 174e41f4b71Sopenharmony_ci JSON.stringify(request.viewData)); 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci} 177e41f4b71Sopenharmony_ci``` 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onSessionDestroy 180e41f4b71Sopenharmony_ci 181e41f4b71Sopenharmony_cionSessionDestroy(session: UIExtensionContentSession): void 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ciCalled when a **UIExtensionContentSession** instance is destroyed for this AutoFillExtensionAbility. 184e41f4b71Sopenharmony_ci 185e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 186e41f4b71Sopenharmony_ci 187e41f4b71Sopenharmony_ci**Parameters** 188e41f4b71Sopenharmony_ci 189e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 190e41f4b71Sopenharmony_ci| -------- | -------- | -------- | -------- | 191e41f4b71Sopenharmony_ci| session | [UIExtensionContentSession](js-apis-app-ability-uiExtensionContentSession.md) | Yes| UI content information related to the AutoFillExtensionAbility.| 192e41f4b71Sopenharmony_ci 193e41f4b71Sopenharmony_ci**Example** 194e41f4b71Sopenharmony_ci 195e41f4b71Sopenharmony_ci```ts 196e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility, UIExtensionContentSession } from '@kit.AbilityKit'; 197e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 198e41f4b71Sopenharmony_ci 199e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 200e41f4b71Sopenharmony_ci onSessionDestroy(session : UIExtensionContentSession) { 201e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onSessionDestroy'); 202e41f4b71Sopenharmony_ci } 203e41f4b71Sopenharmony_ci} 204e41f4b71Sopenharmony_ci``` 205e41f4b71Sopenharmony_ci 206e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onForeground 207e41f4b71Sopenharmony_ci 208e41f4b71Sopenharmony_cionForeground(): void 209e41f4b71Sopenharmony_ci 210e41f4b71Sopenharmony_ciCalled when this AutoFillExtensionAbility is switched from the background to the foreground. 211e41f4b71Sopenharmony_ci 212e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci**Example** 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci```ts 217e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility } from '@kit.AbilityKit'; 218e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 221e41f4b71Sopenharmony_ci onForeground() { 222e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onForeground'); 223e41f4b71Sopenharmony_ci } 224e41f4b71Sopenharmony_ci} 225e41f4b71Sopenharmony_ci``` 226e41f4b71Sopenharmony_ci 227e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onBackground 228e41f4b71Sopenharmony_ci 229e41f4b71Sopenharmony_cionBackground(): void 230e41f4b71Sopenharmony_ci 231e41f4b71Sopenharmony_ciCalled when this AutoFillExtensionAbility is switched from the foreground to the background. 232e41f4b71Sopenharmony_ci 233e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 234e41f4b71Sopenharmony_ci 235e41f4b71Sopenharmony_ci**Example** 236e41f4b71Sopenharmony_ci 237e41f4b71Sopenharmony_ci```ts 238e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility } from '@kit.AbilityKit'; 239e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 242e41f4b71Sopenharmony_ci onBackground() { 243e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onBackground'); 244e41f4b71Sopenharmony_ci } 245e41f4b71Sopenharmony_ci} 246e41f4b71Sopenharmony_ci``` 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci## AutoFillExtensionAbility.onDestroy 249e41f4b71Sopenharmony_ci 250e41f4b71Sopenharmony_cionDestroy(): void | Promise<void> 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ciCalled to clear resources when this AutoFillExtensionAbility is destroyed. This API either returns the result directly or uses a promise to return the result. 253e41f4b71Sopenharmony_ci 254e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 255e41f4b71Sopenharmony_ci 256e41f4b71Sopenharmony_ci**Return value** 257e41f4b71Sopenharmony_ci 258e41f4b71Sopenharmony_ci| Type | Description | 259e41f4b71Sopenharmony_ci| ------------------------------------- | ------------------------------- | 260e41f4b71Sopenharmony_ci| void \| Promise<void> | Returns no value or returns a Promise.| 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci**Example** 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci```ts 265e41f4b71Sopenharmony_ciimport { AutoFillExtensionAbility } from '@kit.AbilityKit'; 266e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ciclass MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 269e41f4b71Sopenharmony_ci onDestroy() { 270e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onDestroy'); 271e41f4b71Sopenharmony_ci } 272e41f4b71Sopenharmony_ci} 273e41f4b71Sopenharmony_ci``` 274