module DEBUGGER__::UI_CDP

def process

def process
  bps = {}
  @src_map = {}
  loop do
    req = @ws_server.extract_data
    case req['method']
    ## boot/configuration
    when 'Debugger.getScriptSource'
      @q_msg << req
    when 'Debugger.enable'
      send_response req
      @q_msg << req
    when 'Runtime.enable'
      send_response req
      send_event 'Runtime.executionContextCreated',
                  context: {
                    id: SecureRandom.hex(16),
                    origin: "http://#{@local_addr.inspect_sockaddr}",
                    name: ''
                  }
    when 'Runtime.getIsolateId'
      send_response req,
                    id: SecureRandom.hex
    when 'Runtime.terminateExecution'
      send_response req
      exit
    when 'Page.startScreencast', 'Emulation.setTouchEmulationEnabled', 'Emulation.setEmitTouchEventsForMouse',
      'Runtime.compileScript', 'Page.getResourceContent', 'Overlay.setPausedInDebuggerMessage',
      'Runtime.releaseObjectGroup', 'Runtime.discardConsoleEntries', 'Log.clear', 'Runtime.runIfWaitingForDebugger'
      send_response req
    ## control
    when 'Debugger.resume'
      send_response req
      send_event 'Debugger.resumed'
      @q_msg << 'c'
      @q_msg << req
    when 'Debugger.stepOver'
      begin
        @session.check_postmortem
        send_response req
        send_event 'Debugger.resumed'
        @q_msg << 'n'
      rescue PostmortemError
        send_fail_response req,
                          code: INVALID_REQUEST,
                          message: "'stepOver' is not supported while postmortem mode"
      ensure
        @q_msg << req
      end
    when 'Debugger.stepInto'
      begin
        @session.check_postmortem
        send_response req
        send_event 'Debugger.resumed'
        @q_msg << 's'
      rescue PostmortemError
        send_fail_response req,
                          code: INVALID_REQUEST,
                          message: "'stepInto' is not supported while postmortem mode"
      ensure
        @q_msg << req
      end
    when 'Debugger.stepOut'
      begin
        @session.check_postmortem
        send_response req
        send_event 'Debugger.resumed'
        @q_msg << 'fin'
      rescue PostmortemError
        send_fail_response req,
                          code: INVALID_REQUEST,
                          message: "'stepOut' is not supported while postmortem mode"
      ensure
        @q_msg << req
      end
    when 'Debugger.setSkipAllPauses'
      skip = req.dig('params', 'skip')
      if skip
        deactivate_bp
      else
        activate_bp bps
      end
      send_response req
    when 'Debugger.pause'
      send_response req
      Process.kill(UI_ServerBase::TRAP_SIGNAL, Process.pid)
    # breakpoint
    when 'Debugger.getPossibleBreakpoints'
      @q_msg << req
    when 'Debugger.setBreakpointByUrl'
      line = req.dig('params', 'lineNumber')
      if regexp = req.dig('params', 'urlRegex')
        b_id = "1:#{line}:#{regexp}"
        bps[b_id] = bps.size
        path = regexp.match(/(.*)\|/)[1].gsub("\\", "")
        add_line_breakpoint(req, b_id, path)
      elsif url = req.dig('params', 'url')
        b_id = "#{line}:#{url}"
        # When breakpoints are set in Script snippet, non-existent path such as "snippet:///Script%20snippet%20%231" sent.
        # That's why we need to check it here.
        if File.exist? url
          bps[b_id] = bps.size
          add_line_breakpoint(req, b_id, url)
        else
          send_response req,
                        breakpointId: b_id,
                        locations: []
        end            
      else
        if hash = req.dig('params', 'scriptHash')
          b_id = "#{line}:#{hash}"
          send_response req,
                        breakpointId: b_id,
                        locations: []
        else
          raise 'Unsupported'
        end
      end
    when 'Debugger.removeBreakpoint'
      b_id = req.dig('params', 'breakpointId')
      bps = del_bp bps, b_id
      send_response req
    when 'Debugger.setBreakpointsActive'
      active = req.dig('params', 'active')
      if active
        activate_bp bps
      else
        deactivate_bp # TODO: Change this part because catch breakpoints should not be deactivated.
      end
      send_response req
    when 'Debugger.setPauseOnExceptions'
      state = req.dig('params', 'state')
      ex = 'Exception'
      case state
      when 'none'
        @q_msg << 'config postmortem = false'
        bps = del_bp bps, ex
      when 'uncaught'
        @q_msg << 'config postmortem = true'
        bps = del_bp bps, ex
      when 'all'
        @q_msg << 'config postmortem = false'
        SESSION.add_catch_breakpoint ex
        bps[ex] = bps.size
      end
      send_response req
    when 'Debugger.evaluateOnCallFrame', 'Runtime.getProperties'
      @q_msg << req
    end
  end
rescue Detach
  @q_msg << 'continue'
end