@@ -1,6 +1,7 @@
11{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
22module Language.Javascript.JSaddle.WKWebView.Internal
33 ( jsaddleMain
4+ , jsaddleMainFile
45 , WKWebView(..)
56 ) where
67
@@ -9,6 +10,7 @@ import Control.Concurrent (forkIO)
910
1011import Data.Monoid ((<>))
1112import Data.ByteString (useAsCString, packCString)
13+import qualified Data.ByteString as BS (ByteString)
1214import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1315import Data.Aeson (encode, decode)
1416
@@ -27,10 +29,32 @@ foreign export ccall jsaddleResult :: StablePtr (Results -> IO ()) -> CString ->
2729foreign export ccall withWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
2830foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> IO ()
2931foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
32+foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
3033foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
3134
35+-- | Run JSaddle in WKWebView
3236jsaddleMain :: JSM () -> WKWebView -> IO ()
3337jsaddleMain f webView = do
38+ jsaddleMain' f webView
39+
40+ useAsCString (toStrict indexHtml) $ loadHTMLString webView
41+ useAsCString (toStrict jsaddleJs) $ evaluateJavaScript webView
42+
43+-- | Run JSaddle in a WKWebView first loading the specified file
44+-- from the mainBundle (relative to the resourcePath).
45+jsaddleMainFile :: BS.ByteString -- ^ The file to navigate to.
46+ -> BS.ByteString -- ^ The path to allow read access to.
47+ -> JSM () -> WKWebView -> IO ()
48+jsaddleMainFile url allowing f webView = do
49+ jsaddleMain' f webView
50+
51+ useAsCString url $ \u ->
52+ useAsCString allowing $ \a ->
53+ loadBundleFile webView u a
54+ useAsCString (toStrict jsaddleJs) $ evaluateJavaScript webView
55+
56+jsaddleMain' :: JSM () -> WKWebView -> IO ()
57+jsaddleMain' f webView = do
3458 (processResult, start) <- runJavaScript (\batch ->
3559 useAsCString (toStrict $ "runJSaddleBatch(" <> encode batch <> ");") $
3660 evaluateJavaScript webView)
@@ -40,9 +64,6 @@ jsaddleMain f webView = do
4064 resultHandler <- newStablePtr processResult
4165 addJSaddleHandler webView startHandler resultHandler
4266
43− useAsCString (toStrict indexHtml) $ loadHTMLString webView
44− useAsCString (toStrict jsaddleJs) $ evaluateJavaScript webView
45−
4667jsaddleStart :: StablePtr (IO ()) -> IO ()
4768jsaddleStart ptrHandler = join (deRefStablePtr ptrHandler)
4869