summaryrefslogtreecommitdiffstats
path: root/chrome/browser/debugger/debugger_remote_service.cc
blob: a856fb6d663f5b4f2eb1e4e455d964f08308cd16 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// Copyright (c) 2009 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.

#include "chrome/browser/debugger/debugger_remote_service.h"

#include "base/json_reader.h"
#include "base/json_writer.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/debugger/devtools_manager.h"
#include "chrome/browser/debugger/devtools_protocol_handler.h"
#include "chrome/browser/debugger/devtools_remote_message.h"
#include "chrome/browser/debugger/inspectable_tab_proxy.h"
#include "chrome/browser/tab_contents/web_contents.h"
#include "chrome/common/devtools_messages.h"
#include "chrome/common/render_messages.h"

const std::string DebuggerRemoteServiceCommand::kAttach = "attach";
const std::string DebuggerRemoteServiceCommand::kDetach = "detach";
const std::string DebuggerRemoteServiceCommand::kDebuggerCommand =
    "debugger_command";
const std::string DebuggerRemoteServiceCommand::kEvaluateJavascript =
    "evaluate_javascript";

const std::string DebuggerRemoteService::kToolName = "V8Debugger";
const std::wstring DebuggerRemoteService::kDataWide = L"data";
const std::wstring DebuggerRemoteService::kResultWide = L"result";

DebuggerRemoteService::DebuggerRemoteService(DevToolsProtocolHandler* delegate)
    : delegate_(delegate) {}

DebuggerRemoteService::~DebuggerRemoteService() {}

// message from remote debugger
void DebuggerRemoteService::HandleMessage(
    const DevToolsRemoteMessage& message) {
  static const std::wstring kCommandWide = L"command";
  const std::string destination = message.destination();
  scoped_ptr<Value> request(JSONReader::Read(message.content(), true));
  if (request.get() == NULL) {
    // Bad JSON
    NOTREACHED();
    return;
  }
  DictionaryValue* content;
  if (!request->IsType(Value::TYPE_DICTIONARY)) {
    NOTREACHED();  // Broken protocol :(
    return;
  }
  content = static_cast<DictionaryValue*>(request.get());
  if (!content->HasKey(kCommandWide)) {
    NOTREACHED();  // Broken protocol :(
    return;
  }
  std::string command;
  DictionaryValue response;

  content->GetString(kCommandWide, &command);
  response.SetString(kCommandWide, command);
  bool send_response = true;
  if (destination.size() == 0) {
    // Unknown command (bad format?)
    NOTREACHED();
    response.SetInteger(kResultWide, Result::kUnknownCommand);
    SendResponse(response, message.tool(), message.destination());
    return;
  }
  int32 tab_uid = -1;
  StringToInt(destination, &tab_uid);

  if (command == DebuggerRemoteServiceCommand::kAttach) {
    // TODO(apavlov): handle 0 for a new tab
    response.SetString(kCommandWide, DebuggerRemoteServiceCommand::kAttach);
    AttachTab(destination, &response);
  } else if (command == DebuggerRemoteServiceCommand::kDetach) {
    response.SetString(kCommandWide, DebuggerRemoteServiceCommand::kDetach);
    DetachTab(destination, &response);
  } else if (command == DebuggerRemoteServiceCommand::kDebuggerCommand) {
    if (tab_uid != -1) {
      DevToolsManager* manager = g_browser_process->devtools_manager();
      if (manager == NULL) {
        response.SetInteger(kResultWide, Result::kDebuggerError);
      }
      WebContents* web_contents = ToWebContents(tab_uid);
      if (web_contents != NULL) {
        DevToolsClientHost* client_host =
            manager->GetDevToolsClientHostFor(*web_contents);
        if (client_host != NULL) {
          std::string v8_command;
          DictionaryValue* v8_command_value;
          content->GetDictionary(kDataWide, &v8_command_value);
          JSONWriter::Write(v8_command_value, false, &v8_command);
          g_browser_process->devtools_manager()->ForwardToDevToolsAgent(
              *client_host, DevToolsAgentMsg_DebuggerCommand(v8_command));
          send_response = false;
          // Do not send response right now as the JSON will be received from
          // the V8 debugger asynchronously
        } else {
          // tab_uid is not being debugged (Attach has not been invoked)
          response.SetInteger(kResultWide, Result::kIllegalTabState);
        }
      } else {
        // Unknown tab_uid from remote debugger
        response.SetInteger(kResultWide, Result::kUnknownTab);
      }
    } else {
      // Invalid tab_uid from remote debugger (perhaps NaN)
      response.SetInteger(kResultWide, Result::kUnknownTab);
    }
  } else if (command == DebuggerRemoteServiceCommand::kEvaluateJavascript) {
    if (tab_uid != -1) {
      WebContents* web_contents = ToWebContents(tab_uid);
      if (web_contents != NULL) {
        RenderViewHost* rvh = web_contents->render_view_host();
        if (rvh != NULL) {
          std::wstring javascript;
          content->GetString(kDataWide, &javascript);
          rvh->Send(new ViewMsg_ScriptEvalRequest(
              rvh->routing_id(), L"", javascript));
          send_response = false;
        } else {
          // No RenderViewHost
          response.SetInteger(kResultWide, Result::kDebuggerError);
        }
      } else {
        // Unknown tab_uid from remote debugger
        response.SetInteger(kResultWide, Result::kUnknownTab);
      }
    }
  } else {
    // Unknown command
    NOTREACHED();
    response.SetInteger(kResultWide, Result::kUnknownCommand);
  }

  if (send_response) {
    SendResponse(response, message.tool(), message.destination());
  }
}

void DebuggerRemoteService::OnConnectionLost() {
  delegate_->inspectable_tab_proxy()->OnRemoteDebuggerDetached();
}

void DebuggerRemoteService::SendResponse(const Value& response,
                                         const std::string& tool,
                                         const std::string& destination) {
  std::string response_content;
  JSONWriter::Write(&response, false, &response_content);
  scoped_ptr<DevToolsRemoteMessage> response_message(
      DevToolsRemoteMessageBuilder::instance().Create(tool,
                                                      destination,
                                                      response_content));
  delegate_->Send(*response_message.get());
}

WebContents* DebuggerRemoteService::ToWebContents(int32 tab_uid) {
  const InspectableTabProxy::ControllersMap& navcon_map =
      delegate_->inspectable_tab_proxy()->controllers_map();
  InspectableTabProxy::ControllersMap::const_iterator it =
      navcon_map.find(tab_uid);
  if (it != navcon_map.end()) {
    TabContents* tab_contents = it->second->tab_contents();
    if (tab_contents == NULL) {
      return NULL;
    } else {
      return tab_contents->AsWebContents();
    }
  } else {
    return NULL;
  }
}

void DebuggerRemoteService::DebuggerOutput(int32 tab_id,
                                           const std::string& message) {
  std::string content;
  content.append("{\"command\":\"")
      .append(DebuggerRemoteServiceCommand::kDebuggerCommand)
      .append("\",\"result\":")
      .append(IntToString(Result::kOk))
      .append(",\"data\":")
      .append(message)
      .append("}");
  scoped_ptr<DevToolsRemoteMessage> response_message(
      DevToolsRemoteMessageBuilder::instance().Create(
          kToolName,
          IntToString(tab_id),
          content));
  delegate_->Send(*(response_message.get()));
}

void DebuggerRemoteService::AttachTab(const std::string& destination,
                                      DictionaryValue* response) {
  int32 tab_uid = -1;
  StringToInt(destination, &tab_uid);
  if (tab_uid < 0) {
    // Bad tab_uid received from remote debugger (perhaps NaN)
    response->SetInteger(kResultWide, Result::kUnknownTab);
    return;
  }
  if (tab_uid == 0) {  // single tab_uid
    // We've been asked to open a new tab with URL.
    // TODO(apavlov): implement
    NOTIMPLEMENTED();
    response->SetInteger(kResultWide, Result::kUnknownTab);
    return;
  }
  WebContents* web_contents = ToWebContents(tab_uid);
  if (web_contents == NULL) {
    // No active web contents with tab_uid
    response->SetInteger(kResultWide, Result::kUnknownTab);
    return;
  }
  if (g_browser_process->devtools_manager()->GetDevToolsClientHostFor(
      *web_contents) == NULL) {
    DevToolsClientHost* client_host =
        delegate_->inspectable_tab_proxy()->NewClientHost(tab_uid, this);
    DevToolsManager* manager = g_browser_process->devtools_manager();
    if (manager != NULL) {
      manager->RegisterDevToolsClientHostFor(*web_contents, client_host);
      manager->ForwardToDevToolsAgent(*client_host, DevToolsAgentMsg_Attach());
      response->SetInteger(kResultWide, Result::kOk);
    } else {
      response->SetInteger(kResultWide, Result::kDebuggerError);
    }
  } else {
    // DevToolsClientHost for this tab already registered
    response->SetInteger(kResultWide, Result::kIllegalTabState);
  }
}

void DebuggerRemoteService::DetachTab(const std::string& destination,
                                      DictionaryValue* response) {
  int32 tab_uid = -1;
  int resultCode = -1;
  StringToInt(destination, &tab_uid);
  if (tab_uid == -1) {
    // Bad tab_uid received from remote debugger (NaN)
    if (response != NULL) {
      response->SetInteger(kResultWide, Result::kUnknownTab);
    }
    return;
  }
  WebContents* web_contents = ToWebContents(tab_uid);
  if (web_contents == NULL) {
    // Unknown tab
    resultCode = Result::kUnknownTab;
  } else {
    DevToolsManager* manager = g_browser_process->devtools_manager();
    if (manager != NULL) {
      DevToolsClientHost* client_host =
          manager->GetDevToolsClientHostFor(*web_contents);
      if (client_host != NULL) {
        manager->ForwardToDevToolsAgent(
            *client_host, DevToolsAgentMsg_Detach());
        client_host->InspectedTabClosing();
        resultCode = Result::kOk;
      } else {
        // No client host registered
        resultCode = Result::kUnknownTab;
      }
    } else {
      // No DevToolsManager
      resultCode = Result::kDebuggerError;
    }
  }
  if (response != NULL) {
    response->SetInteger(kResultWide, resultCode);
  }
}