• 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_EXECUTE_CALLBACK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_EXECUTE_CALLBACK_H
18 
19 #include <string>
20 
21 #include "nweb_value_callback.h"
22 #include "core/common/container_scope.h"
23 
24 namespace OHOS::Ace {
25 using namespace OHOS::NWeb;
26 class WebJavaScriptExecuteCallBack : public OHOS::NWeb::NWebValueCallback<std::string> {
27 public:
28     WebJavaScriptExecuteCallBack() = delete;
WebJavaScriptExecuteCallBack(int32_t instanceId)29     explicit WebJavaScriptExecuteCallBack(int32_t instanceId) : instanceId_(instanceId) {}
30 
OnReceiveValue(std::string result)31     void OnReceiveValue(std::string result) override
32     {
33         ContainerScope scope(instanceId_);
34         if (callback_) {
35             callback_(result);
36         }
37     }
SetCallBack(const std::function<void (std::string)> && callback)38     void SetCallBack(const std::function<void(std::string)>&& callback)
39     {
40         callback_ = callback;
41     }
42     std::function<void(std::string)> callback_;
43     int32_t instanceId_;
44 };
45 
46 class WebMessageValueCallBackImpl : public OHOS::NWeb::NWebValueCallback<std::shared_ptr<NWebMessage>> {
47 public:
48     WebMessageValueCallBackImpl() = delete;
WebMessageValueCallBackImpl(int32_t instanceId)49     explicit WebMessageValueCallBackImpl(int32_t instanceId) : instanceId_(instanceId) {}
50 
OnReceiveValue(std::shared_ptr<NWebMessage> result)51     void OnReceiveValue(std::shared_ptr<NWebMessage> result) override
52     {
53         ContainerScope scope(instanceId_);
54         if (callback_ && result && result->GetType() == NWebValue::Type::STRING) {
55             callback_(result->GetString());
56         }
57     }
SetCallBack(const std::function<void (std::string)> && callback)58     void SetCallBack(const std::function<void(std::string)>&& callback)
59     {
60         callback_ = callback;
61     }
62     std::function<void(std::string)> callback_;
63     int32_t instanceId_;
64 };
65 } // namespace OHOS::Ace
66 
67 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_EXECUTE_CALLBACK_H
68