Index: chrome/browser/ui/webui/inspect_ui.cc |
diff --git a/chrome/browser/ui/webui/inspect_ui.cc b/chrome/browser/ui/webui/inspect_ui.cc |
index 0aa1ab127788e313e2055a4d28bb7b2d1f7c0a7d..4176676e0e91a26e5cf55714d0ad7164549091b1 100644 |
--- a/chrome/browser/ui/webui/inspect_ui.cc |
+++ b/chrome/browser/ui/webui/inspect_ui.cc |
@@ -15,6 +15,7 @@ |
#include "base/utf_string_conversions.h" |
#include "base/values.h" |
#include "chrome/browser/devtools/devtools_window.h" |
+#include "chrome/browser/devtools/protocol_http_request.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
@@ -58,7 +59,11 @@ using content::WebUIMessageHandler; |
using content::WorkerService; |
using content::WorkerServiceObserver; |
+namespace { |
+ |
static const char kDataFile[] = "targets-data.json"; |
+static const char kAdbQuery[] = "adb-query/"; |
+static const char kLocalXhr[] = "local-xhr/"; |
static const char kExtensionTargetType[] = "extension"; |
static const char kPageTargetType[] = "page"; |
@@ -76,8 +81,6 @@ static const char kNameField[] = "name"; |
static const char kFaviconUrlField[] = "favicon_url"; |
static const char kPidField[] = "pid"; |
-namespace { |
- |
DictionaryValue* BuildTargetDescriptor( |
const std::string& target_type, |
bool attached, |
@@ -169,12 +172,9 @@ void SendDescriptors( |
callback.Run(base::RefCountedString::TakeString(&json_string)); |
} |
-bool HandleRequestCallback( |
+bool HandleDataRequestCallback( |
const std::string& path, |
const content::WebUIDataSource::GotDataCallback& callback) { |
- if (path != kDataFile) |
- return false; |
- |
std::set<RenderViewHost*> tab_rvhs; |
for (TabContentsIterator it; !it.done(); it.Next()) |
tab_rvhs.insert(it->GetRenderViewHost()); |
@@ -213,16 +213,6 @@ bool HandleRequestCallback( |
return true; |
} |
-content::WebUIDataSource* CreateInspectUIHTMLSource() { |
- content::WebUIDataSource* source = |
- content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); |
- source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); |
- source->AddResourcePath("inspect.js", IDR_INSPECT_JS); |
- source->SetDefaultResource(IDR_INSPECT_HTML); |
- source->SetRequestFilter(base::Bind(&HandleRequestCallback)); |
- return source; |
-} |
- |
class InspectMessageHandler : public WebUIMessageHandler { |
public: |
InspectMessageHandler() {} |
@@ -358,10 +348,12 @@ class InspectUI::WorkerCreationDestructionListener |
InspectUI::InspectUI(content::WebUI* web_ui) |
: WebUIController(web_ui), |
- observer_(new WorkerCreationDestructionListener(this)) { |
+ observer_(new WorkerCreationDestructionListener(this)), |
+ ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
web_ui->AddMessageHandler(new InspectMessageHandler()); |
Profile* profile = Profile::FromWebUI(web_ui); |
+ adb_bridge_ = DevToolsAdbBridge::Start(); |
content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); |
registrar_.Add(this, |
@@ -383,6 +375,16 @@ void InspectUI::RefreshUI() { |
web_ui()->CallJavascriptFunction("populateLists"); |
} |
+// static |
+bool InspectUI::WeakHandleRequestCallback( |
+ const base::WeakPtr<InspectUI>& inspect_ui, |
+ const std::string& path, |
+ const content::WebUIDataSource::GotDataCallback& callback) { |
+ if (!inspect_ui.get()) |
+ return false; |
+ return inspect_ui->HandleRequestCallback(path, callback); |
+} |
+ |
void InspectUI::Observe(int type, |
const content::NotificationSource& source, |
const content::NotificationDetails& details) { |
@@ -396,7 +398,65 @@ void InspectUI::StopListeningNotifications() |
{ |
if (!observer_) |
return; |
+ if (adb_bridge_) { |
+ adb_bridge_->Stop(); |
+ adb_bridge_ = NULL; |
+ } |
observer_->InspectUIDestroyed(); |
observer_ = NULL; |
registrar_.RemoveAll(); |
} |
+ |
+content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() { |
+ content::WebUIDataSource* source = |
+ content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); |
+ source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); |
+ source->AddResourcePath("inspect.js", IDR_INSPECT_JS); |
+ source->SetDefaultResource(IDR_INSPECT_HTML); |
+ source->SetRequestFilter(base::Bind(&InspectUI::WeakHandleRequestCallback, |
+ weak_factory_.GetWeakPtr())); |
+ return source; |
+} |
+ |
+bool InspectUI::HandleRequestCallback( |
+ const std::string& path, |
+ const content::WebUIDataSource::GotDataCallback& callback) { |
+ if (path == kDataFile) |
+ return HandleDataRequestCallback(path, callback); |
+ if (path.find(kAdbQuery) == 0) |
+ return HandleAdbQueryCallback(path, callback); |
+ if (path.find(kLocalXhr) == 0) |
+ return HandleLocalXhrCallback(path, callback); |
+ return false; |
+} |
+ |
+bool InspectUI::HandleAdbQueryCallback( |
+ const std::string& path, |
+ const content::WebUIDataSource::GotDataCallback& callback) { |
+ std::string query = path.substr(strlen(kAdbQuery)); |
+ adb_bridge_->Query(query, base::Bind(&InspectUI::RespondOnUIThread, |
+ weak_factory_.GetWeakPtr(), callback)); |
+ return true; |
+} |
+ |
+bool InspectUI::HandleLocalXhrCallback( |
+ const std::string& path, |
+ const content::WebUIDataSource::GotDataCallback& callback) { |
+ std::string url = "http://localhost:" + path.substr(strlen(kLocalXhr)); |
+ new ProtocolHttpRequest(Profile::FromWebUI(web_ui()), url, |
+ base::Bind(&InspectUI::RespondOnUIThread, |
+ weak_factory_.GetWeakPtr(), callback)); |
+ return true; |
+} |
+ |
+void InspectUI::RespondOnUIThread( |
+ const content::WebUIDataSource::GotDataCallback& callback, |
+ const std::string& error, |
+ const std::string& data) { |
+ ListValue result; |
+ result.AppendString(error); |
+ result.AppendString(data); |
+ std::string json_string; |
+ base::JSONWriter::Write(&result, &json_string); |
+ callback.Run(base::RefCountedString::TakeString(&json_string)); |
+} |