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