[WRTjs] Refactor popup
[platform/framework/web/chromium-efl.git] / wrt / src / browser / api / wrt_api_web_contents.cc
1 #include "wrt/src/browser/api/wrt_api_web_contents.h"
2
3 #include <Elementary.h>
4
5 #include "base/functional/bind.h"
6 #include "content/browser/renderer_host/edge_effect.h"
7 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
8 #include "electron/shell/common/node_includes.h"
9 #include "tizen_src/chromium_impl/tizen/system_info.h"
10 #include "tizen_src/chromium_impl/ui/ozone/platform/efl/im_context_efl.h"
11 #include "tizen_src/ewk/efl_integration/browser/input_picker/color_chooser_efl.h"
12 #include "wrt/src/browser/wrt_input_picker.h"
13 #include "wrt/src/browser/wrt_javascript_dialog_manager.h"
14 #include "wrt/src/browser/wrt_native_window.h"
15 #include "wrt/src/browser/wrt_web_contents.h"
16 #include "wrt/src/common/application_data.h"
17
18 #if BUILDFLAG(IS_TIZEN_TV)
19 #include "wrt/src/browser/tv/native_web_runtime_delegate_tv.h"
20 #include "wrt/src/browser/tv/popup_tv.h"
21 #endif
22
23 namespace wrt {
24
25 namespace api {
26
27 WebContents::WebContents(
28     v8::Isolate* isolate, const gin_helper::Dictionary& options)
29     : electron::api::WebContents(isolate, options) {}
30
31 WebContents::WebContents(v8::Isolate* isolate,
32                          std::unique_ptr<content::WebContents> web_contents,
33                          Type type)
34     : electron::api::WebContents(isolate, std::move(web_contents), type) {}
35
36 WebContents::~WebContents() {}
37
38 void WebContents::NavigationStateChanged(
39     content::WebContents* source, content::InvalidateTypes changed_flags) {
40   if (changed_flags & content::INVALIDATE_TYPE_URL) {
41     auto* wrt_wc = WRTWebContents::FromWebContents(web_contents());
42     if (wrt_wc)
43       wrt_wc->ResetControllers();
44   }
45 }
46
47 bool WebContents::DidAddMessageToConsole(
48     content::WebContents* source,
49     blink::mojom::ConsoleMessageLevel level,
50     const std::u16string& message,
51     int32_t line_no,
52     const std::u16string& source_id) {
53 #if BUILDFLAG(IS_TIZEN_TV)
54   return false;
55 #else
56   return Emit("console-message", static_cast<int32_t>(level), message, line_no,
57               source_id);
58 #endif
59 }
60
61 bool WebContents::HandleContextMenu(
62     content::RenderFrameHost& render_frame_host,
63     const content::ContextMenuParams& params) {
64   auto& app_data = ApplicationData::GetInstance();
65   return !app_data.setting_info().context_menu_enabled();
66 }
67
68 content::JavaScriptDialogManager* WebContents::GetJavaScriptDialogManager(
69     content::WebContents* source) {
70   if (!dialog_manager_)
71     dialog_manager_ = std::make_unique<WRTJavaScriptDialogManager>();
72
73   return dialog_manager_.get();
74 }
75
76 std::unique_ptr<content::ColorChooser> WebContents::OpenColorChooser(
77     content::WebContents* web_contents,
78     SkColor color,
79     const std::vector<blink::mojom::ColorSuggestionPtr>& suggestions) {
80   auto* color_chooser_efl = new content::ColorChooserEfl(*web_contents);
81   auto* evas_object = WRTNativeWindow::GetTopWindow();
82   wrt_input_picker_.reset(new WRTInputPicker(GetWebContents(), evas_object));
83   wrt_input_picker_->ShowColorPicker(SkColorGetR(color), SkColorGetG(color),
84                                      SkColorGetB(color), SkColorGetA(color));
85   return std::unique_ptr<content::ColorChooserEfl>(color_chooser_efl);
86 }
87
88 void WebContents::WillDraw(int new_rotation, gfx::Size frame_data_output_size) {
89 #if !BUILDFLAG(IS_TIZEN_TV)
90   if (!IsMobileProfile())
91     return;
92 #endif
93
94   const bool is_manual_rotation_not_initialized =
95       (rotation_ == -1) && (frame_output_size_.width() == 0);
96   const bool is_changed_landscape_to_landscape =
97       (rotation_ == 90 && new_rotation == 270) ||
98       (rotation_ == 270 && new_rotation == 90);
99   const bool is_changed_portrait_to_portrait =
100       (rotation_ == 0 && new_rotation == 180) ||
101       (rotation_ == 180 && new_rotation == 0);
102   const bool is_frame_data_width_changed =
103       frame_output_size_.width() != frame_data_output_size.width();
104   // While javascript dialog(alert, etc.) is showing, renderer is blocked.
105   // As a result, rotated frame is not rendered although device is rotated
106   // when javascript dialog is showing.
107   auto* dialog_manager = static_cast<WRTJavaScriptDialogManager*>(
108       GetJavaScriptDialogManager(GetWebContents()));
109   const bool is_showing_javascript_dialog = dialog_manager &&
110                                             dialog_manager->IsShowing() &&
111                                             rotation_ != new_rotation;
112   if (is_manual_rotation_not_initialized || is_changed_landscape_to_landscape ||
113       is_changed_portrait_to_portrait || is_frame_data_width_changed ||
114       is_showing_javascript_dialog) {
115     rotation_ = new_rotation;
116     frame_output_size_.set_width(frame_data_output_size.width());
117     auto* ee = WRTNativeWindow::GetPlatformCanvas();
118     if (EINA_TRUE == ecore_evas_wm_rotation_manual_rotation_done_get(ee)) {
119       LOG(INFO) << "finalize window rotation done";
120       ecore_evas_wm_rotation_manual_rotation_done(ee);
121     }
122   }
123
124 #if BUILDFLAG(IS_TIZEN_TV)
125   if (frame_output_size_.height() != frame_output_size_.height()) {
126 #if !defined(WRT_JS_BRINGUP)
127     auto rwhva = static_cast<content::RenderWidgetHostViewAura*>(
128         GetWebContents()->GetRenderWidgetHostView());
129     if (rwhva && rwhva->aura_efl_helper() &&
130         rwhva->aura_efl_helper()->GetIMContextEfl()) {
131       rwhva->aura_efl_helper()->GetIMContextEfl()->RequestHide();
132     }
133 #endif
134   }
135   frame_output_size_.set_height(frame_output_size_.height());
136 #endif
137 }
138
139 #if BUILDFLAG(IS_TIZEN_TV)
140 void WebContents::ShowMicOpenedNotification(bool show) {
141   if (show) {
142     auto pcm_title =
143         NativeWebRuntimeDelegateTV::GetInstance().GetAppTitleForPcm();
144     if (!pcm_title.empty()) {
145       PopupTV::ShowVoiceRecognitionToast(pcm_title);
146     }
147   } else {
148     PopupTV::HideVoiceRecognitionToast();
149   }
150 }
151 #endif
152
153 void WebContents::DidReceiveNonEmtpyLayoutFrame() {
154   EmitReadyToShow();
155 }
156
157 void WebContents::DidFirstVisuallyNonEmptyPaint() {
158   EmitReadyToShow();
159 }
160
161 void WebContents::WebContentsDestroyed() {
162   if (dialog_manager_)
163     dialog_manager_.reset();
164
165   electron::api::WebContents::WebContentsDestroyed();
166 }
167
168 void WebContents::EmitReadyToShow() {
169   auto* window = WRTNativeWindow::FromWebContents(GetWebContents());
170   if (!window->IsClosed() && !window->IsVisible())
171     Emit("ready-to-show");
172 }
173
174 }  // namespace api
175
176 }  // namespace wrt
177
178 namespace electron {
179 namespace api {
180
181 // static
182 gin::Handle<WebContents> WebContents::New(
183     v8::Isolate* isolate,
184     const gin_helper::Dictionary& options) {
185   WebContents* api_web_contents =
186       new wrt::api::WebContents(isolate, options);
187   gin::Handle<WebContents> handle =
188       gin::CreateHandle(isolate, api_web_contents);
189   v8::TryCatch try_catch(isolate);
190   gin_helper::CallMethod(isolate, handle.get(), "_init");
191   if (try_catch.HasCaught()) {
192     node::errors::TriggerUncaughtException(isolate, try_catch);
193   }
194   return handle;
195 }
196
197 // static
198 gin::Handle<WebContents> WebContents::CreateAndTake(
199     v8::Isolate* isolate,
200     std::unique_ptr<content::WebContents> web_contents,
201     Type type) {
202   WebContents* api_web_contents =
203       new wrt::api::WebContents(isolate, std::move(web_contents), type);
204   gin::Handle<WebContents> handle =
205       gin::CreateHandle(isolate, api_web_contents);
206   v8::TryCatch try_catch(isolate);
207   gin_helper::CallMethod(isolate, handle.get(), "_init");
208   if (try_catch.HasCaught()) {
209     node::errors::TriggerUncaughtException(isolate, try_catch);
210   }
211   return handle;
212 }
213
214 }  // namespace api
215 }  // namespace electron