 
 RPCClient = require './rpc_client'
 
-DEFAULT_HANDSHAKE_TIMEOUT_MS = 10 * 1000 # 10 seconds
+DEFAULT_HANDSHAKE_TIMEOUT_MS = 10000 # 10 seconds
+SW_CONNECT_TIMEOUT_MS = 3000 # 3s
 
 selfWindow = if window? then window else self
 

      clearInterval @iabInterval

  replyInAppBrowserWindow: (data) =>
    escapedData = data.replace(/'/g, "\\'")
    @iabWindow.executeScript {
      code: "
      if(window._portalOnMessage)

    })

    useSw ?= navigator.serviceWorker and window.location.protocol isnt 'http:'
    if useSw
      # only use service workers if current page has one
      @ready = new Promise (resolve, reject) ->
        readyTimeout = setTimeout resolve, SW_CONNECT_TIMEOUT_MS

        navigator.serviceWorker.ready
        .catch -> null
        .then (registration) =>
          worker = registration?.active
          if worker
            @sw = new RPCClient({
              timeout: timeout
              postMessage: (msg, origin) =>
                swMessageChannel = new MessageChannel()
                swMessageChannel?.port1.onmessage = (e) =>
                  @onMessage e, {isServiceWorker: true}
                worker.postMessage(
                  msg, [swMessageChannel.port2]
                )
            })
          clearTimeout readyTimeout
          resolve()
    else
      @ready = Promise.resolve true

