@@ -3,6 +3,7 @@ module Reflex.Dom.Xhr.Foreign ( module Reflex.Dom.Xhr.Foreign
3 , XhrResponseType
3 , XhrResponseType
6import qualified Data.Text as T
7import qualified Data.Text as T
7import Data.Text (Text)
8import Data.Text (Text)
8import System.Glib.FFI
9import System.Glib.FFI
@@ -17,6 +18,7 @@ import Reflex.Dom.Xhr.ResponseType
17import Reflex.Dom.Xhr.Exception
18import Reflex.Dom.Xhr.Exception
18import Control.Concurrent.MVar
19import Control.Concurrent.MVar
19import Control.Exception.Base
20import Control.Exception.Base
21+import Graphics.UI.Gtk.WebKit.Types hiding (Text)
21import Reflex.Dom.Internal.Foreign
23import Reflex.Dom.Internal.Foreign
@@ -28,23 +30,36 @@ data XMLHttpRequest
29 deriving (Eq, Ord)
31 deriving (Eq, Ord)
31−data XhrResponseBody = XhrResponseBody { unXhrResponseBody :: JSValueRef }
33+class IsXhrPayload a where
34+ xmlHttpRequestSend :: XMLHttpRequest -> a -> IO ()
36+instance IsXhrPayload () where
37+ xmlHttpRequestSend xhr _ = xmlHttpRequestSendPayload xhr Nothing
39+instance IsXhrPayload String where
40+ xmlHttpRequestSend xhr = xmlHttpRequestSendPayload xhr . Just
42+instance IsXhrPayload File where
43+ xmlHttpRequestSend = error "xmlHttpRequestSend{File}: not implemented"
33stringToJSValue :: JSContextRef -> String -> IO JSValueRef
45stringToJSValue :: JSContextRef -> String -> IO JSValueRef
34stringToJSValue ctx s = jsvaluemakestring ctx =<< jsstringcreatewithutf8cstring s
46stringToJSValue ctx s = jsvaluemakestring ctx =<< jsstringcreatewithutf8cstring s
36−toResponseType :: XhrResponseType -> String
48+fromResponseType :: XhrResponseType -> String
37−toResponseType XhrResponseType_Default = ""
49+fromResponseType XhrResponseType_Default = ""
38−toResponseType XhrResponseType_ArrayBuffer = "arraybuffer"
50+fromResponseType XhrResponseType_ArrayBuffer = "arraybuffer"
39−toResponseType XhrResponseType_Blob = "blob"
51+fromResponseType XhrResponseType_Blob = "blob"
40−toResponseType XhrResponseType_Document = "document"
52+fromResponseType XhrResponseType_Text = "text"
41−toResponseType XhrResponseType_JSON = "json"
53+
42−toResponseType XhrResponseType_Text = "text"
54+toResponseType :: String -> Maybe XhrResponseType
55+toResponseType "" = Just XhrResponseType_Default
56+toResponseType "arraybuffer" = Just XhrResponseType_ArrayBuffer
57+toResponseType "blob" = Just XhrResponseType_Blob
58+toResponseType "text" = Just XhrResponseType_Text
59+toResponseType _ = Nothing
44xmlHttpRequestNew :: WebView -> IO XMLHttpRequest
61xmlHttpRequestNew :: WebView -> IO XMLHttpRequest
45−xmlHttpRequestNew wv = do
62+xmlHttpRequestNew wv = withWebViewContext wv $ \jsContext -> do
46− wf <- webViewGetMainFrame wv
47− jsContext <- webFrameGetGlobalContext wf
48 xhrScript <- jsstringcreatewithutf8cstring "new XMLHttpRequest()"
63 xhrScript <- jsstringcreatewithutf8cstring "new XMLHttpRequest()"
49 xhr' <- jsevaluatescript jsContext xhrScript nullPtr nullPtr 1 nullPtr
64 xhr' <- jsevaluatescript jsContext xhrScript nullPtr nullPtr 1 nullPtr
50 jsvalueprotect jsContext xhr'
65 jsvalueprotect jsContext xhr'
@@ -83,43 +98,35 @@ xmlHttpRequestGetReadyState xhr = do
83 d <- jsvaluetonumber c rs nullPtr
98 d <- jsvaluetonumber c rs nullPtr
84 return $ truncate d
99 return $ truncate d
101+xmlHttpRequestGetResponseType :: XMLHttpRequest -> IO (Maybe XhrResponseType)
102+xmlHttpRequestGetResponseType xhr = do
103+ script <- jsstringcreatewithutf8cstring "this.responseType"
104+ rt <- jsevaluatescript (xhrContext xhr) script (xhrValue xhr) nullPtr 1 nullPtr
105+ ms <- fromJSStringMaybe (xhrContext xhr) rt
106+ return $ join $ fmap toResponseType ms
86xmlHttpRequestGetResponse :: XMLHttpRequest -> IO (Maybe XhrResponseBody)
108xmlHttpRequestGetResponse :: XMLHttpRequest -> IO (Maybe XhrResponseBody)
87xmlHttpRequestGetResponse xhr = do
109xmlHttpRequestGetResponse xhr = do
88 let c = xhrContext xhr
110 let c = xhrContext xhr
111+ mrt <- xmlHttpRequestGetResponseType xhr
89 script <- jsstringcreatewithutf8cstring "this.response"
112 script <- jsstringcreatewithutf8cstring "this.response"
90 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
113 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
91 isNull <- jsvalueisnull c t
114 isNull <- jsvalueisnull c t
92 case isNull of
115 case isNull of
93 True -> return Nothing
116 True -> return Nothing
94− False -> return $ Just $ XhrResponseBody t
117+ False -> case mrt of
118+ Just XhrResponseType_ArrayBuffer -> Just . XhrResponseBody_ArrayBuffer <$> bsFromArrayBuffer c t
119+ Just XhrResponseType_Blob -> Just . XhrResponseBody_Blob . Blob . castForeignPtr <$> newForeignPtr_ t
120+ Just XhrResponseType_Default -> fmap (XhrResponseBody_Default . T.pack) <$> fromJSStringMaybe c t
121+ Just XhrResponseType_Text -> fmap (XhrResponseBody_Text . T.pack) <$> fromJSStringMaybe c t
96xmlHttpRequestGetResponseText :: XMLHttpRequest -> IO (Maybe Text)
124xmlHttpRequestGetResponseText :: XMLHttpRequest -> IO (Maybe Text)
97xmlHttpRequestGetResponseText xhr = do
125xmlHttpRequestGetResponseText xhr = do
98 let c = xhrContext xhr
126 let c = xhrContext xhr
99 script <- jsstringcreatewithutf8cstring "this.responseText"
127 script <- jsstringcreatewithutf8cstring "this.responseText"
100 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
128 t <- jsevaluatescript c script (xhrValue xhr) nullPtr 1 nullPtr
101− isNull <- jsvalueisnull c t
129+ fmap (fmap T.pack) $ fromJSStringMaybe c t
103− True -> return Nothing
105− j <- jsvaluetostringcopy c t nullPtr
106− l <- jsstringgetmaximumutf8cstringsize j
107− s <- allocaBytes (fromIntegral l) $ \ps -> do
108− _ <- jsstringgetutf8cstring'_ j ps (fromIntegral l)
110− return $ Just $ T.pack s
112−class IsXhrPayload a where
113− xmlHttpRequestSend :: XMLHttpRequest -> a -> IO ()
115−instance IsXhrPayload () where
116− xmlHttpRequestSend xhr _ = xmlHttpRequestSendPayload xhr Nothing
118−instance IsXhrPayload String where
119− xmlHttpRequestSend xhr = xmlHttpRequestSendPayload xhr . Just
121−instance IsXhrPayload File where
122− xmlHttpRequestSend = error "xmlHttpRequestSend{File}: not implemented"
124xmlHttpRequestSendPayload :: XMLHttpRequest -> Maybe String -> IO ()
131xmlHttpRequestSendPayload :: XMLHttpRequest -> Maybe String -> IO ()
125xmlHttpRequestSendPayload xhr payload = do
132xmlHttpRequestSendPayload xhr payload = do