2nd PWA code added. icon download logic and libsoup using make to shortcut.. 69/92869/9
authorLim DoHyung <delight.lim@samsung.com>
Wed, 19 Oct 2016 07:39:12 +0000 (16:39 +0900)
committerHye Kyoung Hwang <cookie@samsung.com>
Mon, 24 Oct 2016 07:49:01 +0000 (00:49 -0700)
Change-Id: Iccc93caee38320d5e12887be09fae1c28c0d3091
Signed-off-by: Lim DoHyung <delight.lim@samsung.com>
CMakeLists.txt
core/AbstractWebEngine/AbstractWebEngine.h
packaging/org.tizen.browser.spec
services/WebEngineService/WebEngineService.cpp
services/WebEngineService/WebEngineService.h
services/WebEngineService/WebView.cpp
services/WebEngineService/WebView.h

index cee5784060ad7cad51650c8b69b29120639bab86..229bf5ad38a3cf6cf85b275071f6baf01224f79a 100755 (executable)
@@ -78,6 +78,7 @@ if(TIZEN_BUILD)
         efl-extension
         libssl
         shortcut
+        libsoup-2.4
     )
 
    if (${PROFILE} MATCHES "mobile")
index 41682e7c8243627f43e5f2a0fa32c3ad68ea7a7c..91520642a06fcfce72fc03cf5a718c4f1edeedcb 100644 (file)
@@ -547,10 +547,6 @@ public:
      */
     boost::signals2::signal<void(bool)> fullscreenModeSet;
 
-#if PWA
-    boost::signals2::signal<void (std::string)> iconDownload;
-#endif
-
     /**
      * Register H/W back key callback for the current webview
      */
index 50982c47b1f8e35d2d1278224da6042b2bf54d85..0434041e9a88ab4f85980ce8940c8e1d86833473 100755 (executable)
@@ -34,6 +34,7 @@ BuildRequires:  pkgconfig(libtzplatform-config)
 %if "%{?profile}" == "mobile"
 BuildRequires:  pkgconfig(capi-system-device)
 BuildRequires:  pkgconfig(capi-system-system-settings)
+BuildRequires:  pkgconfig(libsoup-2.4)
 %endif
 BuildRequires:  browser-provider-devel
 BuildRequires:  pkgconfig(efl-extension)
index 0fb38325a4196692219d17b26dcdbfe2c52e24ed..9c2ed35821c80e799841a2d741b8c5ac987bb278 100755 (executable)
@@ -157,9 +157,6 @@ void WebEngineService::connectSignals(std::shared_ptr<WebView> webView)
     webView->setCertificatePem.connect(boost::bind(&WebEngineService::_setCertificatePem, this, _1, _2));
     webView->setWrongCertificatePem.connect(boost::bind(&WebEngineService::_setWrongCertificatePem, this, _1, _2));
     webView->fullscreenModeSet.connect([this](auto state){fullscreenModeSet(state);});
-#if PWA
-    webView->iconDownload.connect(boost::bind(&WebEngineService::_iconDownload, this, _1));
-#endif
     webView->getRotation.connect(boost::bind(&WebEngineService::_getRotation, this));
     webView->rotatePrepared.connect([this](){rotatePrepared();});
     webView->unsecureConnection.connect(boost::bind(&WebEngineService::_unsecureConnection, this));
@@ -184,9 +181,6 @@ void WebEngineService::disconnectSignals(std::shared_ptr<WebView> webView)
     webView->IMEStateChanged.disconnect(boost::bind(&WebEngineService::_IMEStateChanged, this, _1));
     webView->redirectedWebPage.disconnect(boost::bind(&WebEngineService::_redirectedWebPage, this, _1, _2));
     webView->fullscreenModeSet.disconnect_all_slots();
-#if PWA
-    webView->iconDownload.disconnect(boost::bind(&WebEngineService::_iconDownload, this));
-#endif
     webView->getRotation.disconnect(boost::bind(&WebEngineService::_getRotation, this));
     webView->rotatePrepared.disconnect_all_slots();
     webView->unsecureConnection.disconnect(boost::bind(&WebEngineService::_unsecureConnection, this));
@@ -236,12 +230,6 @@ void WebEngineService::requestManifest()
     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
     m_currentWebView->requestManifest();
 }
-
-void WebEngineService::_iconDownload(std::string download_uri)
-{
-    BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
-    m_downloadControl->launch_download_app(download_uri.c_str());
-}
 #endif
 
 bool WebEngineService::isLoadError() const
index bb8c5598ddce5bb14b26eef9aefd57b25221f4fb..7db0e082c7f48cbb06cd53b4d19d93441350aef0 100644 (file)
@@ -245,9 +245,6 @@ private:
     void _redirectedWebPage(const std::string& oldUrl, const std::string& newUrl);
     void _setCertificatePem(const std::string& uri, const std::string& pem);
     void _setWrongCertificatePem(const std::string& uri, const std::string& pem);
-#if PWA
-    void _iconDownload(std::string download_uri);
-#endif
 
     int _getRotation();
     void setWebViewSettings(std::shared_ptr<WebView> webView);
index 45a4a32bdce5133d5b1a87db1426c0690e9a15e4..f6fd04acbdd27f61b43934dd75773eb40bcd185d 100755 (executable)
 #include <device/haptic.h>
 #include <Ecore.h>
 
+#if PWA
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <tzplatform_config.h>
+#endif
+
 #define certificate_crt_path CERTS_DIR
 #define APPLICATION_NAME_FOR_USER_AGENT "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.69 safari/537.36 "
 
@@ -70,7 +76,11 @@ namespace webengine_service {
 
 const std::string WebView::COOKIES_PATH = "cookies";
 #if PWA
-std::string WebView::m_pwaData = "";
+std::string WebView::s_pwaData = "";
+std::string WebView::s_name = "";
+std::string WebView::s_start_url = "";
+std::string WebView::s_icon = "";
+#define DOWNLOAD_PATH tzplatform_getenv(TZ_USER_DOWNLOADS)
 #endif
 
 struct SnapshotItemData {
@@ -78,7 +88,6 @@ struct SnapshotItemData {
     tizen_browser::tools::SnapshotType snapshot_type;
 };
 
-
 WebView::WebView(Evas_Object * obj, TabId tabId, const std::string& title, bool incognitoMode)
     : m_parent(obj)
     , m_tabId(tabId)
@@ -660,87 +669,79 @@ void WebView::requestManifest(void)
     ewk_view_request_manifest(m_ewkView, dataSetManifest, this);
 }
 
-void WebView::dataSetManifest(Evas_Object* view, Ewk_View_Request_Manifest* manifest, void* /*data*/)
+void WebView::dataSetManifest(Evas_Object* view, Ewk_View_Request_Manifest* manifest, void* data)
 {
     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
 
-    if (view) {
+    if (view && data) {
+        WebView * self = reinterpret_cast<WebView *>(data);
+
+        uint8_t* theme_r = nullptr; uint8_t* theme_g = nullptr;
+        uint8_t* theme_b = nullptr; uint8_t* theme_a = nullptr;
+        uint8_t* bg_r = nullptr; uint8_t* bg_g = nullptr;
+        uint8_t* bg_b = nullptr; uint8_t* bg_a = nullptr;
+
         const char* short_name(ewk_manifest_short_name_get(manifest));
         const char* name(ewk_manifest_name_get(manifest));
         const char* start_url(ewk_manifest_start_url_get(manifest));
         const char* icon_src(ewk_manifest_icons_src_get(manifest, 0));
         int orientation_type = ewk_manifest_orientation_type_get(manifest);
         int display_mode = ewk_manifest_web_display_mode_get(manifest);
-        // TODO Due to build break and change in ewk API for now theme_color,
-        // and background_color have hardcoded value. Sample new api usage is in below lines:
-        // int rt, gt, bt, at, rb, gb, bb, ab;
-        long theme_color = 0; //ewk_manifest_theme_color_get(manifest, rt, gt, bt, at);
-        long background_color = 0; //ewk_manifest_background_color_get(manifest, rb, gb, bb, ab);
+        long theme_color = ewk_manifest_theme_color_get(manifest, theme_r, theme_g, theme_b, theme_a);
+        long background_color = ewk_manifest_background_color_get(manifest, bg_r, bg_g, bg_b, bg_a);
         size_t icon_count = ewk_manifest_icons_count_get(manifest);
 
         std::string str_short_name = "";
-        std::string str_name = "";
-        std::string str_start_url = "";
         std::string str_icon_src = "";
 
-        if (short_name != NULL) {
+        if (short_name)
             str_short_name = short_name;
-        }
-        if (name != NULL) {
-            str_name = name;
-        }
-        if (start_url != NULL) {
-            str_start_url = start_url;
-        }
-        if (icon_src != NULL) {
+        if (name)
+            s_name = name;
+        if (start_url)
+            s_start_url = start_url;
+        if (icon_src)
             str_icon_src = icon_src;
-        }
 
         std::string retVal("browser_shortcut:://");
         retVal.append("pwa_shortName:"); retVal.append(str_short_name.c_str()); retVal.append(",");
-        retVal.append("pwa_name:"); retVal.append(str_name.c_str()); retVal.append(",");
-        retVal.append("pwa_uri:"); retVal.append(str_start_url.c_str()); retVal.append(",");
+        retVal.append("pwa_name:"); retVal.append(s_name.c_str()); retVal.append(",");
+        retVal.append("pwa_uri:"); retVal.append(s_start_url.c_str()); retVal.append(",");
         retVal.append("pwa_orientation:"); retVal.append(std::to_string(orientation_type)); retVal.append(",");
         retVal.append("pwa_displayMode:"); retVal.append(std::to_string(display_mode)); retVal.append(",");
         retVal.append("pwa_themeColor:"); retVal.append(std::to_string(theme_color)); retVal.append(",");
+        retVal.append("theme_r:"); retVal.append(std::to_string((int)theme_r)); retVal.append(",");
+        retVal.append("theme_g:"); retVal.append(std::to_string((int)theme_g)); retVal.append(",");
+        retVal.append("theme_b:"); retVal.append(std::to_string((int)theme_b)); retVal.append(",");
+        retVal.append("theme_a:"); retVal.append(std::to_string((int)theme_a)); retVal.append(",");
         retVal.append("pwa_backgroundColor:"); retVal.append(std::to_string(background_color)); retVal.append(",");
+        retVal.append("bg_r:"); retVal.append(std::to_string((int)bg_r)); retVal.append(",");
+        retVal.append("bg_g:"); retVal.append(std::to_string((int)bg_g)); retVal.append(",");
+        retVal.append("bg_b:"); retVal.append(std::to_string((int)bg_b)); retVal.append(",");
+        retVal.append("bg_a:"); retVal.append(std::to_string((int)bg_a)); retVal.append(",");
         retVal.append("icon_count:"); retVal.append(std::to_string(icon_count)); retVal.append(",");
         retVal.append("icon_src:"); retVal.append(str_icon_src.c_str()); retVal.append(",");
 
         BROWSER_LOGD("[%s:%d] retVal : %s", __PRETTY_FUNCTION__, __LINE__, retVal.c_str());
-        m_pwaData = retVal;
-        //self->iconDownload(icon_src);
+        s_pwaData = retVal;
 
-/*
-        size_t len = strlen(icon_src.c_str());
-        char array[100] = {0,};
-        for(int i = 0; i < (int)len; i++) {
-            array[i] = icon_src[i];
-        }
-        int key = 0;
-        std::string result = "";
-
-        for(int i = len-1; i >= 0; i--) {
-            if(array[i] == '/') {
-                key = i;
-                break;
-            }
-        }
-        for(int j = key+1; j < (int)len; j++) {
-            result = result + array[j];
-        }
-        BROWSER_LOGD("[%s:%d] result : %s", __PRETTY_FUNCTION__, __LINE__, result.c_str());
-        std::string icon = "/opt/usr/home/owner/content/Downloads/" + result;
-*/
-        if (shortcut_add_to_home(name, LAUNCH_BY_URI, start_url, NULL, 0, result_cb, NULL) != SHORTCUT_ERROR_NONE) {
-            BROWSER_LOGE("[%s:%d] Fail to add to homescreen", __PRETTY_FUNCTION__, __LINE__);
-        } else {
-            BROWSER_LOGE("[%s:%d] Success to add to homescreen", __PRETTY_FUNCTION__, __LINE__);
-        }
+        size_t len = strlen(icon_src);
+        auto result = str_icon_src.substr(str_icon_src.find_last_of("/"), len);
+        s_icon = DOWNLOAD_PATH + result;
+        self->request_file_download(icon_src, s_icon, __download_result_cb, NULL);
         BROWSER_LOGD("[%s:%d] dataSetManifest callback function end!", __PRETTY_FUNCTION__, __LINE__);
     }
 }
 
+void WebView::makeShortcut(const std::string& name, const std::string& pwaData, const std::string& icon)
+{
+    if (shortcut_add_to_home(name.c_str(),LAUNCH_BY_URI,pwaData.c_str(),
+            icon.c_str(),0,result_cb,nullptr) != SHORTCUT_ERROR_NONE)
+        BROWSER_LOGE("[%s:%d] Fail to add to homescreen", __PRETTY_FUNCTION__, __LINE__);
+    else
+        BROWSER_LOGE("[%s:%d] Success to add to homescreen", __PRETTY_FUNCTION__, __LINE__);
+}
+
 int WebView::result_cb(int ret, void *data) {
 
     if (data) {
@@ -750,6 +751,53 @@ int WebView::result_cb(int ret, void *data) {
     }
     return 0;
 }
+
+void WebView::request_file_download(const char *uri, const std::string& file_path, download_finish_callback cb, void *data)
+{
+    BROWSER_LOGD("[%s:%d]", __PRETTY_FUNCTION__, __LINE__);
+    BROWSER_LOGD("uri = [%s], file_path = [%s]", uri, file_path.c_str());
+
+    SoupSession *soup_session = nullptr;
+    SoupMessage *soup_msg = nullptr;
+
+    soup_session = soup_session_async_new();
+    g_object_set(soup_session, SOUP_SESSION_TIMEOUT, 15, nullptr);
+
+    soup_msg = soup_message_new("GET", uri);
+    download_request *request = new(std::nothrow) download_request(strdup(file_path.c_str()), cb, data);
+    soup_session_queue_message(soup_session, soup_msg, __file_download_finished_cb, static_cast<void*>(request));
+
+    g_object_unref(soup_session);
+}
+
+void WebView::__file_download_finished_cb(SoupSession *session, SoupMessage *msg, gpointer data)
+{
+    BROWSER_LOGD("[%s:%d] session : %s", __PRETTY_FUNCTION__, __LINE__, session);
+
+    if (data) {
+        download_request *request = static_cast<download_request*>(data);
+        SoupBuffer *body = soup_message_body_flatten(msg->response_body);
+        int fd = 0;
+        if (body->data && (body->length > 0) && ((fd = open(request->file_path.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) >= 0)) {
+            unsigned int write_len = write(fd, body->data, body->length);
+            close(fd);
+            if (write_len == body->length)
+                request->cb(request->file_path.c_str(), request->data);
+            else
+                unlink(request->file_path.c_str());
+        }
+        g_object_unref(msg);
+        soup_buffer_free(body);
+        delete request;
+    }
+    makeShortcut(s_name, s_pwaData, s_icon);
+}
+
+void WebView::__download_result_cb(const std::string& file_path, void *data)
+{
+    BROWSER_LOGD("[%s:%d] file_path = [%s], data = [%s]", file_path.c_str(), data);
+    BROWSER_LOGD("[%s:%d] complete !", __PRETTY_FUNCTION__, __LINE__);
+}
 #endif
 
 std::string WebView::getTitle(void)
index bdf864152cf36b3dac41f60d458792e2423dd2e8..205c1daec5ca1aa2f9e9eead49f4ee5bf4d45784 100755 (executable)
 #include <app.h>
 #include "AbstractRotatable.h"
 
+#if PWA
+#include <glib.h>
+#include <libsoup/soup.h>
+#endif
+
 typedef enum _context_menu_type {
     TEXT_ONLY = 0,
     INPUT_FIELD,
@@ -65,6 +70,8 @@ namespace tizen_browser {
 namespace basic_webengine {
 namespace webengine_service {
 
+using download_finish_callback = void (*)(const std::string& file_path, void *data);
+
 class WebView
         : public tizen_browser::interfaces::AbstractRotatable
 {
@@ -77,9 +84,6 @@ public:
 
     void setURI(const std::string &);
     std::string getURI(void);
-#if PWA
-    void requestManifest(void);
-#endif
     std::string getTitle(void);
 
     std::string getUserAgent(void);
@@ -101,6 +105,31 @@ public:
     bool isLoading();
     bool isLoadError() const;
 
+#if PWA
+    void requestManifest(void);
+    void request_file_download(const char *uri, const std::string& file_path, download_finish_callback cb, void *data);
+
+    struct download_request
+    {
+    public:
+        download_request(char* file_path_ = nullptr, download_finish_callback cb_ = nullptr, void* data_ = nullptr)
+    : file_path(file_path_),
+      cb(cb_),
+      data(data_)
+    {}
+
+        ~download_request() {}
+
+        std::string file_path;
+        download_finish_callback cb;
+        void *data;
+
+    private:
+        download_request& operator=(const download_request&);
+        download_request(const download_request&);
+    };
+#endif
+
     std::map<std::string, std::vector<std::string> > parse_uri(const char *uriToParse);
 
     Evas_Object * getLayout();
@@ -278,9 +307,6 @@ public:
     boost::signals2::signal<void (const std::string&, const std::string&)> redirectedWebPage;
     boost::signals2::signal<void()> unsecureConnection;
     boost::signals2::signal<void(bool)> fullscreenModeSet;
-#if PWA
-    boost::signals2::signal<void (std::string)> iconDownload;
-#endif
 
 protected:
     std::string getRedirectedURL() {return m_redirectedURL;};
@@ -297,6 +323,9 @@ private:
 #if PWA
     static void dataSetManifest(Evas_Object* view, Ewk_View_Request_Manifest* manifest, void*);
     static int result_cb(int ret, void *data);
+    static void makeShortcut(const std::string& name, const std::string& pwaData, const std::string& icon);
+    static void __file_download_finished_cb(SoupSession *session, SoupMessage *msg, gpointer data);
+    static void __download_result_cb(const std::string& file_path, void *data);
 #endif
 
     context_menu_type _get_menu_type(Ewk_Context_Menu *menu);
@@ -378,7 +407,11 @@ private:
 
     static const std::string COOKIES_PATH;
 #if PWA
-    static std::string m_pwaData;
+    static std::string s_pwaData;
+    static std::string s_name;
+    static std::string s_start_url;
+    static std::string s_icon;
+    static const std::string DOWNLOAD_PATH;
 #endif
 
     int m_status_code;