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_WEBVIEW_CLIENT_IMPL_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H 18 19 #include "foundation/ace/ace_engine/frameworks/base/memory/referenced.h" 20 #include "nweb_handler.h" 21 22 #include "base/log/log.h" 23 #include "core/common/container_scope.h" 24 25 namespace OHOS::Ace { 26 class WebDelegate; 27 28 class DownloadListenerImpl : public OHOS::NWeb::NWebDownloadCallback { 29 public: 30 DownloadListenerImpl() = default; DownloadListenerImpl(int32_t instanceId)31 explicit DownloadListenerImpl(int32_t instanceId) : instanceId_(instanceId) {} 32 ~DownloadListenerImpl() = default; 33 34 void OnDownloadStart(const std::string& url, const std::string& userAgent, const std::string& contentDisposition, 35 const std::string& mimetype, long contentLength) override; 36 SetWebDelegate(const WeakPtr<WebDelegate> & delegate)37 void SetWebDelegate(const WeakPtr<WebDelegate>& delegate) 38 { 39 webDelegate_ = delegate; 40 } 41 42 private: 43 WeakPtr<WebDelegate> webDelegate_; 44 int32_t instanceId_ = -1; 45 }; 46 47 class WebClientImpl : public OHOS::NWeb::NWebHandler { 48 public: 49 WebClientImpl() = default; WebClientImpl(int32_t instanceId)50 explicit WebClientImpl(int32_t instanceId) : instanceId_(instanceId) {} 51 ~WebClientImpl() = default; 52 53 void SetNWeb(std::shared_ptr<OHOS::NWeb::NWeb> nweb) override; 54 void OnProxyDied() override; 55 void OnRouterPush(const std::string& param) override; 56 bool OnConsoleLog(const OHOS::NWeb::NWebConsoleLog& message) override; 57 void OnMessage(const std::string& param) override; 58 void OnPageLoadBegin(const std::string& url) override; 59 void OnPageLoadEnd(int httpStatusCode, const std::string& url) override; 60 void OnLoadingProgress(int newProgress) override; 61 void OnPageTitle(const std::string &title) override; 62 void OnGeolocationHide() override; 63 void OnGeolocationShow(const std::string& origin, 64 OHOS::NWeb::NWebGeolocationCallbackInterface* callback) override; 65 66 bool OnAlertDialogByJS(const std::string &url, 67 const std::string &message, 68 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 69 bool OnBeforeUnloadByJS(const std::string &url, 70 const std::string &message, 71 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 72 bool OnConfirmDialogByJS(const std::string &url, 73 const std::string &message, 74 std::shared_ptr<NWeb::NWebJSDialogResult> result) override; 75 bool OnFileSelectorShow(std::shared_ptr<NWeb::FileSelectorCallback> callback, 76 std::shared_ptr<NWeb::NWebFileSelectorParams> params) override; 77 78 void OnFocus() override; 79 void OnResourceLoadError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 80 std::shared_ptr<OHOS::NWeb::NWebUrlResourceError> error) override; 81 void OnHttpError(std::shared_ptr<OHOS::NWeb::NWebUrlResourceRequest> request, 82 std::shared_ptr<OHOS::NWeb::NWebUrlResourceResponse> response) override; 83 void OnRenderExited(OHOS::NWeb::RenderExitReason reason) override; 84 void OnRefreshAccessedHistory(const std::string& url, bool isReload) override; 85 bool OnHandleInterceptUrlLoading(const std::string& url) override; SetWebDelegate(const WeakPtr<WebDelegate> & delegate)86 void SetWebDelegate(const WeakPtr<WebDelegate>& delegate) 87 { 88 webDelegate_ = delegate; 89 } 90 GetWebDelegate()91 const RefPtr<WebDelegate> GetWebDelegate() const 92 { 93 return webDelegate_.Upgrade(); 94 } 95 96 private: 97 std::weak_ptr<OHOS::NWeb::NWeb> webviewWeak_; 98 WeakPtr<WebDelegate> webDelegate_; 99 int32_t instanceId_ = -1; 100 }; 101 } // namespace OHOS::Ace 102 103 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEBVIEW_CLIENT_IMPL_H