1# ExtensionContext 2 3The **ExtensionContext** module, inherited from **Context**, implements the context for Extension abilities. 4 5This module provides APIs for accessing resources of a specific Extension ability. An Extension ability can use the context directly provided by **ExtensionContext** or that extended from **ExtensionContext**. For example, **ServiceExtension** uses [ServiceExtensionContext](js-apis-inner-application-serviceExtensionContext.md), which extends the capabilities of starting, stopping, binding, and unbinding abilities based on **ExtensionContext**. 6 7> **NOTE** 8> 9> - The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version. 10> - The APIs of this module can be used only in the stage model. 11 12## Modules to Import 13 14```ts 15import common from '@ohos.app.ability.common'; 16``` 17 18## Attributes 19 20**System capability**: SystemCapability.Ability.AbilityRuntime.Core 21 22| Name| Type| Readable| Writable| Description| 23| -------- | -------- | -------- | -------- | -------- | 24| currentHapModuleInfo | [HapModuleInfo](js-apis-bundle-HapModuleInfo.md) | Yes| No| Information about the HAP file<br>(See **api\bundle\hapModuleInfo.d.ts** in the **SDK** directory.) | 25| config | [Configuration](js-apis-app-ability-configuration.md) | Yes| No| Module configuration information.<br>(See **api\@ohos.app.ability.Configuration.d.ts** in the **SDK** directory.)| 26| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundleManager-extensionAbilityInfo.md) | Yes| No| Extension ability information.<br>(See **api\bundle\extensionAbilityInfo.d.ts** in the **SDK** directory.)| 27 28## When to Use 29**ExtensionContext** provides information about an Extension ability, module, and HAP file. You can use the information based on service requirements. The following uses **ServiceExtension** as an example to describe a use case of **ExtensionContext**. 30 31**Scenario description** 32To adapt to devices with different performance, an application provides three modules: highPerformance, midPerformance, and lowPerformance. Each of them provides a Service Extension ability for the entry. During application installation, the application market installs the HAP file of the entry and the HAP file of the module that matches the device performance. During application running, the entry parses **ServiceExtensionContext.HapModuleInfo** to obtain the HAP file information and executes service logic based on this file. 33 34 35 36**Example** 37 38Define a **ServiceExtension** with the same name for the three modules. 39```ts 40import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; 41import Want from '@ohos.application.Want'; 42export default class TheServiceExtension extends ServiceExtension { 43 onCreate(want:Want) { 44 console.log('ServiceAbility onCreate, want: ' + want.abilityName); 45 // Pass ExtensionContext to entry via globalThis. 46 globalThis.ExtensionContext = this.context; 47 } 48 49 onRequest(want, startId) { 50 console.log('ServiceAbility onRequest, want: ' + want.abilityName + ', startId: ' + startId); 51 } 52 53 onConnect(want) { 54 console.log('ServiceAbility onConnect, want:' + want.abilityName); 55 return null; 56 } 57 58 onDisconnect(want) { 59 console.log('ServiceAbility onDisconnect, want:' + want.abilityName); 60 } 61 62 onDestroy() { 63 console.log('ServiceAbility onDestroy'); 64 } 65}; 66``` 67 68Start **ServiceExtension** within the **onCreate** callback of the main ability of the entry. 69```ts 70import Ability from '@ohos.app.ability.Ability'; 71export default class MainAbility extends Ability { 72 onCreate(want, launchParam) { 73 console.log('[Demo] MainAbility onCreate'); 74 let wantExt = { 75 deviceId: '', 76 bundleName: 'com.example.TheServiceExtension', 77 abilityName: 'TheServiceExtension', 78 }; 79 this.context.startServiceExtensionAbility(wantExt); 80 } 81}; 82``` 83 84Create a **ServiceModule.ts** file in the entry to execute service logic. 85```ts 86export default class ServiceModel { 87 moduleName: string; 88 89 constructor() {} 90 91 executeTask() { 92 if (globalThis.ExtensionContext === undefined) { 93 console.log('ERROR, ServiceExtension does not exist'); 94 return; 95 } 96 97 let moduleInfo = globalThis.ExtensionContext.currentHapModuleInfo; 98 this.moduleName = moduleInfo.name; 99 // Execute service logic based on the module name, which differentiates devices with different performance. 100 switch (this.moduleName) { 101 case 'highPerformance': 102 console.log('This is high performance device.'); 103 // Execute the corresponding service logic. 104 break; 105 case 'midPerformance': 106 console.log('This is mid performance device.'); 107 // Execute the corresponding service logic. 108 break; 109 case 'lowPerformance': 110 console.log('This is low performance device.'); 111 // Execute the corresponding service logic. 112 break; 113 default: 114 console.log('ERROR, invalid moduleName.'); 115 break; 116 } 117 } 118}; 119``` 120