[Release] wrt-plugins-common_0.3.94
[platform/framework/web/wrt-plugins-common.git] / src / standards / W3C / Widget / JSPreferences.cpp
index 2f962f0..1f6326f 100644 (file)
 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
 #include <StorageEvent/StorageEventMgr.h>
 #include <StorageEvent/IStorageEvent.h>
-#include <js-overlay/js_iframe_support.h>
-#include <js-overlay/js_overlay_addEventListener.h>
+#include <dispatch-event/dispatch_event_support.h>
+
+// import temporarily from JSContextRefPrivate.h
+extern "C" JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx);
 
 using namespace std;
 using namespace WrtDeviceApis;
@@ -256,17 +258,18 @@ JSValueRef JSPreferences::removeItem(JSContextRef context,
 
         std::string key = converter.toString(arguments[0]);
 
-        auto event = modifyItemAndCreateEvent(ModificationType::RemoveItem,
-                                              thisObject,
-                                              key);
-
-        auto iframe = IFrameSupport::
-                getIFrameObjectForWidget(getWidgetObject(thisObject));
+        DPL::Optional<std::string> oldValue = getIStorage(thisObject)->getValue(key);
+        getIStorage(thisObject)->removeItem(key);
+        DPL::Optional<std::string> newValue = getIStorage(thisObject)->getValue(key);
 
-        Assert(iframe && "Iframe is NULL");
+        JSContextRef g_context  = JSContextGetGlobalContext(context);
+        std::string oldValueStr = "";
+        std::string newValueStr = "";
 
-        AddEventListenerSupport::
-            CallStorageListenersFromDifferentIFrames(iframe, event);
+        if (!!oldValue) { oldValueStr = *oldValue; }
+        if (!!newValue) { newValueStr = *newValue; }
+        
+        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValueStr, newValueStr, "");
 
         LogDebug("end");
         return JSValueMakeNull(context);
@@ -295,17 +298,18 @@ JSValueRef JSPreferences::setItem(JSContextRef context,
         std::string key = converter.toString(arguments[0]);
         std::string value = converter.tryString(arguments[1]);
 
-        auto event = modifyItemAndCreateEvent(ModificationType::SetItem,
-                                              thisObject,
-                                              key,
-                                              value);
+        DPL::Optional<std::string> oldValue = getIStorage(thisObject)->getValue(key);
+        getIStorage(thisObject)->setItem(key, value, false);
+        DPL::Optional<std::string> newValue = getIStorage(thisObject)->getValue(key);
 
-        auto iframe = IFrameSupport::
-                getIFrameObjectForWidget(getWidgetObject(thisObject));
-        Assert(iframe && "Iframe is NULL");
+        JSContextRef g_context  = JSContextGetGlobalContext(context);
+        std::string oldValueStr = "";
+        std::string newValueStr = "";
 
-        AddEventListenerSupport::
-            CallStorageListenersFromDifferentIFrames(iframe, event);
+        if (!!oldValue) { oldValueStr = *oldValue; }
+        if (!!newValue) { newValueStr = *newValue; }
+        
+        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValueStr, newValueStr, "");
 
         LogDebug("end");
 
@@ -331,16 +335,9 @@ JSValueRef JSPreferences::clear(JSContextRef context,
     Try {
         getIStorage(thisObject)->clear(false);
 
-        auto iframe = IFrameSupport::
-                getIFrameObjectForWidget(getWidgetObject(thisObject));
-
-        Assert(iframe && "Iframe is NULL");
-
-        //create event object
-        IStorageEventPtr storageEvent = getStorageEvent();
+        JSContextRef g_context  = JSContextGetGlobalContext(context);
 
-        AddEventListenerSupport::
-            CallStorageListenersFromDifferentIFrames(iframe, storageEvent);
+        DispatchEventSupport::dispatchStorageEvent(g_context, "", "", "", "");
 
         LogDebug("end");
 
@@ -516,17 +513,18 @@ bool JSPreferences::setProperty(JSContextRef context,
         std::string key = converter.toString(propertyName);
         std::string value = converter.toString(jvalue);
 
-        auto event = modifyItemAndCreateEvent(ModificationType::SetItem,
-                                              object,
-                                              key,
-                                              value);
+        DPL::Optional<std::string> oldValue = getIStorage(object)->getValue(key);
+        getIStorage(object)->setItem(key, value, false);
+        DPL::Optional<std::string> newValue = getIStorage(object)->getValue(key);
 
-        auto iframe = IFrameSupport::
-                getIFrameObjectForWidget(getWidgetObject(object));
-        Assert(iframe && "Iframe is NULL");
+        JSContextRef g_context  = JSContextGetGlobalContext(context);
+        std::string oldValueStr = "";
+        std::string newValueStr = "";
 
-        AddEventListenerSupport::
-            CallStorageListenersFromDifferentIFrames(iframe, event);
+        if (!!oldValue) { oldValueStr = *oldValue; }
+        if (!!newValue) { newValueStr = *newValue; }
+        
+        DispatchEventSupport::dispatchStorageEvent(g_context, key, oldValueStr, newValueStr, "");
 
         LogDebug("end");