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 NWEB_NAPI_WEBVIEW_CONTROLLER_H 17 #define NWEB_NAPI_WEBVIEW_CONTROLLER_H 18 19 #include <condition_variable> 20 #include <mutex> 21 22 #include "napi/native_api.h" 23 #include "napi/native_common.h" 24 #include "napi/native_node_api.h" 25 #include "webview_controller.h" 26 #include "uv.h" 27 28 namespace OHOS { 29 namespace NWeb { 30 const std::string WEBVIEW_CONTROLLER_CLASS_NAME = "WebviewController"; 31 const std::string WEB_MESSAGE_PORT_CLASS_NAME = "WebMessagePort"; 32 const std::string WEB_HITTESTTYPE_V9_ENUM_NAME = "HitTestTypeV9"; 33 const std::string WEB_HITTESTTYPE_ENUM_NAME = "WebHitTestType"; 34 const std::string WEB_HISTORY_LIST_CLASS_NAME = "WebHistoryList"; 35 36 class NapiWebviewController { 37 public: NapiWebviewController()38 NapiWebviewController() {} 39 ~NapiWebviewController() = default; 40 41 static napi_value Init(napi_env env, napi_value exports); 42 43 private: 44 static napi_value JsConstructor(napi_env env, napi_callback_info info); 45 46 static napi_value InitializeWebEngine(napi_env env, napi_callback_info info); 47 48 static napi_value SetWebDebuggingAccess(napi_env env, napi_callback_info info); 49 50 static napi_value InnerGetWebDebuggingAccess(napi_env env, napi_callback_info info); 51 52 static napi_value SetWebId(napi_env env, napi_callback_info info); 53 54 static napi_value InnerJsProxy(napi_env env, napi_callback_info info); 55 56 static napi_value InnerGetCustomeSchemeCmdLine(napi_env env, napi_callback_info info); 57 58 static napi_value AccessForward(napi_env env, napi_callback_info info); 59 60 static napi_value AccessBackward(napi_env env, napi_callback_info info); 61 62 static napi_value Forward(napi_env env, napi_callback_info info); 63 64 static napi_value Backward(napi_env env, napi_callback_info info); 65 66 static napi_value AccessStep(napi_env env, napi_callback_info info); 67 68 static napi_value ClearHistory(napi_env env, napi_callback_info info); 69 70 static napi_value OnActive(napi_env env, napi_callback_info info); 71 72 static napi_value OnInactive(napi_env env, napi_callback_info info); 73 74 static napi_value Refresh(napi_env env, napi_callback_info info); 75 76 static napi_value ZoomIn(napi_env env, napi_callback_info info); 77 78 static napi_value ZoomOut(napi_env env, napi_callback_info info); 79 80 static napi_value GetWebId(napi_env env, napi_callback_info info); 81 82 static napi_value GetUserAgent(napi_env env, napi_callback_info info); 83 84 static napi_value GetTitle(napi_env env, napi_callback_info info); 85 86 static napi_value GetPageHeight(napi_env env, napi_callback_info info); 87 88 static napi_value BackOrForward(napi_env env, napi_callback_info info); 89 90 static napi_value StoreWebArchive(napi_env env, napi_callback_info info); 91 92 static napi_value StoreWebArchiveInternal(napi_env env, napi_callback_info info, 93 const std::string &baseName, bool autoName); 94 95 static napi_value CreateWebMessagePorts(napi_env env, napi_callback_info info); 96 97 static napi_value PostMessage(napi_env env, napi_callback_info info); 98 99 static napi_value GetHitTestValue(napi_env env, napi_callback_info info); 100 101 static napi_value RequestFocus(napi_env env, napi_callback_info info); 102 103 static napi_value LoadUrl(napi_env env, napi_callback_info info); 104 105 static napi_value LoadUrlWithHttpHeaders(napi_env env, napi_callback_info info, const std::string& url, 106 const napi_value* argv, WebviewController* webviewController); 107 108 static napi_value LoadData(napi_env env, napi_callback_info info); 109 110 static napi_value GetHitTest(napi_env env, napi_callback_info info); 111 112 static napi_value ClearMatches(napi_env env, napi_callback_info info); 113 114 static napi_value SearchNext(napi_env env, napi_callback_info info); 115 116 static napi_value SearchAllAsync(napi_env env, napi_callback_info info); 117 118 static napi_value ClearSslCache(napi_env env, napi_callback_info info); 119 120 static napi_value ClearClientAuthenticationCache(napi_env env, napi_callback_info info); 121 122 static napi_value Stop(napi_env env, napi_callback_info info); 123 124 static napi_value Zoom(napi_env env, napi_callback_info info); 125 126 static napi_value RegisterJavaScriptProxy(napi_env env, napi_callback_info info); 127 128 static napi_value DeleteJavaScriptRegister(napi_env env, napi_callback_info info); 129 130 static napi_value RunJavaScript(napi_env env, napi_callback_info info); 131 132 static napi_value RunJavaScriptInternal(napi_env env, napi_callback_info info, 133 const std::string &script); 134 135 static napi_value GetUrl(napi_env env, napi_callback_info info); 136 137 static napi_value GetOriginalUrl(napi_env env, napi_callback_info info); 138 139 static napi_value SetNetworkAvailable(napi_env env, napi_callback_info info); 140 141 static napi_value InnerGetWebId(napi_env env, napi_callback_info info); 142 143 static napi_value HasImage(napi_env env, napi_callback_info info); 144 145 static napi_value HasImageInternal(napi_env env, napi_callback_info info); 146 147 static napi_value RemoveCache(napi_env env, napi_callback_info info); 148 149 static napi_value getBackForwardEntries(napi_env env, napi_callback_info info); 150 151 static napi_value GetFavicon(napi_env env, napi_callback_info info); 152 153 static napi_value SerializeWebState(napi_env env, napi_callback_info info); 154 155 static napi_value RestoreWebState(napi_env env, napi_callback_info info); 156 157 static napi_value ScrollPageDown(napi_env env, napi_callback_info info); 158 159 static napi_value ScrollPageUp(napi_env env, napi_callback_info info); 160 161 static napi_value ScrollTo(napi_env env, napi_callback_info info); 162 163 static napi_value ScrollBy(napi_env env, napi_callback_info info); 164 165 static napi_value SlideScroll(napi_env env, napi_callback_info info); 166 167 static napi_value CustomizeSchemes(napi_env env, napi_callback_info info); 168 169 static napi_value InnerGetThisVar(napi_env env, napi_callback_info info); 170 }; 171 172 class NWebValueCallbackImpl : public OHOS::NWeb::NWebValueCallback<std::shared_ptr<NWebMessage>> { 173 public: NWebValueCallbackImpl(napi_env env,napi_ref callback)174 NWebValueCallbackImpl(napi_env env, napi_ref callback) : env_(env), callback_(callback) {} 175 ~NWebValueCallbackImpl(); 176 void OnReceiveValue(std::shared_ptr<NWebMessage> result) override; 177 178 private: 179 napi_env env_; 180 napi_ref callback_; 181 static void UvWebMessageOnReceiveValueCallback(uv_work_t *work, int status); 182 }; 183 184 class NapiWebMessagePort { 185 public: 186 NapiWebMessagePort() = default; 187 ~NapiWebMessagePort() = default; 188 189 struct WebMsgPortParam { 190 napi_env env_; 191 napi_ref callback_; 192 std::shared_ptr<NWebMessage> msg_; 193 std::mutex mutex_; 194 bool ready_ = false; 195 std::condition_variable condition_; 196 }; 197 198 static napi_value JsConstructor(napi_env env, napi_callback_info info); 199 200 static napi_value Close(napi_env env, napi_callback_info info); 201 202 static napi_value PostMessageEvent(napi_env env, napi_callback_info info); 203 204 static napi_value OnMessageEvent(napi_env env, napi_callback_info info); 205 }; 206 207 class NapiWebHistoryList { 208 public: 209 NapiWebHistoryList() = default; 210 ~NapiWebHistoryList() = default; 211 212 static napi_value JsConstructor(napi_env env, napi_callback_info info); 213 214 static napi_value GetItem(napi_env env, napi_callback_info info); 215 216 private: 217 static napi_value GetFavicon(napi_env env, std::shared_ptr<NWebHistoryItem> item); 218 }; 219 } // namespace NWeb 220 } // namespace OHOS 221 222 #endif // NWEB_NAPI_WEBVIEW_CONTROLLER_H 223