Implement notification permission request
authorJihoon Chung <jihoon.chung@samsung.com>
Sat, 29 Dec 2012 05:42:28 +0000 (14:42 +0900)
committerGerrit Code Review <gerrit2@kim11>
Wed, 2 Jan 2013 04:40:47 +0000 (13:40 +0900)
[Issue#] N/A
[Problem] N/A
[Cause] N/A
[Solution] Implement notification permission request
[SCMRequest] N/A

Change-Id: Icdf686e56aa2fe8bd0aba506f31f3a99b30d040d

src/view/common/CMakeLists.txt
src/view/webkit/CMakeLists.txt
src/view/webkit/view_logic.cpp
src/view/webkit/view_logic_web_notification_support.cpp [moved from src/view/common/view_logic_web_notification_support.cpp with 61% similarity]
src/view/webkit/view_logic_web_notification_support.h [moved from src/view/common/view_logic_web_notification_support.h with 88% similarity]

index d4df698..0f704a8 100644 (file)
@@ -33,7 +33,6 @@ PKG_CHECK_MODULES(SYS_VIEW_COMMON_DEP
     libpcrecpp
     libsoup-2.4
     ui-gadget-1
-    notification    # native notifications
     security-client
     security-core
     secure-storage
@@ -68,7 +67,6 @@ SET(VIEW_COMMON_SOURCES
     ${PROJECT_SOURCE_DIR}/src/view/common/view_logic_uri_support.cpp
     ${PROJECT_SOURCE_DIR}/src/view/common/view_logic_user_agent_support.cpp
     ${PROJECT_SOURCE_DIR}/src/view/common/view_logic_vibration_support.cpp
-    ${PROJECT_SOURCE_DIR}/src/view/common/view_logic_web_notification_support.cpp
 )
 
 SET(VIEW_COMMON_INCLUDES
index 78b3904..16f2b6b 100644 (file)
@@ -24,6 +24,7 @@ PKG_CHECK_MODULES(VIEW_MODULE_DEP
     dpl-dbus-efl
     dpl-utils-efl
     dpl-wrt-dao-ro
+    notification
     wrt-commons-custom-handler-dao-rw
     wrt-plugin-js-overlay
     wrt-popup-wrt-runner
@@ -45,6 +46,7 @@ SET(VIEW_MODULE_SOURCES
     ${PROJECT_SOURCE_DIR}/src/view/webkit/view_logic_scheme_support.cpp
     ${PROJECT_SOURCE_DIR}/src/view/webkit/view_logic_geolocation_support_webkit2.cpp
     ${PROJECT_SOURCE_DIR}/src/view/webkit/view_logic_usermedia_support.cpp
+    ${PROJECT_SOURCE_DIR}/src/view/webkit/view_logic_web_notification_support.cpp
     ${PROJECT_SOURCE_DIR}/src/view/webkit/view_logic_web_storage_support.cpp
     ${PROJECT_SOURCE_DIR}/src/view/webkit/bundles/plugin_module_support.cpp
     ${VIEW_MODULE_SOURCES}
index 4699fb6..16f38cd 100644 (file)
 #include <common/view_logic_uri_support.h>
 #include <common/view_logic_user_agent_support.h>
 #include <common/view_logic_vibration_support.h>
-#include <common/view_logic_web_notification_support.h>
 #include <view_logic_scheme_support.h>
 #include <view_logic_filesystem_support.h>
 #include <view_logic_geolocation_support_webkit2.h>
 #include <view_logic_usermedia_support.h>
+#include <view_logic_web_notification_support.h>
 #include <view_logic_web_storage_support.h>
 #include "bundles/plugin_module_support.h"
 #include <popup-runner/PopupInvoker.h>
@@ -1574,14 +1574,25 @@ void ViewLogic::notificationPermissionRequestCallback(
     LogDebug("notificationPermissionRequestCallback called");
     Assert(data);
     ViewLogic* This = static_cast<ViewLogic*>(data);
+    if (This->m_securityOriginSupport->isNeedPermissionCheck(
+        SecurityOriginDB::FEATURE_WEB_NOTIFICATION)
+        == WrtDB::SETTINGS_TYPE_OFF)
+    {
+        Ewk_Notification_Permission_Request* request =
+            static_cast<Ewk_Notification_Permission_Request*>(eventInfo);
+        ewk_notification_permission_request_response(
+            This->m_ewkContext,
+            request,
+            EINA_FALSE);
+        return;
+    }
 
     Assert(eventInfo);
-    Ewk_Notification_Permission_Request* request =
-        static_cast<Ewk_Notification_Permission_Request*>(eventInfo);
-    ewk_notification_permission_request_response(
+    ViewModule::WebNotification::webNotificationPermissionRequest(
+        This->m_window,
+        This->m_securityOriginSupport->getSecurityOriginDAO(),
         This->m_ewkContext,
-        request,
-        EINA_TRUE);
+        eventInfo);
     return;
 }
 
 #include <string>
 #include <dpl/log/log.h>
 #include <dpl/string.h>
+#include <wrt-commons/security-origin-dao/security_origin_dao_types.h>
+#include <wrt-commons/security-origin-dao/security_origin_dao.h>
+#include <common/view_logic_security_origin_support.h>
 
 #include <notification.h>
 #include <pcrecpp.h>
 #include <sstream>
 #include <curl/curl.h>
 #include <widget_model.h>
+#include <EWebKit2.h>
 
 namespace {
 const char* PATTERN_CHECK_EXTERNAL = "^http(s)?://\\w+.*$";
+const char* const WEB_NOTIFICATION_ASK_BODY_PREFIX = "Do you want to allow ";
+const char* const WEB_NOTIFICATION_ASK_BODY_POSTFIX =
+    " to use web notificaion?";
+const char* const WEB_NOTIFICATION_ASK_CHECK = "Don't ask again";
+
 }
 
 namespace ViewModule {
 namespace WebNotification {
 
+using namespace SecurityOriginDB;
+using namespace ViewModule::SecurityOriginSupportUtil;
+
+class WebNotificationPermissionData
+    : public ViewModule::SecurityOriginSupportUtil::PermissionData
+{
+  public:
+    Ewk_Context* m_ewkContext;
+
+    WebNotificationPermissionData(
+        SecurityOriginDB::SecurityOriginDAO* originDao,
+        SecurityOriginDB::SecurityOriginData originData,
+        Ewk_Context* ewkContext,
+        void* data) :
+            ViewModule::SecurityOriginSupportUtil::PermissionData(
+                originDao,
+                originData,
+                data),
+            m_ewkContext(ewkContext)
+    {
+    };
+};
+
 bool notificationShow(WebNotificationDataPtr notiData);
 bool notificationHide(WebNotificationDataPtr notiData);
 bool isExternalUri(const std::string &pattern, const std::string &uri);
 bool downloadImage(WebNotificationDataPtr notiData);
 size_t curlWriteData(void *ptr, size_t size, size_t nmemb, FILE *stream);
+void askUserForWebNotificationPermission(
+    Evas_Object* window,
+    WebNotificationPermissionData* data);
+static void popupCallback(void* data, Evas_Object* obj, void* eventInfo);
 
 WebNotificationData::WebNotificationData(WidgetModel* widgetModel,
                                          uint64_t id) :
@@ -55,6 +91,57 @@ WebNotificationData::~WebNotificationData()
 {
 }
 
+void webNotificationPermissionRequest(
+    Evas_Object* window,
+    SecurityOriginDAO* securityOriginDAO,
+    Ewk_Context* ewkContext,
+    void* data)
+{
+    LogDebug("permissionRequest called");
+    Assert(securityOriginDAO);
+    Assert(data);
+    Ewk_Notification_Permission_Request* request =
+        static_cast<Ewk_Notification_Permission_Request*>(data);
+    const Ewk_Security_Origin* ewkOrigin =
+        ewk_notification_permission_request_origin_get(request);
+    Assert(ewkOrigin);
+
+    SecurityOriginData securityOriginData(
+        FEATURE_WEB_NOTIFICATION,
+        Origin(
+            DPL::FromUTF8String(ewk_security_origin_protocol_get(ewkOrigin)),
+            DPL::FromUTF8String(ewk_security_origin_host_get(ewkOrigin)),
+            ewk_security_origin_port_get(ewkOrigin)));
+
+    // check cache database
+    Result result = securityOriginDAO->getResult(securityOriginData);
+    if (RESULT_ALLOW_ONCE == result || RESULT_ALLOW_ALWAYS == result) {
+        LogDebug("allow");
+        ewk_notification_permission_request_response(ewkContext,
+                                                     request,
+                                                     EINA_TRUE);
+        return;
+    } else if (RESULT_DENY_ONCE == result || RESULT_DENY_ALWAYS == result) {
+        LogDebug("deny");
+        ewk_notification_permission_request_response(ewkContext,
+                                                     request,
+                                                     EINA_FALSE);
+        return;
+    }
+
+    // ask to user
+    WebNotificationPermissionData* permissionData =
+        new WebNotificationPermissionData(securityOriginDAO,
+                           securityOriginData,
+                           ewkContext,
+                           data);
+
+    // suspend notification
+    ewk_notification_permission_request_suspend(request);
+    askUserForWebNotificationPermission(window, permissionData);
+    return;
+}
+
 bool showWebNotification(WebNotificationDataPtr notiData)
 {
     LogInfo("showWebNotification called");
@@ -250,5 +337,55 @@ size_t curlWriteData(void *ptr, size_t size, size_t nmemb, FILE *stream)
     return written;
 }
 
+void askUserForWebNotificationPermission(
+    Evas_Object* window,
+    WebNotificationPermissionData* data)
+{
+    LogDebug("askUserForWebNotificationPermission called");
+    std::string body =
+        WEB_NOTIFICATION_ASK_BODY_PREFIX +
+        DPL::ToUTF8String(data->m_originData.origin.host) +
+        WEB_NOTIFICATION_ASK_BODY_POSTFIX;
+    Evas_Object* popup = createPopup(window,
+                                     body.c_str(),
+                                     WEB_NOTIFICATION_ASK_CHECK,
+                                     popupCallback,
+                                     data);
+    if (popup == NULL) {
+        LogError("Fail to create popup object");
+        delete data;
+        return;
+    } else {
+        evas_object_show(popup);
+    }
+}
+
+void popupCallback(void* data, Evas_Object* obj, void* /*eventInfo*/)
+{
+    LogDebug("popupCallback");
+    Assert(data);
+    WebNotificationPermissionData* permData =
+        static_cast<WebNotificationPermissionData*>(data);
+    Ewk_Notification_Permission_Request* request =
+        static_cast<Ewk_Notification_Permission_Request*>(permData->m_data);
+
+    Evas_Object* popup = getPopup(obj);
+    Result result = getResult(obj);
+
+    if (result != RESULT_UNKNOWN) {
+        permData->m_originDao->setSecurityOriginData(permData->m_originData, result);
+    }
+    Eina_Bool ret =
+        (result == RESULT_ALLOW_ALWAYS || result == RESULT_ALLOW_ONCE) ?
+            EINA_TRUE : EINA_FALSE;
+    ewk_notification_permission_request_response(permData->m_ewkContext,
+                                                 request,
+                                                 ret);
+    delete permData;
+    evas_object_hide(popup);
+    evas_object_del(popup);
+}
+
+
 } // namespace WebNotification
 } //namespace ViewModule
 #include <memory>
 #include <stdint.h>
 #include <dpl/exception.h>
+#include <EWebKit2.h>
 
 //Forward declarations
 class WidgetModel;
+namespace SecurityOriginDB {
+    class SecurityOriginDAO;
+}
 
 namespace ViewModule {
 namespace WebNotification {
@@ -55,7 +59,11 @@ class Exception {
     DECLARE_EXCEPTION_TYPE(Base, DownloadImageError)
 };
 
-
+void webNotificationPermissionRequest(
+    Evas_Object* window,
+    SecurityOriginDB::SecurityOriginDAO* securityOriginDAO,
+    Ewk_Context* ewkContext,
+    void* data);
 bool showWebNotification(WebNotificationDataPtr notiData);
 
 } // namespace SecuritySupport