• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 NWEB_WEBVIEW_JAVASCRIPT_RESULT_CALLBACK_IMPL_H
17 #define NWEB_WEBVIEW_JAVASCRIPT_RESULT_CALLBACK_IMPL_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 #include <unordered_map>
23 #include <vector>
24 
25 #include "napi/native_api.h"
26 #include "napi/native_common.h"
27 #include "napi/native_node_api.h"
28 #include "nweb_javascript_result_callback.h"
29 #include "nweb_value.h"
30 #include "uv.h"
31 
32 namespace OHOS::NWeb {
33 struct JavaScriptObj {
34     napi_env env = nullptr;
35     std::unordered_map<std::string, napi_ref> methodMap;
36 };
37 
38 class WebviewJavaScriptResultCallBack : public NWebJavaScriptResultCallBack {
39 public:
WebviewJavaScriptResultCallBack()40     WebviewJavaScriptResultCallBack() {}
41 
42     ~WebviewJavaScriptResultCallBack() override;
43 
44     std::shared_ptr<NWebValue> GetJavaScriptResult(std::vector<std::shared_ptr<NWebValue>> args,
45         const std::string& method, const std::string& objName) override;
46 
47     void RegisterJavaScriptProxy(napi_env env, napi_value obj, const std::string& objName,
48         const std::vector<std::string>& methodList);
49 
50     bool DeleteJavaScriptRegister(const std::string& objName);
51 
52 private:
53     std::unordered_map<std::string, JavaScriptObj> objectMap_;
54     static void ParseNwebValue2NapiValue(napi_env env, std::shared_ptr<OHOS::NWeb::NWebValue> value,
55         std::vector<napi_value>& argv);
56 
57     static void ParseNapiValue2NwebValue(napi_env env, napi_value value, std::shared_ptr<NWebValue> nwebValue);
58     static void UvJsCallbackThreadWoker(uv_work_t *work, int status);
59     struct NapiJsCallBackParm {
60         napi_env env_;
61         napi_ref callback_;
62         std::vector<std::shared_ptr<NWebValue>> args_;
63         std::shared_ptr<NWebValue> value_;
64         std::mutex mutex_;
65         std::condition_variable condition_;
66         bool ready_ = false;
67     };
68 };
69 }
70 #endif
71