• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/bridge/js_frontend/engine/v8/v8_input_bridge.h"
17 
18 #include "frameworks/bridge/common/utils/utils.h"
19 
20 namespace OHOS::Ace::Framework {
21 namespace {
22 
GetInputOption(const v8::Local<v8::Context> & ctx,const v8::Local<v8::Value> & valObject,InputOption & inputOption)23 void GetInputOption(const v8::Local<v8::Context>& ctx, const v8::Local<v8::Value>& valObject, InputOption& inputOption)
24 {
25     v8::Isolate* isolate = ctx->GetIsolate();
26     ACE_DCHECK(isolate);
27     v8::HandleScope handleScope(isolate);
28 
29     if (!valObject->IsObject()) {
30         LOGE("none found attrs");
31         return;
32     }
33     v8::Local<v8::Object> v8ValObj;
34     if (!valObject->ToObject(ctx).ToLocal(&v8ValObj)) {
35         LOGE("Value to Object fail");
36         return;
37     }
38     v8::Local<v8::Array> properties = v8ValObj->GetOwnPropertyNames(ctx).ToLocalChecked();
39     int32_t len = properties->Length();
40     for (int32_t i = 0; i < len; i++) {
41         v8::Local<v8::Value> key;
42         if (!properties->Get(ctx, i).ToLocal(&key)) {
43             LOGW("key is null. Ignoring!");
44             continue;
45         }
46         v8::String::Utf8Value keyV8Str(isolate, key);
47         const char* keyStr = *keyV8Str;
48         if (keyStr == nullptr) {
49             continue;
50         }
51         v8::Local<v8::Value> val = v8ValObj->Get(ctx, key).ToLocalChecked();
52         if (val->IsNumber() || val->IsBoolean() || val->IsString()) {
53             v8::String::Utf8Value valV8Str(ctx->GetIsolate(), val);
54             const char* valStr = *valV8Str;
55             if (valStr == nullptr) {
56                 continue;
57             }
58             if (strcmp(keyStr, DOM_INPUT_OPTION_ICON) == 0) {
59                 inputOption.image = valStr;
60             } else if (strcmp(keyStr, DOM_INPUT_OPTION_CONTENT) == 0) {
61                 inputOption.text = valStr;
62             } else {
63                 LOGD("key : %{public}s unsupported. Ignoring!", keyStr);
64             }
65         } else {
66             LOGD("value of unsupported type. Ignoring!");
67         }
68     }
69 }
70 
71 } // namespace
72 
ParseInputOptions(const v8::Local<v8::Context> & ctx,const v8::Local<v8::Value> & valArray)73 void V8InputBridge::ParseInputOptions(const v8::Local<v8::Context>& ctx, const v8::Local<v8::Value>& valArray)
74 {
75     v8::Isolate* isolate = ctx->GetIsolate();
76     ACE_DCHECK(isolate);
77     v8::HandleScope handleScope(isolate);
78 
79     v8::Local<v8::Object> v8ValObj;
80     if (!valArray->ToObject(ctx).ToLocal(&v8ValObj)) {
81         LOGE("Value to Object fail");
82         return;
83     }
84     v8::Local<v8::Array> properties = v8ValObj->GetOwnPropertyNames(ctx).ToLocalChecked();
85     int32_t len = properties->Length();
86     for (int32_t i = 0; i < len; ++i) {
87         v8::Local<v8::Value> itemKey = properties->Get(ctx, i).ToLocalChecked();
88         v8::Local<v8::Value> itemVal = v8ValObj->Get(ctx, itemKey).ToLocalChecked();
89         InputOption inputOption;
90         if (itemVal->IsObject()) {
91             GetInputOption(ctx, itemVal, inputOption);
92             inputOptions_.push_back(inputOption);
93         }
94     }
95 }
96 
97 } // namespace OHOS::Ace::Framework