已合并
add context interface #4395
add context interface #4395
已合并
majian_lsyg创建于 2025年12月17日
4 个文件变更+427-0
@@ -18,7 +18,9 @@ ohos_ndk_headers("ability_runtime_ndk_header") {
18 sources = [18 sources = [
19 "./ability_runtime_common.h",19 "./ability_runtime_common.h",
20 "./application_context.h",20 "./application_context.h",
21+ "./context.h",
21 "./context_constant.h",22 "./context_constant.h",
23+ "./extension_ability.h",
22 "./start_options.h",24 "./start_options.h",
23 ]25 ]
24}26}
@@ -32,7 +34,9 @@ ohos_ndk_library("libability_runtime") {
32 system_capability_headers = [34 system_capability_headers = [
33 "AbilityKit/ability_runtime/ability_runtime_common.h",35 "AbilityKit/ability_runtime/ability_runtime_common.h",
34 "AbilityKit/ability_runtime/application_context.h",36 "AbilityKit/ability_runtime/application_context.h",
37+ "AbilityKit/ability_runtime/context.h",
35 "AbilityKit/ability_runtime/context_constant.h",38 "AbilityKit/ability_runtime/context_constant.h",
39+ "AbilityKit/ability_runtime/extension_ability.h",
36 "AbilityKit/ability_runtime/start_options.h",40 "AbilityKit/ability_runtime/start_options.h",
37 ]41 ]
38}42}
@@ -0,0 +1,292 @@
1+/*
2+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+/**
17+ * @addtogroup AbilityRuntime
18+ * @{
19+ *
20+ * @brief Provide the definition of the C interface for the context AbilityRuntime
21+ *
22+ * @since 24
23+ */
24+ 
25+/**
26+ * @file context.h
27+ *
28+ * @brief Declare the common types for the context AbilityRuntime.
29+ *
30+ * @library libability_runtime.so
31+ * @kit AbilityKit
32+ * @syscap SystemCapability.Ability.AbilityRuntime.Core
33+ * @since 24
34+ */
35+ 
36+#ifndef ABILITY_RUNTIME_CONTEXT_H
37+#define ABILITY_RUNTIME_CONTEXT_H
38+ 
39+#include <stdint.h>
40+#include "ability_runtime_common.h"
41+#include "context_constant.h"
42+ 
43+#ifdef __cplusplus
44+extern "C" {
45+#endif
46+ 
47+/**
48+ * @brief Define the AbilityRuntime_Context structure type.
49+ *
50+ * @since 24
51+ */
52+struct AbilityRuntime_Context;
53+ 
54+/**
55+ * @brief Defines the pointer to AbilityRuntime_Context.
56+ *
57+ * @since 24
58+ */
59+typedef struct AbilityRuntime_Context* AbilityRuntime_ContextHandle;
60+ 
61+/**
62+ * @brief Obtain the cache directory of the context.
63+ *
64+ * @param context The context to get cache directory from.
65+ * @param buffer A pointer to a buffer that receives the cache directory of the context.
66+ * @param bufferSize The length of the buffer.
67+ * @param writeLength The string length actually written to the buffer,
68+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
69+ * @return The error code.
70+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
71+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
72+ * or the buffer size is less than the minimum buffer size.
73+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
74+ * @since 24
75+ */
76+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetCacheDir(
77+ AbilityRuntime_ContextHandle context, char* buffer, int32_t bufferSize, int32_t* writeLength);
78+ 
79+/**
80+ * @brief Obtain the temp directory of the context.
81+ *
82+ * @param context The context to get temp directory from.
83+ * @param buffer A pointer to a buffer that receives the temp directory of the context.
84+ * @param bufferSize The length of the buffer.
85+ * @param writeLength The string length actually written to the buffer,
86+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
87+ * @return The error code.
88+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
89+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
90+ * or the buffer size is less than the minimum buffer size.
91+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
92+ * @since 24
93+ */
94+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetTempDir(
95+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
96+ 
97+/**
98+ * @brief Obtain the files directory of the context.
99+ *
100+ * @param context The context to get files directory from.
101+ * @param buffer A pointer to a buffer that receives the files directory of the context.
102+ * @param bufferSize The length of the buffer.
103+ * @param writeLength The string length actually written to the buffer,
104+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
105+ * @return The error code.
106+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
107+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
108+ * or the buffer size is less than the minimum buffer size.
109+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
110+ * @since 24
111+ */
112+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetFilesDir(
113+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
114+ 
115+/**
116+ * @brief Obtain the database directory of the context.
117+ *
118+ * @param context The context to get database directory from.
119+ * @param buffer A pointer to a buffer that receives the database directory of the context.
120+ * @param bufferSize The length of the buffer.
121+ * @param writeLength The string length actually written to the buffer,
122+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
123+ * @return The error code.
124+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
125+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
126+ * or the buffer size is less than the minimum buffer size.
127+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
128+ * @since 24
129+ */
130+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetDatabaseDir(
131+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
132+ 
133+/**
134+ * @brief Obtain the preferences directory of the context.
135+ *
136+ * @param context The context to get preferences directory from.
137+ * @param buffer A pointer to a buffer that receives the preferences directory of the context.
138+ * @param bufferSize The length of the buffer.
139+ * @param writeLength The string length actually written to the buffer,
140+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
141+ * @return The error code.
142+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
143+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
144+ * or the buffer size is less than the minimum buffer size.
145+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
146+ * @since 24
147+ */
148+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetPreferencesDir(
149+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
150+ 
151+/**
152+ * @brief Obtain the bundle code directory of the context.
153+ *
154+ * @param context The context to get bundle code directory from.
155+ * @param buffer A pointer to a buffer that receives the bundle code directory of the context.
156+ * @param bufferSize The length of the buffer.
157+ * @param writeLength The string length actually written to the buffer,
158+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
159+ * @return The error code.
160+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
161+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
162+ * or the buffer size is less than the minimum buffer size.
163+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
164+ * @since 24
165+ */
166+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetBundleCodeDir(
167+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
168+ 
169+/**
170+ * @brief Obtain the distributed files directory of the context.
171+ *
172+ * @param context The context to get distributed files directory from.
173+ * @param buffer A pointer to a buffer that receives the distributed files directory of the context.
174+ * @param bufferSize The length of the buffer.
175+ * @param writeLength The string length actually written to the buffer,
176+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
177+ * @return The error code.
178+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
179+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
180+ * or the buffer size is less than the minimum buffer size.
181+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
182+ * @since 24
183+ */
184+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetDistributedFilesDir(
185+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
186+ 
187+/**
188+ * @brief Obtain the resource directory of the context.
189+ *
190+ * @param context The context to get resource directory from.
191+ * @param buffer A pointer to a buffer that receives the resource directory of the context.
192+ * @param bufferSize The length of the buffer.
193+ * @param writeLength The string length actually written to the buffer,
194+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
195+ * @return The error code.
196+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
197+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
198+ * or the buffer size is less than the minimum buffer size.
199+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
200+ * @since 24
201+ */
202+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetResourceDir(AbilityRuntime_ContextHandle context,
203+ char* buffer, const int32_t bufferSize, int32_t* writeLength);
204+ 
205+/**
206+ * @brief Obtain the cloud file directory of the context.
207+ *
208+ * @param context The context to get cloud file directory from.
209+ * @param buffer A pointer to a buffer that receives the cloud file directory of the context.
210+ * @param bufferSize The length of the buffer.
211+ * @param writeLength The string length actually written to the buffer,
212+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
213+ * @return The error code.
214+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
215+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
216+ * or the buffer size is less than the minimum buffer size.
217+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
218+ * @since 24
219+ */
220+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetCloudFileDir(
221+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
222+ 
223+/**
224+ * @brief Obtain the area mode of the context.
225+ *
226+ * @param context The context to get the area mode from.
227+ * @param areaMode A pointer to the area mode.
228+ * @return The error code.
229+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
230+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the areaMode is null.
231+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
232+ * @since 24
233+ */
234+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetAreaMode(
235+ AbilityRuntime_ContextHandle context, AbilityRuntime_AreaMode* areaMode);
236+ 
237+/**
238+ * @brief Set the area mode of the context.
239+ *
240+ * @param context The context to set the area mode for.
241+ * @param areaMode The area mode.
242+ * @return The error code.
243+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
244+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the areaMode is null.
245+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
246+ * @since 24
247+ */
248+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_SetAreaMode(
249+ AbilityRuntime_ContextHandle context, AbilityRuntime_AreaMode areaMode);
250+ 
251+/**
252+ * @brief Obtain the log file directory of the context.
253+ *
254+ * @param context The context to get log file directory from.
255+ * @param buffer A pointer to a buffer that receives the log file directory of the context.
256+ * @param bufferSize The length of the buffer.
257+ * @param writeLength The string length actually written to the buffer,
258+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
259+ * @return The error code.
260+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
261+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
262+ * or the buffer size is less than the minimum buffer size.
263+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
264+ * @since 24
265+ */
266+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetLogFileDir(
267+ AbilityRuntime_ContextHandle context, char* buffer, const int32_t bufferSize, int32_t* writeLength);
268+ 
269+/**
270+ * @brief Obtain the process name.
271+ *
272+ * @param context The context to get the process name from.
273+ * @param buffer A pointer to a buffer that receives the process name.
274+ * @param bufferSize The length of the buffer.
275+ * @param writeLength The string length actually written to the buffer,
276+ * when returning {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR}.
277+ * @return The error code.
278+ * {@link ABILITY_RUNTIME_ERROR_CODE_NO_ERROR} if the operation is successful.
279+ * {@link ABILITY_RUNTIME_ERROR_CODE_PARAM_INVALID} if the buffer or writeLength is null,
280+ * or the buffer size is less than the minimum buffer size.
281+ * {@link ABILITY_RUNTIME_ERROR_CODE_CONTEXT_NOT_EXIST} if the context does not exist.
282+ * @since 24
283+ */
284+AbilityRuntime_ErrorCode OH_AbilityRuntime_Context_GetProcessName(
285+ AbilityRuntime_ContextHandle context, char* buffer, int32_t bufferSize, int32_t* writeLength);
286+ 
287+#ifdef __cplusplus
288+}
289+#endif
290+ 
291+/** @} */
292+#endif // ABILITY_RUNTIME_CONTEXT_H
@@ -0,0 +1,79 @@
1+/*
2+ * Copyright (c) 2026 Huawei Device Co., Ltd.
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ *
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ *
9+ * Unless required by applicable law or agreed to in writing, software
10+ * distributed under the License is distributed on an "AS IS" BASIS,
11+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+ * See the License for the specific language governing permissions and
13+ * limitations under the License.
14+ */
15+ 
16+/**
17+ * @addtogroup AbilityRuntime
18+ * @{
19+ *
20+ * @brief Provide the definition of the C interface for the extension ability AbilityRuntime
21+ *
22+ * @since 24
23+ */
24+ 
25+/**
26+ * @file extension_ability.h
27+ *
28+ * @brief Declare the common types for the extension ability AbilityRuntime.
29+ *
30+ * @library libability_runtime.so
31+ * @kit AbilityKit
32+ * @syscap SystemCapability.Ability.AbilityRuntime.Core
33+ * @since 24
34+ */
35+ 
36+#ifndef ABILITY_RUNTIME_EXTENSION_ABILITY_H
37+#define ABILITY_RUNTIME_EXTENSION_ABILITY_H
38+ 
39+#include "ability_runtime_common.h"
40+ 
41+#ifdef __cplusplus
42+extern "C" {
43+#endif
44+ 
45+/**
46+ * @brief Define the AbilityRuntime_ExtensionInstance structure type.
47+ *
48+ * @since 24
49+ */
50+struct AbilityRuntime_ExtensionInstance;
51+ 
52+/**
53+ * @brief Defines the pointer to AbilityRuntime_ExtensionInstance.
54+ *
55+ * @since 24
56+ */
57+typedef struct AbilityRuntime_ExtensionInstance* AbilityRuntime_ExtensionInstanceHandle;
58+ 
59+/**
60+ * @brief Define the function that must be in the native code to instantiate the native extension ability.
61+ *
62+ * @since 24
63+ */
64+typedef void AbilityRuntime_Extension_CreateFunc(
65+ AbilityRuntime_ExtensionInstanceHandle handle, const char *abilityName);
66+ 
67+/**
68+ * @brief The name of the function that native extension ability instance looks for when launching its native code.
69+ *
70+ * @since 24
71+ */
72+AbilityRuntime_Extension_CreateFunc OH_AbilityRuntime_OnNativeExtensionCreate;
73+ 
74+#ifdef __cplusplus
75+}
76+#endif
77+ 
78+/** @} */
79+#endif // ABILITY_RUNTIME_EXTENSION_ABILITY_H
@@ -202,5 +202,57 @@
202 {202 {
203 "first_introduced": "23",203 "first_introduced": "23",
204 "name": "OH_AbilityRuntime_ApplicationContextNotifyPageChanged"204 "name": "OH_AbilityRuntime_ApplicationContextNotifyPageChanged"
205+ },
206+ {
207+ "first_introduced": "24",
208+ "name": "OH_AbilityRuntime_Context_GetCacheDir"
209+ },
210+ {
211+ "first_introduced": "24",
212+ "name": "OH_AbilityRuntime_Context_GetTempDir"
213+ },
214+ {
215+ "first_introduced": "24",
216+ "name": "OH_AbilityRuntime_Context_GetFilesDir"
217+ },
218+ {
219+ "first_introduced": "24",
220+ "name": "OH_AbilityRuntime_Context_GetDatabaseDir"
221+ },
222+ {
223+ "first_introduced": "24",
224+ "name": "OH_AbilityRuntime_Context_GetPreferencesDir"
225+ },
226+ {
227+ "first_introduced": "24",
228+ "name": "OH_AbilityRuntime_Context_GetBundleCodeDir"
229+ },
230+ {
231+ "first_introduced": "24",
232+ "name": "OH_AbilityRuntime_Context_GetDistributedFilesDir"
233+ },
234+ {
235+ "first_introduced": "24",
236+ "name": "OH_AbilityRuntime_Context_GetResourceDir"
237+ },
238+ {
239+ "first_introduced": "24",
240+ "name": "OH_AbilityRuntime_Context_GetCloudFileDir"
241+ },
242+ {
243+ "first_introduced": "24",
244+ "name": "OH_AbilityRuntime_Context_GetAreaMode"
245+ },
246+ {
247+ "first_introduced": "24",
248+ "name": "OH_AbilityRuntime_Context_SetAreaMode"
249+ },
250+ {
251+ "first_introduced": "24",
252+ "name": "OH_AbilityRuntime_Context_GetLogFileDir"
253+ },
254+ {
255+ "first_introduced": "24",
256+ "name": "OH_AbilityRuntime_Context_GetProcessName"
205 }257 }
206]258]