     @isParentValidFn ?= -> true
     timeout ?= null
     @isListening = false
+    @isFramed = window.self isnt window.top
+    @parent = window.parent
+
     @client = new RPCClient({
       timeout: timeout
-      postMessage: (msg, origin) ->
-        window.parent?.postMessage msg, origin
+      postMessage: (msg, origin) =>
+        @parent?.postMessage msg, origin
     })
     # All parents must respond to 'ping' with 'pong'
     @registeredMethods = {
       ping: -> 'pong'
     }
+
+  setParent: (parent) =>
+    @parent = parent
+    @isFramed = true
 
   # Binds global message listener
   # Must be called before .call()

        throw new Error 'Method not found'
      return fn.apply null, params

    if IS_FRAMED
      frameError = null
      @validation
      .then =>


RPCClient = require './rpc_client'

HANDSHAKE_TIMEOUT_MS = 10 * 1000 # 10 seconds

class PortalGun
