• 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 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     void GetJavaScriptResultV2(const std::vector<std::shared_ptr<NWebHapValue>>& args, const std::string& method,
55         const std::string& objectName, int32_t routingId, int32_t objectId,
56         std::shared_ptr<NWebHapValue> result) override;
57 
58     void GetJavaScriptResultFlowbufV2(const std::vector<std::shared_ptr<NWebHapValue>>& args, const std::string& method,
59         const std::string& objectName, int fd, int32_t routingId, int32_t objectId,
60         std::shared_ptr<NWebHapValue> result) override;
61 
62     void GetJavaScriptObjectMethodsV2(int32_t objectId, std::shared_ptr<NWebHapValue> result) override;
63 
64     using JavaScriptCallBackImpl = std::function<std::shared_ptr<WebJSValue>(
65         const std::string& objectName,
66         const std::string& objectMethod,
67         const std::vector<std::shared_ptr<WebJSValue>>& args)>;
68 
SetJavaScriptCallBack(const JavaScriptCallBackImpl && javaScriptCallBackImpl)69     void SetJavaScriptCallBack(const JavaScriptCallBackImpl&& javaScriptCallBackImpl)
70     {
71         javaScriptCallBackImpl_ = javaScriptCallBackImpl;
72     }
73 
74 private:
75     std::shared_ptr<WebJSValue> GetJavaScriptResult(const std::vector<std::shared_ptr<WebJSValue>>& args,
76         const std::string& method, const std::string& object_name);
77 
78 protected:
79     JavaScriptCallBackImpl javaScriptCallBackImpl_;
80 
81 private:
82     WeakPtr<WebDelegate> webDelegate_;
83 };
84 } // namespace OHOS::Ace
85 
86 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_RESULT_CALLBACK_H
87