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
|
// 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.
#include "chrome/browser/dom_ui/debugger_ui.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/debugger/debugger_shell.h"
#include "chrome/browser/debugger/debugger_wrapper.h"
#include "chrome/browser/dom_ui/chrome_url_data_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/resource_bundle.h"
#include "chrome/common/url_constants.h"
#include "net/base/mime_util.h"
#include "grit/debugger_resources.h"
namespace {
class DebuggerHTMLSource : public ChromeURLDataManager::DataSource {
public:
// Creates our datasource and sets our user message to a specific message
// from our string bundle.
DebuggerHTMLSource()
: DataSource("debugger", MessageLoop::current()) { }
// Called when the network layer has requested a resource underneath
// the path we registered.
virtual void StartDataRequest(const std::string& path, int request_id) {
int resource_id = 0;
if (!path.length()) {
resource_id = IDR_DEBUGGER_HTML;
} else if (path == "debugger.js") {
resource_id = IDR_DEBUGGER_JS;
} else if (path == "debugger.css") {
resource_id = IDR_DEBUGGER_CSS;
} else {
SendResponse(request_id, NULL);
return;
}
std::wstring debugger_path =
CommandLine::ForCurrentProcess()->GetSwitchValue(
switches::kJavaScriptDebuggerPath);
std::string data_str;
if (!debugger_path.empty() && file_util::PathExists(debugger_path)) {
if (path.empty())
file_util::AppendToPath(&debugger_path, L"debugger.html");
else
file_util::AppendToPath(&debugger_path, UTF8ToWide(path));
if (!file_util::ReadFileToString(debugger_path, &data_str)) {
SendResponse(request_id, NULL);
return;
}
} else {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
data_str = rb.GetDataResource(resource_id);
}
scoped_refptr<RefCountedBytes> data_bytes(new RefCountedBytes);
data_bytes->data.resize(data_str.size());
std::copy(data_str.begin(), data_str.end(), data_bytes->data.begin());
SendResponse(request_id, data_bytes);
}
virtual std::string GetMimeType(const std::string& path) const {
// Currently but three choices {"", "debugger.js", "debugger.css"}.
// Map the extension to mime-type, defaulting to "text/html".
std::string mime_type("text/html");
#if defined(OS_WIN)
FilePath file_path(ASCIIToWide(path));
#elif defined(OS_POSIX)
FilePath file_path(path);
#endif
net::GetMimeTypeFromFile(file_path, &mime_type);
return mime_type;
}
private:
DISALLOW_COPY_AND_ASSIGN(DebuggerHTMLSource);
};
class DebuggerHandler : public DOMMessageHandler {
public:
explicit DebuggerHandler(DOMUI* dom_ui) : DOMMessageHandler(dom_ui) {
dom_ui->RegisterMessageCallback("DebuggerHostMessage",
NewCallback(this, &DebuggerHandler::HandleDebuggerHostMessage));
}
void HandleDebuggerHostMessage(const Value* content) {
if (!content || !content->IsType(Value::TYPE_LIST)) {
NOTREACHED();
return;
}
const ListValue* args = static_cast<const ListValue*>(content);
if (args->GetSize() < 1) {
NOTREACHED();
return;
}
#ifndef CHROME_DEBUGGER_DISABLED
DebuggerWrapper* wrapper = g_browser_process->debugger_wrapper();
DebuggerHost* debugger_host = wrapper->GetDebugger();
if (!debugger_host) {
NOTREACHED();
return;
}
debugger_host->OnDebuggerHostMsg(args);
#endif
}
private:
DISALLOW_COPY_AND_ASSIGN(DebuggerHandler);
};
} // namespace
DebuggerUI::DebuggerUI(WebContents* contents) : DOMUI(contents) {
AddMessageHandler(new DebuggerHandler(this));
DebuggerHTMLSource* html_source = new DebuggerHTMLSource();
g_browser_process->io_thread()->message_loop()->PostTask(FROM_HERE,
NewRunnableMethod(&chrome_url_data_manager,
&ChromeURLDataManager::AddDataSource,
html_source));
}
|