1# AutoFillRequest 2 3AutoFillRequest provides page data and callbacks when a callback is triggered for the AutoFillExtensionAbility. 4 5> **NOTE** 6> 7> 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. 8> The APIs provided by this module are system APIs. 9> The APIs of this module can be used only in the stage model. 10 11## Modules to Import 12 13```ts 14import autoFillManager from '@ohos.app.ability.autoFillManager'; 15``` 16 17## FillRequest 18 19Defines the information about an auto-fill request. 20 21**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 22 23| Name | Type | Mandatory| Description | 24| ----------- | -------------------- | ---- | ------------------------------------------------------------ | 25| type | [AutoFillType](js-apis-inner-application-autoFillType.md) | Yes | Type of the element to be automatically filled in. | 26| viewData | [ViewData](js-apis-inner-application-viewData.md) | Yes | Page data. | 27 28## SaveRequest 29 30Defines the information about an auto-saving request. 31 32**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 33 34| Name | Type | Mandatory| Description | 35| ----------- | -------------------- | ---- | ------------------------------------------------------------ | 36| viewData | [ViewData](js-apis-inner-application-viewData.md) | Yes | Page data. | 37 38## FillResponse 39 40Defines the information about the response to an auto-fill request. 41 42**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 43 44| Name | Type | Mandatory| Description | 45| ----------- | -------------------- | ---- | ------------------------------------------------------------ | 46| viewData | [ViewData](js-apis-inner-application-viewData.md) | Yes | Page data. | 47 48## FillRequestCallback 49 50Implements callbacks for an auto-fill request, which is used to automatically fill in or generate a password. The callbacks can be used to notify the client of the success or failure of the request. 51 52### FillRequestCallback.onSuccess 53 54onSuccess(response: FillResponse): void 55 56Called when an auto-fill request is successfully processed. 57 58**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 59 60**Parameters** 61 62| Name| Type| Mandatory| Description| 63| -------- | -------- | -------- | ------------------------------ | 64| response | [FillResponse](#fillresponse) | Yes| Information about the response to the auto-fill response.| 65 66**Error codes** 67 68| ID| Error Message| 69| ------- | -------------------------------- | 70| 16000050 | Internal error. | 71 72**Example** 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 storage_fill = new LocalStorage( 88 { 89 'fillCallback': callback, 90 'message': "AutoFill Page", 91 'viewData': request.viewData, 92 }); 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.getShared(); 112 let fillCallback = storage.get<autoFillManager.FillRequestCallback>('fillCallback'); 113 let viewData: autoFillManager.ViewData | undefined = storage.get<autoFillManager.ViewData>('viewData'); 114 115 @Entry 116 @Component 117 struct AutoFillPage { 118 119 build() { 120 Row() { 121 Column() { 122 Text('AutoFill Page') 123 .fontSize(50) 124 .fontWeight(FontWeight.Bold) 125 } 126 127 Button('onSuccess') 128 .onClick(() => { 129 viewData.pageNodeInfos[0].value = "user1"; 130 viewData.pageNodeInfos[1].value = "user1 password"; 131 viewData.pageNodeInfos[2].value = "user1 generate new password"; 132 hilog.info(0x0000, 'testTag', 'autofill success with viewData: %{public}s', JSON.stringify(viewData)); 133 try { 134 fillCallback.onSuccess({ viewData: viewData }); 135 } catch (error) { 136 console.error(`catch error, code: ${(error as Base.BusinessError).code}, message: ${(error as Base.BusinessError).message}`); 137 } 138 }) 139 .width('100%') 140 } 141 .height('100%') 142 } 143 } 144 ``` 145 146### FillRequestCallback.onFailure 147 148onFailure(): void 149 150Called when an auto-fill request fails to be processed. 151 152**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 153 154**Error codes** 155 156| ID| Error Message| 157| ------- | -------------------------------- | 158| 16000050 | Internal error. | 159 160**Example** 161 162 ```ts 163 // MyAutoFillExtensionAbility.ts 164 import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility'; 165 import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 166 import autoFillManager from '@ohos.app.ability.autoFillManager'; 167 import hilog from '@ohos.hilog'; 168 169 class MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 170 onFillRequest(session : UIExtensionContentSession, 171 request : autoFillManager.FillRequest, 172 callback : autoFillManager.FillRequestCallback) { 173 hilog.info(0x0000, 'testTag', '%{public}s', 'autofill onFillRequest'); 174 try { 175 let storage_fill = new LocalStorage( 176 { 177 'fillCallback': callback, 178 'message': "AutoFill Page", 179 'viewData': request.viewData, 180 }); 181 if (session) { 182 session.loadContent('pages/AutoFill Page', storage_fill); 183 } else { 184 hilog.error(0x0000, 'testTag', '%{public}s', 'session is null'); 185 } 186 } catch (err) { 187 hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content'); 188 } 189 } 190 } 191 ``` 192 193 ```ts 194 // AutoFillPage.ets 195 import autoFillManager from '@ohos.app.ability.autoFillManager'; 196 import Base from '@ohos.base'; 197 import hilog from '@ohos.hilog'; 198 199 let storage = LocalStorage.getShared(); 200 let fillCallback = storage.get<autoFillManager.FillRequestCallback>('fillCallback'); 201 202 @Entry 203 @Component 204 struct AutoFillPage { 205 206 build() { 207 Row() { 208 Column() { 209 Text('AutoFill Page') 210 .fontSize(50) 211 .fontWeight(FontWeight.Bold) 212 } 213 Button('onFailure') 214 .onClick(() => { 215 hilog.info(0x0000, 'testTag', 'autofill failure'); 216 try { 217 fillCallback.onFailure(); 218 } catch (error) { 219 console.error(`catch error, code: ${(error as Base.BusinessError).code}, 220 message: ${(error as Base.BusinessError).message}`); 221 } 222 }) 223 .width('100%') 224 } 225 .height('100%') 226 } 227 } 228 ``` 229 230### FillRequestCallback.onCancel 231 232onCancel(): void 233 234Called when an auto-fill request is canceled. 235 236**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 237 238**Error codes** 239 240| ID| Error Message| 241| ------- | -------------------------------- | 242| 16000050 | Internal error. | 243 244**Example** 245 246 ```ts 247 // MyAutoFillExtensionAbility.ts 248 import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility'; 249 import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 250 import autoFillManager from '@ohos.app.ability.autoFillManager'; 251 import hilog from '@ohos.hilog'; 252 253 class MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 254 onFillRequest(session: UIExtensionContentSession, 255 request: autoFillManager.FillRequest, 256 callback: autoFillManager.FillRequestCallback) { 257 hilog.info(0x0000, 'testTag', '%{public}s', 'autofill onFillRequest'); 258 try { 259 let storage_fill = new LocalStorage( 260 { 261 'fillCallback': callback, 262 'message': "AutoFill Page", 263 'viewData': request.viewData, 264 }); 265 if (session) { 266 session.loadContent('pages/AutoFillPage', storage_fill); 267 } else { 268 hilog.error(0x0000, 'testTag', '%{public}s', 'session is null'); 269 } 270 } catch (err) { 271 hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content'); 272 } 273 } 274 } 275 ``` 276 277 ```ts 278 // AutoFillPage.ets 279 import autoFillManager from '@ohos.app.ability.autoFillManager'; 280 import Base from '@ohos.base'; 281 import hilog from '@ohos.hilog'; 282 283 let storage = LocalStorage.getShared(); 284 let fillCallback = storage.get<autoFillManager.FillRequestCallback>('fillCallback'); 285 286 @Entry 287 @Component 288 struct AutoFillPage { 289 290 build() { 291 Row() { 292 Column() { 293 Text('Hello World') 294 .fontSize(50) 295 .fontWeight(FontWeight.Bold) 296 } 297 298 Button('onCancel') 299 .onClick(() => { 300 hilog.info(0x0000, 'testTag', 'autofill cancel'); 301 try { 302 fillCallback.onCancel(); 303 } catch (error) { 304 console.error(`catch error, code: ${(error as Base.BusinessError).code}, 305 message: ${(error as Base.BusinessError).message}`); 306 } 307 }) 308 .width('100%') 309 } 310 .height('100%') 311 } 312 } 313 ``` 314 315## SaveRequestCallback 316 317Implements callbacks for an automatic or a manual saving request. 318 319### SaveRequestCallback.onSuccess 320 321onSuccess(): void 322 323Called when a saving request is successfully processed. 324 325**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 326 327**Error codes** 328 329| ID| Error Message| 330| ------- | -------------------------------- | 331| 16000050 | Internal error. | 332 333**Example** 334 335 ```ts 336 // MyAutoFillExtensionAbility.ts 337 import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility'; 338 import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 339 import autoFillManager from '@ohos.app.ability.autoFillManager'; 340 import hilog from '@ohos.hilog'; 341 342 class MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 343 onSaveRequest(session: UIExtensionContentSession, 344 request: autoFillManager.SaveRequest, 345 callback: autoFillManager.SaveRequestCallback) { 346 hilog.info(0x0000, 'testTag', '%{public}s', 'onSaveRequest'); 347 try { 348 let storage_save = new LocalStorage( 349 { 350 'message': "AutoFill Page", 351 'saveCallback': callback, 352 'viewData': request.viewData 353 }); 354 if (session) { 355 session.loadContent('pages/SavePage', storage_save); 356 } else { 357 hilog.error(0x0000, 'testTag', '%{public}s', 'session is null'); 358 } 359 } catch (err) { 360 hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content'); 361 } 362 } 363 } 364 ``` 365 366 ```ts 367 // SavePage.ets 368 import autoFillManager from '@ohos.app.ability.autoFillManager'; 369 import Base from '@ohos.base'; 370 import hilog from '@ohos.hilog'; 371 372 let storage = LocalStorage.getShared(); 373 let saveCallback = storage.get<autoFillManager.SaveRequestCallback>('saveCallback'); 374 375 @Entry 376 @Component 377 struct SavePage { 378 379 build() { 380 Row() { 381 Column() { 382 Text('SavePage') 383 .fontSize(50) 384 .fontWeight(FontWeight.Bold) 385 } 386 387 Button('onSuccess') 388 .onClick(() => { 389 hilog.info(0x0000, 'testTag', 'autosave success'); 390 try { 391 saveCallback.onSuccess(); 392 } catch (error) { 393 console.error(`catch error, code: ${(error as Base.BusinessError).code}, message: ${(error as Base.BusinessError).message}`); 394 } 395 }) 396 .width('100%') 397 } 398 .height('100%') 399 } 400 } 401 ``` 402 403### SaveRequestCallback.onFailure 404 405onFailure(): void 406 407Called when a saving request fails to be processed. 408 409**System capability**: SystemCapability.Ability.AbilityRuntime.AbilityCore 410 411**Error codes** 412 413| ID| Error Message| 414| ------- | -------------------------------- | 415| 16000050 | Internal error. | 416 417**Example** 418 419 ```ts 420 // MyAutoFillExtensionAbility.ts 421 import AutoFillExtensionAbility from '@ohos.app.ability.AutoFillExtensionAbility'; 422 import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 423 import autoFillManager from '@ohos.app.ability.autoFillManager'; 424 import hilog from '@ohos.hilog'; 425 426 class MyAutoFillExtensionAbility extends AutoFillExtensionAbility { 427 onSaveRequest(session: UIExtensionContentSession, 428 request: autoFillManager.SaveRequest, 429 callback: autoFillManager.SaveRequestCallback) { 430 hilog.info(0x0000, 'testTag', '%{public}s', 'onSaveRequest'); 431 try { 432 let storage_save = new LocalStorage( 433 { 434 'message': "AutoFill Page", 435 'saveCallback': callback, 436 'viewData': request.viewData 437 }); 438 if (session) { 439 session.loadContent('pages/SavePage', storage_save); 440 } else { 441 hilog.error(0x0000, 'testTag', '%{public}s', 'session is null'); 442 } 443 } catch (err) { 444 hilog.error(0x0000, 'testTag', '%{public}s', 'failed to load content'); 445 } 446 } 447 } 448 ``` 449 450 ```ts 451 // SavePage.ets 452 import autoFillManager from '@ohos.app.ability.autoFillManager'; 453 import Base from '@ohos.base'; 454 import hilog from '@ohos.hilog'; 455 456 let storage = LocalStorage.getShared(); 457 let saveCallback = storage.get<autoFillManager.SaveRequestCallback>('saveCallback'); 458 459 @Entry 460 @Component 461 struct SavePage { 462 build() { 463 Row() { 464 Column() { 465 Text('Save Page') 466 .fontSize(50) 467 .fontWeight(FontWeight.Bold) 468 } 469 470 Button('onFailure') 471 .onClick(() => { 472 hilog.info(0x0000, 'testTag', 'autofill failure'); 473 try { 474 saveCallback.onFailure(); 475 } catch (error) { 476 console.error(`catch error, code: ${(error as Base.BusinessError).code}, message: ${(error as Base.BusinessError).message}`); 477 } 478 }) 479 .width('100%') 480 } 481 .height('100%') 482 } 483 } 484 ``` 485