1 /* 2 * Copyright (c) 2021 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_DECLARATION_WEB_WEB_CLIENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_WEB_WEB_CLIENT_H 18 19 #include <functional> 20 21 #include "base/memory/ace_type.h" 22 23 namespace OHOS::Ace { 24 25 class WebClient { 26 public: 27 using ReloadCallback = std::function<bool()>; 28 using UpdateUrlCallback = std::function<void(const std::string& url)>; 29 WebClient &operator = (const WebClient &) = delete; 30 WebClient(const WebClient &) = delete; 31 ~WebClient() = default; 32 GetInstance()33 static WebClient& GetInstance() 34 { 35 static WebClient instance; 36 return instance; 37 } 38 RegisterReloadCallback(ReloadCallback && callback)39 void RegisterReloadCallback(ReloadCallback &&callback) 40 { 41 reloadCallback_ = callback; 42 } 43 RegisterUpdageUrlCallback(UpdateUrlCallback && callback)44 void RegisterUpdageUrlCallback(UpdateUrlCallback &&callback) 45 { 46 updateUrlCallback_ = callback; 47 } 48 UpdateWebviewUrl(const std::string & url)49 void UpdateWebviewUrl(const std::string& url) 50 { 51 if (updateUrlCallback_) { 52 return updateUrlCallback_(url); 53 } 54 } 55 ReloadWebview()56 bool ReloadWebview() 57 { 58 if (reloadCallback_) { 59 return reloadCallback_(); 60 } else { 61 return false; 62 } 63 } 64 65 private: 66 WebClient() = default; 67 ReloadCallback reloadCallback_; 68 UpdateUrlCallback updateUrlCallback_; 69 }; 70 71 } // namespace OHOS::Ace 72 73 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_DECLARATION_WEB_WEB_CLIENT_H 74