1e41f4b71Sopenharmony_ci# @ohos.app.ability.sendableContextManager (sendableContextManager) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe sendableContextManager module provides APIs for converting between Context and [SendableContext](js-apis-inner-application-sendableContext.md) objects. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> - The initial APIs of this module are supported since API version 12. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8e41f4b71Sopenharmony_ci> - The APIs of this module can be used only in the stage model. 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci## When to Use 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ciThis module is used to transfer data between concurrent ArkTS instances (including the main thread and the worker thread of TaskPool or Worker). 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ciFor example, when transferring sendable data from the main thread to a child thread, the following conversion steps are involved to ensure efficient data transfer: 15e41f4b71Sopenharmony_ci- Conversion from Context to SendableContext for the main thread to transfer sendable data to the child thread. 16e41f4b71Sopenharmony_ci- Conversion from SendableContext to Context for the child thread to use the sendable data. 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ciThe Context here is different from that created by [createModuleContext](js-apis-inner-application-context.md#contextcreatemodulecontext). The differences are as follows: 19e41f4b71Sopenharmony_ci- Context involved in the conversion: ArkTS concurrent instances hold different application-side Context instances that correspond to the same underlying Context object. When the Context properties and methods in an instance are modified, the Context properties and methods in the related instances are modified accordingly. The eventHub attribute in the Context instance is special. The eventHub objects in different instances are independent from each other and cannot be used across ArkTS instances. 20e41f4b71Sopenharmony_ci- Context created using [createModuleContext](js-apis-inner-application-context.md#contextcreatemodulecontext): ArkTS concurrent instances hold different application-side Context objects that correspond to different underlying Context objects. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci## Constraints 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ciThe Context types used in the conversion must be the same. Currently, the following types of Context support conversion: [Context](js-apis-inner-application-context.md), [ApplicationContext](js-apis-inner-application-applicationContext.md), [AbilityStageContext](js-apis-inner-application-abilityStageContext.md), and [UIAbilityContext](js-apis-inner-application-uiAbilityContext.md). 25e41f4b71Sopenharmony_ci 26e41f4b71Sopenharmony_ci## Modules to Import 27e41f4b71Sopenharmony_ci 28e41f4b71Sopenharmony_ci```ts 29e41f4b71Sopenharmony_ciimport { sendableContextManager } from '@kit.AbilityKit'; 30e41f4b71Sopenharmony_ci``` 31e41f4b71Sopenharmony_ci 32e41f4b71Sopenharmony_ci## Properties 33e41f4b71Sopenharmony_ci 34e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 35e41f4b71Sopenharmony_ci 36e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 39e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 40e41f4b71Sopenharmony_ci| SendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | Yes| Level-2 module **SendableContext**.| 41e41f4b71Sopenharmony_ci 42e41f4b71Sopenharmony_ci**Example** 43e41f4b71Sopenharmony_ci 44e41f4b71Sopenharmony_ci```ts 45e41f4b71Sopenharmony_ciimport { sendableContextManager } from '@kit.AbilityKit'; 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_cilet sendableContext: sendableContextManager.SendableContext; 48e41f4b71Sopenharmony_ci``` 49e41f4b71Sopenharmony_ci 50e41f4b71Sopenharmony_ci## sendableContextManager.convertFromContext 51e41f4b71Sopenharmony_ci 52e41f4b71Sopenharmony_ciconvertFromContext(context: common.Context): SendableContext; 53e41f4b71Sopenharmony_ci 54e41f4b71Sopenharmony_ciConverts a **Context** object to a **SendableContext** object. 55e41f4b71Sopenharmony_ci 56e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 57e41f4b71Sopenharmony_ci 58e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 59e41f4b71Sopenharmony_ci 60e41f4b71Sopenharmony_ci**Parameters** 61e41f4b71Sopenharmony_ci 62e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 63e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 64e41f4b71Sopenharmony_ci| context | common.Context | Yes| **Context** object, which supports the base class **Context** and the child classes **ApplicationContext**, **AbilityStageContext**, and **UIAbilityContext**.| 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**Error codes** 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 69e41f4b71Sopenharmony_ci 70e41f4b71Sopenharmony_ci| ID| Error Message| 71e41f4b71Sopenharmony_ci| ------- | ------- | 72e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 73e41f4b71Sopenharmony_ci 74e41f4b71Sopenharmony_ci**Example** 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci```ts 77e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, sendableContextManager } from '@kit.AbilityKit'; 78e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 79e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci@Sendable 82e41f4b71Sopenharmony_ciexport class SendableObject { 83e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext) { 84e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 85e41f4b71Sopenharmony_ci } 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 88e41f4b71Sopenharmony_ci // other sendable object 89e41f4b71Sopenharmony_ci} 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 92e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 95e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci // convert and post 98e41f4b71Sopenharmony_ci try { 99e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 100e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext); 101e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 102e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 103e41f4b71Sopenharmony_ci } catch (error) { 104e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 105e41f4b71Sopenharmony_ci } 106e41f4b71Sopenharmony_ci } 107e41f4b71Sopenharmony_ci} 108e41f4b71Sopenharmony_ci``` 109e41f4b71Sopenharmony_ci 110e41f4b71Sopenharmony_ci## sendableContextManager.convertToContext 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ciconvertToContext(sendableContext: SendableContext): common.Context 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ciConverts a **SendableContext** object to a **Context** object. 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci**Parameters** 121e41f4b71Sopenharmony_ci 122e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 123e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 124e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | Yes| **SendableContext** object.| 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**Error codes** 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 129e41f4b71Sopenharmony_ci 130e41f4b71Sopenharmony_ci| ID| Error Message| 131e41f4b71Sopenharmony_ci| ------- | ------- | 132e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 133e41f4b71Sopenharmony_ci 134e41f4b71Sopenharmony_ci**Example** 135e41f4b71Sopenharmony_ci 136e41f4b71Sopenharmony_ciContext passed by the main thread: 137e41f4b71Sopenharmony_ci```ts 138e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 139e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 140e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_ci@Sendable 143e41f4b71Sopenharmony_ciexport class SendableObject { 144e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 145e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 146e41f4b71Sopenharmony_ci this.contextName = contextName; 147e41f4b71Sopenharmony_ci } 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 150e41f4b71Sopenharmony_ci contextName: string; 151e41f4b71Sopenharmony_ci} 152e41f4b71Sopenharmony_ci 153e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 154e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 155e41f4b71Sopenharmony_ci 156e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 157e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ci // convert and post 160e41f4b71Sopenharmony_ci try { 161e41f4b71Sopenharmony_ci let context: common.Context = this.context as common.Context; 162e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(context); 163e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'BaseContext'); 164e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 165e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 166e41f4b71Sopenharmony_ci } catch (error) { 167e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 168e41f4b71Sopenharmony_ci } 169e41f4b71Sopenharmony_ci } 170e41f4b71Sopenharmony_ci} 171e41f4b71Sopenharmony_ci``` 172e41f4b71Sopenharmony_ci 173e41f4b71Sopenharmony_ciContext received by the Worker thread: 174e41f4b71Sopenharmony_ci```ts 175e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 176e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 177e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 178e41f4b71Sopenharmony_ci 179e41f4b71Sopenharmony_ci@Sendable 180e41f4b71Sopenharmony_ciexport class SendableObject { 181e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 182e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 183e41f4b71Sopenharmony_ci this.contextName = contextName; 184e41f4b71Sopenharmony_ci } 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 187e41f4b71Sopenharmony_ci contextName: string; 188e41f4b71Sopenharmony_ci} 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 193e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 194e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 195e41f4b71Sopenharmony_ci if (object.contextName == 'BaseContext') { 196e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to context.'); 197e41f4b71Sopenharmony_ci try { 198e41f4b71Sopenharmony_ci let context: common.Context = sendableContextManager.convertToContext(sendableContext); 199e41f4b71Sopenharmony_ci // Obtain the sandbox path after obtaining the Context object. 200e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 201e41f4b71Sopenharmony_ci } catch (error) { 202e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToContext failed %{public}s', JSON.stringify(error)); 203e41f4b71Sopenharmony_ci } 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci} 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 208e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 209e41f4b71Sopenharmony_ci} 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 212e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 213e41f4b71Sopenharmony_ci} 214e41f4b71Sopenharmony_ci``` 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci## sendableContextManager.convertToApplicationContext 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ciconvertToApplicationContext(sendableContext: SendableContext): common.ApplicationContext 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ciConverts a **SendableContext** object to an **ApplicationContext** object. 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci**Parameters** 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 229e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 230e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | Yes| **SendableContext** object.| 231e41f4b71Sopenharmony_ci 232e41f4b71Sopenharmony_ci**Error codes** 233e41f4b71Sopenharmony_ci 234e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 235e41f4b71Sopenharmony_ci 236e41f4b71Sopenharmony_ci| ID| Error Message| 237e41f4b71Sopenharmony_ci| ------- | ------- | 238e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 239e41f4b71Sopenharmony_ci 240e41f4b71Sopenharmony_ci**Example** 241e41f4b71Sopenharmony_ci 242e41f4b71Sopenharmony_ciContext passed by the main thread: 243e41f4b71Sopenharmony_ci```ts 244e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 245e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 246e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 247e41f4b71Sopenharmony_ci 248e41f4b71Sopenharmony_ci@Sendable 249e41f4b71Sopenharmony_ciexport class SendableObject { 250e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 251e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 252e41f4b71Sopenharmony_ci this.contextName = contextName; 253e41f4b71Sopenharmony_ci } 254e41f4b71Sopenharmony_ci 255e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 256e41f4b71Sopenharmony_ci contextName: string; 257e41f4b71Sopenharmony_ci} 258e41f4b71Sopenharmony_ci 259e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 260e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 263e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci // convert and post 266e41f4b71Sopenharmony_ci try { 267e41f4b71Sopenharmony_ci let context: common.Context = this.context as common.Context; 268e41f4b71Sopenharmony_ci let applicationContext = context.getApplicationContext(); 269e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(applicationContext); 270e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'ApplicationContext'); 271e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 272e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 273e41f4b71Sopenharmony_ci } catch (error) { 274e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci } 277e41f4b71Sopenharmony_ci} 278e41f4b71Sopenharmony_ci``` 279e41f4b71Sopenharmony_ci 280e41f4b71Sopenharmony_ciContext received by the Worker thread: 281e41f4b71Sopenharmony_ci```ts 282e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 283e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 284e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci@Sendable 287e41f4b71Sopenharmony_ciexport class SendableObject { 288e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 289e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 290e41f4b71Sopenharmony_ci this.contextName = contextName; 291e41f4b71Sopenharmony_ci } 292e41f4b71Sopenharmony_ci 293e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 294e41f4b71Sopenharmony_ci contextName: string; 295e41f4b71Sopenharmony_ci} 296e41f4b71Sopenharmony_ci 297e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 298e41f4b71Sopenharmony_ci 299e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 300e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 301e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 302e41f4b71Sopenharmony_ci if (object.contextName == 'ApplicationContext') { 303e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to application context.'); 304e41f4b71Sopenharmony_ci try { 305e41f4b71Sopenharmony_ci let context: common.ApplicationContext = sendableContextManager.convertToApplicationContext(sendableContext); 306e41f4b71Sopenharmony_ci // Obtain the sandbox path after obtaining the Context object. 307e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 308e41f4b71Sopenharmony_ci } catch (error) { 309e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToApplicationContext failed %{public}s', JSON.stringify(error)); 310e41f4b71Sopenharmony_ci } 311e41f4b71Sopenharmony_ci } 312e41f4b71Sopenharmony_ci} 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 315e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 316e41f4b71Sopenharmony_ci} 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 319e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 320e41f4b71Sopenharmony_ci} 321e41f4b71Sopenharmony_ci``` 322e41f4b71Sopenharmony_ci 323e41f4b71Sopenharmony_ci## sendableContextManager.convertToAbilityStageContext 324e41f4b71Sopenharmony_ci 325e41f4b71Sopenharmony_ciconvertToAbilityStageContext(sendableContext: SendableContext): common.AbilityStageContext 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ciConverts a **SendableContext** object to an **AbilityStageContext** object. 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 330e41f4b71Sopenharmony_ci 331e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 332e41f4b71Sopenharmony_ci 333e41f4b71Sopenharmony_ci**Parameters** 334e41f4b71Sopenharmony_ci 335e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 336e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 337e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | Yes| **SendableContext** object.| 338e41f4b71Sopenharmony_ci 339e41f4b71Sopenharmony_ci**Error codes** 340e41f4b71Sopenharmony_ci 341e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 342e41f4b71Sopenharmony_ci 343e41f4b71Sopenharmony_ci| ID| Error Message| 344e41f4b71Sopenharmony_ci| ------- | ------- | 345e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ci**Example** 348e41f4b71Sopenharmony_ci 349e41f4b71Sopenharmony_ciContext passed by the main thread: 350e41f4b71Sopenharmony_ci```ts 351e41f4b71Sopenharmony_ciimport { UIAbility, sendableContextManager } from '@kit.AbilityKit'; 352e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 353e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 354e41f4b71Sopenharmony_ci 355e41f4b71Sopenharmony_ci@Sendable 356e41f4b71Sopenharmony_ciexport class SendableObject { 357e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 358e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 359e41f4b71Sopenharmony_ci this.contextName = contextName; 360e41f4b71Sopenharmony_ci } 361e41f4b71Sopenharmony_ci 362e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 363e41f4b71Sopenharmony_ci contextName: string; 364e41f4b71Sopenharmony_ci} 365e41f4b71Sopenharmony_ci 366e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 367e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 368e41f4b71Sopenharmony_ci 369e41f4b71Sopenharmony_ci onCreate(): void { 370e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage onCreate'); 371e41f4b71Sopenharmony_ci 372e41f4b71Sopenharmony_ci // convert and post 373e41f4b71Sopenharmony_ci try { 374e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 375e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'AbilityStageContext'); 376e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'AbilityStage post message'); 377e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 378e41f4b71Sopenharmony_ci } catch (error) { 379e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 380e41f4b71Sopenharmony_ci } 381e41f4b71Sopenharmony_ci } 382e41f4b71Sopenharmony_ci} 383e41f4b71Sopenharmony_ci``` 384e41f4b71Sopenharmony_ci 385e41f4b71Sopenharmony_ciContext received by the Worker thread: 386e41f4b71Sopenharmony_ci```ts 387e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 388e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 389e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 390e41f4b71Sopenharmony_ci 391e41f4b71Sopenharmony_ci@Sendable 392e41f4b71Sopenharmony_ciexport class SendableObject { 393e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 394e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 395e41f4b71Sopenharmony_ci this.contextName = contextName; 396e41f4b71Sopenharmony_ci } 397e41f4b71Sopenharmony_ci 398e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 399e41f4b71Sopenharmony_ci contextName: string; 400e41f4b71Sopenharmony_ci} 401e41f4b71Sopenharmony_ci 402e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 403e41f4b71Sopenharmony_ci 404e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 405e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 406e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 407e41f4b71Sopenharmony_ci if (object.contextName == 'AbilityStageContext') { 408e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to abilitystage context.'); 409e41f4b71Sopenharmony_ci try { 410e41f4b71Sopenharmony_ci let context: common.AbilityStageContext = sendableContextManager.convertToAbilityStageContext(sendableContext); 411e41f4b71Sopenharmony_ci // Obtain the sandbox path after obtaining the Context object. 412e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 413e41f4b71Sopenharmony_ci } catch (error) { 414e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToAbilityStageContext failed %{public}s', JSON.stringify(error)); 415e41f4b71Sopenharmony_ci } 416e41f4b71Sopenharmony_ci } 417e41f4b71Sopenharmony_ci} 418e41f4b71Sopenharmony_ci 419e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 420e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 421e41f4b71Sopenharmony_ci} 422e41f4b71Sopenharmony_ci 423e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 424e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 425e41f4b71Sopenharmony_ci} 426e41f4b71Sopenharmony_ci``` 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci## sendableContextManager.convertToUIAbilityContext 429e41f4b71Sopenharmony_ci 430e41f4b71Sopenharmony_ciconvertToUIAbilityContext(sendableContext: SendableContext): common.UIAbilityContext 431e41f4b71Sopenharmony_ci 432e41f4b71Sopenharmony_ciConverts a **SendableContext** object to a **UIAbilityContext** object. 433e41f4b71Sopenharmony_ci 434e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Ability.AbilityRuntime.Core 435e41f4b71Sopenharmony_ci 436e41f4b71Sopenharmony_ci**Atomic service API**: This API can be used in atomic services since API version 12. 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci**Parameters** 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci| Name| Type| Mandatory| Description| 441e41f4b71Sopenharmony_ci| ------- | ------- | ------- | ------- | 442e41f4b71Sopenharmony_ci| sendableContext | [SendableContext](js-apis-inner-application-sendableContext.md) | Yes| **SendableContext** object.| 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci**Error codes** 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ciFor details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci| ID| Error Message| 449e41f4b71Sopenharmony_ci| ------- | ------- | 450e41f4b71Sopenharmony_ci| 401 | If the input parameter invalid. Possible causes: 1.Incorrect parameter types; 2.Parameter verification failed. | 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci**Example** 453e41f4b71Sopenharmony_ci 454e41f4b71Sopenharmony_ciContext passed by the main thread: 455e41f4b71Sopenharmony_ci```ts 456e41f4b71Sopenharmony_ciimport { AbilityConstant, UIAbility, Want, common, sendableContextManager } from '@kit.AbilityKit'; 457e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 458e41f4b71Sopenharmony_ciimport { worker } from '@kit.ArkTS'; 459e41f4b71Sopenharmony_ci 460e41f4b71Sopenharmony_ci@Sendable 461e41f4b71Sopenharmony_ciexport class SendableObject { 462e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 463e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 464e41f4b71Sopenharmony_ci this.contextName = contextName; 465e41f4b71Sopenharmony_ci } 466e41f4b71Sopenharmony_ci 467e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 468e41f4b71Sopenharmony_ci contextName: string; 469e41f4b71Sopenharmony_ci} 470e41f4b71Sopenharmony_ci 471e41f4b71Sopenharmony_ciexport default class EntryAbility extends UIAbility { 472e41f4b71Sopenharmony_ci worker: worker.ThreadWorker = new worker.ThreadWorker('entry/ets/workers/Worker.ets'); 473e41f4b71Sopenharmony_ci 474e41f4b71Sopenharmony_ci onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 475e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci // convert and post 478e41f4b71Sopenharmony_ci try { 479e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = sendableContextManager.convertFromContext(this.context); 480e41f4b71Sopenharmony_ci let object: SendableObject = new SendableObject(sendableContext, 'EntryAbilityContext'); 481e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'Ability post message'); 482e41f4b71Sopenharmony_ci this.worker.postMessageWithSharedSendable(object); 483e41f4b71Sopenharmony_ci } catch (error) { 484e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertFromContext failed %{public}s', JSON.stringify(error)); 485e41f4b71Sopenharmony_ci } 486e41f4b71Sopenharmony_ci } 487e41f4b71Sopenharmony_ci} 488e41f4b71Sopenharmony_ci``` 489e41f4b71Sopenharmony_ci 490e41f4b71Sopenharmony_ciContext received by the Worker thread: 491e41f4b71Sopenharmony_ci```ts 492e41f4b71Sopenharmony_ciimport { ErrorEvent, MessageEvents, ThreadWorkerGlobalScope, worker } from '@kit.ArkTS'; 493e41f4b71Sopenharmony_ciimport { common, sendableContextManager } from '@kit.AbilityKit'; 494e41f4b71Sopenharmony_ciimport { hilog } from '@kit.PerformanceAnalysisKit'; 495e41f4b71Sopenharmony_ci 496e41f4b71Sopenharmony_ci@Sendable 497e41f4b71Sopenharmony_ciexport class SendableObject { 498e41f4b71Sopenharmony_ci constructor(sendableContext: sendableContextManager.SendableContext, contextName: string) { 499e41f4b71Sopenharmony_ci this.sendableContext = sendableContext; 500e41f4b71Sopenharmony_ci this.contextName = contextName; 501e41f4b71Sopenharmony_ci } 502e41f4b71Sopenharmony_ci 503e41f4b71Sopenharmony_ci sendableContext: sendableContextManager.SendableContext; 504e41f4b71Sopenharmony_ci contextName: string; 505e41f4b71Sopenharmony_ci} 506e41f4b71Sopenharmony_ci 507e41f4b71Sopenharmony_ciconst workerPort: ThreadWorkerGlobalScope = worker.workerPort; 508e41f4b71Sopenharmony_ci 509e41f4b71Sopenharmony_ciworkerPort.onmessage = (e: MessageEvents) => { 510e41f4b71Sopenharmony_ci let object: SendableObject = e.data; 511e41f4b71Sopenharmony_ci let sendableContext: sendableContextManager.SendableContext = object.sendableContext; 512e41f4b71Sopenharmony_ci if (object.contextName == 'EntryAbilityContext') { 513e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'convert to uiability context.'); 514e41f4b71Sopenharmony_ci try { 515e41f4b71Sopenharmony_ci let context: common.UIAbilityContext = sendableContextManager.convertToUIAbilityContext(sendableContext); 516e41f4b71Sopenharmony_ci // Obtain the sandbox path after obtaining the Context object. 517e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', 'worker context.databaseDir: %{public}s', context.databaseDir); 518e41f4b71Sopenharmony_ci } catch (error) { 519e41f4b71Sopenharmony_ci hilog.error(0x0000, 'testTag', 'convertToUIAbilityContext failed %{public}s', JSON.stringify(error)); 520e41f4b71Sopenharmony_ci } 521e41f4b71Sopenharmony_ci } 522e41f4b71Sopenharmony_ci} 523e41f4b71Sopenharmony_ci 524e41f4b71Sopenharmony_ciworkerPort.onmessageerror = (e: MessageEvents) => { 525e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onmessageerror'); 526e41f4b71Sopenharmony_ci} 527e41f4b71Sopenharmony_ci 528e41f4b71Sopenharmony_ciworkerPort.onerror = (e: ErrorEvent) => { 529e41f4b71Sopenharmony_ci hilog.info(0x0000, 'testTag', '%{public}s', 'onerror'); 530e41f4b71Sopenharmony_ci} 531e41f4b71Sopenharmony_ci``` 532