| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/inspect_ui.h" | 5 #include "chrome/browser/ui/webui/inspect_ui.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 #include "content/public/browser/web_contents.h" | 35 #include "content/public/browser/web_contents.h" |
| 36 #include "content/public/browser/web_ui.h" | 36 #include "content/public/browser/web_ui.h" |
| 37 #include "content/public/browser/web_ui_data_source.h" | 37 #include "content/public/browser/web_ui_data_source.h" |
| 38 #include "content/public/browser/web_ui_message_handler.h" | 38 #include "content/public/browser/web_ui_message_handler.h" |
| 39 #include "content/public/browser/worker_service.h" | 39 #include "content/public/browser/worker_service.h" |
| 40 #include "content/public/browser/worker_service_observer.h" | 40 #include "content/public/browser/worker_service_observer.h" |
| 41 #include "content/public/common/process_type.h" | 41 #include "content/public/common/process_type.h" |
| 42 #include "grit/browser_resources.h" | 42 #include "grit/browser_resources.h" |
| 43 #include "grit/generated_resources.h" | 43 #include "grit/generated_resources.h" |
| 44 #include "net/base/escape.h" | 44 #include "net/base/escape.h" |
| 45 #include "net/base/net_errors.h" |
| 45 #include "ui/base/resource/resource_bundle.h" | 46 #include "ui/base/resource/resource_bundle.h" |
| 46 | 47 |
| 47 using content::BrowserThread; | 48 using content::BrowserThread; |
| 48 using content::ChildProcessData; | 49 using content::ChildProcessData; |
| 49 using content::DevToolsAgentHost; | 50 using content::DevToolsAgentHost; |
| 50 using content::DevToolsClientHost; | 51 using content::DevToolsClientHost; |
| 51 using content::DevToolsManager; | 52 using content::DevToolsManager; |
| 52 using content::RenderProcessHost; | 53 using content::RenderProcessHost; |
| 53 using content::RenderViewHost; | 54 using content::RenderViewHost; |
| 54 using content::RenderViewHostDelegate; | 55 using content::RenderViewHostDelegate; |
| 55 using content::RenderWidgetHost; | 56 using content::RenderWidgetHost; |
| 56 using content::WebContents; | 57 using content::WebContents; |
| 57 using content::WebUIMessageHandler; | 58 using content::WebUIMessageHandler; |
| 58 using content::WorkerService; | 59 using content::WorkerService; |
| 59 using content::WorkerServiceObserver; | 60 using content::WorkerServiceObserver; |
| 60 | 61 |
| 61 namespace { | 62 namespace { |
| 62 | 63 |
| 63 static const char kDataFile[] = "targets-data.json"; | 64 static const char kDataFile[] = "targets-data.json"; |
| 64 static const char kAdbQuery[] = "adb-query/"; | 65 static const char kAdbQuery[] = "adb-query/"; |
| 65 static const char kAdbDevices[] = "adb-devices"; | 66 static const char kAdbPages[] = "adb-pages"; |
| 66 static const char kLocalXhr[] = "local-xhr/"; | 67 static const char kLocalXhr[] = "local-xhr/"; |
| 67 | 68 |
| 68 static const char kExtensionTargetType[] = "extension"; | 69 static const char kExtensionTargetType[] = "extension"; |
| 69 static const char kPageTargetType[] = "page"; | 70 static const char kPageTargetType[] = "page"; |
| 70 static const char kWorkerTargetType[] = "worker"; | 71 static const char kWorkerTargetType[] = "worker"; |
| 72 static const char kAdbTargetType[] = "adb_page"; |
| 71 | 73 |
| 72 static const char kInspectCommand[] = "inspect"; | 74 static const char kInspectCommand[] = "inspect"; |
| 73 static const char kTerminateCommand[] = "terminate"; | 75 static const char kTerminateCommand[] = "terminate"; |
| 74 | 76 |
| 75 static const char kTargetTypeField[] = "type"; | 77 static const char kTargetTypeField[] = "type"; |
| 76 static const char kAttachedField[] = "attached"; | 78 static const char kAttachedField[] = "attached"; |
| 77 static const char kProcessIdField[] = "processId"; | 79 static const char kProcessIdField[] = "processId"; |
| 78 static const char kRouteIdField[] = "routeId"; | 80 static const char kRouteIdField[] = "routeId"; |
| 79 static const char kUrlField[] = "url"; | 81 static const char kUrlField[] = "url"; |
| 80 static const char kNameField[] = "name"; | 82 static const char kNameField[] = "name"; |
| 81 static const char kFaviconUrlField[] = "favicon_url"; | 83 static const char kFaviconUrlField[] = "faviconUrl"; |
| 82 static const char kPidField[] = "pid"; | 84 static const char kPidField[] = "pid"; |
| 85 static const char kAdbSerialField[] = "adbSerial"; |
| 86 static const char kAdbModelField[] = "adbModel"; |
| 87 static const char kAdbPageIdField[] = "adbPageId"; |
| 88 static const char kAdbDebugUrl[] = "adbDebugUrl"; |
| 83 | 89 |
| 84 DictionaryValue* BuildTargetDescriptor( | 90 DictionaryValue* BuildTargetDescriptor( |
| 85 const std::string& target_type, | 91 const std::string& target_type, |
| 86 bool attached, | 92 bool attached, |
| 87 const GURL& url, | 93 const GURL& url, |
| 88 const std::string& name, | 94 const std::string& name, |
| 89 const GURL& favicon_url, | 95 const GURL& favicon_url, |
| 90 int process_id, | 96 int process_id, |
| 91 int route_id, | 97 int route_id, |
| 92 base::ProcessHandle handle = base::kNullProcessHandle) { | 98 base::ProcessHandle handle = base::kNullProcessHandle) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 worker_info[i].url, | 168 worker_info[i].url, |
| 163 UTF16ToUTF8(worker_info[i].name), | 169 UTF16ToUTF8(worker_info[i].name), |
| 164 GURL(), | 170 GURL(), |
| 165 worker_info[i].process_id, | 171 worker_info[i].process_id, |
| 166 worker_info[i].route_id, | 172 worker_info[i].route_id, |
| 167 worker_info[i].handle)); | 173 worker_info[i].handle)); |
| 168 } | 174 } |
| 169 | 175 |
| 170 std::string json_string; | 176 std::string json_string; |
| 171 base::JSONWriter::Write(rvh_list, &json_string); | 177 base::JSONWriter::Write(rvh_list, &json_string); |
| 172 | |
| 173 callback.Run(base::RefCountedString::TakeString(&json_string)); | 178 callback.Run(base::RefCountedString::TakeString(&json_string)); |
| 174 } | 179 } |
| 175 | 180 |
| 176 bool HandleDataRequestCallback( | 181 bool HandleDataRequestCallback( |
| 177 const std::string& path, | 182 const std::string& path, |
| 178 const content::WebUIDataSource::GotDataCallback& callback) { | 183 const content::WebUIDataSource::GotDataCallback& callback) { |
| 179 std::set<RenderViewHost*> tab_rvhs; | 184 std::set<RenderViewHost*> tab_rvhs; |
| 180 for (TabContentsIterator it; !it.done(); it.Next()) | 185 for (TabContentsIterator it; !it.done(); it.Next()) |
| 181 tab_rvhs.insert(it->GetRenderViewHost()); | 186 tab_rvhs.insert(it->GetRenderViewHost()); |
| 182 | 187 |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 InspectUI* discovery_ui_; | 336 InspectUI* discovery_ui_; |
| 332 }; | 337 }; |
| 333 | 338 |
| 334 InspectUI::InspectUI(content::WebUI* web_ui) | 339 InspectUI::InspectUI(content::WebUI* web_ui) |
| 335 : WebUIController(web_ui), | 340 : WebUIController(web_ui), |
| 336 observer_(new WorkerCreationDestructionListener(this)), | 341 observer_(new WorkerCreationDestructionListener(this)), |
| 337 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 342 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 338 web_ui->AddMessageHandler(new InspectMessageHandler()); | 343 web_ui->AddMessageHandler(new InspectMessageHandler()); |
| 339 | 344 |
| 340 Profile* profile = Profile::FromWebUI(web_ui); | 345 Profile* profile = Profile::FromWebUI(web_ui); |
| 341 adb_bridge_ = DevToolsAdbBridge::Start(); | 346 adb_bridge_.reset(new DevToolsAdbBridge(profile)); |
| 342 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); | 347 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); |
| 343 | 348 |
| 344 registrar_.Add(this, | 349 registrar_.Add(this, |
| 345 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | 350 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 346 content::NotificationService::AllSources()); | 351 content::NotificationService::AllSources()); |
| 347 registrar_.Add(this, | 352 registrar_.Add(this, |
| 348 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 353 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 349 content::NotificationService::AllSources()); | 354 content::NotificationService::AllSources()); |
| 350 registrar_.Add(this, | 355 registrar_.Add(this, |
| 351 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 356 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 376 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) | 381 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) |
| 377 RefreshUI(); | 382 RefreshUI(); |
| 378 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) | 383 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) |
| 379 StopListeningNotifications(); | 384 StopListeningNotifications(); |
| 380 } | 385 } |
| 381 | 386 |
| 382 void InspectUI::StopListeningNotifications() | 387 void InspectUI::StopListeningNotifications() |
| 383 { | 388 { |
| 384 if (!observer_) | 389 if (!observer_) |
| 385 return; | 390 return; |
| 386 if (adb_bridge_) { | 391 adb_bridge_.reset(); |
| 387 adb_bridge_->Stop(); | |
| 388 adb_bridge_ = NULL; | |
| 389 } | |
| 390 observer_->InspectUIDestroyed(); | 392 observer_->InspectUIDestroyed(); |
| 391 observer_ = NULL; | 393 observer_ = NULL; |
| 392 registrar_.RemoveAll(); | 394 registrar_.RemoveAll(); |
| 393 } | 395 } |
| 394 | 396 |
| 395 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() { | 397 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() { |
| 396 content::WebUIDataSource* source = | 398 content::WebUIDataSource* source = |
| 397 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); | 399 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); |
| 398 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); | 400 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); |
| 399 source->AddResourcePath("inspect.js", IDR_INSPECT_JS); | 401 source->AddResourcePath("inspect.js", IDR_INSPECT_JS); |
| 400 source->SetDefaultResource(IDR_INSPECT_HTML); | 402 source->SetDefaultResource(IDR_INSPECT_HTML); |
| 401 source->SetRequestFilter(base::Bind(&InspectUI::WeakHandleRequestCallback, | 403 source->SetRequestFilter(base::Bind(&InspectUI::WeakHandleRequestCallback, |
| 402 weak_factory_.GetWeakPtr())); | 404 weak_factory_.GetWeakPtr())); |
| 403 return source; | 405 return source; |
| 404 } | 406 } |
| 405 | 407 |
| 406 bool InspectUI::HandleRequestCallback( | 408 bool InspectUI::HandleRequestCallback( |
| 407 const std::string& path, | 409 const std::string& path, |
| 408 const content::WebUIDataSource::GotDataCallback& callback) { | 410 const content::WebUIDataSource::GotDataCallback& callback) { |
| 409 if (path == kDataFile) | 411 if (path == kDataFile) |
| 410 return HandleDataRequestCallback(path, callback); | 412 return HandleDataRequestCallback(path, callback); |
| 411 if (path.find(kAdbDevices) == 0) | 413 if (path.find(kAdbPages) == 0) |
| 412 return HandleAdbDevicesCallback(path, callback); | 414 return HandleAdbPagesCallback(path, callback); |
| 413 if (path.find(kAdbQuery) == 0) | 415 if (path.find(kAdbQuery) == 0) |
| 414 return HandleAdbQueryCallback(path, callback); | 416 return HandleAdbQueryCallback(path, callback); |
| 415 if (path.find(kLocalXhr) == 0) | 417 if (path.find(kLocalXhr) == 0) |
| 416 return HandleLocalXhrCallback(path, callback); | 418 return HandleLocalXhrCallback(path, callback); |
| 417 return false; | 419 return false; |
| 418 } | 420 } |
| 419 | 421 |
| 420 bool InspectUI::HandleAdbQueryCallback( | 422 bool InspectUI::HandleAdbQueryCallback( |
| 421 const std::string& path, | 423 const std::string& path, |
| 422 const content::WebUIDataSource::GotDataCallback& callback) { | 424 const content::WebUIDataSource::GotDataCallback& callback) { |
| 423 std::string query = net::UnescapeURLComponent( | 425 std::string query = net::UnescapeURLComponent( |
| 424 path.substr(strlen(kAdbQuery)), | 426 path.substr(strlen(kAdbQuery)), |
| 425 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES | | 427 net::UnescapeRule::NORMAL | net::UnescapeRule::SPACES | |
| 426 net::UnescapeRule::URL_SPECIAL_CHARS); | 428 net::UnescapeRule::URL_SPECIAL_CHARS); |
| 427 adb_bridge_->Query(query, base::Bind(&InspectUI::RespondOnUIThread, | 429 adb_bridge_->Query(query, base::Bind(&InspectUI::RespondOnUIThread, |
| 428 weak_factory_.GetWeakPtr(), callback)); | 430 weak_factory_.GetWeakPtr(), callback)); |
| 429 return true; | 431 return true; |
| 430 } | 432 } |
| 431 | 433 |
| 432 bool InspectUI::HandleAdbDevicesCallback( | 434 bool InspectUI::HandleAdbPagesCallback( |
| 433 const std::string& path, | 435 const std::string& path, |
| 434 const content::WebUIDataSource::GotDataCallback& callback) { | 436 const content::WebUIDataSource::GotDataCallback& callback) { |
| 435 adb_bridge_->Devices(); | 437 adb_bridge_->Pages(base::Bind(&InspectUI::OnAdbPages, |
| 436 std::string json_string = ""; | 438 weak_factory_.GetWeakPtr(), |
| 437 callback.Run(base::RefCountedString::TakeString(&json_string)); | 439 callback)); |
| 438 return true; | 440 return true; |
| 439 } | 441 } |
| 440 | 442 |
| 443 void InspectUI::OnAdbPages( |
| 444 const content::WebUIDataSource::GotDataCallback& callback, |
| 445 int result, |
| 446 DevToolsAdbBridge::RemotePages* pages) { |
| 447 if (result != net::OK) |
| 448 return; |
| 449 ListValue targets; |
| 450 scoped_ptr<DevToolsAdbBridge::RemotePages> my_pages(pages); |
| 451 for (DevToolsAdbBridge::RemotePages::iterator it = my_pages->begin(); |
| 452 it != my_pages->end(); ++it) { |
| 453 DevToolsAdbBridge::RemotePage* page = it->get(); |
| 454 DictionaryValue* target_data = BuildTargetDescriptor(kAdbTargetType, |
| 455 false, GURL(page->url()), page->title(), GURL(page->favicon_url()), 0, |
| 456 0); |
| 457 target_data->SetString(kAdbSerialField, page->serial()); |
| 458 target_data->SetString(kAdbModelField, page->model()); |
| 459 target_data->SetString(kAdbPageIdField, page->id()); |
| 460 target_data->SetString(kAdbDebugUrl, page->debug_url()); |
| 461 targets.Append(target_data); |
| 462 } |
| 463 |
| 464 std::string json_string = ""; |
| 465 base::JSONWriter::Write(&targets, &json_string); |
| 466 callback.Run(base::RefCountedString::TakeString(&json_string)); |
| 467 } |
| 468 |
| 441 bool InspectUI::HandleLocalXhrCallback( | 469 bool InspectUI::HandleLocalXhrCallback( |
| 442 const std::string& path, | 470 const std::string& path, |
| 443 const content::WebUIDataSource::GotDataCallback& callback) { | 471 const content::WebUIDataSource::GotDataCallback& callback) { |
| 444 std::string url = "http://localhost:" + path.substr(strlen(kLocalXhr)); | 472 std::string url = "http://localhost:" + path.substr(strlen(kLocalXhr)); |
| 445 new ProtocolHttpRequest(Profile::FromWebUI(web_ui()), url, | 473 new ProtocolHttpRequest(Profile::FromWebUI(web_ui()), url, |
| 446 base::Bind(&InspectUI::RespondOnUIThread, | 474 base::Bind(&InspectUI::RespondOnUIThread, |
| 447 weak_factory_.GetWeakPtr(), callback)); | 475 weak_factory_.GetWeakPtr(), callback)); |
| 448 return true; | 476 return true; |
| 449 } | 477 } |
| 450 | 478 |
| 451 void InspectUI::RespondOnUIThread( | 479 void InspectUI::RespondOnUIThread( |
| 452 const content::WebUIDataSource::GotDataCallback& callback, | 480 const content::WebUIDataSource::GotDataCallback& callback, |
| 453 int result_code, | 481 int result_code, |
| 454 const std::string& response) { | 482 const std::string& response) { |
| 455 ListValue result; | 483 ListValue result; |
| 456 result.AppendInteger(result_code); | 484 result.AppendInteger(result_code); |
| 457 result.AppendString(response); | 485 result.AppendString(response); |
| 458 std::string json_string; | 486 std::string json_string; |
| 459 base::JSONWriter::Write(&result, &json_string); | 487 base::JSONWriter::Write(&result, &json_string); |
| 460 callback.Run(base::RefCountedString::TakeString(&json_string)); | 488 callback.Run(base::RefCountedString::TakeString(&json_string)); |
| 461 } | 489 } |
| OLD | NEW |