[Tizen][Web] Check WebEngineManager available + Ignore duplicate destroy
[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 namespace Dali
33 {
34 namespace Plugin
35 {
36 WebEngineManager& WebEngineManager::Get()
37 {
38   static WebEngineManager instance;
39   return instance;
40 }
41
42 bool WebEngineManager::IsAvailable()
43 {
44   return Get().mWebEngineManagerAvailable;
45 }
46
47 WebEngineManager::WebEngineManager()
48 : mSlotDelegate(this),
49   mWebEngineManagerAvailable(true)
50 {
51   elm_init(0, 0);
52   ewk_init();
53   mWindow = ecore_evas_new("wayland_egl", 0, 0, 1, 1, 0);
54
55   Ewk_Context* context = ewk_context_default_get();
56   mWebEngineContext.reset(new TizenWebEngineContext(context));
57
58   Ewk_Cookie_Manager* manager = ewk_context_cookie_manager_get(context);
59   mWebEngineCookieManager.reset(new TizenWebEngineCookieManager(manager));
60   Dali::LifecycleController::Get().TerminateSignal().Connect(mSlotDelegate, &WebEngineManager::OnTerminated);
61 }
62
63 WebEngineManager::~WebEngineManager()
64 {
65   if(mWebEngineManagerAvailable)
66   {
67     // Call OnTerminated directly.
68     OnTerminated();
69   }
70 }
71
72 Ecore_Evas* WebEngineManager::GetWindow()
73 {
74   return mWindow;
75 }
76
77 Dali::WebEngineContext* WebEngineManager::GetContext()
78 {
79   return mWebEngineContext.get();
80 }
81
82 Dali::WebEngineCookieManager* WebEngineManager::GetCookieManager()
83 {
84   return mWebEngineCookieManager.get();
85 }
86
87 void WebEngineManager::Add(Evas_Object* webView, Dali::WebEnginePlugin* engine)
88 {
89   mWebEngines[webView] = engine;
90 }
91
92 void WebEngineManager::Remove(Evas_Object* webView)
93 {
94   auto iter = mWebEngines.find(webView);
95   if(iter != mWebEngines.end())
96   {
97     mWebEngines.erase(iter);
98   }
99 }
100
101 Dali::WebEnginePlugin* WebEngineManager::Find(Evas_Object* webView)
102 {
103   auto iter = mWebEngines.find(webView);
104   if(iter != mWebEngines.end())
105   {
106     return iter->second;
107   }
108   else
109   {
110     return nullptr;
111   }
112 }
113
114 void WebEngineManager::OnTerminated()
115 {
116   // App is terminated. Now web engine is not available anymore.
117   mWebEngineManagerAvailable = false;
118
119   Dali::LifecycleController::Get().TerminateSignal().Disconnect(mSlotDelegate, &WebEngineManager::OnTerminated);
120
121   for(auto it = mWebEngines.begin(); it != mWebEngines.end(); it++)
122   {
123     // Destroy WebEngine
124     auto webEnginePlugin = it->second;
125     if(webEnginePlugin)
126     {
127       webEnginePlugin->Destroy();
128     }
129   }
130   mWebEngines.clear();
131   ecore_evas_free(mWindow);
132
133   // Release context and cookie manager before ewk_shutdown.
134   mWebEngineContext.reset();
135   mWebEngineCookieManager.reset();
136
137   ewk_shutdown();
138   elm_shutdown();
139   DALI_LOG_RELEASE_INFO("#WebEngineManager is destroyed fully.\n");
140 }
141
142 } // namespace Plugin
143 } // namespace Dali
144
145 extern "C" DALI_EXPORT_API Dali::WebEngineContext* GetWebEngineContext()
146 {
147   return Dali::Plugin::WebEngineManager::Get().GetContext();
148 }
149
150 extern "C" DALI_EXPORT_API Dali::WebEngineCookieManager* GetWebEngineCookieManager()
151 {
152   return Dali::Plugin::WebEngineManager::Get().GetCookieManager();
153 }