1 /*
2 * Copyright (c) 2021 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 "bridge/declarative_frontend/jsview/js_environment.h"
17
18 #include "base/memory/referenced.h"
19 #include "core/common/ace_application_info.h"
20 #include "core/common/container.h"
21 #include "core/common/environment/environment_proxy.h"
22 #include "frameworks/base/i18n/localization.h"
23 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
24 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
25
26 namespace OHOS::Ace::Framework {
27
JSBind(BindingTarget globalObj)28 void JSEnvironment::JSBind(BindingTarget globalObj)
29 {
30 JSClass<JSEnvironment>::Declare("EnvironmentSetting");
31 JSClass<JSEnvironment>::CustomMethod("getAccessibilityEnabled", &JSEnvironment::GetAccessibilityEnabled);
32 JSClass<JSEnvironment>::CustomMethod("getColorMode", &JSEnvironment::GetColorMode);
33 JSClass<JSEnvironment>::CustomMethod("getFontScale", &JSEnvironment::GetFontScale);
34 JSClass<JSEnvironment>::CustomMethod("getFontWeightScale", &JSEnvironment::GetFontWeightScale);
35 JSClass<JSEnvironment>::CustomMethod("getLayoutDirection", &JSEnvironment::GetLayoutDirection);
36 JSClass<JSEnvironment>::CustomMethod("getLanguageCode", &JSEnvironment::GetLanguageCode);
37 JSClass<JSEnvironment>::CustomMethod("onValueChanged", &JSEnvironment::onChange);
38 JSClass<JSEnvironment>::Bind(globalObj, JSEnvironment::ConstructorCallback, JSEnvironment::DestructorCallback);
39 }
40
ConstructorCallback(const JSCallbackInfo & args)41 void JSEnvironment::ConstructorCallback(const JSCallbackInfo& args)
42 {
43 auto environment = Referenced::MakeRefPtr<JSEnvironment>();
44 environment->IncRefCount();
45 args.SetReturnValue(Referenced::RawPtr(environment));
46 }
47
DestructorCallback(JSEnvironment * environment)48 void JSEnvironment::DestructorCallback(JSEnvironment* environment)
49 {
50 if (environment != nullptr) {
51 environment->DecRefCount();
52 }
53 }
54
GetAccessibilityEnabled(const JSCallbackInfo & args)55 void JSEnvironment::GetAccessibilityEnabled(const JSCallbackInfo& args)
56 {
57 std::string value;
58 #if defined(PREVIEW)
59 value = "false";
60 #else
61 auto container = Container::Current();
62 if (!container) {
63 LOGW("container is null");
64 return;
65 }
66 auto executor = container->GetTaskExecutor();
67 value = EnvironmentProxy::GetInstance()->GetEnvironment(executor)->GetAccessibilityEnabled();
68 #endif
69 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
70 auto returnValue = JSVal(ToJSValue(value));
71 auto returnPtr = JSRef<JSVal>::Make(returnValue);
72 args.SetReturnValue(returnPtr);
73 }
74
GetColorMode(const JSCallbackInfo & args)75 void JSEnvironment::GetColorMode(const JSCallbackInfo& args)
76 {
77 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
78 auto colorMode = SystemProperties::GetColorMode();
79 auto returnValue = JSVal(ToJSValue(static_cast<int32_t>(colorMode)));
80 auto returnPtr = JSRef<JSVal>::Make(returnValue);
81 args.SetReturnValue(returnPtr);
82 }
83
onChange(const JSCallbackInfo & args)84 void JSEnvironment::onChange(const JSCallbackInfo& args)
85 {
86 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
87 }
88
GetFontScale(const JSCallbackInfo & args)89 void JSEnvironment::GetFontScale(const JSCallbackInfo& args)
90 {
91 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
92 auto container = Container::Current();
93 if (!container) {
94 LOGW("container is null");
95 return;
96 }
97 auto context = container->GetPipelineContext();
98 auto returnValue = JSVal(ToJSValue(context->GetFontScale()));
99 auto returnPtr = JSRef<JSVal>::Make(returnValue);
100 args.SetReturnValue(returnPtr);
101 }
102
GetFontWeightScale(const JSCallbackInfo & args)103 void JSEnvironment::GetFontWeightScale(const JSCallbackInfo& args)
104 {
105 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
106 auto weightScale = SystemProperties::GetFontWeightScale();
107 auto returnValue = JSVal(ToJSValue(weightScale));
108 auto returnPtr = JSRef<JSVal>::Make(returnValue);
109 args.SetReturnValue(returnPtr);
110 }
111
GetLayoutDirection(const JSCallbackInfo & args)112 void JSEnvironment::GetLayoutDirection(const JSCallbackInfo& args)
113 {
114 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
115 auto isRTL = AceApplicationInfo::GetInstance().IsRightToLeft();
116 auto value = isRTL ? 0 : 1;
117 auto returnValue = JSVal(ToJSValue(value));
118 auto returnPtr = JSRef<JSVal>::Make(returnValue);
119 args.SetReturnValue(returnPtr);
120 }
121
GetLanguageCode(const JSCallbackInfo & args)122 void JSEnvironment::GetLanguageCode(const JSCallbackInfo& args)
123 {
124 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(args.GetExecutionContext());
125 auto location = Localization::GetInstance();
126 auto language = location->GetLanguage();
127 auto returnValue = JSVal(ToJSValue(language));
128 auto returnPtr = JSRef<JSVal>::Make(returnValue);
129 args.SetReturnValue(returnPtr);
130 }
131
132 } // namespace OHOS::Ace::Framework
133