summaryrefslogtreecommitdiffstats
path: root/webkit/inspector/DebuggerIPC.js
blob: 15835fc7ce9edc61b36c3105d88e31c92e42adf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Implementation of debugger inter-process communication.
 * Debugger UI can send messages to the DebuggerHost object living in 
 * Chrome browser process. The messages are either processed by DebuggerHost
 * itself or trigger IPC message(such as break, evaluate script etc) from 
 * browser process to the renderer where the v8 instance being debugged
 * will process them.
 */
 
var DebuggerIPC = {};

/**
 * Set up default debugger UI.
 * @param {DebuggerPanel|DebuggerConsole} debuggerUI
 */
DebuggerIPC.init = function(debuggerUI) {
  DebuggerIPC.debuggerUI = debuggerUI;
}

/**
 * Sends JSON message to DebuggerHost.
 * @param {Array.<Object>} nameAndArguments
 */
DebuggerIPC.sendMessage = function(nameAndArguments) {
  //alert("DebuggerIPC.sendMessage " + nameAndArguments);
  dprint("DebuggerIPC.callMethod([" + nameAndArguments + "])");
  // convert all arguments to strings
  // TODO(yurys): extend chrome.send to accept array of any value
  // objects not only strings
  for (var i = 0; i < nameAndArguments.length; i++) {
    if (typeof nameAndArguments[i] != "string") {
      nameAndArguments[i] = "" + nameAndArguments[i];
    }
  }

  chrome.send("DebuggerHostMessage", nameAndArguments);
};

/**
 * Handles messages from DebuggerHost.
 * @param {Object} msg An object representing the message.
 */
DebuggerIPC.onMessageReceived = function(msg) {
  //alert("DebuggerIPC.onMessageReceived " + msg.event);
  var ui = DebuggerIPC.debuggerUI;
  dprint("onMessageReceived: " + (msg && msg.event));
  if (msg.type == "event") {
    if (msg.event == "setDebuggerBreak") {
      var val = msg.body.argument;
      ui.setDebuggerBreak(val);
    } else if (msg.event == "appendText") {
      var text = msg.body.text;
      ui.appendText(text);
    } else if (msg.event == "focusOnCommandLine") {
      dprint("focusOnCommandLine event received");
      // messages to DebugShell
    } else if (msg.event == "initDebugShell") {
      // DebugShell.initDebugShell();
      dprint(msg.event + " done");
    } else if (msg.event == "on_attach") {
      if (DebugShell.singleton) {
        var args = msg.body.arguments;
        if (!args || args.length != 1) {
          dprint(msg.event + " failed: invalid arguments");
          return;
        }
        var title = args[0];
        DebugShell.singleton.on_attach(title);
        dprint(msg.event + " done");
      } else {
        dprint(msg.event + " failed: DebugShell.singleton == null");
      }
    } else if (msg.event == "on_disconnect") {
      if (DebugShell.singleton) {
        DebugShell.singleton.on_disconnect();
        dprint(msg.event + " done");
      } else {
        dprint(msg.event + " failed: DebugShell.singleton == null");
      }
    } else if (msg.event == "exit") {
      if (DebugShell.singleton) {
        DebugShell.singleton.exit();
        dprint(msg.event + " done");
      } else {
        dprint(msg.event + " failed: DebugShell.singleton == null");
      }
    } else if (msg.event == "response") {
      if (DebugShell.singleton) {
        var args = msg.body.arguments;
        if (!args || args.length != 1) {
          dprint(msg.event + " failed: invalid argument");
          return;
        }
        DebugShell.singleton.response(args[0]);
        dprint(msg.event + " done");
      } else {
        ui.appendText(msg.event + " failed: DebugShell.singleton == null");
      }
    } else {
      ui.appendText("Unknown event: " + msg.event);
    }
  } else {
    ui.appendText("Unknown message type: " + msg.type);
  }
};