[WRTjs] Refactor popup
[platform/framework/web/chromium-efl.git] / wrt / src / browser / wrt_xwalk_extension_browser.cc
1 // Copyright (c) 2023 Samsung Electronics Co., Ltd. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "wrt/src/browser/wrt_xwalk_extension_browser.h"
6
7 #include <dlfcn.h>
8
9 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/navigation_entry.h"
11 #include "electron/shell/browser/browser.h"
12 #include "tizen_src/chromium_impl/components/xwalk_extensions/browser/xwalk_extension_manager.h"
13 #include "wrt/src/base/file_utils.h"
14 #include "wrt/src/base/string_utils.h"
15 #include "wrt/src/browser/native_web_runtime.h"
16 #include "wrt/src/browser/splash_screen.h"
17 #include "wrt/src/browser/wrt_browser_context.h"
18 #include "wrt/src/browser/wrt_native_window.h"
19 #include "wrt/src/browser/wrt_web_contents.h"
20 #include "wrt/src/service/wrt_service_manager.h"
21
22 #ifndef XWALK_EXTENSION_PATH
23   #error XWALK_EXTENSION_PATH is not set.
24 #endif
25
26 namespace wrt {
27
28 // static
29 std::map<std::string, std::string>
30     WRTXWalkExtensionBrowser::preload_extensions_;
31
32 WRTXWalkExtensionBrowser::WRTXWalkExtensionBrowser() {
33   preload_extensions_["tizen"] =
34       XWALK_EXTENSION_PATH + std::string("/libtizen.so");
35   preload_extensions_["common"] =
36       XWALK_EXTENSION_PATH + std::string("/libtizen_common.so");
37   preload_extensions_["tizen.application"] =
38       XWALK_EXTENSION_PATH + std::string("/libtizen_application.so");
39   preload_extensions_["xwalk"] =
40       XWALK_EXTENSION_PATH + std::string("/libtizen_utils.so");
41 }
42
43 void WRTXWalkExtensionBrowser::PreloadExtensions() {
44   for (auto& extension : preload_extensions_) {
45     LOG(INFO) << "preload name: " << extension.first
46               << ",lib:" << extension.second;
47     void* dlhandle = dlopen(extension.second.c_str(), RTLD_NOW | RTLD_GLOBAL);
48     if (!dlhandle) {
49       LOG(WARNING) << "Fail to load libs : " << dlerror();
50       continue;
51     }
52     XWalkExtensionManager::GetInstance()->SetPreloadHandle(
53         extension.second, dlhandle);
54   }
55 }
56
57 std::string WRTXWalkExtensionBrowser::HandleRuntimeMessageInternal(
58     const std::string& type, const std::string& value) {
59   if (electron::Browser::Get()->is_quitting()) {
60     LOG(ERROR) << "App is quitting, handle runtime message is skipped";
61     return std::string();
62   }
63   if (type == "tizen://hide") {
64     LOG(ERROR) << "tizen://hide";
65     auto native_window = WRTNativeWindow::GetMainNativeWindow();
66     if (native_window)
67       native_window->LowerWindow();
68   } else if (type == "tizen://exit") {
69     LOG(ERROR) << "tizen://exit";
70     NativeWebRuntime::GetInstance().RequestQuit();
71   } else if (type == "tizen://changeUA") {
72     for (auto* web_contents : content::WebContentsImpl::GetAllWebContents()) {
73       auto& controller = web_contents->GetController();
74       bool override = !value.empty();
75       for (int i = 0; i < controller.GetEntryCount(); ++i)
76         controller.GetEntryAtIndex(i)->SetIsOverridingUserAgent(override);
77 #if BUILDFLAG(IS_TIZEN_TV)
78       web_contents->SetReloadByUserAgentChange(false);
79 #endif
80       web_contents->SetUserAgentOverride(
81           blink::UserAgentOverride::UserAgentOnly(value), false);
82     }
83     return "lazy_success";
84   } else if (type == "tizen://deleteAllCookies") {
85     LOG(INFO) << "tizen://deleteAllCookies";
86     WRTBrowserContext::GetInstance()->DeleteCookies();
87     return "success";
88   } else if (type == "tizen://hide_splash_screen") {
89     LOG(INFO) << "tizen://hide_splash_screen";
90     std::vector<std::string> params = {"custom"};
91     NativeWebRuntime::GetInstance().NotifyMessage("hideSplashScreen", params);
92   } else if (type == "tizen://setAudioLatencyMode") {
93     LOG(INFO) << "tizen://setAudioLatencyMode - " << value;
94     int mode = content::BrowserContext::AudioLatencyMode::ERROR;
95     base::StringToInt(value, &mode);
96     WRTBrowserContext::GetInstance()->SetAudioLatencyMode(
97         static_cast<content::BrowserContext::AudioLatencyMode>(mode));
98     return "success";
99   } else if (type == "tizen://getAudioLatencyMode") {
100     auto mode = WRTBrowserContext::GetInstance()->GetAudioLatencyMode();
101     LOG(INFO) << "tizen://getAudioLatencyMode - " << mode;
102     return std::to_string(mode);
103   } else {
104     LOG(ERROR) << "Unknown message type : " << type;
105   }
106   return std::string();
107 }
108
109 void WRTXWalkExtensionBrowser::GetRuntimeVariable(
110     const char* key, char* value, size_t value_len) {
111   if (!key || !value)
112     return;
113
114   std::string runtime_variable;
115   if (ApplicationData::IsServiceApp()) {
116     auto service_manager = WRTServiceManager::Get();
117     runtime_variable = service_manager->GetRuntimeVariable(key);
118   } else {
119     auto& runtime = NativeWebRuntime::GetInstance();
120     runtime_variable = runtime.GetRuntimeVariable(key);
121   }
122   if (!runtime_variable.empty())
123     strncpy(value, runtime_variable.c_str(), value_len);
124 }
125
126 }  // namespace wrt