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 "foundation/arkui/ace_engine/frameworks/base/memory/referenced.h" 23 24 namespace OHOS::Ace { 25 using namespace OHOS::NWeb; 26 27 class WebDelegate; 28 29 class WebJavaScriptExecuteCallBack : public OHOS::NWeb::NWebMessageValueCallback { 30 public: 31 WebJavaScriptExecuteCallBack() = default; WebJavaScriptExecuteCallBack(const WeakPtr<WebDelegate> & delegate)32 explicit WebJavaScriptExecuteCallBack(const WeakPtr<WebDelegate>& delegate) : webDelegate_(delegate) {} 33 virtual ~WebJavaScriptExecuteCallBack() = default; 34 35 virtual void OnReceiveValue(std::shared_ptr<NWebMessage> result) override; SetCallBack(const std::function<void (std::string)> && callback)36 void SetCallBack(const std::function<void(std::string)>&& callback) 37 { 38 callback_ = callback; 39 } 40 41 std::function<void(std::string)> callback_; 42 private: 43 WeakPtr<WebDelegate> webDelegate_; 44 }; 45 46 class WebMessageValueCallBackImpl : public OHOS::NWeb::NWebMessageValueCallback { 47 public: 48 WebMessageValueCallBackImpl() = default; WebMessageValueCallBackImpl(const WeakPtr<WebDelegate> & delegate)49 explicit WebMessageValueCallBackImpl(const WeakPtr<WebDelegate>& delegate) : webDelegate_(delegate) {} 50 virtual ~WebMessageValueCallBackImpl() = default; 51 52 virtual void OnReceiveValue(std::shared_ptr<NWebMessage> result) override; SetCallBack(const std::function<void (std::string)> && callback)53 void SetCallBack(const std::function<void(std::string)>&& callback) 54 { 55 callback_ = callback; 56 } 57 58 std::function<void(std::string)> callback_; 59 private: 60 WeakPtr<WebDelegate> webDelegate_; 61 }; 62 } // namespace OHOS::Ace 63 64 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_JAVASCRIPT_EXECUTE_CALLBACK_H 65