• 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 "core/components/web/resource/web_resource.h"
17 
18 namespace OHOS::Ace {
19 
20 const char WEB_PARAM_NONE[] = "";
21 const char WEB_PARAM_AND[] = "#HWJS-&-#";
22 const char WEB_PARAM_VALUE[] = "value";
23 const char WEB_PARAM_EQUALS[] = "#HWJS-=-#";
24 const char WEB_PARAM_BEGIN[] = "#HWJS-?-#";
25 const char WEB_METHOD[] = "method";
26 const char WEB_EVENT[] = "event";
27 const char WEB_RESULT_FAIL[] = "fail";
28 
Release(const std::function<void (bool)> & onRelease)29 void WebResource::Release(const std::function<void(bool)>& onRelease)
30 {
31     if (id_ == WEB_INVALID_ID) {
32         return;
33     }
34     auto context = context_.Upgrade();
35     if (!context) {
36         return;
37     }
38 
39     auto resRegister = context->GetPlatformResRegister();
40     if (resRegister == nullptr) {
41         return;
42     }
43 
44     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
45     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
46     auto releaseTask = [weakWeb = AceType::WeakClaim(this), weakRes, onRelease] {
47         auto webResource = weakWeb.Upgrade();
48         auto resRegister = weakRes.Upgrade();
49         if (webResource == nullptr || resRegister == nullptr) {
50             return;
51         }
52         bool ret = resRegister->ReleaseResource(webResource->hash_);
53         if (ret) {
54             webResource->id_ = WEB_INVALID_ID;
55             webResource->hash_.clear();
56         }
57 
58         if (onRelease) {
59             onRelease(ret);
60         }
61     };
62     if (platformTaskExecutor.IsRunOnCurrentThread()) {
63         releaseTask();
64     } else {
65         platformTaskExecutor.PostTask(releaseTask, "ArkUIWebReleaseResource");
66     }
67 }
68 
GetDoubleParam(const std::string & param,const std::string & name) const69 double WebResource::GetDoubleParam(const std::string& param, const std::string& name) const
70 {
71     size_t len = name.length();
72     size_t pos = param.find(name);
73     double result = 0.0;
74 
75     if (pos != std::string::npos) {
76         std::stringstream ss;
77 
78         ss << param.substr(pos + 1 + len);
79         ss >> result;
80     }
81 
82     return result;
83 }
84 
GetIntParam(const std::string & param,const std::string & name) const85 int32_t WebResource::GetIntParam(const std::string& param, const std::string& name) const
86 {
87     size_t len = name.length();
88     size_t pos = param.find(name);
89     int32_t result = 0;
90 
91     if (pos != std::string::npos) {
92         std::stringstream ss;
93 
94         ss << param.substr(pos + 1 + len);
95         ss >> result;
96     }
97 
98     return result;
99 }
100 
MakeResourceHash() const101 std::string WebResource::MakeResourceHash() const
102 {
103     std::stringstream hashCode;
104     hashCode << type_ << "@" << id_;
105 
106     return hashCode.str();
107 }
108 
MakeEventHash(const std::string & event) const109 std::string WebResource::MakeEventHash(const std::string& event) const
110 {
111     std::string eventHash = hash_;
112 
113     eventHash += std::string(WEB_EVENT);
114     eventHash += std::string(WEB_PARAM_EQUALS);
115     eventHash += event;
116     eventHash += std::string(WEB_PARAM_BEGIN);
117 
118     return eventHash;
119 }
120 
MakeMethodHash(const std::string & method) const121 std::string WebResource::MakeMethodHash(const std::string& method) const
122 {
123     std::string methodHash = hash_;
124 
125     methodHash += std::string(WEB_METHOD);
126     methodHash += std::string(WEB_PARAM_EQUALS);
127     methodHash += method;
128     methodHash += std::string(WEB_PARAM_BEGIN);
129 
130     return methodHash;
131 }
132 
OnError(const std::string & errorCode,const std::string & errorMsg)133 void WebResource::OnError(const std::string& errorCode, const std::string& errorMsg)
134 {
135     if (onError_) {
136         onError_(errorCode, errorMsg);
137     }
138 }
139 
CallResRegisterMethod(const std::string & method,const std::string & param,const std::function<void (std::string &)> & callback)140 void WebResource::CallResRegisterMethod(
141     const std::string& method, const std::string& param, const std::function<void(std::string&)>& callback)
142 {
143     if (method.empty()) {
144         return;
145     }
146 
147     auto context = context_.Upgrade();
148     if (!context) {
149         return;
150     }
151 
152     auto resRegister = context->GetPlatformResRegister();
153     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
154 
155     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
156     platformTaskExecutor.PostTask([method, param, weakRes, callback] {
157         auto resRegister = weakRes.Upgrade();
158         if (resRegister == nullptr) {
159             return;
160         }
161         std::string result;
162         resRegister->OnMethodCall(method, param, result);
163         if (callback) {
164             callback(result);
165         }
166     }, "ArkUIWebCallResRegisterMethod");
167 }
168 
GetStringParam(const std::string & param,const std::string & name) const169 std::string WebResource::GetStringParam(const std::string& param, const std::string& name) const
170 {
171     size_t len = name.length();
172     size_t pos = param.find(name);
173     std::string result;
174 
175     if (pos != std::string::npos) {
176         std::stringstream ss;
177 
178         ss << param.substr(pos + 1 + len);
179         ss >> result;
180     }
181     return result;
182 }
183 
184 } // namespace OHOS::Ace