@@ -1,6 +1,8 @@
1{-# LANGUAGE QuasiQuotes, ForeignFunctionInterface #-}
1{-# LANGUAGE QuasiQuotes, ForeignFunctionInterface #-}
2module Reflex.Dom.Xhr.Foreign where
3module Reflex.Dom.Xhr.Foreign where
4import qualified Data.Text as T
6import qualified Data.Text as T
5import Data.Text (Text)
7import Data.Text (Text)
6import System.Glib.FFI
8import System.Glib.FFI
@@ -9,11 +11,11 @@ import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
9import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
11import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSObjectRef
10import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSStringRef
12import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSStringRef
11import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSValueRef
13import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSValueRef
12−import Graphics.UI.Gtk.WebKit.JavaScriptCore.WebFrame
13import Reflex.Dom.Xhr.ResponseType
14import Reflex.Dom.Xhr.ResponseType
14import Reflex.Dom.Xhr.Exception
15import Reflex.Dom.Xhr.Exception
15import Control.Concurrent.MVar
16import Control.Concurrent.MVar
16import Control.Exception.Base
17import Control.Exception.Base
18+import Graphics.UI.Gtk.WebKit.Types hiding (Text)
18import Reflex.Dom.Internal.Foreign
20import Reflex.Dom.Internal.Foreign
@@ -25,23 +27,24 @@ data XMLHttpRequest
26 deriving (Eq, Ord)
28 deriving (Eq, Ord)
28−data XhrResponseBody = XhrResponseBody { unXhrResponseBody :: JSValueRef }
30stringToJSValue :: JSContextRef -> String -> IO JSValueRef
30stringToJSValue :: JSContextRef -> String -> IO JSValueRef
31stringToJSValue ctx s = jsvaluemakestring ctx =<< jsstringcreatewithutf8cstring s
31stringToJSValue ctx s = jsvaluemakestring ctx =<< jsstringcreatewithutf8cstring s
33−toResponseType :: XhrResponseType -> String
33+fromResponseType :: XhrResponseType -> String
34−toResponseType XhrResponseType_Default = ""
34+fromResponseType XhrResponseType_Default = ""
35−toResponseType XhrResponseType_ArrayBuffer = "arraybuffer"
35+fromResponseType XhrResponseType_ArrayBuffer = "arraybuffer"
36−toResponseType XhrResponseType_Blob = "blob"
36+fromResponseType XhrResponseType_Blob = "blob"
37−toResponseType XhrResponseType_Document = "document"
37+fromResponseType XhrResponseType_Text = "text"
38−toResponseType XhrResponseType_JSON = "json"
38+
39−toResponseType XhrResponseType_Text = "text"
39+toResponseType :: String -> Maybe XhrResponseType
40+toResponseType "" = Just XhrResponseType_Default
41+toResponseType "arraybuffer" = Just XhrResponseType_ArrayBuffer
42+toResponseType "blob" = Just XhrResponseType_Blob
43+toResponseType "text" = Just XhrResponseType_Text
44+toResponseType _ = Nothing
41xmlHttpRequestNew :: WebView -> IO XMLHttpRequest
46xmlHttpRequestNew :: WebView -> IO XMLHttpRequest
42−xmlHttpRequestNew wv = do
47+xmlHttpRequestNew wv = withWebViewContext wv $ \jsContext -> do
43− wf <- webViewGetMainFrame wv
44− jsContext <- webFrameGetGlobalContext wf
45 xhrScript <- jsstringcreatewithutf8cstring "new XMLHttpRequest()"
48 xhrScript <- jsstringcreatewithutf8cstring "new XMLHttpRequest()"
46 xhr' <- jsevaluatescript jsContext xhrScript nullPtr nullPtr 1 nullPtr
49 xhr' <- jsevaluatescript jsContext xhrScript nullPtr nullPtr 1 nullPtr
47 jsvalueprotect jsContext xhr'
50 jsvalueprotect jsContext xhr'
@@ -80,31 +83,35 @@ xmlHttpRequestGetReadyState xhr = do
80 d <- jsvaluetonumber c rs nullPtr
83 d <- jsvaluetonumber c rs nullPtr
81 return $ truncate d
84 return $ truncate d
86+xmlHttpRequestGetResponseType :: XMLHttpRequest -> IO (Maybe XhrResponseType)
87+xmlHttpRequestGetResponseType xhr = do
88+ script <- jsstringcreatewithutf8cstring "this.responseType"
89+ rt <- jsevaluatescript (xhrContext xhr) script (xhrValue xhr) nullPtr 1 nullPtr
90+ ms <- fromJSStringMaybe (xhrContext xhr) rt
91+ return $ join $ fmap toResponseType ms
83xmlHttpRequestGetResponse :: XMLHttpRequest -> IO (Maybe XhrResponseBody)
93xmlHttpRequestGetResponse :: XMLHttpRequest -> IO (Maybe XhrResponseBody)
84xmlHttpRequestGetResponse xhr = do
94xmlHttpRequestGetResponse xhr = do
85 let c = xhrContext xhr
95 let c = xhrContext xhr
96+ mrt <- xmlHttpRequestGetResponseType xhr
86 script <- jsstringcreatewithutf8cstring "this.response"
97 script <- jsstringcreatewithutf8cstring "this.response"
87 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
98 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
88 isNull <- jsvalueisnull c t
99 isNull <- jsvalueisnull c t
89 case isNull of
100 case isNull of
90 True -> return Nothing
101 True -> return Nothing
91− False -> return $ Just $ XhrResponseBody t
102+ False -> case mrt of
103+ Just XhrResponseType_ArrayBuffer -> Just . XhrResponseBody_ArrayBuffer <$> bsFromArrayBuffer c t
104+ Just XhrResponseType_Blob -> Just . XhrResponseBody_Blob . Blob . castForeignPtr <$> newForeignPtr_ t
105+ Just XhrResponseType_Default -> fmap (XhrResponseBody_Default . T.pack) <$> fromJSStringMaybe c t
106+ Just XhrResponseType_Text -> fmap (XhrResponseBody_Text . T.pack) <$> fromJSStringMaybe c t
93xmlHttpRequestGetResponseText :: XMLHttpRequest -> IO (Maybe Text)
109xmlHttpRequestGetResponseText :: XMLHttpRequest -> IO (Maybe Text)
94xmlHttpRequestGetResponseText xhr = do
110xmlHttpRequestGetResponseText xhr = do
95 let c = xhrContext xhr
111 let c = xhrContext xhr
96 script <- jsstringcreatewithutf8cstring "this.responseText"
112 script <- jsstringcreatewithutf8cstring "this.responseText"
97 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
113 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
98− isNull <- jsvalueisnull c t
114+ fmap (fmap T.pack) $ fromJSStringMaybe c t
100− True -> return Nothing
102− j <- jsvaluetostringcopy c t nullPtr
103− l <- jsstringgetmaximumutf8cstringsize j
104− s <- allocaBytes (fromIntegral l) $ \ps -> do
105− _ <- jsstringgetutf8cstring'_ j ps (fromIntegral l)
107− return $ Just $ T.pack s
109xmlHttpRequestSend :: XMLHttpRequest -> Maybe String -> IO ()
116xmlHttpRequestSend :: XMLHttpRequest -> Maybe String -> IO ()
110xmlHttpRequestSend xhr payload = do
117xmlHttpRequestSend xhr payload = do