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 FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_RESULT_CALLBACK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_RESULT_CALLBACK_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "nweb_javascript_result_callback.h"
23 #include "nweb_value.h"
24 #include "core/components/web/resource/web_javascript_value.h"
25 #include "foundation/arkui/ace_engine/frameworks/base/memory/referenced.h"
26 
27 namespace OHOS::Ace {
28 using namespace OHOS::NWeb;
29 
30 class WebDelegate;
31 
32 class WebJavaScriptResultCallBack : public NWebJavaScriptResultCallBack {
33 public:
34     WebJavaScriptResultCallBack() = delete;
WebJavaScriptResultCallBack(const WeakPtr<WebDelegate> & delegate)35     WebJavaScriptResultCallBack(const WeakPtr<WebDelegate>& delegate) : webDelegate_(delegate) {}
36 
37     ~WebJavaScriptResultCallBack() = default;
38 
39     std::shared_ptr<NWebValue> GetJavaScriptResult(std::vector<std::shared_ptr<NWebValue>> args,
40         const std::string& method, const std::string& object_name, int32_t routing_id, int32_t object_id) override;
41 
42     std::shared_ptr<NWebValue> GetJavaScriptResultFlowbuf(std::vector<std::shared_ptr<NWebValue>> args,
43         const std::string& method, const std::string& object_name,
44         int fd, int32_t routing_id, int32_t object_id) override;
45 
46     bool HasJavaScriptObjectMethods(int32_t object_id, const std::string& method_name) override;
47 
48     std::shared_ptr<NWebValue> GetJavaScriptObjectMethods(int32_t object_id) override;
49 
50     void RemoveJavaScriptObjectHolder(int32_t holder, int32_t object_id) override;
51 
52     void RemoveTransientJavaScriptObject() override;
53 
54     using JavaScriptCallBackImpl = std::function<std::shared_ptr<WebJSValue>(
55         const std::string& objectName,
56         const std::string& objectMethod,
57         const std::vector<std::shared_ptr<WebJSValue>>& args)>;
58 
SetJavaScriptCallBack(const JavaScriptCallBackImpl && javaScriptCallBackImpl)59     void SetJavaScriptCallBack(const JavaScriptCallBackImpl&& javaScriptCallBackImpl)
60     {
61         javaScriptCallBackImpl_ = javaScriptCallBackImpl;
62     }
63 
64 protected:
65     JavaScriptCallBackImpl javaScriptCallBackImpl_;
66 
67 private:
68     WeakPtr<WebDelegate> webDelegate_;
69 };
70 } // namespace OHOS::Ace
71 
72 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_RESULT_CALLBACK_H
73