1# @ohos.data.cloudExtension (Device-Cloud Sharing Extension) 2 3The **cloudExtension** module provides APIs for third-party vendors to implement the device-cloud sharing service. You can use these APIs to share the device data to the server and implement device-cloud data sharing, including sharing and unsharing data, exiting a share, changing the privilege (operation permissions) on the shared data, querying participants by data identifier or invitation code, and confirming or changing a sharing invitation. 4 5Before you get started, it is helpful to understand the following concepts: 6 7- **sharingResource**: an identifier of the string type generated for each data record shared by an application when device-cloud sync is performed. It uniquely identifies the data record being shared. 8- **Participant**: all participants involved in a share, including the inviter and invitees. 9- **invitationCode**: an invitation code generated by the share server for a share operation. It is generated after a share is initiated and attached to an invitation to be pushed to the devices of target invitees. The target invitees then confirm the invitation via this code. 10- **CloudService**: device-cloud sync server, which implements data sync across devices with the same account for the same application. 11- **ShareCenter**: device-cloud sharing server, which implements cross-account and cross-device data sharing for the same application. 12 13> **NOTE** 14> 15> - 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. 16> 17> - The APIs provided by this module are system APIs. 18 19## Modules to Import 20 21```ts 22import cloudExtension from '@ohos.data.cloudExtension'; 23``` 24 25## Result<T> 26 27Represents the data sharing result. 28 29**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 30 31| Name | Type | Mandatory | Description | 32| ----------- | --------------------------- | --- | ------------ | 33| code | number | Yes | Error code. | 34| description | string | No | Detailed description of the error code. The default value is **undefined**. | 35| value | T | No | Value returned. The specific type is specified by the **T** parameter. The default value is **undefined**. | 36 37## cloudExtension.createCloudServiceStub 38 39createCloudServiceStub(instance: CloudService): Promise<rpc.RemoteObject> 40 41Creates a [RemoteObject](js-apis-rpc.md#remoteobject) object based on a [CloudService](#cloudservice) instance. The system calls the [CloudService](#cloudservice) interface through this object. This API uses a promise to return the result. 42 43**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 44 45**Parameters** 46 47| Name | Type | Mandatory| Description | 48| --------- | ------------------------------- | ---- | -------------------------------- | 49| instance | [CloudService](#cloudservice) | Yes | Instance of the [CloudService](#cloudservice) class. | 50 51**Return value** 52 53| Type | Description | 54| ------------------- | ------------------------- | 55| Promise<[rpc.RemoteObject](js-apis-rpc.md#remoteobject)> | Promise used to return the [RemoteObject](js-apis-rpc.md#remoteobject) object of [CloudService](#cloudservice) created.| 56 57**Example** 58 59```ts 60import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 61import Want from '@ohos.app.ability.Want'; 62import rpc from '@ohos.rpc'; 63 64export default class MyCloudService implements cloudExtension.CloudService { 65 constructor() {} 66 async connectShareCenter(userId: number, bundleName: string): Promise<rpc.RemoteObject> { 67 // ... 68 } 69} 70 71export default class MyServiceExtension extends ServiceExtensionAbility { 72 onCreate(want: Want) { 73 console.info(`onCreate: ${want}`); 74 } 75 onRequest(want: Want, startId: number) { 76 console.info(`onRequest: ${want} ${startId}`); 77 } 78 onConnect(want: Want): rpc.RemoteObject | Promise<rpc.RemoteObject> { 79 console.info(`onConnect: ${want}`); 80 return cloudExtension.createCloudServiceStub(new MyCloudService()); 81 } 82 onDisconnect(want: Want) { 83 console.info(`onDisconnect: ${want}`); 84 } 85 onDestroy() { 86 console.info('onDestroy'); 87 } 88} 89``` 90 91## cloudExtension.createShareServiceStub 92 93createShareServiceStub(instance: ShareCenter): Promise<rpc.RemoteObject> 94 95Creates a [RemoteObject](js-apis-rpc.md#remoteobject) object based on a [ShareCenter](#sharecenter) instance. The system calls the [ShareCenter](#sharecenter) interface through this object. This API uses a promise to return the result. 96 97**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 98 99**Parameters** 100 101| Name | Type | Mandatory| Description | 102| --------- | ------------------------------- | ---- | -------------------------------- | 103| instance | [ShareCenter](#sharecenter) | Yes | Instance of the [ShareCenter](#sharecenter) class. | 104 105**Return value** 106 107| Type | Description | 108| ------------------- | ------------------------- | 109| Promise<[rpc.RemoteObject](js-apis-rpc.md#remoteobject)> | Promise used to return the [RemoteObject](js-apis-rpc.md#remoteobject) object of [ShareCenter](#sharecenter) created.| 110 111**Example** 112 113```ts 114import rpc from '@ohos.rpc'; 115 116export default class MyShareCenter implements cloudExtension.ShareCenter { 117 constructor() {} 118 // ... 119} 120 121export default class MyCloudService implements cloudExtension.CloudService { 122 constructor() {} 123 async connectShareCenter(userId: number, bundleName: string): Promise<rpc.RemoteObject> { 124 console.info(`connect share center, bundle: ${bundleName}`); 125 return cloudExtension.createShareServiceStub(new MyShareCenter()); 126 } 127} 128``` 129 130## CloudService 131 132Provides a class for interworking with the cloud sync service. You need to inherit this class and implement APIs of this class. The system calls these APIs to interact and use the cloud sync service. 133 134### connectShareCenter 135 136connectShareCenter(userId: number, bundleName: string): Promise<rpc.RemoteObject> 137 138Obtains the [RemoteObject](js-apis-rpc.md#remoteobject) object of [ShareCenter](#sharecenter), which is created by [createShareServiceStub](#cloudextensioncreateshareservicestub). This API uses a promise to return the result. 139 140**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 141 142**Parameters** 143 144| Name | Type | Mandatory| Description | 145| ------- | ----------------------- | ---- | ----------------------------------------------- | 146| userId | number | Yes | User ID. | 147| bundleName | string | Yes | Bundle name of the application. | 148 149**Return value** 150 151| Type | Description | 152| ------------------- | ------------------------- | 153| Promise<[rpc.RemoteObject](js-apis-rpc.md#remoteobject)> | Promise used to return the [RemoteObject](js-apis-rpc.md#remoteobject) object of [ShareCenter](#sharecenter) obtained.| 154 155**Example** 156 157```ts 158import rpc from '@ohos.rpc'; 159 160export default class MyShareCenter implements cloudExtension.ShareCenter { 161 constructor() {} 162 // ... 163} 164 165export default class MyCloudService implements cloudExtension.CloudService { 166 constructor() {} 167 async connectShareCenter(userId: number, bundleName: string): Promise<rpc.RemoteObject> { 168 console.info(`connect share center, bundle: ${bundleName}`); 169 return cloudExtension.createShareServiceStub(new MyShareCenter()); 170 } 171} 172``` 173 174## ShareCenter 175 176Provides a class for interworking with the **sharedCenter** service. You need to inherit this class and implement APIs of this class. The system calls these APIs to initiate, cancel, or exit a device-cloud share. 177 178### share 179 180share(userId: number, bundleName: string, sharingResource: string, participants: Array<cloudData.sharing.Participant>): Promise<Result<Array<Result<cloudData.sharing.Participant>>>> 181 182Shares data. This API uses a promise to return the result. The application that initiates the share, shared resource ID, participants of the share need to be specified. 183 184**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 185 186**Parameters** 187 188| Name | Type | Mandatory| Description | 189| ------- | ----------------------- | ---- | ----------------------------------------------- | 190| userId | number | Yes | User ID. | 191| bundleName | string | Yes | Bundle name of the application. | 192| sharingResource | string | Yes | Shared resource ID. | 193| participants | Array<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)> | Yes | Participants of the share. | 194 195**Return value** 196 197| Type | Description | 198| ------------------- | ------------------------- | 199| Promise<[Result](#resultt)<Array<[Result](#resultt)<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)>>>> | Promise used to return the result.| 200 201**Example** 202 203```ts 204import cloudData from '@ohos.data.cloudData'; 205 206type Participant = cloudData.sharing.Participant; 207 208export default class MyShareCenter implements cloudExtension.ShareCenter { 209 constructor() {} 210 async share(userId: number, bundleName: string, sharingResource: string, participants: Array<Participant>): 211 Promise<cloudExtension.Result<Array<cloudExtension.Result<Participant>>>> { 212 console.info(`share, bundle: ${bundleName}`); 213 // Connect to ShareCenter and obtain the return value. 214 // ... 215 // Return the result obtained from ShareCenter. 216 let result: Array<cloudExtension.Result<Participant>> = []; 217 participants.forEach((item => { 218 result.push({ 219 code: cloudData.sharing.SharingCode.SUCCESS, 220 description: 'share succeeded' 221 }) 222 })) 223 return { 224 code: cloudData.sharing.SharingCode.SUCCESS, 225 description: 'share succeeded', 226 value: result 227 } 228 } 229 // ... 230} 231``` 232 233### unshare 234 235unshare(userId: number, bundleName: string, sharingResource: string, participants: Array<cloudData.sharing.Participant>): Promise<Result<Array<Result<cloudData.sharing.Participant>>>> 236 237Unshares data. This API uses a promise to return the result. The application, shared resource ID, and participants for the data to unshare need to be specified. 238 239**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 240 241**Parameters** 242 243| Name | Type | Mandatory| Description | 244| ------- | ----------------------- | ---- | ----------------------------------------------- | 245| userId | number | Yes | User ID. | 246| bundleName | string | Yes | Bundle name of the application. | 247| sharingResource | string | Yes | Shared resource ID. | 248| participants | Array<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)> | Yes | Participants of the share. | 249 250**Return value** 251 252| Type | Description | 253| ------------------- | ------------------------- | 254| Promise<[Result](#resultt)<Array<[Result](#resultt)<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)>>>> | Promise used to return the result.| 255 256**Example** 257 258```ts 259import cloudData from '@ohos.data.cloudData'; 260 261type Participant = cloudData.sharing.Participant; 262 263export default class MyShareCenter implements cloudExtension.ShareCenter { 264 constructor() {} 265 async unshare(userId: number, bundleName: string, sharingResource: string, participants: Array<Participant>): 266 Promise<cloudExtension.Result<Array<cloudExtension.Result<Participant>>>> { 267 console.info(`unshare, bundle: ${bundleName}`); 268 //Connect to ShareCenter and obtain the return value of the unshare operation. 269 // ... 270 // Return the result obtained from ShareCenter. 271 let result: Array<cloudExtension.Result<Participant>> = []; 272 participants.forEach((item => { 273 result.push({ 274 code: cloudData.sharing.SharingCode.SUCCESS, 275 description: 'unshare succeeded' 276 }) 277 })) 278 return { 279 code: cloudData.sharing.SharingCode.SUCCESS, 280 description: 'unshare succeeded', 281 value: result 282 } 283 } 284 // ... 285} 286``` 287 288### exit 289 290exit(userId: number, bundleName: string, sharingResource: string): Promise<Result<void>> 291 292Exits a device-cloud share. This API uses a promise to return the result. The application and shared resource ID need to be specified. 293 294**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 295 296**Parameters** 297 298| Name | Type | Mandatory| Description | 299| ------- | ----------------------- | ---- | ----------------------------------------------- | 300| userId | number | Yes | User ID. | 301| bundleName | string | Yes | Bundle name of the application. | 302| sharingResource | string | Yes | Shared resource ID. | 303 304**Return value** 305 306| Type | Description | 307| ------------------- | ------------------------- | 308| Promise<[Result](#resultt)<void>> | Promise used to return the result.| 309 310**Example** 311 312```ts 313import cloudData from '@ohos.data.cloudData'; 314 315export default class MyShareCenter implements cloudExtension.ShareCenter { 316 constructor() {} 317 async exit(userId: number, bundleName: string, sharingResource: string): 318 Promise<cloudExtension.Result<void>> { 319 console.info(`exit share, bundle: ${bundleName}`); 320 // Connect to ShareCenter and obtain the return value of the exit operation. 321 // ... 322 // Return the result obtained from ShareCenter. 323 return { 324 code: cloudData.sharing.SharingCode.SUCCESS, 325 description: 'exit share succeeded' 326 } 327 } 328 // ... 329} 330``` 331 332### changePrivilege 333 334changePrivilege(userId: number, bundleName: string, sharingResource: string, participants: Array<cloudData.sharing.Participant>): Promise<Result<Array<Result<cloudData.sharing.Participant>>>> 335 336Changes the privilege (operation permissions) on the shared data. This API uses a promise to return the result. The application, shared resource ID, and the participants with new privilege need to be specified. 337 338**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 339 340**Parameters** 341 342| Name | Type | Mandatory| Description | 343| ------- | ----------------------- | ---- | ----------------------------------------------- | 344| userId | number | Yes | User ID. | 345| bundleName | string | Yes | Bundle name of the application. | 346| sharingResource | string | Yes | Shared resource ID. | 347| participants | Array<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)> | Yes | Participants with new privilege. | 348 349**Return value** 350 351| Type | Description | 352| ------------------- | ------------------------- | 353| Promise<[Result](#resultt)<Array<[Result](#resultt)<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)>>>> | Promise used to return the result.| 354 355**Example** 356 357```ts 358import cloudData from '@ohos.data.cloudData'; 359 360type Participant = cloudData.sharing.Participant; 361 362export default class MyShareCenter implements cloudExtension.ShareCenter { 363 constructor() {} 364 async changePrivilege(userId: number, bundleName: string, sharingResource: string, participants: Array<Participant>): 365 Promise<cloudExtension.Result<Array<cloudExtension.Result<Participant>>>> { 366 console.info(`change privilege, bundle: ${bundleName}`); 367 // Connect to ShareCenter and obtain the return value of the privilege change operation. 368 // ... 369 // Return the result obtained from ShareCenter. 370 let result: Array<cloudExtension.Result<Participant>> = []; 371 participants.forEach((item => { 372 result.push({ 373 code: cloudData.sharing.SharingCode.SUCCESS, 374 description: 'change privilege succeeded' 375 }) 376 })) 377 return { 378 code: cloudData.sharing.SharingCode.SUCCESS, 379 description: 'change privilege succeeded', 380 value: result 381 } 382 } 383 // ... 384} 385``` 386 387### queryParticipants 388 389queryParticipants(userId: number, bundleName: string, sharingResource: string): Promise<Result<Array<cloudData.sharing.Participant>>> 390 391Queries the participants of a share. This API uses a promise to return the result. The application and shared resource ID need to be specified. 392 393**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 394 395**Parameters** 396 397| Name | Type | Mandatory| Description | 398| ------- | ----------------------- | ---- | ----------------------------------------------- | 399| userId | number | Yes | User ID. | 400| bundleName | string | Yes | Bundle name of the application. | 401| sharingResource | string | Yes | Shared resource ID. | 402 403**Return value** 404 405| Type | Description | 406| ------------------------------------------------------------ | --------------------------------------- | 407| Promise<[Result](#resultt)<Array<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)>>> | Promise used to return the participants obtained.| 408 409**Example** 410 411```ts 412import cloudData from '@ohos.data.cloudData'; 413 414type Participant = cloudData.sharing.Participant; 415 416export default class MyShareCenter implements cloudExtension.ShareCenter { 417 constructor() {} 418 async queryParticipants(userId: number, bundleName: string, sharingResource: string): 419 Promise<cloudExtension.Result<Array<Participant>>> { 420 console.info(`query participants, bundle: ${bundleName}`); 421 // Connect to ShareCenter and obtain the return value of the query operation. 422 // ... 423 // Return the result obtained from ShareCenter. 424 let participants = new Array<cloudData.sharing.Participant>(); 425 participants.push({ 426 identity: '000000000', 427 role: cloudData.sharing.Role.ROLE_INVITEE, 428 state: cloudData.sharing.State.STATE_ACCEPTED, 429 privilege: { 430 writable: false, 431 readable: true, 432 creatable: false, 433 deletable: false, 434 shareable: false 435 }, 436 attachInfo: '' 437 }) 438 participants.push({ 439 identity: '111111111', 440 role: cloudData.sharing.Role.ROLE_INVITEE, 441 state: cloudData.sharing.State.STATE_ACCEPTED, 442 privilege: { 443 writable: false, 444 readable: true, 445 creatable: false, 446 deletable: false, 447 shareable: false 448 }, 449 attachInfo: '' 450 }) 451 return { 452 code: cloudData.sharing.SharingCode.SUCCESS, 453 description: 'query participants succeeded', 454 value: participants 455 } 456 } 457 // ... 458} 459``` 460 461### queryParticipantsByInvitation 462 463queryParticipantsByInvitation(userId: number, bundleName: string, invitationCode: string): Promise<Result<Array<cloudData.sharing.Participant>>> 464 465Queries the participants of a share based on the invitation code. This API uses a promise to return the result. The application and the invitation code of the shared data need to be specified. 466 467**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 468 469**Parameters** 470 471| Name | Type | Mandatory| Description | 472| ------- | ----------------------- | ---- | ----------------------------------------------- | 473| userId | number | Yes | User ID. | 474| bundleName | string | Yes | Bundle name of the application. | 475| invitationCode | string | Yes | Invitation code for the share. | 476 477**Return value** 478 479| Type | Description | 480| ------------------- | ------------------------- | 481| Promise<[Result](#resultt)<Array<[cloudData.sharing.Participant](js-apis-data-cloudData.md#participant11)>>> | Promise used to return the participants obtained.| 482 483**Example** 484 485```ts 486import cloudData from '@ohos.data.cloudData'; 487 488type Participant = cloudData.sharing.Participant; 489 490export default class MyShareCenter implements cloudExtension.ShareCenter { 491 constructor() {} 492 async queryParticipantsByInvitation(userId: number, bundleName: string, invitationCode: string): 493 Promise<cloudExtension.Result<Array<Participant>>> { 494 console.info(`query participants by invitation, bundle: ${bundleName}`); 495 // Connect to ShareCenter and obtain the return value of the query operation. 496 // ... 497 // Return the result obtained from ShareCenter. 498 let participants = new Array<cloudData.sharing.Participant>(); 499 participants.push({ 500 identity: '000000000', 501 role: cloudData.sharing.Role.ROLE_INVITEE, 502 state: cloudData.sharing.State.STATE_ACCEPTED, 503 privilege: { 504 writable: false, 505 readable: true, 506 creatable: false, 507 deletable: false, 508 shareable: false 509 }, 510 attachInfo: '' 511 }) 512 participants.push({ 513 identity: '111111111', 514 role: cloudData.sharing.Role.ROLE_INVITEE, 515 state: cloudData.sharing.State.STATE_ACCEPTED, 516 privilege: { 517 writable: false, 518 readable: true, 519 creatable: false, 520 deletable: false, 521 shareable: false 522 }, 523 attachInfo: '' 524 }) 525 return { 526 code: cloudData.sharing.SharingCode.SUCCESS, 527 description: 'query participants by invitation succeeded', 528 value: participants 529 } 530 } 531 // ... 532} 533``` 534 535### confirmInvitation 536 537confirmInvitation(userId: number, bundleName: string, invitationCode: string, state: cloudData.sharing.State): Promise<Result<string>> 538 539Confirms the invitation for a share. This API uses a promise to return the result. The application, invitation code for the share, and the confirmation state need to be specified. 540 541**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 542 543**Parameters** 544 545| Name | Type | Mandatory| Description | 546| ------- | ----------------------- | ---- | ----------------------------------------------- | 547| userId | number | Yes | User ID. | 548| bundleName | string | Yes | Bundle name of the application. | 549| invitationCode | string | Yes | Invitation code for the share. | 550| state | [cloudData.sharing.State](js-apis-data-cloudData.md#state11) | Yes | Confirmation state of the invitation. | 551 552**Return value** 553 554| Type | Description | 555| ------------------- | ------------------------- | 556| Promise<[Result](#resultt)<string>> | Promise used to return the shared resource ID.| 557 558**Example** 559 560```ts 561import cloudData from '@ohos.data.cloudData'; 562 563export default class MyShareCenter implements cloudExtension.ShareCenter { 564 constructor() {} 565 async confirmInvitation(userId: number, bundleName: string, invitationCode: string, state: cloudData.sharing.State): 566 Promise<cloudExtension.Result<string>> { 567 console.info(`confirm invitation, bundle: ${bundleName}`); 568 // Connect to ShareCenter and obtain the return value of the invitation confirmation operation. 569 // ... 570 // Return the result obtained from ShareCenter. 571 return { 572 code: cloudData.sharing.SharingCode.SUCCESS, 573 description: 'confirm invitation succeeded', 574 value: 'sharing_resource_test' 575 } 576 } 577 // ... 578} 579``` 580 581### changeConfirmation 582 583changeConfirmation(userId: number, bundleName: string, sharingResource: string, state: cloudData.sharing.State): Promise<Result<void>> 584 585Changes the confirmation state of a share invitation. This API uses a promise to return the result. The application, shared resource ID, and the new conformation state need to be specified. 586 587**System capability**: SystemCapability.DistributedDataManager.CloudSync.Server 588 589**Parameters** 590 591| Name | Type | Mandatory| Description | 592| ------- | ----------------------- | ---- | ----------------------------------------------- | 593| userId | number | Yes | User ID. | 594| bundleName | string | Yes | Bundle name of the application. | 595| sharingResource | string | Yes | Shared resource ID. | 596| state | [cloudData.sharing.State](js-apis-data-cloudData.md#state11) | Yes | New confirmation state. | 597 598**Return value** 599 600| Type | Description | 601| ------------------- | ------------------------- | 602| Promise<[Result](#resultt)<void>> | Promise used to return the result.| 603 604**Example** 605 606```ts 607import cloudData from '@ohos.data.cloudData'; 608 609export default class MyShareCenter implements cloudExtension.ShareCenter { 610 constructor() {} 611 async changeConfirmation(userId: number, bundleName: string, sharingResource: string, state: cloudData.sharing.State): 612 Promise<cloudExtension.Result<void>> { 613 console.info(`change confirm, bundle: ${bundleName}`); 614 // Connect to ShareCenter and obtain the return value of the state change operation. 615 // ... 616 // Return the result obtained from ShareCenter. 617 return { 618 code: cloudData.sharing.SharingCode.SUCCESS, 619 description: 'change confirm succeeded' 620 } 621 } 622 // ... 623} 624``` 625## Complete Sample Code 626 627The classes in the preceding examples are implemented using **implements**, and the sample code cannot be executed independently until all the methods in the parent classes are implemented. The following provides complete sample code for your reference. 628 629```ts 630import cloudExtension from '@ohos.data.cloudExtension'; 631import ServiceExtensionAbility from '@ohos.app.ability.ServiceExtensionAbility'; 632import Want from '@ohos.app.ability.Want'; 633import rpc from '@ohos.rpc'; 634import cloudData from '@ohos.data.cloudData'; 635 636type Participant = cloudData.sharing.Participant; 637 638class MyShareCenter implements cloudExtension.ShareCenter { 639 constructor() { 640 } 641 642 async share(userId: number, bundleName: string, sharingResource: string, participants: Array<Participant>): 643 Promise<cloudExtension.Result<Array<cloudExtension.Result<Participant>>>> { 644 console.info(`share, bundle: ${bundleName}`); 645 // Connect to ShareCenter and obtain the return value. 646 // ... 647 // Return the result obtained from ShareCenter. 648 let result: Array<cloudExtension.Result<Participant>> = []; 649 participants.forEach((item => { 650 result.push({ 651 code: cloudData.sharing.SharingCode.SUCCESS, 652 description: 'share succeeded' 653 }) 654 })) 655 return { 656 code: cloudData.sharing.SharingCode.SUCCESS, 657 description: 'share succeeded', 658 value: result 659 } 660 } 661 662 async unshare(userId: number, bundleName: string, sharingResource: string, participants: Array<Participant>): 663 Promise<cloudExtension.Result<Array<cloudExtension.Result<Participant>>>> { 664 console.info(`unshare, bundle: ${bundleName}`); 665 //Connect to ShareCenter and obtain the return value of the unshare operation. 666 // ... 667 // Return the result obtained from ShareCenter. 668 let result: Array<cloudExtension.Result<Participant>> = []; 669 participants.forEach((item => { 670 result.push({ 671 code: cloudData.sharing.SharingCode.SUCCESS, 672 description: 'unshare succeeded' 673 }) 674 })) 675 return { 676 code: cloudData.sharing.SharingCode.SUCCESS, 677 description: 'unshare succeeded', 678 value: result 679 } 680 } 681 682 async exit(userId: number, bundleName: string, sharingResource: string): 683 Promise<cloudExtension.Result<void>> { 684 console.info(`exit share, bundle: ${bundleName}`); 685 // Connect to ShareCenter and obtain the return value of the exit operation. 686 // ... 687 // Return the result obtained from ShareCenter. 688 return { 689 code: cloudData.sharing.SharingCode.SUCCESS, 690 description: 'exit share succeeded' 691 } 692 } 693 694 async changePrivilege(userId: number, bundleName: string, sharingResource: string, participants: Array<Participant>): 695 Promise<cloudExtension.Result<Array<cloudExtension.Result<Participant>>>> { 696 console.info(`change privilege, bundle: ${bundleName}`); 697 // Connect to ShareCenter and obtain the return value of the privilege change operation. 698 // ... 699 // Return the result obtained from ShareCenter. 700 let result: Array<cloudExtension.Result<Participant>> = []; 701 participants.forEach((item => { 702 result.push({ 703 code: cloudData.sharing.SharingCode.SUCCESS, 704 description: 'change privilege succeeded' 705 }) 706 })) 707 return { 708 code: cloudData.sharing.SharingCode.SUCCESS, 709 description: 'change privilege succeeded', 710 value: result 711 } 712 } 713 714 async queryParticipants(userId: number, bundleName: string, sharingResource: string): 715 Promise<cloudExtension.Result<Array<Participant>>> { 716 console.info(`query participants, bundle: ${bundleName}`); 717 // Connect to ShareCenter and obtain the return value of the query operation. 718 // ... 719 // Return the result obtained from ShareCenter. 720 let participants = new Array<cloudData.sharing.Participant>(); 721 participants.push({ 722 identity: '000000000', 723 role: cloudData.sharing.Role.ROLE_INVITEE, 724 state: cloudData.sharing.State.STATE_ACCEPTED, 725 privilege: { 726 writable: false, 727 readable: true, 728 creatable: false, 729 deletable: false, 730 shareable: false 731 }, 732 attachInfo: '' 733 }) 734 participants.push({ 735 identity: '111111111', 736 role: cloudData.sharing.Role.ROLE_INVITEE, 737 state: cloudData.sharing.State.STATE_ACCEPTED, 738 privilege: { 739 writable: false, 740 readable: true, 741 creatable: false, 742 deletable: false, 743 shareable: false 744 }, 745 attachInfo: '' 746 }) 747 return { 748 code: cloudData.sharing.SharingCode.SUCCESS, 749 description: 'query participants succeeded', 750 value: participants 751 } 752 } 753 754 async queryParticipantsByInvitation(userId: number, bundleName: string, invitationCode: string): 755 Promise<cloudExtension.Result<Array<Participant>>> { 756 console.info(`query participants by invitation, bundle: ${bundleName}`); 757 // Connect to ShareCenter and obtain the return value of the query operation. 758 // ... 759 // Return the result obtained from ShareCenter. 760 let participants = new Array<cloudData.sharing.Participant>(); 761 participants.push({ 762 identity: '000000000', 763 role: cloudData.sharing.Role.ROLE_INVITEE, 764 state: cloudData.sharing.State.STATE_ACCEPTED, 765 privilege: { 766 writable: false, 767 readable: true, 768 creatable: false, 769 deletable: false, 770 shareable: false 771 }, 772 attachInfo: '' 773 }) 774 participants.push({ 775 identity: '111111111', 776 role: cloudData.sharing.Role.ROLE_INVITEE, 777 state: cloudData.sharing.State.STATE_ACCEPTED, 778 privilege: { 779 writable: false, 780 readable: true, 781 creatable: false, 782 deletable: false, 783 shareable: false 784 }, 785 attachInfo: '' 786 }) 787 return { 788 code: cloudData.sharing.SharingCode.SUCCESS, 789 description: 'query participants by invitation succeeded', 790 value: participants 791 } 792 } 793 794 async confirmInvitation(userId: number, bundleName: string, invitationCode: string, state: cloudData.sharing.State): 795 Promise<cloudExtension.Result<string>> { 796 console.info(`confirm invitation, bundle: ${bundleName}`); 797 // Connect to ShareCenter and obtain the return value of the invitation confirmation operation. 798 // ... 799 // Return the result obtained from ShareCenter. 800 return { 801 code: cloudData.sharing.SharingCode.SUCCESS, 802 description: 'confirm invitation succeeded', 803 value: 'sharing_resource_test' 804 } 805 } 806 807 async changeConfirmation(userId: number, bundleName: string, sharingResource: string, state: cloudData.sharing.State): 808 Promise<cloudExtension.Result<void>> { 809 console.info(`change confirm, bundle: ${bundleName}`); 810 // Connect to ShareCenter and obtain the return value of the state change operation. 811 // ... 812 // Return the result obtained from ShareCenter. 813 return { 814 code: cloudData.sharing.SharingCode.SUCCESS, 815 description: 'change confirm succeeded' 816 } 817 } 818} 819 820class MyCloudService implements cloudExtension.CloudService { 821 constructor() { 822 } 823 824 async connectShareCenter(userId: number, bundleName: string): Promise<rpc.RemoteObject> { 825 console.info(`connect share center, bundle: ${bundleName}`); 826 return cloudExtension.createShareServiceStub(new MyShareCenter()); 827 } 828} 829 830export default class MyServiceExtension extends ServiceExtensionAbility { 831 onCreate(want: Want) { 832 console.info(`onCreate: ${want}`); 833 } 834 835 onRequest(want: Want, startId: number) { 836 console.info(`onRequest: ${want} ${startId}`); 837 } 838 839 onConnect(want: Want): rpc.RemoteObject | Promise<rpc.RemoteObject> { 840 console.info(`onConnect: ${want}`); 841 return cloudExtension.createCloudServiceStub(new MyCloudService()); 842 } 843 844 onDisconnect(want: Want) { 845 console.info(`onDisconnect: ${want}`); 846 } 847 848 onDestroy() { 849 console.info('onDestroy'); 850 } 851} 852``` 853<!--no_check--> 854