1 /*
2 Copyright (C) 2009-2010 ProFUSION embedded systems
3 Copyright (C) 2009-2011 Samsung Electronics
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19 */
20
21 #include "config.h"
22 #include "ewk_main.h"
23
24 #include "EWebKit.h"
25 #include "FileSystem.h"
26 #include "Logging.h"
27 #include "PageCache.h"
28 #include "PageGroup.h"
29 #include "ewk_private.h"
30 #include "ewk_settings.h"
31 #include "runtime/InitializeThreading.h"
32
33 #include <Ecore.h>
34 #include <Ecore_Evas.h>
35 #include <Edje.h>
36 #include <Eina.h>
37 #include <Evas.h>
38 #include <stdlib.h>
39 #include <sys/stat.h>
40 #include <wtf/Threading.h>
41
42 #if ENABLE(GLIB_SUPPORT)
43 #include <glib-object.h>
44 #include <glib.h>
45
46 #ifdef ENABLE_GTK_PLUGINS_SUPPORT
47 #include <gtk/gtk.h>
48 #endif
49
50 #endif
51
52 #if USE(SOUP)
53 // REMOVE-ME: see todo below
54 #include "ResourceHandle.h"
55 #include <libsoup/soup.h>
56 #endif
57
58 static int _ewk_init_count = 0;
59
60 /**
61 * \var _ewk_log_dom
62 * @brief the log domain identifier that is used with EINA's macros
63 */
64 int _ewk_log_dom = -1;
65
66 static Eina_Bool _ewk_init_body(void);
67
68 /**
69 * Initializes webkit's instance.
70 *
71 * - initializes components needed by Efl,
72 * - sets web database location,
73 * - sets page cache capacity,
74 * - increases a reference count of webkit's instance.
75 *
76 * @return a reference count of webkit's instance on success or 0 on failure
77 */
ewk_init(void)78 int ewk_init(void)
79 {
80 if (_ewk_init_count)
81 return ++_ewk_init_count;
82
83 if (!eina_init())
84 goto error_eina;
85
86 _ewk_log_dom = eina_log_domain_register("ewebkit", EINA_COLOR_ORANGE);
87 if (_ewk_log_dom < 0) {
88 EINA_LOG_CRIT("could not register log domain 'ewebkit'");
89 goto error_log_domain;
90 }
91
92 if (!evas_init()) {
93 CRITICAL("could not init evas.");
94 goto error_evas;
95 }
96
97 if (!ecore_init()) {
98 CRITICAL("could not init ecore.");
99 goto error_ecore;
100 }
101
102 if (!ecore_evas_init()) {
103 CRITICAL("could not init ecore_evas.");
104 goto error_ecore_evas;
105 }
106
107 if (!edje_init()) {
108 CRITICAL("could not init edje.");
109 goto error_edje;
110 }
111
112 _ewk_init_body();
113
114 return ++_ewk_init_count;
115
116 error_edje:
117 ecore_evas_shutdown();
118 error_ecore_evas:
119 ecore_shutdown();
120 error_ecore:
121 evas_shutdown();
122 error_evas:
123 eina_log_domain_unregister(_ewk_log_dom);
124 _ewk_log_dom = -1;
125 error_log_domain:
126 eina_shutdown();
127 error_eina:
128 return 0;
129 }
130
131 /**
132 * Decreases a reference count of webkit's instance, possibly destroying it.
133 *
134 * If the reference count reaches 0 webkit's instance is destroyed.
135 *
136 * @return a reference count of webkit's instance
137 */
ewk_shutdown(void)138 int ewk_shutdown(void)
139 {
140 _ewk_init_count--;
141 if (_ewk_init_count)
142 return _ewk_init_count;
143
144 ecore_evas_shutdown();
145 ecore_shutdown();
146 evas_shutdown();
147 eina_log_domain_unregister(_ewk_log_dom);
148 _ewk_log_dom = -1;
149 eina_shutdown();
150
151 return 0;
152 }
153
_ewk_init_body(void)154 Eina_Bool _ewk_init_body(void)
155 {
156
157 #if ENABLE(GLIB_SUPPORT)
158 g_type_init();
159
160 if (!g_thread_supported())
161 g_thread_init(0);
162
163 #ifdef ENABLE_GTK_PLUGINS_SUPPORT
164 gdk_threads_init();
165 if (!gtk_init_check(0, 0))
166 WRN("Could not initialize GTK support.");
167 #endif
168
169 if (!ecore_main_loop_glib_integrate())
170 WRN("Ecore was not compiled with GLib support, some plugins will not "
171 "work (ie: Adobe Flash)");
172 #endif
173
174 JSC::initializeThreading();
175 WTF::initializeMainThread();
176 WebCore::InitializeLoggingChannelsIfNecessary();
177
178 // Page cache capacity (in pages). Comment from Mac port:
179 // (Research indicates that value / page drops substantially after 3 pages.)
180 // FIXME: Expose this with an API and/or calculate based on available resources
181 WebCore::pageCache()->setCapacity(3);
182 WebCore::PageGroup::setShouldTrackVisitedLinks(true);
183
184 String home = WebCore::homeDirectoryPath();
185 struct stat state;
186 // check home directory first
187 if (stat(home.utf8().data(), &state) == -1) {
188 // Exit now - otherwise you may have some crash later
189 int errnowas = errno;
190 CRITICAL("Can't access HOME dir (or /tmp) - no place to save databases: %s", strerror(errnowas));
191 return EINA_FALSE;
192 }
193
194 WTF::String wkdir = home + "/.webkit";
195 if (WebCore::makeAllDirectories(wkdir)) {
196 ewk_settings_web_database_path_set(wkdir.utf8().data());
197 ewk_settings_icon_database_path_set(wkdir.utf8().data());
198
199 #if ENABLE(OFFLINE_WEB_APPLICATIONS)
200 ewk_settings_cache_directory_path_set(wkdir.utf8().data());
201 #endif
202 }
203
204 // TODO: this should move to WebCore, already reported to webkit-gtk folks:
205 #if USE(SOUP)
206 if (1) {
207 SoupSession* session = WebCore::ResourceHandle::defaultSession();
208 soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_SNIFFER);
209 soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_DECODER);
210 }
211 #endif
212
213 return EINA_TRUE;
214 }
215
216