1 /*
2 * Copyright (c) 2022 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 #include <cstdio>
17 #include <cstring>
18 #include <pthread.h>
19 #include <unistd.h>
20
21 #include "app_log_wrapper.h"
22 #include "js_default_app.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
SetNamedProperty(napi_env env,napi_value dstObj,const char * objName,const char * propName)26 static void SetNamedProperty(napi_env env, napi_value dstObj, const char *objName, const char *propName)
27 {
28 napi_value prop = nullptr;
29 NAPI_CALL_RETURN_VOID(env, napi_create_string_utf8(env, objName, NAPI_AUTO_LENGTH, &prop));
30 NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, dstObj, propName, prop));
31 }
32
CreateApplicationType(napi_env env,napi_value value)33 static void CreateApplicationType(napi_env env, napi_value value)
34 {
35 SetNamedProperty(env, value, "Web Browser", "BROWSER");
36 SetNamedProperty(env, value, "Image Gallery", "IMAGE");
37 SetNamedProperty(env, value, "Audio Player", "AUDIO");
38 SetNamedProperty(env, value, "Video Player", "VIDEO");
39 SetNamedProperty(env, value, "PDF Viewer", "PDF");
40 SetNamedProperty(env, value, "Word Viewer", "WORD");
41 SetNamedProperty(env, value, "Excel Viewer", "EXCEL");
42 SetNamedProperty(env, value, "PPT Viewer", "PPT");
43 }
44
DefaultAppExport(napi_env env,napi_value exports)45 static napi_value DefaultAppExport(napi_env env, napi_value exports)
46 {
47 napi_value applicationType = nullptr;
48 NAPI_CALL(env, napi_create_object(env, &applicationType));
49 CreateApplicationType(env, applicationType);
50
51 napi_property_descriptor desc[] = {
52 DECLARE_NAPI_FUNCTION("isDefaultApplication", IsDefaultApplication),
53 DECLARE_NAPI_FUNCTION("getDefaultApplication", GetDefaultApplication),
54 DECLARE_NAPI_FUNCTION("setDefaultApplication", SetDefaultApplication),
55 DECLARE_NAPI_FUNCTION("resetDefaultApplication", ResetDefaultApplication),
56 DECLARE_NAPI_PROPERTY("ApplicationType", applicationType),
57 };
58
59 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
60 APP_LOGD("init js default app success.");
61 return exports;
62 }
63
64 static napi_module default_app_module = {
65 .nm_version = 1,
66 .nm_flags = 0,
67 .nm_filename = nullptr,
68 .nm_register_func = DefaultAppExport,
69 .nm_modname = "bundle.defaultAppManager",
70 .nm_priv = ((void *)0),
71 .reserved = {0}
72 };
73
DefaultAppRegister(void)74 extern "C" __attribute__((constructor)) void DefaultAppRegister(void)
75 {
76 napi_module_register(&default_app_module);
77 }
78 }
79 }
80