1 /*
2  * Copyright (c) 2021-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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_WEB_CONTROLLER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_WEB_CONTROLLER_H
18 
19 #include "base/memory/referenced.h"
20 #include "bridge/declarative_frontend/engine/bindings.h"
21 #include "core/components/declaration/web/web_declaration.h"
22 #include "core/components/web/web_component.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_web.h"
24 
25 namespace OHOS::Ace::Framework {
26 class JSWebController : public Referenced {
27 public:
28     JSWebController();
29     ~JSWebController() override = default;
30 
31     static void JSBind(BindingTarget globalObj);
32     static void Constructor(const JSCallbackInfo& args);
33     static void Destructor(JSWebController* webController);
34 
35     void Reload() const;
36     void LoadUrl(const JSCallbackInfo& args);
37     void ExecuteTypeScript(const JSCallbackInfo& args);
38     void LoadDataWithBaseUrl(const JSCallbackInfo& args);
39     void Refresh(const JSCallbackInfo& args);
40     void StopLoading(const JSCallbackInfo& args);
41     void GetHitTestResult(const JSCallbackInfo& args);
42     void GetHitTestValue(const JSCallbackInfo& args);
43     void GetCookieManager(const JSCallbackInfo& args);
44     void GetPageHeight(const JSCallbackInfo& args);
45     void GetTitle(const JSCallbackInfo& args);
46     void CreateWebMessagePorts(const JSCallbackInfo& args);
47     void PostWebMessage(const JSCallbackInfo& args);
48     void GetWebId(const JSCallbackInfo& args);
49     void GetDefaultUserAgent(const JSCallbackInfo& args);
50     void AddJavascriptInterface(const JSCallbackInfo& args);
51     void RemoveJavascriptInterface(const JSCallbackInfo& args);
52     void SetJavascriptInterface(const JSCallbackInfo& args);
53     void OnInactive(const JSCallbackInfo& args);
54     void OnActive(const JSCallbackInfo& args);
55     void Zoom(const JSCallbackInfo& args);
56     void ZoomIn(const JSCallbackInfo& args);
57     void ZoomOut(const JSCallbackInfo& args);
58     void RequestFocus(const JSCallbackInfo& args);
59     void SearchAllAsync(const JSCallbackInfo& args);
60     void ClearMatches(const JSCallbackInfo& args);
61     void SearchNext(const JSCallbackInfo& args);
62     void GetUrl(const JSCallbackInfo& args);
63 
64     // Backward and Forward for web page
65     void Backward(const JSCallbackInfo& args);
66     void Forward(const JSCallbackInfo& args);
67     void BackOrForward(const JSCallbackInfo& args);
68     void AccessStep(const JSCallbackInfo& args);
69     void AccessBackward(const JSCallbackInfo& args);
70     void AccessForward(const JSCallbackInfo& args);
71     void ClearHistory(const JSCallbackInfo& args);
72     void ClearSslCache(const JSCallbackInfo& args);
73     void ClearClientAuthenticationCache(const JSCallbackInfo& args);
74 
GetController()75     const RefPtr<WebController>& GetController() const
76     {
77         return webController_;
78     }
79 
SetController(const RefPtr<WebController> & webController)80     void SetController(const RefPtr<WebController>& webController)
81     {
82         webController_ = webController;
83     }
84 
85 protected:
86     void InitJavascriptInterface();
87     void SetJavascriptCallBackImpl();
88     std::shared_ptr<WebJSValue> GetJavaScriptResult(
89         const std::string& objectName,
90         const std::string& objectMethod,
91         const std::vector<std::shared_ptr<WebJSValue>>& args);
92 
93 private:
94     using ObjectClassMap  = std::map<std::string, JSRef<JSObject>>;
95     ObjectClassMap objectorMap_;
96     int32_t instanceId_ = INSTANCE_ID_UNDEFINED;
97     std::unordered_map<std::string, std::vector<std::string>> methods_;
98     RefPtr<WebController> webController_;
99     ACE_DISALLOW_COPY_AND_MOVE(JSWebController);
100     JSRef<JSObject> jsWebCookie_;
101     bool jsWebCookieInit_ = false;
102 };
103 } // namespace OHOS::Ace::Framework
104 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_WEB_CONTROLLER_H
105