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_ == INVALID_ID) {
38 return;
39 }
40 // TODO: add support for ng.
41 auto context = context_.Upgrade();
42 if (!context) {
43 LOGE("fail to release resource due to context is null");
44 return;
45 }
46
47 auto resRegister = context->GetPlatformResRegister();
48 if (resRegister == nullptr) {
49 return;
50 }
51
52 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
53 auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
54 auto releaseTask = [weakWeb = AceType::WeakClaim(this), weakRes, onRelease] {
55 auto webResource = weakWeb.Upgrade();
56 auto resRegister = weakRes.Upgrade();
57 if (webResource == nullptr || resRegister == nullptr) {
58 LOGE("webDelegate or resRegister is null!");
59 return;
60 }
61 bool ret = resRegister->ReleaseResource(webResource->hash_);
62 if (ret) {
63 webResource->id_ = INVALID_ID;
64 webResource->hash_.clear();
65 }
66
67 if (onRelease) {
68 onRelease(ret);
69 }
70 };
71 if (platformTaskExecutor.IsRunOnCurrentThread()) {
72 releaseTask();
73 } else {
74 platformTaskExecutor.PostTask(releaseTask);
75 }
76 }
77
GetDoubleParam(const std::string & param,const std::string & name) const78 double WebResource::GetDoubleParam(const std::string& param, const std::string& name) const
79 {
80 size_t len = name.length();
81 size_t pos = param.find(name);
82 double result = 0.0;
83
84 if (pos != std::string::npos) {
85 std::stringstream ss;
86
87 ss << param.substr(pos + 1 + len);
88 ss >> result;
89 }
90
91 return result;
92 }
93
GetIntParam(const std::string & param,const std::string & name) const94 int32_t WebResource::GetIntParam(const std::string& param, const std::string& name) const
95 {
96 size_t len = name.length();
97 size_t pos = param.find(name);
98 int32_t result = 0;
99
100 if (pos != std::string::npos) {
101 std::stringstream ss;
102
103 ss << param.substr(pos + 1 + len);
104 ss >> result;
105 }
106
107 return result;
108 }
109
MakeResourceHash() const110 std::string WebResource::MakeResourceHash() const
111 {
112 std::stringstream hashCode;
113 hashCode << type_ << "@" << id_;
114
115 return hashCode.str();
116 }
117
MakeEventHash(const std::string & event) const118 std::string WebResource::MakeEventHash(const std::string& event) const
119 {
120 std::string eventHash = hash_;
121
122 eventHash += std::string(WEB_EVENT);
123 eventHash += std::string(WEB_PARAM_EQUALS);
124 eventHash += event;
125 eventHash += std::string(WEB_PARAM_BEGIN);
126
127 return eventHash;
128 }
129
MakeMethodHash(const std::string & method) const130 std::string WebResource::MakeMethodHash(const std::string& method) const
131 {
132 std::string methodHash = hash_;
133
134 methodHash += std::string(WEB_METHOD);
135 methodHash += std::string(WEB_PARAM_EQUALS);
136 methodHash += method;
137 methodHash += std::string(WEB_PARAM_BEGIN);
138
139 return methodHash;
140 }
141
OnError(const std::string & errorCode,const std::string & errorMsg)142 void WebResource::OnError(const std::string& errorCode, const std::string& errorMsg)
143 {
144 if (onError_) {
145 onError_(errorCode, errorMsg);
146 }
147 }
148
CallResRegisterMethod(const std::string & method,const std::string & param,const std::function<void (std::string &)> & callback)149 void WebResource::CallResRegisterMethod(
150 const std::string& method, const std::string& param, const std::function<void(std::string&)>& callback)
151 {
152 if (method.empty()) {
153 return;
154 }
155
156 auto context = context_.Upgrade();
157 if (!context) {
158 LOGE("fail to get context to call res register method");
159 return;
160 }
161
162 auto resRegister = context->GetPlatformResRegister();
163 auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
164
165 auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
166 platformTaskExecutor.PostTask([method, param, weakRes, callback] {
167 auto resRegister = weakRes.Upgrade();
168 if (resRegister == nullptr) {
169 LOGE("resRegister is null!");
170 return;
171 }
172 std::string result;
173 resRegister->OnMethodCall(method, param, result);
174 if (callback) {
175 callback(result);
176 }
177 });
178 }
179
GetStringParam(const std::string & param,const std::string & name) const180 std::string WebResource::GetStringParam(const std::string& param, const std::string& name) const
181 {
182 size_t len = name.length();
183 size_t pos = param.find(name);
184 std::string result;
185
186 if (pos != std::string::npos) {
187 std::stringstream ss;
188
189 ss << param.substr(pos + 1 + len);
190 ss >> result;
191 }
192 return result;
193 }
194
195 } // namespace OHOS::Ace