1 /*
2  * Copyright (c) 2023-2024 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/declarative_frontend/jsview/js_cached_image.h"
17 
18 namespace OHOS::Ace::Framework {
19 
IsASTCResource(const JSRef<JSVal> & jsValue)20 static bool IsASTCResource(const JSRef<JSVal>& jsValue)
21 {
22     if (!jsValue->IsObject()) {
23         return false;
24     }
25 
26     JSRef<JSObject> jsObj = JSRef<JSObject>::Cast(jsValue);
27     if (jsObj->IsUndefined()) {
28         return false;
29     }
30     auto sources = jsObj->GetProperty("sources");
31     auto column = jsObj->GetProperty("column");
32     if (sources->IsEmpty() || column->IsEmpty()) {
33         return false;
34     }
35     return true;
36 }
37 
Create(const JSCallbackInfo & info)38 void JSCachedImage::Create(const JSCallbackInfo& info)
39 {
40     if (info.Length() < 1) {
41         return;
42     }
43 
44     if (!IsASTCResource(info[0])) {
45         JSImage::Create(info);
46     }
47 }
48 
JSBind(BindingTarget globalObj)49 void JSCachedImage::JSBind(BindingTarget globalObj)
50 {
51     JSClass<JSCachedImage>::Declare("CachedImage");
52     MethodOptions opt = MethodOptions::NONE;
53     JSClass<JSCachedImage>::StaticMethod("create", &JSCachedImage::Create, opt);
54     JSClass<JSCachedImage>::InheritAndBind<JSImage>(globalObj);
55 }
56 
JSBindMediaCachedImage(BindingTarget globalObj)57 void JSCachedImage::JSBindMediaCachedImage(BindingTarget globalObj)
58 {
59     JSClass<JSCachedImage>::Declare("MediaCachedImage");
60     MethodOptions opt = MethodOptions::NONE;
61     JSClass<JSCachedImage>::StaticMethod("create", &JSCachedImage::Create, opt);
62     JSClass<JSCachedImage>::InheritAndBind<JSImage>(globalObj);
63 }
64 
65 } // namespace OHOS::Ace::Framework
66