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## Attributes 13 14**System capability**: SystemCapability.Ability.AbilityRuntime.Core 15 16| Name| Type| Readable| Writable| Description| 17| -------- | -------- | -------- | -------- | -------- | 18| 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.) | 19| 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.)| 20| extensionAbilityInfo | [ExtensionAbilityInfo](js-apis-bundleManager-extensionAbilityInfo.md) | Yes| No| Extension ability information.<br>(See **api\bundle\extensionAbilityInfo.d.ts** in the **SDK** directory.)| 21 22## When to Use 23**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**. 24 25**Scenario description** 26To 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. 27 28![Example](figures/en_us_image_ExtensionContext_Example.png) 29 30**Example** 31 32Define a **ServiceExtension** with the same name for the three modules. 33```ts 34import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; 35import Want from '@ohos.application.Want'; 36export default class TheServiceExtension extends ServiceExtension { 37 onCreate(want:Want) { 38 console.log('ServiceAbility onCreate, want: ' + want.abilityName); 39 // Pass ExtensionContext to entry via globalThis. 40 globalThis.ExtensionContext = this.context; 41 } 42 43 onRequest(want, startId) { 44 console.log('ServiceAbility onRequest, want: ' + want.abilityName + ', startId: ' + startId); 45 } 46 47 onConnect(want) { 48 console.log('ServiceAbility onConnect, want:' + want.abilityName); 49 return null; 50 } 51 52 onDisconnect(want) { 53 console.log('ServiceAbility onDisconnect, want:' + want.abilityName); 54 } 55 56 onDestroy() { 57 console.log('ServiceAbility onDestroy'); 58 } 59}; 60``` 61 62Start **ServiceExtension** within the **onCreate** callback of the main ability of the entry. 63```ts 64import Ability from '@ohos.app.ability.Ability'; 65export default class MainAbility extends Ability { 66 onCreate(want, launchParam) { 67 console.log('[Demo] MainAbility onCreate'); 68 let wantExt = { 69 deviceId: '', 70 bundleName: 'com.example.TheServiceExtension', 71 abilityName: 'TheServiceExtension', 72 }; 73 this.context.startServiceExtensionAbility(wantExt); 74 } 75}; 76``` 77 78Create a **ServiceModule.ts** file in the entry to execute service logic. 79```ts 80export default class ServiceModel { 81 moduleName: string; 82 83 constructor() {} 84 85 executeTask() { 86 if (globalThis.ExtensionContext === undefined) { 87 console.log('ERROR, ServiceExtension does not exist'); 88 return; 89 } 90 91 let moduleInfo = globalThis.ExtensionContext.currentHapModuleInfo; 92 this.moduleName = moduleInfo.name; 93 // Execute service logic based on the module name, which differentiates devices with different performance. 94 switch (this.moduleName) { 95 case 'highPerformance': 96 console.log('This is high performance device.'); 97 // Execute the corresponding service logic. 98 break; 99 case 'midPerformance': 100 console.log('This is mid performance device.'); 101 // Execute the corresponding service logic. 102 break; 103 case 'lowPerformance': 104 console.log('This is low performance device.'); 105 // Execute the corresponding service logic. 106 break; 107 default: 108 console.log('ERROR, invalid moduleName.'); 109 break; 110 } 111 } 112}; 113``` 114