{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- | Functions and types for working with Google Chrome extensions.
module Test.WebDriver.Chrome.Extension
       ( ChromeExtension
       , loadExtension
       , loadRawExtension
       ) where
import Data.ByteString.Lazy as LBS
import Data.ByteString.Base64.Lazy as B64
import Data.Text.Lazy
import Data.Text.Lazy.Encoding (decodeLatin1)
import Data.Aeson
import Control.Applicative
import Control.Monad.Base

import Prelude -- hides some "unused import" warnings

-- |An opaque type representing a Google Chrome extension. Values of this type
-- are passed to the 'Test.Webdriver.chromeExtensions' field.
newtype ChromeExtension = ChromeExtension Text
                        deriving (ChromeExtension -> ChromeExtension -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ChromeExtension -> ChromeExtension -> Bool
$c/= :: ChromeExtension -> ChromeExtension -> Bool
== :: ChromeExtension -> ChromeExtension -> Bool
$c== :: ChromeExtension -> ChromeExtension -> Bool
Eq, Int -> ChromeExtension -> ShowS
[ChromeExtension] -> ShowS
ChromeExtension -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ChromeExtension] -> ShowS
$cshowList :: [ChromeExtension] -> ShowS
show :: ChromeExtension -> String
$cshow :: ChromeExtension -> String
showsPrec :: Int -> ChromeExtension -> ShowS
$cshowsPrec :: Int -> ChromeExtension -> ShowS
Show, ReadPrec [ChromeExtension]
ReadPrec ChromeExtension
Int -> ReadS ChromeExtension
ReadS [ChromeExtension]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ChromeExtension]
$creadListPrec :: ReadPrec [ChromeExtension]
readPrec :: ReadPrec ChromeExtension
$creadPrec :: ReadPrec ChromeExtension
readList :: ReadS [ChromeExtension]
$creadList :: ReadS [ChromeExtension]
readsPrec :: Int -> ReadS ChromeExtension
$creadsPrec :: Int -> ReadS ChromeExtension
Read, [ChromeExtension] -> Encoding
[ChromeExtension] -> Value
ChromeExtension -> Encoding
ChromeExtension -> Value
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [ChromeExtension] -> Encoding
$ctoEncodingList :: [ChromeExtension] -> Encoding
toJSONList :: [ChromeExtension] -> Value
$ctoJSONList :: [ChromeExtension] -> Value
toEncoding :: ChromeExtension -> Encoding
$ctoEncoding :: ChromeExtension -> Encoding
toJSON :: ChromeExtension -> Value
$ctoJSON :: ChromeExtension -> Value
ToJSON, Value -> Parser [ChromeExtension]
Value -> Parser ChromeExtension
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [ChromeExtension]
$cparseJSONList :: Value -> Parser [ChromeExtension]
parseJSON :: Value -> Parser ChromeExtension
$cparseJSON :: Value -> Parser ChromeExtension
FromJSON)

-- |Load a .crx file as a 'ChromeExtension'.
loadExtension :: MonadBase IO m => FilePath -> m ChromeExtension
loadExtension :: forall (m :: * -> *). MonadBase IO m => String -> m ChromeExtension
loadExtension String
path = forall (b :: * -> *) (m :: * -> *) α. MonadBase b m => b α -> m α
liftBase forall a b. (a -> b) -> a -> b
$ ByteString -> ChromeExtension
loadRawExtension forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> IO ByteString
LBS.readFile String
path

-- |Load raw .crx data as a 'ChromeExtension'.
loadRawExtension :: ByteString -> ChromeExtension
loadRawExtension :: ByteString -> ChromeExtension
loadRawExtension = Text -> ChromeExtension
ChromeExtension forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
decodeLatin1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
B64.encode