1 /*
2 * Copyright (c) 2021-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 "frameworks/bridge/js_frontend/engine/v8/v8_badge_bridge.h"
17
18 namespace OHOS::Ace::Framework {
19
ParseBadgeConfig(v8::Local<v8::Context> ctx,v8::Local<v8::Value> valObject)20 void V8BadgeBridge::ParseBadgeConfig(v8::Local<v8::Context> ctx, v8::Local<v8::Value> valObject)
21 {
22 v8::Isolate* isolate = ctx->GetIsolate();
23 if (isolate == nullptr) {
24 LOGE("isolate is none");
25 return;
26 }
27 v8::HandleScope handleScope(isolate);
28 if (!valObject->IsObject()) {
29 LOGE("none found attrs");
30 return;
31 }
32 v8::Local<v8::Object> v8ValObj;
33 if (!valObject->ToObject(ctx).ToLocal(&v8ValObj)) {
34 LOGE("Value to Object fail");
35 return;
36 }
37 v8::Local<v8::Array> properties = v8ValObj->GetOwnPropertyNames(ctx).ToLocalChecked();
38 int32_t len = properties->Length();
39 for (int32_t i = 0; i < len; i++) {
40 v8::Local<v8::Value> key;
41 if (!properties->Get(ctx, i).ToLocal(&key)) {
42 LOGW("key is null. Ignoring!");
43 continue;
44 }
45 v8::String::Utf8Value keyV8Str(isolate, key);
46 const char* keyStr = *keyV8Str;
47 if (keyStr == nullptr) {
48 continue;
49 }
50 v8::Local<v8::Value> val = v8ValObj->Get(ctx, key).ToLocalChecked();
51 if (val->IsString() || val->IsNumber()) {
52 v8::String::Utf8Value valV8Str(ctx->GetIsolate(), val);
53 const char* valStr = *valV8Str;
54 if (valStr == nullptr) {
55 continue;
56 }
57 // static linear map must be sorted by key.
58 static const LinearMapNode<void (*)(const char*, V8BadgeBridge&)> badgeConfigOperators[] = {
59 { DOM_BADGE_COLOR,
60 [](const char* valStr, V8BadgeBridge& badgeBridge) {
61 badgeBridge.badgeConfig_.badgeColor = { Color::FromString(valStr), true };
62 } },
63 { DOM_BADGE_CIRCLE_SIZE,
64 [](const char* valStr, V8BadgeBridge& badgeBridge) {
65 badgeBridge.badgeConfig_.badgeSize = { StringToDimension(valStr), true };
66 } },
67 { DOM_BADGE_TEXT_COLOR,
68 [](const char* valStr, V8BadgeBridge& badgeBridge) {
69 badgeBridge.badgeConfig_.textColor = { Color::FromString(valStr), true };
70 } },
71 { DOM_BADGE_TEXT_FONT_SIZE,
72 [](const char* valStr, V8BadgeBridge& badgeBridge) {
73 badgeBridge.badgeConfig_.textSize = { StringToDimension(valStr), true };
74 } }
75 };
76 auto operatorIter = BinarySearchFindIndex(badgeConfigOperators, ArraySize(badgeConfigOperators), keyStr);
77 if (operatorIter != -1) {
78 badgeConfigOperators[operatorIter].value(valStr, *this);
79 } else {
80 LOGD("key : %{public}s unsupported. Ignoring!", keyStr);
81 }
82 } else {
83 LOGD("value of unsupported type. Ignoring!");
84 }
85 }
86 }
87
88 } // namespace OHOS::Ace::Framework