ed0ed11b17ee08d4d0fedbad7dac1bf4e6331a5c
[platform/core/uifw/dali-extension.git] / dali-extension / web-engine-chromium / tizen-web-engine-manager.cpp
1 /*
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "tizen-web-engine-manager.h"
19
20 #include "tizen-web-engine-context.h"
21 #include "tizen-web-engine-cookie-manager.h"
22
23 #include <Elementary.h>
24
25 #include <dali/devel-api/adaptor-framework/lifecycle-controller.h>
26 #include <dali/integration-api/debug.h>
27
28 #include <ewk_context.h>
29 #include <ewk_context_internal.h>
30 #include <ewk_main.h>
31
32 #include <stdexcept>
33
34 namespace Dali
35 {
36 namespace Plugin
37 {
38
39 WebEngineManager& WebEngineManager::Get()
40 {
41   static WebEngineManager instance;
42   return instance;
43 }
44
45 bool WebEngineManager::IsAvailable()
46 {
47   return Get().mWebEngineManagerAvailable;
48 }
49
50 WebEngineManager::WebEngineManager()
51 : mSlotDelegate(this),
52   mWebEngineManagerAvailable(true)
53 {
54   DALI_LOG_RELEASE_INFO("#WebEngineManager is created.\n");
55
56   elm_init(0, 0);
57   ewk_init();
58   mWindow = ecore_evas_new("wayland_egl", 0, 0, 1, 1, 0);
59
60   Ewk_Context* context = ewk_context_default_get();
61   mWebEngineContext.reset(new TizenWebEngineContext(context));
62
63   Ewk_Cookie_Manager* manager = ewk_context_cookie_manager_get(context);
64   mWebEngineCookieManager.reset(new TizenWebEngineCookieManager(manager));
65   Dali::LifecycleController::Get().TerminateSignal().Connect(mSlotDelegate, &WebEngineManager::OnTerminated);
66
67   DALI_LOG_RELEASE_INFO("#WebEngineManager is created fully.\n");
68 }
69
70 WebEngineManager::~WebEngineManager()
71 {
72   if(mWebEngineManagerAvailable)
73   {
74     try
75     {
76       // Call OnTerminated directly.
77       OnTerminated();
78     }
79     catch(std::invalid_argument const& ex)
80     {
81       DALI_LOG_RELEASE_INFO("Failed to destroy web engine:%s!\n", ex.what());
82     }
83   }
84 }
85
86 Ecore_Evas* WebEngineManager::GetWindow()
87 {
88   return mWindow;
89 }
90
91 Dali::WebEngineContext* WebEngineManager::GetContext()
92 {
93   return mWebEngineContext.get();
94 }
95
96 Dali::WebEngineCookieManager* WebEngineManager::GetCookieManager()
97 {
98   return mWebEngineCookieManager.get();
99 }
100
101 void WebEngineManager::Add(Evas_Object* webView, Dali::WebEnginePlugin* engine)
102 {
103   mWebEngines[webView] = engine;
104 }
105
106 void WebEngineManager::Remove(Evas_Object* webView)
107 {
108   auto iter = mWebEngines.find(webView);
109   if(iter != mWebEngines.end())
110   {
111     mWebEngines.erase(iter);
112   }
113 }
114
115 Dali::WebEnginePlugin* WebEngineManager::Find(Evas_Object* webView)
116 {
117   auto iter = mWebEngines.find(webView);
118   if(iter != mWebEngines.end())
119   {
120     return iter->second;
121   }
122   return nullptr;
123 }
124
125 Evas_Object* WebEngineManager::Find(Dali::WebEnginePlugin* plugin)
126 {
127   for(auto it = mWebEngines.begin(); it != mWebEngines.end(); it++)
128   {
129     if (it->second == plugin)
130     {
131       return it->first;
132     }
133   }
134   return nullptr;
135 }
136
137 void WebEngineManager::OnTerminated()
138 {
139   // Ignore duplicated termination
140   if(DALI_UNLIKELY(!mWebEngineManagerAvailable))
141   {
142     return;
143   }
144   DALI_LOG_RELEASE_INFO("#WebEngineManager is destroyed.\n");
145
146   // App is terminated. Now web engine is not available anymore.
147   mWebEngineManagerAvailable = false;
148
149   for(auto it = mWebEngines.begin(); it != mWebEngines.end(); it++)
150   {
151     // Destroy WebEngine
152     auto webEnginePlugin = it->second;
153     if(webEnginePlugin)
154     {
155       webEnginePlugin->Destroy();
156     }
157   }
158   mWebEngines.clear();
159   ecore_evas_free(mWindow);
160
161   // Release context and cookie manager before ewk_shutdown.
162   mWebEngineContext.reset();
163   mWebEngineCookieManager.reset();
164
165   ewk_shutdown();
166   elm_shutdown();
167   DALI_LOG_RELEASE_INFO("#WebEngineManager is destroyed fully.\n");
168 }
169
170 } // namespace Plugin
171 } // namespace Dali
172
173 extern "C" DALI_EXPORT_API Dali::WebEngineContext* GetWebEngineContext()
174 {
175   return Dali::Plugin::WebEngineManager::Get().GetContext();
176 }
177
178 extern "C" DALI_EXPORT_API Dali::WebEngineCookieManager* GetWebEngineCookieManager()
179 {
180   return Dali::Plugin::WebEngineManager::Get().GetCookieManager();
181 }