[WRTjs] Refactor popup
[platform/framework/web/chromium-efl.git] / wrt / src / browser / wrt_browser_client.cc
1 #include "wrt/src/browser/wrt_browser_client.h"
2
3 #include "components/navigation_interception/intercept_navigation_throttle.h"
4 #include "content/public/browser/browser_task_traits.h"
5 #include "content/public/browser/browser_thread.h"
6 #include "content/public/browser/navigation_handle.h"
7 #include "content/public/browser/render_process_host.h"
8 #include "content/public/common/content_switches.h"
9 #include "electron/shell/browser/protocol_registry.h"
10 #include "extensions/buildflags/buildflags.h"
11 #include "services/network/public/mojom/network_context.mojom.h"
12 #include "services/service_manager/public/cpp/binder_registry.h"
13 #include "services/service_manager/public/cpp/manifest.h"
14 #include "third_party/blink/public/common/associated_interfaces/associated_interface_registry.h"
15 #include "third_party/blink/public/common/web_preferences/web_preferences.h"
16 #include "tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.h"
17 #if !defined(WRT_JS_BRINGUP)
18 #include "tizen_src/chromium_impl/content/browser/browsable_app_control.h"
19 #endif
20 #include "tizen_src/chromium_impl/tizen/system_info.h"
21 #include "tizen_src/ewk/efl_integration/common/content_switches_efl.h"
22 #include "tizen_src/ewk/efl_integration/common/version_info.h"
23 #include "tizen_src/ewk/efl_integration/shared_url_loader_factory_efl.h"
24 #include "v8/include/v8.h"
25 #include "wrt/src/browser/native_web_runtime.h"
26 #include "wrt/src/browser/net/wrt_proxying_url_loader_factory.h"
27 #include "wrt/src/browser/wrt_browser_main_parts.h"
28 #include "wrt/src/browser/wrt_devtools_manager_delegate.h"
29 #include "wrt/src/browser/wrt_native_window.h"
30 #include "wrt/src/browser/wrt_notification_presenter.h"
31 #include "wrt/src/browser/wrt_render_message_filter.h"
32 #include "wrt/src/browser/wrt_web_contents.h"
33 #include "wrt/src/browser/wrt_web_contents_view_delegate.h"
34 #include "wrt/src/common/application_data.h"
35 #include "wrt/src/common/constants.h"
36 #include "wrt/src/common/locale_manager.h"
37
38 #if defined(TIZEN_WEB_SPEECH_RECOGNITION)
39 #include "content/browser/speech/tts_platform_impl_tizen.h"
40 #endif
41
42 #if BUILDFLAG(ENABLE_NACL) || \
43     (defined(TIZEN_PEPPER_EXTENSIONS) && BUILDFLAG(ENABLE_PLUGINS))
44 #include "content/browser/renderer_host/pepper/browser_pepper_host_factory_efl.h"
45 #include "content/public/browser/browser_ppapi_host.h"
46 #include "ppapi/host/ppapi_host.h"
47 #endif
48
49 #if BUILDFLAG(ENABLE_NACL)
50 #include "components/nacl/browser/nacl_browser.h"
51 #include "components/nacl/browser/nacl_host_message_filter.h"
52 #include "components/nacl/browser/nacl_process_host.h"
53 #include "components/nacl/common/nacl_constants.h"
54 #include "components/nacl/common/nacl_paths.h"
55 #include "components/nacl/common/nacl_process_type.h"
56 #include "components/nacl/loader/nacl_loader_manifest.h"
57 #include "components/nacl/zygote/nacl_fork_delegate_linux.h"
58 #include "content/grit/content_resources.h"
59 #include "content/public/browser/browser_child_process_host.h"
60 #include "content/public/browser/child_process_data.h"
61 #endif
62
63 #if defined(TIZEN_PEPPER_PLAYER)
64 #include "content/browser/renderer_host/pepper/media_player/pepper_media_player_message_filter.h"
65 #endif
66
67 #if ENABLE_CHROME_EXTENSIONS
68 #include "extensions/browser/extension_registry.h"
69 #endif
70
71 #if BUILDFLAG(IS_TIZEN_TV)
72 #include "url/url_constants.h"
73 #endif
74
75 #if !defined(WRT_JS_BRINGUP)
76 #if BUILDFLAG(IS_TIZEN_TV) || defined(ENABLE_TIZEN_SOCKETS)
77 #include "tizen_src/chromium_impl/services/sockets/public/cpp/manifest.h"
78 #include "tizen_src/chromium_impl/services/sockets/public/mojom/constants.mojom.h"
79 #include "tizen_src/chromium_impl/services/sockets/public/mojom/host_resolver_tizen.mojom.h"
80 #include "tizen_src/chromium_impl/services/sockets/public/mojom/socket_factory_tizen.mojom.h"
81 #include "tizen_src/chromium_impl/services/sockets/tizen_sockets_service.h"
82 #endif
83 #endif
84
85 namespace wrt {
86
87 namespace {
88
89 #if defined(ENABLE_AUTO_ZOOM)
90 const char* kAiZoomTargetKey = "http://samsung.com/tv/metadata/ai.zoom.target";
91 const char* kAiZoomTargetNumberKey =
92     "http://samsung.com/tv/metadata/ai.zoom.target.num";
93 const char* kAiZoomFocusPriorityKey =
94     "http://samsung.com/tv/metadata/ai.zoom.focus.priority";
95 const char* kAiZoomAnimationKey =
96     "http://samsung.com/tv/metadata/ai.zoom.animation";
97 #endif
98
99 bool HandleFailedNavigationOnUIThread(content::WebContents* source,
100                                       std::string fallback_url) {
101   GURL url(fallback_url);
102   content::NavigationController::LoadURLParams params(url);
103
104   params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE;
105   source->GetController().LoadURLWithParams(params);
106   return true;
107 }
108
109 bool ShouldIgnoreNavigationOnUIThread(
110     content::NavigationHandle* navigation_handle) {
111   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
112   DCHECK(navigation_handle);
113 #if defined(WRT_JS_BRINGUP)
114   return false;
115 #else
116   auto url = navigation_handle->GetUrl();
117   if (!url.is_valid() || !url.SchemeIs(kAppControlScheme) ||
118       (!navigation_handle->HasUserGesture() &&
119           !navigation_handle->WasServerRedirect()))
120     return false;
121
122   BrowsableAppControl app_control(navigation_handle->GetWebContents(),
123                                   base::Bind(&HandleFailedNavigationOnUIThread),
124                                   url.possibly_invalid_spec());
125   return app_control.Proceed();
126 #endif
127 }
128
129 }  // namespace
130
131 WRTBrowserClient::WRTBrowserClient() {}
132
133 WRTBrowserClient::~WRTBrowserClient() {}
134
135 std::unique_ptr<content::BrowserMainParts>
136 WRTBrowserClient::CreateBrowserMainParts(bool is_integration_test) {
137   return std::make_unique<WRTBrowserMainParts>();
138 }
139
140 void WRTBrowserClient::RenderProcessWillLaunch(
141     content::RenderProcessHost* host) {
142   ElectronBrowserClient::RenderProcessWillLaunch(host);
143
144   host->AddFilter(new WRTRenderMessageFilter());
145
146 #if defined(TIZEN_WEB_SPEECH_RECOGNITION)
147   content::TtsPlatformImplTizen::SetTtsMode(
148       NativeWebRuntime::GetInstance().GetTTSMode());
149 #endif
150
151 #if BUILDFLAG(ENABLE_NACL)
152   int id = host->GetID();
153   host->AddFilter(new nacl::NaClHostMessageFilter(
154       id,
155       // @FIXME(dennis.oh) Check if IsOffTheRecord is set?
156       false, host->GetBrowserContext()->GetPath()));
157 #endif
158 #if defined(TIZEN_PEPPER_PLAYER)
159   host->AddFilter(new content::PepperMediaPlayerMessageFilter(host->GetID()));
160 #endif
161 }
162
163 #if BUILDFLAG(ENABLE_NACL)
164 content::BrowserPpapiHost* WRTBrowserClient::GetExternalBrowserPpapiHost(
165     int plugin_process_id) {
166   content::BrowserChildProcessHostIterator iter(PROCESS_TYPE_NACL_LOADER);
167   while (!iter.Done()) {
168     nacl::NaClProcessHost* host =
169         static_cast<nacl::NaClProcessHost*>(iter.GetDelegate());
170     if (host->process() && host->process()->GetData().id == plugin_process_id) {
171       // Found the plugin.
172       return host->browser_ppapi_host();
173     }
174     ++iter;
175   }
176   return nullptr;
177 }
178 #endif
179
180 void WRTBrowserClient::AppendExtraCommandLineSwitches(
181     base::CommandLine* command_line,
182     int process_id) {
183   ElectronBrowserClient::AppendExtraCommandLineSwitches(command_line, process_id);
184
185   std::string process_type =
186       command_line->GetSwitchValueASCII(::switches::kProcessType);
187   if (process_type != ::switches::kRendererProcess)
188     return;
189
190   auto current_command_line = base::CommandLine::ForCurrentProcess();
191   command_line->AppendSwitchASCII(switches::kTizenAppId,
192                                   current_command_line->GetSwitchValueASCII(
193                                       switches::kTizenAppId));
194 }
195
196 bool WRTBrowserClient::CanCreateWindow(
197     content::RenderFrameHost* opener,
198     const GURL& opener_url,
199     const GURL& opener_top_level_frame_url,
200     const url::Origin& source_origin,
201     content::mojom::WindowContainerType container_type,
202     const GURL& target_url,
203     const content::Referrer& referrer,
204     const std::string& frame_name,
205     WindowOpenDisposition disposition,
206     const blink::mojom::WindowFeatures& features,
207     bool user_gesture,
208     bool opener_suppressed,
209     bool* no_javascript_access) {
210   if (!NativeWebRuntime::GetInstance().ShouldAllowNavigation(target_url.spec()))
211     return false;
212
213   auto& app_data = ApplicationData::GetInstance();
214   if (!app_data.IsElectronApp()) {
215     // Window can be opened from script for Tizen Apps
216     // hence no need to check user_gesture
217     *no_javascript_access = false;
218     return true;
219   }
220
221   return ElectronBrowserClient::CanCreateWindow(
222       opener, opener_url, opener_top_level_frame_url, source_origin,
223       container_type, target_url, referrer, frame_name, disposition, features,
224       user_gesture, opener_suppressed, no_javascript_access);
225 }
226
227 void WRTBrowserClient::OverrideWebkitPrefs(
228     content::WebContents* web_contents,
229     blink::web_pref::WebPreferences* prefs) {
230   ElectronBrowserClient::OverrideWebkitPrefs(web_contents, prefs);
231
232   auto& runtime = NativeWebRuntime::GetInstance();
233   prefs->default_encoding = runtime.GetEncodingType();
234   prefs->uses_encoding_detector = false;
235
236   if (IsMobileProfile() || IsWearableProfile()) {
237     prefs->default_minimum_page_scale_factor = 0.25f;
238     prefs->default_maximum_page_scale_factor = 5.f;
239   } else {
240     prefs->default_minimum_page_scale_factor = 1.f;
241     prefs->default_maximum_page_scale_factor = 4.f;
242   }
243
244   if (runtime.IsTizenWebKitCompatibilityEnabled()) {
245     LOG(INFO) << "Adding compatibility mode configurations";
246     unsigned major, minor, release;
247     runtime.GetTizenVersion(major, minor, release);
248     prefs->tizen_version_major = major;
249     prefs->tizen_version_minor = minor;
250     prefs->tizen_version_release = release;
251     prefs->text_autosizing_enabled = false;
252   }
253
254 #if defined(TIZEN_VIDEO_HOLE)
255   // TODO: Overlay screen cannot be used with mirror blur.
256   prefs->video_hole_enabled = true;
257 #endif
258
259 #if defined(ENABLE_AUTO_ZOOM)
260   auto& app_data = ApplicationData::GetInstance();
261   auto apply_pref = [&app_data](auto& target, const char* settings_key) {
262     if (app_data.meta_data_info().HasKey(settings_key)) {
263       target = app_data.meta_data_info().GetValue(settings_key);
264     }
265   };
266   apply_pref(prefs->ai_zoom_default_target, kAiZoomTargetKey);
267   apply_pref(prefs->ai_zoom_default_target_number, kAiZoomTargetNumberKey);
268   apply_pref(prefs->ai_zoom_default_focus_priority, kAiZoomFocusPriorityKey);
269   apply_pref(prefs->ai_zoom_default_animation, kAiZoomAnimationKey);
270 #endif
271
272 #if BUILDFLAG(IS_TIZEN_TV)
273   if (!ApplicationData::GetInstance().IsElectronApp()) {
274     prefs->additional_shared_array_buffer_schemes.emplace_back(
275         url::kFileScheme);
276   }
277 #endif
278 }
279
280 #if BUILDFLAG(ENABLE_NACL) || \
281     (defined(TIZEN_PEPPER_EXTENSIONS) && BUILDFLAG(ENABLE_PLUGINS))
282 void WRTBrowserClient::DidCreatePpapiPlugin(content::BrowserPpapiHost* host) {
283   host->GetPpapiHost()->AddHostFactoryFilter(
284       std::unique_ptr<ppapi::host::HostFactory>(
285           new BrowserPepperHostFactoryEfl(host)));
286 }
287 #endif
288
289 #if defined(TIZEN_PEPPER_EXTENSIONS)
290 bool WRTBrowserClient::AllowPepperSocketAPI(
291     content::BrowserContext* browser_context,
292     const GURL& url,
293     bool private_api,
294     const content::SocketPermissionRequest* params) {
295   return true;
296 }
297
298 bool WRTBrowserClient::IsPluginAllowedToUseDevChannelAPIs(
299     content::BrowserContext* browser_context,
300     const GURL& url) {
301   return true;
302 }
303 #endif
304
305 void WRTBrowserClient::BindHostReceiverForRenderer(
306     content::RenderProcessHost* render_process_host,
307     mojo::GenericPendingReceiver receiver) {
308 #if !defined(WRT_JS_BRINGUP)
309 #if BUILDFLAG(IS_TIZEN_TV) || defined(ENABLE_TIZEN_SOCKETS)
310   if (auto r = receiver.As<sockets::mojom::SocketFactoryTizen>()) {
311     sockets::BindSocketFactoryTizen(std::move(r));
312     return;
313   }
314
315   if (auto r = receiver.As<sockets::mojom::HostResolverTizen>()) {
316     sockets::BindHostResolverTizen(std::move(r));
317     return;
318   }
319 #endif  // BUILDFLAG(IS_TIZEN_TV) || defined(ENABLE_TIZEN_SOCKETS)
320 #endif
321
322   electron::ElectronBrowserClient::BindHostReceiverForRenderer(
323       render_process_host, std::move(receiver));
324 }
325
326 std::unique_ptr<content::DevToolsManagerDelegate>
327 WRTBrowserClient::CreateDevToolsManagerDelegate() {
328   return std::make_unique<WRTDevToolsManagerDelegate>();
329 }
330
331 electron::NotificationPresenter* WRTBrowserClient::GetNotificationPresenter() {
332   if (!notification_presenter_)
333     notification_presenter_.reset(new WRTNotificationPresenter);
334   return notification_presenter_.get();
335 }
336
337 bool WRTBrowserClient::ForceSniffingFileUrlsForHtml() {
338   return true;
339 }
340
341 std::string WRTBrowserClient::GetApplicationLocale() {
342   return LocaleManager::GetSystemLocale();
343 }
344
345 std::unique_ptr<content::WebContentsViewDelegate>
346 WRTBrowserClient::GetWebContentsViewDelegate(
347     content::WebContents* web_contents) {
348   return std::make_unique<WRTWebContentsViewDelegate>(web_contents);
349 }
350
351 std::string WRTBrowserClient::GetUserAgent() {
352   return EflWebView::VersionInfo::GetInstance()->DefaultUserAgent();
353 }
354
355 scoped_refptr<network::SharedURLLoaderFactory>
356 WRTBrowserClient::GetSystemSharedURLLoaderFactory() {
357   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
358   if (!shared_url_loader_factory_)
359     shared_url_loader_factory_ = new content::SharedURLLoaderFactoryEfl();
360
361   return shared_url_loader_factory_;
362 }
363
364 bool WRTBrowserClient::ShouldEnableStrictSiteIsolation() {
365   auto& app_data = ApplicationData::GetInstance();
366   if (app_data.IsElectronApp())
367     return ElectronBrowserClient::ShouldEnableStrictSiteIsolation();
368   // TODO: On m85, unless this is disabled for Tizen apps,
369   // behavior tcts related to CSP are failing; Need to check
370   // if there are engine side changes between m76 & m85 causing
371   // this since SSI has been enabled for WRT since m76
372   return false;
373 }
374
375 bool WRTBrowserClient::WillCreateURLLoaderFactory(
376     content::BrowserContext* browser_context,
377     content::RenderFrameHost* frame_host,
378     int render_process_id,
379     URLLoaderFactoryType type,
380     const url::Origin& request_initiator,
381     absl::optional<int64_t> navigation_id,
382     ukm::SourceIdObj ukm_source_id,
383     mojo::PendingReceiver<network::mojom::URLLoaderFactory>* factory_receiver,
384     mojo::PendingRemote<network::mojom::TrustedURLLoaderHeaderClient>*
385         header_client,
386     bool* bypass_redirect_checks,
387     bool* disable_secure_dns,
388     network::mojom::URLLoaderFactoryOverridePtr* factory_override,
389     scoped_refptr<base::SequencedTaskRunner> navigation_response_task_runner) {
390   auto proxied_receiver = std::move(*factory_receiver);
391   mojo::PendingRemote<network::mojom::URLLoaderFactory> target_factory_remote;
392   *factory_receiver = target_factory_remote.InitWithNewPipeAndPassReceiver();
393
394   auto* protocol_registry =
395       electron::ProtocolRegistry::FromBrowserContext(browser_context);
396   new WRTProxyingURLLoaderFactory(
397       protocol_registry->intercept_handlers(),
398       std::move(proxied_receiver), std::move(target_factory_remote));
399
400   if (bypass_redirect_checks)
401     *bypass_redirect_checks = true;
402
403   return true;
404 }
405
406 void WRTBrowserClient::ConfigureNetworkContextParams(
407     content::BrowserContext* browser_context,
408     bool in_memory,
409     const base::FilePath& relative_partition_path,
410     network::mojom::NetworkContextParams* context_params,
411     cert_verifier::mojom::CertVerifierCreationParams*
412         cert_verifier_creation_params) {
413   ElectronBrowserClient::ConfigureNetworkContextParams(
414       browser_context, in_memory, relative_partition_path,
415       context_params, cert_verifier_creation_params);
416
417   base::FilePath path(ApplicationData::GetInstance().GetUserDataPath());
418   context_params->file_paths = network::mojom::NetworkContextFilePaths::New();
419   base::FilePath cookie_path = path.Append(".cookie/Cookies");
420   context_params->file_paths->data_directory = cookie_path.DirName();
421   context_params->file_paths->cookie_database_name = cookie_path.BaseName();
422   context_params->persist_session_cookies = true;
423   context_params->enable_encrypted_cookies = false;
424
425   if (!context_params->cookie_manager_params) {
426     context_params->cookie_manager_params =
427         network::mojom::CookieManagerParams::New();
428   }
429   context_params->cookie_manager_params->allow_file_scheme_cookies = true;
430 }
431
432 void WRTBrowserClient::ExposeInterfacesToRenderer(
433     service_manager::BinderRegistry* registry,
434     blink::AssociatedInterfaceRegistry* associated_registry,
435     content::RenderProcessHost* render_process_host) {
436   registry->AddInterface(
437       base::BindRepeating(&XWalkExtensionManager::Bind),
438       content::GetUIThreadTaskRunner({}));
439 }
440
441 std::vector<std::unique_ptr<content::NavigationThrottle>>
442 WRTBrowserClient::CreateThrottlesForNavigation(
443     content::NavigationHandle* handle) {
444   auto throttles = ElectronBrowserClient::CreateThrottlesForNavigation(handle);
445   auto& app_data = ApplicationData::GetInstance();
446   if (app_data.IsBrowsableApp() && handle->IsInMainFrame()) {
447     throttles.push_back(
448         std::make_unique<navigation_interception::InterceptNavigationThrottle>(
449             handle, base::BindRepeating(&ShouldIgnoreNavigationOnUIThread),
450             navigation_interception::SynchronyMode::kSync));
451   }
452
453   return throttles;
454 }
455
456 bool WRTBrowserClient::DoesSiteRequireDedicatedProcess(
457     content::BrowserContext* browser_context,
458     const GURL& effective_site_url) {
459   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
460 #if ENABLE_CHROME_EXTENSIONS
461   auto* registry = extensions::ExtensionRegistry::Get(browser_context);
462   const auto* extension = registry->enabled_extensions()
463                               .GetExtensionOrAppByURL(effective_site_url);
464   if (extension)
465     return true;
466 #endif
467   return ElectronBrowserClient::DoesSiteRequireDedicatedProcess(
468              browser_context, effective_site_url);
469 }
470
471 void WRTBrowserClient::GetAdditionalWebUISchemes(
472     std::vector<std::string>* additional_schemes) {
473   additional_schemes->push_back(kWRTUIScheme);
474 }
475
476 void WRTBrowserClient::RegisterAssociatedInterfaceBindersForRenderFrameHost(
477     content::RenderFrameHost& render_frame_host,
478     blink::AssociatedInterfaceRegistry& associated_registry) {
479   associated_registry.AddInterface<mojom::WRTBrowser>(base::BindRepeating(
480       [](content::RenderFrameHost* render_frame_host,
481          mojo::PendingAssociatedReceiver<mojom::WRTBrowser> receiver) {
482         WRTWebContents::BindBrowser(std::move(receiver), render_frame_host);
483       },
484       &render_frame_host));
485
486   ElectronBrowserClient::RegisterAssociatedInterfaceBindersForRenderFrameHost(
487       render_frame_host, associated_registry);
488 }
489
490 }  // namespace wrt