Limit number of opened tabs in brower
[profile/tv/apps/web/browser.git] / services / WebKitEngineService / WebView.cpp
index e80c3ec..33170c5 100644 (file)
@@ -37,6 +37,7 @@
 
 #include "AbstractWebEngine/AbstractWebEngine.h"
 #include "AbstractWebEngine/TabThumbCache.h"
+#include "app_common.h"
 #include "BrowserAssert.h"
 #include "BrowserLogger.h"
 #include "EflTools.h"
 #include "ServiceManager.h"
 
 #define certificate_crt_path CERTS_DIR
+#if MERGE_ME
 #define APPLICATION_NAME_FOR_USER_AGENT "SamsungBrowser/1.0"
+#else
+#define APPLICATION_NAME_FOR_USER_AGENT "Mozilla/5.0 (X11; SMART-TV; Linux) AppleWebkit/538.1 (KHTML, like Gecko) Safari/538.1"
+#endif
+
+//TODO: temporary user agent for mobile display, change to proper one
+#define APPLICATION_NAME_FOR_USER_AGENT_MOBILE "Mozilla/5.0 (Linux; Tizen 3.0; SAMSUNG SM-Z130H) AppleWebKit/538.1 (KHTML, like Gecko) SamsungBrowser/1.0 Mobile Safari/538.1"
 
 using namespace tizen_browser::tools;
 
@@ -53,10 +61,14 @@ namespace tizen_browser {
 namespace basic_webengine {
 namespace webkitengine_service {
 
+const std::string WebView::COOKIES_PATH = "cookies";
+
 WebView::WebView(Evas_Object * obj, TabId tabId)
     : m_parent(obj)
     , m_tabId(tabId)
-    , m_ewkView(NULL)
+    , m_ewkContext(ewk_context_new())
+    , m_title(std::string())
+    , m_ewkView(nullptr)
     , m_isLoading(false)
     , m_loadError(false)
 {
@@ -70,19 +82,15 @@ WebView::~WebView()
     if (m_ewkView) {
         unregisterCallbacks();
     }
+
+    ewk_context_delete(m_ewkContext);
 }
 
-void WebView::init(Evas_Object * opener)
+void WebView::init(bool desktopMode, Evas_Object * opener)
 {
 #if defined(USE_EWEBKIT)
-    Ewk_Context *context = ewk_context_default_get();
-    if (context)
-    {
-        ewk_context_cache_model_set(context, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
-        ewk_context_certificate_file_set(context, certificate_crt_path);
-    }
-
-    m_ewkView = ewk_view_add(evas_object_evas_get(m_parent));
+    M_ASSERT(m_ewkContext);
+    m_ewkView = ewk_view_add_with_context(evas_object_evas_get(m_parent), m_ewkContext);
 
     evas_object_data_set(m_ewkView, "_container", this);
     BROWSER_LOGD("%s:%d %s self=%p", __FILE__, __LINE__, __func__, this);
@@ -90,25 +98,22 @@ void WebView::init(Evas_Object * opener)
     evas_object_color_set(m_ewkView, 255, 255, 255, 255);
     evas_object_size_hint_weight_set(m_ewkView, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
     evas_object_size_hint_align_set(m_ewkView, EVAS_HINT_FILL, EVAS_HINT_FILL);
-    ewk_view_application_name_for_user_agent_set(m_ewkView, APPLICATION_NAME_FOR_USER_AGENT);
+    if (desktopMode) {
+        switchToDesktopMode();
+    } else {
+        switchToMobileMode();
+    }
     //\todo: when value is other than 1.0, scroller is located improperly
 //    ewk_view_device_pixel_ratio_set(m_ewkView, 1.0f);
 
 #if PLATFORM(TIZEN)
     ewk_view_resume(m_ewkView);
 #endif
-#if 0
-    // set local storage, favion, cookies
-    std::string webkit_path =  boost::any_cast <std::string> (config.get("webkit/dir"));
-//    ewk_context_web_storage_path_set(context, (webkit_path + std::string("/storage")).c_str());
-    ewk_context_favicon_database_directory_set(context, (webkit_path + std::string("/favicon")).c_str());
-    ewk_cookie_manager_persistent_storage_set(ewk_context_cookie_manager_get(context)
-                                             , (webkit_path + std::string("/cookies.db")).c_str()
-                                             , EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
-
-///\note Odroid modification - not exists in WebKit API
-//     ewk_cookie_manager_widget_cookie_directory_set(ewk_context_cookie_manager_get(context), webkit_path.c_str());
-#endif
+
+    ewk_context_cache_model_set(m_ewkContext, EWK_CACHE_MODEL_PRIMARY_WEBBROWSER);
+    std::string path = app_get_data_path() + COOKIES_PATH;
+    ewk_cookie_manager_persistent_storage_set(ewk_context_cookie_manager_get(m_ewkContext),  path.c_str(), EWK_COOKIE_PERSISTENT_STORAGE_SQLITE);
+
     setupEwkSettings();
     registerCallbacks();
 #else
@@ -207,7 +212,7 @@ std::string WebView::getURI(void)
 {
 #if defined(USE_EWEBKIT)
     BROWSER_LOGD("%s:%d %s uri=%s", __FILE__, __LINE__, __func__, ewk_view_url_get(m_ewkView));
-    return tizen_browser::tools::fromChar(ewk_view_url_get(m_ewkView));
+    return fromChar(ewk_view_url_get(m_ewkView));
 #else
     return std::string();
 #endif
@@ -295,11 +300,20 @@ void WebView::setPrivateMode(bool state)
     Ewk_Settings * settings = ewk_page_group_settings_get(ewk_view_page_group_get(m_ewkView));
 #endif
     ewk_settings_private_browsing_enabled_set(settings, state);
-    if(state){
-         ewk_cookie_manager_accept_policy_set(ewk_context_cookie_manager_get(ewk_context_default_get()), EWK_COOKIE_ACCEPT_POLICY_NEVER);
-    }
-    else{
-         ewk_cookie_manager_accept_policy_set(ewk_context_cookie_manager_get(ewk_context_default_get()), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
+    if (m_ewkView)
+    {
+        Ewk_Context *context = ewk_view_context_get(m_ewkView);
+        if (context)
+        {
+            if(state)
+            {
+                 ewk_cookie_manager_accept_policy_set(ewk_context_cookie_manager_get(context), EWK_COOKIE_ACCEPT_POLICY_NEVER);
+            }
+            else
+            {
+                 ewk_cookie_manager_accept_policy_set(ewk_context_cookie_manager_get(context), EWK_COOKIE_ACCEPT_POLICY_ALWAYS);
+            }
+        }
     }
 #endif
 }
@@ -415,15 +429,15 @@ void WebView::confirmationResult(WebConfirmationPtr confirmation)
 #endif
 }
 
-std::shared_ptr<tizen_browser::tools::BrowserImage> WebView::captureSnapshot(int targetWidth, int targetHeight)
+std::shared_ptr<BrowserImage> WebView::captureSnapshot(int targetWidth, int targetHeight)
 {
-    BROWSER_LOGD("%s:%d %s NOT IMPLEMENTED, returning empty BrowserImage object", __FILE__, __LINE__, __func__);
+    BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
     M_ASSERT(m_ewkView);
     M_ASSERT(targetWidth);
     M_ASSERT(targetHeight);
     Evas_Coord vw, vh;
-    std::shared_ptr<tizen_browser::tools::BrowserImage> noImage = std::make_shared<tizen_browser::tools::BrowserImage>();
-    evas_object_geometry_get(m_ewkView, NULL, NULL, &vw, &vh);
+    std::shared_ptr<BrowserImage> noImage = std::make_shared<BrowserImage>();
+    evas_object_geometry_get(m_ewkView, nullptr, nullptr, &vw, &vh);
     if (vw == 0 || vh == 0)
         return noImage;
 
@@ -446,15 +460,14 @@ std::shared_ptr<tizen_browser::tools::BrowserImage> WebView::captureSnapshot(int
     if (area.w == 0 || area.h == 0)
         return noImage;
 
-    double scaleW = (double)targetWidth / (double)(area.w);
-    double scaleH = (double)targetHeight / (double)(area.h);
 
+    BROWSER_LOGD("[%s:%d] Before snapshot (screenshot) - look at the time of taking snapshot below",__func__, __LINE__);
 #if defined(USE_EWEBKIT)
 #if PLATFORM(TIZEN)
-    Evas_Object *snapshot = nullptr;
-    // TODO use ewk_view_screenshot_contents_get_async API here
+    Evas_Object *snapshot = ewk_view_screenshot_contents_get( m_ewkView, area, 1.0, evas_object_evas_get(m_ewkView));
+    BROWSER_LOGD("[%s:%d] Snapshot (screenshot) catched, evas pointer: %p",__func__, __LINE__, snapshot);
     if (snapshot)
-        return tizen_browser::tools::EflTools::getBrowserImage(snapshot);
+        return EflTools::getBrowserImage(snapshot);
 #endif
 #endif
 
@@ -488,11 +501,12 @@ void WebView::__newWindowRequest(void *data, Evas_Object *, void *out)
     M_ASSERT(m_webEngine);
 
     /// \todo: Choose newly created tab.
-    TabId id = m_webEngine->addTab(std::string(), &self->getTabId());
-    BROWSER_LOGD("Created tab: %s", id.toString().c_str());
-
-    Evas_Object* tab_ewk_view = m_webEngine->getTabView(id);
-    *static_cast<Evas_Object**>(out) = tab_ewk_view;
+    TabId id;
+    if (m_webEngine->currentTabId() != (id = m_webEngine->addTab(std::string(), &self->getTabId()))) {
+        BROWSER_LOGD("Created tab: %s", id.toString().c_str());
+        Evas_Object* tab_ewk_view = m_webEngine->getTabView(id);
+        *static_cast<Evas_Object**>(out) = tab_ewk_view;
+    }
 }
 
 void WebView::__closeWindowRequest(void *data, Evas_Object *, void *)
@@ -533,6 +547,7 @@ void WebView::__loadFinished(void * data, Evas_Object * /* obj */, void * /* eve
     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
 
     WebView * self = reinterpret_cast<WebView *>(data);
+
     self->m_isLoading = false;
     self->m_loadProgress = 1;
 
@@ -548,7 +563,6 @@ void WebView::__loadProgress(void * data, Evas_Object * /* obj */, void * event_
 
     WebView * self = reinterpret_cast<WebView *>(data);
     self->m_loadProgress = *(double *)event_info;
-
     self->loadProgress(self->m_loadProgress);
 }
 
@@ -569,7 +583,7 @@ void WebView::__loadError(void* data, Evas_Object * obj, void* ewkError)
     {
         BROWSER_LOGD("Stop signal emitted");
         BROWSER_LOGD("Error description: %s", ewk_error_description_get(error));
-        evas_object_smart_callback_call(obj, "load,stop", NULL);
+        evas_object_smart_callback_call(obj, "load,stop", nullptr);
     }
     else
     {
@@ -583,8 +597,7 @@ void WebView::__titleChanged(void * data, Evas_Object * obj, void * /* event_inf
     BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
 
     WebView * self = reinterpret_cast<WebView *>(data);
-    self->m_title = tizen_browser::tools::fromChar(ewk_view_title_get(obj));
-
+    self->m_title = fromChar(ewk_view_title_get(obj));
     self->titleChanged(self->m_title);
 }
 
@@ -594,11 +607,7 @@ void WebView::__urlChanged(void * data, Evas_Object * /* obj */, void * event_in
 
     WebView * self = reinterpret_cast<WebView *>(data);
     BROWSER_LOGD("URL changed for tab: %s", self->getTabId().toString().c_str());
-    std::shared_ptr<basic_webengine::AbstractWebEngine<Evas_Object>> m_webEngine;
-    m_webEngine = std::dynamic_pointer_cast<basic_webengine::AbstractWebEngine<Evas_Object>, tizen_browser::core::AbstractService>(
-            tizen_browser::core::ServiceManager::getInstance().getService("org.tizen.browser.webkitengineservice"));
-    M_ASSERT(m_webEngine);
-    self->uriChanged(tizen_browser::tools::fromChar(reinterpret_cast<const char *>(event_info)));
+    self->uriChanged(self->getURI());
     self->tabIdChecker(self->m_tabId);
 }
 
@@ -619,7 +628,7 @@ void WebView::__faviconChanged(void* data, Evas_Object*, void*)
         Evas_Object * favicon = ewk_context_icon_database_icon_object_add(ewk_view_context_get(self->m_ewkView), ewk_view_url_get(self->m_ewkView),evas_object_evas_get(self->m_ewkView));
         if (favicon) {
             BROWSER_LOGD("[%s:%d] Favicon received", __PRETTY_FUNCTION__, __LINE__);
-            self->faviconImage = tizen_browser::tools::EflTools::getBrowserImage(favicon);
+            self->faviconImage = EflTools::getBrowserImage(favicon);
             evas_object_unref(favicon);
             self->favIconChanged(self->faviconImage);
         }
@@ -642,8 +651,8 @@ void WebView::__IMEOpened(void* data, Evas_Object*, void*)
 
 std::string WebView::securityOriginToUri(const Ewk_Security_Origin *origin)
 {
-    std::string protocol = tizen_browser::tools::fromChar(ewk_security_origin_protocol_get(origin));
-    std::string uri = tizen_browser::tools::fromChar(ewk_security_origin_host_get(origin));
+    std::string protocol = fromChar(ewk_security_origin_protocol_get(origin));
+    std::string uri = fromChar(ewk_security_origin_host_get(origin));
     std::string url = (boost::format("%1%://%2%") % protocol % uri).str();
     return url;
 }
@@ -792,7 +801,7 @@ bool WebView::hasFocus() const
 
 double WebView::getZoomFactor() const
 {
-    if(EINA_UNLIKELY(m_ewkView == NULL)){
+    if(EINA_UNLIKELY(m_ewkView == nullptr)) {
         return 1.0;
     }
 
@@ -806,64 +815,53 @@ double WebView::getZoomFactor() const
 void WebView::setZoomFactor(double zoomFactor)
 {
 #if defined(USE_EWEBKIT)
-    if(m_ewkView){
+    if(m_ewkView) {
         //using zoomFactor = 0 sets zoom "fit to screen"
 
-        ewk_view_page_zoom_set(m_ewkView, zoomFactor);
+        if(zoomFactor != getZoomFactor()) 
+            ewk_view_page_zoom_set(m_ewkView, zoomFactor);
     }
 #endif
 }
 
+void WebView::scrollView(const int& dx, const int& dy)
+{
+    ewk_view_scroll_by(m_ewkView, dx, dy);
+}
 
-const TabId& WebView::getTabId(){
+const TabId& WebView::getTabId() {
     return m_tabId;
 }
 
 
-std::shared_ptr<tizen_browser::tools::BrowserImage> WebView::getFavicon() {
+std::shared_ptr<BrowserImage> WebView::getFavicon()
+{
     BROWSER_LOGD("%s:%d, TabId: %s", __PRETTY_FUNCTION__, __LINE__, m_tabId.toString().c_str());
-    M_ASSERT(m_ewkView);
-
-#if defined(USE_EWEBKIT)
-    if (faviconImage.get() == NULL) {
 
-#if PLATFORM(TIZEN)
-//    Evas_Object * favicon = ewk_view_favicon_get(m_ewkView);
     Evas_Object * favicon = ewk_context_icon_database_icon_object_add(ewk_view_context_get(m_ewkView), ewk_view_url_get(m_ewkView),evas_object_evas_get(m_ewkView));
-#else
-    Ewk_Favicon_Database * database = ewk_context_favicon_database_get(ewk_view_context_get(m_ewkView));
-    Evas_Object * favicon = ewk_favicon_database_icon_get(database, ewk_view_url_get(m_ewkView), evas_object_evas_get(m_ewkView));
-#endif
+    faviconImage = EflTools::getBrowserImage(favicon);
+    evas_object_unref(favicon);
 
-#ifndef NDEBUG
-        int w = 0, h = 0;
-        evas_object_image_size_get(favicon, &w, &h);
-        BROWSER_LOGD("[%s]: Info about favicon: w:%d h:%d, type: %s", __func__, w, h, evas_object_type_get(favicon));
-#endif
-        if (favicon) {
-            std::shared_ptr<tizen_browser::tools::BrowserImage>
-                image = tizen_browser::tools::EflTools::getBrowserImage(favicon);
-
-            evas_object_unref(favicon);
-
-            return image;
-        }
-    } else {
+    if(faviconImage.get())
         return faviconImage;
-    }
-#endif
 
-    BROWSER_LOGE("[%s:%d]: Returned favicon is empty!", __PRETTY_FUNCTION__, __LINE__);
-    return std::make_shared<tizen_browser::tools::BrowserImage>();
+    BROWSER_LOGD("[%s:%d] Returned favicon is empty!",  __PRETTY_FUNCTION__, __LINE__);
+    return std::make_shared<BrowserImage>();
 }
 
 void WebView::clearPrivateData()
 {
     BROWSER_LOGD("Clearing private data");
 #if defined(USE_EWEBKIT)
-    Ewk_Context * context = ewk_context_default_get();
-    ewk_context_web_storage_delete_all(context);
-    ewk_cookie_manager_cookies_clear(ewk_context_cookie_manager_get(context));
+    if (m_ewkView)
+    {
+        Ewk_Context *context = ewk_view_context_get(m_ewkView);
+        if (context)
+        {
+            ewk_context_web_storage_delete_all(context);
+            ewk_cookie_manager_cookies_clear(ewk_context_cookie_manager_get(context));
+        }
+    }
 #endif
 }
 
@@ -874,6 +872,22 @@ void WebView::searchOnWebsite(const std::string & searchString, int flags)
     BROWSER_LOGD("Ewk search; word: %s, result: %d", searchString.c_str(), result);
 }
 
+void WebView::switchToDesktopMode() {
+    BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
+    int res = ewk_view_user_agent_set(m_ewkView, APPLICATION_NAME_FOR_USER_AGENT);
+    m_desktopMode = true;
+}
+
+void WebView::switchToMobileMode() {
+    BROWSER_LOGD("%s:%d %s", __FILE__, __LINE__, __func__);
+    int res = ewk_view_user_agent_set(m_ewkView, APPLICATION_NAME_FOR_USER_AGENT_MOBILE);
+    m_desktopMode = false;
+}
+
+bool WebView::isDesktopMode() const {
+    return m_desktopMode;
+}
+
 } /* namespace webkitengine_service */
 } /* end of basic_webengine */
 } /* end of tizen_browser */