• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef WEBVIEW_JAVA_SCRIPT_RESULT_CALLBACK_H
17 #define WEBVIEW_JAVA_SCRIPT_RESULT_CALLBACK_H
18 
19 #include <string>
20 
21 #include "webview_ffi.h"
22 #include "nweb.h"
23 #include "nweb_javascript_result_callback.h"
24 #include "nweb_web_message.h"
25 #include "webview_log.h"
26 
27 
28 namespace OHOS::Webview {
29 
30 class JavaScriptOb {
31 public:
32     // to be compatible with older webcotroller, be sure to the same as ace and core
33     enum class JavaScriptObjIdErrorCode : int32_t { WEBCONTROLLERERROR = -2, WEBVIEWCONTROLLERERROR = -1, END = 0 };
34 
35     typedef int32_t ObjectID;
36 
CreateNamed(std::vector<std::function<char * (const char *)>> cjFuncs)37     static std::shared_ptr<JavaScriptOb> CreateNamed(std::vector<std::function<char*(const char*)>> cjFuncs)
38     {
39         return std::make_shared<JavaScriptOb>(cjFuncs);
40     }
41 
JavaScriptOb(std::vector<std::function<char * (const char *)>> cjFuncs)42     explicit JavaScriptOb(std::vector<std::function<char*(const char*)>> cjFuncs) : cjFuncs_(cjFuncs), namesCount_(0)
43     {}
44 
IsNamed()45     bool IsNamed()
46     {
47         return namesCount_ > 0;
48     }
AddName()49     void AddName()
50     {
51         ++namesCount_;
52     }
RemoveName()53     void RemoveName()
54     {
55         --namesCount_;
56     }
57 
GetMethodNames()58     std::vector<std::string> GetMethodNames()
59     {
60         return methods_;
61     }
62 
GetPermission()63     std::string GetPermission()
64     {
65         return permission_;
66     }
67 
GetFuncs()68     std::vector<std::function<char*(const char*)>> GetFuncs()
69     {
70         return cjFuncs_;
71     }
72 
HasMethod(const std::string & methodName)73     int HasMethod(const std::string& methodName)
74     {
75         if (methodName.empty()) {
76             WEBVIEWLOGE("HasMethod methodName null");
77             return -1;
78         }
79 
80         if (!isMethodsSetup_) {
81             return -1;
82         }
83         int size = static_cast<int>(methods_.size());
84         for (int i = 0; i < size; i++) {
85             if (methodName == methods_[i]) {
86                 return i;
87             }
88         }
89         return -1;
90     }
91 
FindMethod(const std::string & methodName)92     std::function<char*(const char*)> FindMethod(const std::string& methodName)
93     {
94         if (!isMethodsSetup_) {
95             return nullptr;
96         }
97         auto index = HasMethod(methodName);
98         if (index != -1) {
99             return cjFuncs_[index];
100         }
101         return nullptr;
102     }
103 
SetMethods(std::vector<std::string> methods_name)104     void SetMethods(std::vector<std::string> methods_name)
105     {
106         std::unique_lock<std::mutex> lock(mutex_);
107         methods_ = methods_name;
108         isMethodsSetup_ = true;
109     }
110 
SetPermission(std::string permission)111     void SetPermission(std::string permission)
112     {
113         std::unique_lock<std::mutex> lock(mutex_);
114         permission_ = permission;
115     }
116 
117 private:
118 
119     std::vector<std::function<char*(const char*)>> cjFuncs_;
120 
121     std::vector<std::string> methods_;
122 
123     int namesCount_;
124 
125     bool isMethodsSetup_ = false;
126 
127     std::mutex mutex_;
128 
129     // allow list
130     std::string permission_;
131 };
132 
133 class WebviewJavaScriptResultCallBackImpl : public NWeb::NWebJavaScriptResultCallBack {
134 public:
135     typedef std::unordered_map<std::string, JavaScriptOb::ObjectID> NamedObjectMap;
136     typedef std::unordered_map<JavaScriptOb::ObjectID, std::shared_ptr<JavaScriptOb>> ObjectMap;
137 
WebviewJavaScriptResultCallBackImpl()138     WebviewJavaScriptResultCallBackImpl() {}
139 
140     explicit WebviewJavaScriptResultCallBackImpl(int32_t nwebId);
141 
142     ~WebviewJavaScriptResultCallBackImpl() override;
143 
144     std::shared_ptr<NWeb::NWebValue> GetJavaScriptResult(std::vector<std::shared_ptr<NWeb::NWebValue>> args,
145         const std::string& method, const std::string& objName, int32_t routingId, int32_t objectId) override;
146 
147     std::shared_ptr<NWeb::NWebValue> GetJavaScriptResultFlowbuf(std::vector<std::shared_ptr<NWeb::NWebValue>> args,
148         const std::string& method, const std::string& objName, int fd, int32_t routingId, int32_t objectId) override;
149 
150     bool HasJavaScriptObjectMethods(int32_t objectId, const std::string& methodName) override;
151 
152     std::shared_ptr<NWeb::NWebValue> GetJavaScriptObjectMethods(int32_t objectId) override;
153 
154     std::shared_ptr<JavaScriptOb> FindObject(JavaScriptOb::ObjectID objectId);
155 
156     bool FindObjectIdInJsTd(
157         const std::vector<std::function<char*(const char*)>>& cjFuncs,
158         const std::vector<std::string>& methodList, JavaScriptOb::ObjectID& objectId);
159 
160     JavaScriptOb::ObjectID AddObject(const std::vector<std::function<char*(const char*)>>& cjFuncs);
161 
162     JavaScriptOb::ObjectID RegisterJavaScriptProxy(
163         const std::vector<std::function<char*(const char*)>>& cjFuncs,
164         const std::string& objName, const std::vector<std::string>& methodList, const std::string& permission = "");
165 
166     void RemoveJavaScriptObjectHolder(int32_t holder, JavaScriptOb::ObjectID objectId) override;
167 
168     void RemoveTransientJavaScriptObject() override;
169 
170     bool DeleteJavaScriptRegister(const std::string &objName);
171 
172     void GetJavaScriptResultV2(const std::vector<std::shared_ptr<NWeb::NWebHapValue>>& args, const std::string& method,
173         const std::string& objectName, int32_t routingId, int32_t objectId,
174         std::shared_ptr<NWeb::NWebHapValue> result) override;
175 
176     void GetJavaScriptResultFlowbufV2(const std::vector<std::shared_ptr<NWeb::NWebHapValue>>& args,
177         const std::string& method, const std::string& objectName, int fd, int32_t routingId, int32_t objectId,
178         std::shared_ptr<NWeb::NWebHapValue> result) override;
179 
180     void GetJavaScriptObjectMethodsV2(int32_t objectId, std::shared_ptr<NWeb::NWebHapValue> result) override;
181 
GetNWebId()182     int32_t GetNWebId()
183     {
184         return nwebId_;
185     }
186 
187 private:
188     bool RemoveNamedObject(const std::string& name);
189 
190     JavaScriptOb::ObjectID AddNamedObject(const std::vector<std::function<char*(const char*)>>& cjFuncs,
191         const std::vector<std::string>& methodList, const std::string& objName);
192 
193     std::shared_ptr<NWeb::NWebValue> GetJavaScriptResultSelf(std::vector<std::shared_ptr<NWeb::NWebValue>> args,
194         const std::string& method, const std::string& objName, int32_t routingId, int32_t objectId);
195 
196     bool ConstructArgv(void* ashmem, std::vector<std::shared_ptr<NWeb::NWebValue>> args,
197     	std::vector<std::string>& argv, std::shared_ptr<JavaScriptOb> jsObj, int32_t routingId);
198 
199     char* FlowbufStrAtIndex(void* mem, int flowbufIndex, int* argIndex, int* strLen);
200 
201     std::shared_ptr<NWeb::NWebValue> GetJavaScriptResultSelfHelper(std::shared_ptr<JavaScriptOb> jsObj,
202         const std::string& method, int32_t routingId, std::vector<std::string> argv);
203 
204     std::shared_ptr<NWeb::NWebValue> GetJavaScriptResultSelfFlowbuf(std::vector<std::shared_ptr<NWeb::NWebValue>> args,
205         const std::string& method, const std::string& objName, int fd, int32_t routingId, int32_t objectId);
206 
207     void ConstructArgvV2(void* ashmem, const std::vector<std::shared_ptr<NWeb::NWebHapValue>>& args,
208         std::vector<std::string>& argv, std::shared_ptr<JavaScriptOb> jsObj, int32_t routingId);
209 
210     void GetJavaScriptResultSelfHelperV2(std::shared_ptr<JavaScriptOb> jsObj, const std::string& method,
211         int32_t routingId, const std::vector<std::string>& argv, std::shared_ptr<NWeb::NWebHapValue> result);
212 
213     int32_t nwebId_ = -1;
214 
215     JavaScriptOb::ObjectID nextObjectId_ = 1;
216     NamedObjectMap namedObjects_;
217     ObjectMap objects_;
218 };
219 
220 } // namespace OHOS::Webview
221 #endif
222