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 ServiceExtensionAbility 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 40// Singleton object GlobalContext.ts 41export class GlobalContext { 42 private constructor() {} 43 private static instance: GlobalContext; 44 private _objects = new Map<string, Object>(); 45 46 public static getContext(): GlobalContext { 47 if (!GlobalContext.instance) { 48 GlobalContext.instance = new GlobalContext(); 49 } 50 return GlobalContext.instance; 51 } 52 53 getObject(value: string): Object | undefined { 54 return this._objects.get(value); 55 } 56 57 setObject(key: string, objectClass: Object): void { 58 this._objects.set(key, objectClass); 59 } 60} 61``` 62 63```ts 64import ServiceExtension from '@ohos.app.ability.ServiceExtensionAbility'; 65import Want from '@ohos.app.ability.Want'; 66import rpc from '@ohos.rpc'; 67import { GlobalContext } from '../GlobalContext' 68 69export default class TheServiceExtension extends ServiceExtension { 70 onCreate(want: Want) { 71 console.log('ServiceAbility onCreate, want: ${want.abilityName}'); 72 GlobalContext.getContext().setObject("ExtensionContext", this.context); 73 } 74 75 onRequest(want: Want, startId: number) { 76 console.log('ServiceAbility onRequest, want: ${want.abilityName}, startId: ${startId}'); 77 } 78 79 onConnect(want: Want) { 80 console.log('ServiceAbility onConnect, want: ${want.abilityName}'); 81 let remoteObject = new rpc.RemoteObject("test"); 82 return remoteObject; 83 } 84 85 onDisconnect(want: Want) { 86 console.log('ServiceAbility onDisconnect, want: ${want.abilityName}'); 87 } 88 89 onDestroy() { 90 console.log('ServiceAbility onDestroy'); 91 } 92}; 93``` 94 95Start **ServiceExtension** within the **onCreate** callback of the main ability of the entry. 96```ts 97import UIAbility from '@ohos.app.ability.UIAbility'; 98import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 99import Want from '@ohos.app.ability.Want'; 100 101export default class EntryAbility extends UIAbility { 102 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { 103 console.log('[Demo] EntryAbility onCreate'); 104 let wantExt: Want = { 105 deviceId: '', 106 bundleName: 'com.example.TheServiceExtension', 107 abilityName: 'TheServiceExtension', 108 }; 109 this.context.startServiceExtensionAbility(wantExt); 110 } 111}; 112``` 113 114Create a **ServiceModule.ts** file in the entry to execute service logic. 115```ts 116import common from '@ohos.app.ability.common'; 117import { GlobalContext } from '../GlobalContext'; 118 119export default class ServiceModel { 120 moduleName: string = ''; 121 122 constructor() {} 123 124 executeTask() { 125 if (GlobalContext.getContext().getObject('ExtensionContext') === undefined) { 126 console.log('ERROR, ServiceExtension does not exist'); 127 return; 128 } 129 130 let extensionContext = GlobalContext.getContext().getObject('ExtensionContext') as common.ServiceExtensionContext; 131 this.moduleName = extensionContext.currentHapModuleInfo.name; 132 // Execute service logic based on the module name, which differentiates devices with different performance. 133 switch (this.moduleName) { 134 case 'highPerformance': 135 console.log('This is high performance device.'); 136 // Execute the corresponding service logic. 137 break; 138 case 'midPerformance': 139 console.log('This is mid performance device.'); 140 // Execute the corresponding service logic. 141 break; 142 case 'lowPerformance': 143 console.log('This is low performance device.'); 144 // Execute the corresponding service logic. 145 break; 146 default: 147 console.log('ERROR, invalid moduleName.'); 148 break; 149 } 150 } 151}; 152``` 153