1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file declares the DebuggerRemoteServiceCommand struct and the
6// DebuggerRemoteService class which handles commands directed to the
7// "V8Debugger" tool.
8#ifndef CHROME_BROWSER_DEBUGGER_DEBUGGER_REMOTE_SERVICE_H_
9#define CHROME_BROWSER_DEBUGGER_DEBUGGER_REMOTE_SERVICE_H_
10#pragma once
11
12#include <string>
13
14#include "base/basictypes.h"
15#include "chrome/browser/debugger/devtools_remote.h"
16
17class DevToolsProtocolHandler;
18class DevToolsRemoteMessage;
19class DictionaryValue;
20class Value;
21class TabContents;
22
23// Contains constants for DebuggerRemoteService tool protocol commands
24// (V8-related only).
25struct DebuggerRemoteServiceCommand {
26  static const std::string kAttach;
27  static const std::string kDetach;
28  static const std::string kDebuggerCommand;
29  static const std::string kEvaluateJavascript;
30  static const std::string kFrameNavigate;  // navigation event
31  static const std::string kTabClosed;  // tab closing event
32};
33
34// Handles V8 debugger-related messages from the remote debugger (like
35// attach to V8 debugger, detach from V8 debugger, send command to V8 debugger)
36// and proxies JSON messages from V8 debugger to the remote debugger.
37class DebuggerRemoteService : public DevToolsRemoteListener {
38 public:
39  // |delegate| (never NULL) is the protocol handler instance
40  // which dispatches messages to this service. The responses from the
41  // V8 VM debugger are routed back to |delegate|.
42  // The ownership of |delegate| is NOT transferred to this class.
43  explicit DebuggerRemoteService(DevToolsProtocolHandler* delegate);
44
45  // Handles a JSON message from the tab_uid-associated V8 debugger.
46  void DebuggerOutput(int32 tab_uid, const std::string& message);
47
48  // Handles a frame navigation event.
49  void FrameNavigate(int32 tab_uid, const std::string& url);
50
51  // Handles a tab closing event.
52  void TabClosed(int32 tab_uid);
53
54  // Detaches the remote debugger from the tab specified by |destination|.
55  // It is public so that we can detach from the tab on the remote debugger
56  // connection loss.
57  // If |response| is not NULL, the operation result will be written
58  // as the "result" field in |response|, otherwise the result
59  // will not be propagated back to the caller.
60  void DetachFromTab(const std::string& destination,
61                     DictionaryValue* response);
62
63  // DevToolsRemoteListener interface.
64
65  // Processes |message| from the remote debugger, where the tool is
66  // "V8Debugger". Either sends the reply immediately or waits for an
67  // asynchronous response from the V8 debugger.
68  virtual void HandleMessage(const DevToolsRemoteMessage& message);
69
70  // Gets invoked on the remote debugger [socket] connection loss.
71  // Notifies the InspectableTabProxy of the remote debugger detachment.
72  virtual void OnConnectionLost();
73
74  // Specifies a tool name ("V8Debugger") handled by this class.
75  static const std::string kToolName;
76
77 private:
78  // Operation result returned in the "result" field.
79  typedef enum {
80    RESULT_OK = 0,
81    RESULT_ILLEGAL_TAB_STATE,
82    RESULT_UNKNOWN_TAB,
83    RESULT_DEBUGGER_ERROR,
84    RESULT_UNKNOWN_COMMAND
85  } Result;
86
87  virtual ~DebuggerRemoteService();
88
89  // Attaches a remote debugger to the tab specified by |destination|.
90  // Writes the attachment result (one of Result enum values) into |response|.
91  void AttachToTab(const std::string& destination,
92                   DictionaryValue* response);
93
94  // Retrieves a WebContents instance for the specified |tab_uid|
95  // or NULL if no such tab is found or no WebContents instance
96  // corresponds to that tab.
97  TabContents* ToTabContents(int32 tab_uid);
98
99  // Sends a JSON message with the |response| to the remote debugger.
100  // |tool| and |destination| are used as the respective header values.
101  void SendResponse(const Value& response,
102                    const std::string& tool,
103                    const std::string& destination);
104
105  // Redirects a V8 debugger command from |content| to a V8 debugger associated
106  // with the |tab_uid| and writes the result into |response| if it becomes
107  // known immediately.
108  bool DispatchDebuggerCommand(int tab_uid,
109                               DictionaryValue* content,
110                               DictionaryValue* response);
111
112  // Redirects a Javascript evaluation command from |content| to
113  // a V8 debugger associated with the |tab_uid| and writes the result
114  // into |response| if it becomes known immediately.
115  bool DispatchEvaluateJavascript(int tab_uid,
116                                  DictionaryValue* content,
117                                  DictionaryValue* response);
118
119  // The delegate is used to get an InspectableTabProxy instance.
120  DevToolsProtocolHandler* delegate_;
121  DISALLOW_COPY_AND_ASSIGN(DebuggerRemoteService);
122};
123
124#endif  // CHROME_BROWSER_DEBUGGER_DEBUGGER_REMOTE_SERVICE_H_
125