| 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" |
| 11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "chrome/browser/devtools/devtools_window.h" | 17 #include "chrome/browser/devtools/devtools_window.h" |
| 18 #include "chrome/browser/devtools/protocol_http_request.h" |
| 18 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 19 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 21 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 21 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
| 22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 23 #include "content/public/browser/child_process_data.h" | 24 #include "content/public/browser/child_process_data.h" |
| 24 #include "content/public/browser/devtools_agent_host.h" | 25 #include "content/public/browser/devtools_agent_host.h" |
| 25 #include "content/public/browser/devtools_client_host.h" | 26 #include "content/public/browser/devtools_client_host.h" |
| 26 #include "content/public/browser/devtools_manager.h" | 27 #include "content/public/browser/devtools_manager.h" |
| 27 #include "content/public/browser/favicon_status.h" | 28 #include "content/public/browser/favicon_status.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 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 |
| 62 namespace { |
| 63 |
| 61 static const char kDataFile[] = "targets-data.json"; | 64 static const char kDataFile[] = "targets-data.json"; |
| 65 static const char kAdbQuery[] = "adb-query/"; |
| 66 static const char kLocalXhr[] = "local-xhr/"; |
| 62 | 67 |
| 63 static const char kExtensionTargetType[] = "extension"; | 68 static const char kExtensionTargetType[] = "extension"; |
| 64 static const char kPageTargetType[] = "page"; | 69 static const char kPageTargetType[] = "page"; |
| 65 static const char kWorkerTargetType[] = "worker"; | 70 static const char kWorkerTargetType[] = "worker"; |
| 66 | 71 |
| 67 static const char kInspectCommand[] = "inspect"; | 72 static const char kInspectCommand[] = "inspect"; |
| 68 static const char kTerminateCommand[] = "terminate"; | 73 static const char kTerminateCommand[] = "terminate"; |
| 69 | 74 |
| 70 static const char kTargetTypeField[] = "type"; | 75 static const char kTargetTypeField[] = "type"; |
| 71 static const char kAttachedField[] = "attached"; | 76 static const char kAttachedField[] = "attached"; |
| 72 static const char kProcessIdField[] = "processId"; | 77 static const char kProcessIdField[] = "processId"; |
| 73 static const char kRouteIdField[] = "routeId"; | 78 static const char kRouteIdField[] = "routeId"; |
| 74 static const char kUrlField[] = "url"; | 79 static const char kUrlField[] = "url"; |
| 75 static const char kNameField[] = "name"; | 80 static const char kNameField[] = "name"; |
| 76 static const char kFaviconUrlField[] = "favicon_url"; | 81 static const char kFaviconUrlField[] = "favicon_url"; |
| 77 static const char kPidField[] = "pid"; | 82 static const char kPidField[] = "pid"; |
| 78 | 83 |
| 79 namespace { | |
| 80 | |
| 81 DictionaryValue* BuildTargetDescriptor( | 84 DictionaryValue* BuildTargetDescriptor( |
| 82 const std::string& target_type, | 85 const std::string& target_type, |
| 83 bool attached, | 86 bool attached, |
| 84 const GURL& url, | 87 const GURL& url, |
| 85 const std::string& name, | 88 const std::string& name, |
| 86 const GURL& favicon_url, | 89 const GURL& favicon_url, |
| 87 int process_id, | 90 int process_id, |
| 88 int route_id, | 91 int route_id, |
| 89 base::ProcessHandle handle = base::kNullProcessHandle) { | 92 base::ProcessHandle handle = base::kNullProcessHandle) { |
| 90 DictionaryValue* target_data = new DictionaryValue(); | 93 DictionaryValue* target_data = new DictionaryValue(); |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 worker_info[i].route_id, | 165 worker_info[i].route_id, |
| 163 worker_info[i].handle)); | 166 worker_info[i].handle)); |
| 164 } | 167 } |
| 165 | 168 |
| 166 std::string json_string; | 169 std::string json_string; |
| 167 base::JSONWriter::Write(rvh_list, &json_string); | 170 base::JSONWriter::Write(rvh_list, &json_string); |
| 168 | 171 |
| 169 callback.Run(base::RefCountedString::TakeString(&json_string)); | 172 callback.Run(base::RefCountedString::TakeString(&json_string)); |
| 170 } | 173 } |
| 171 | 174 |
| 172 bool HandleRequestCallback( | 175 bool HandleDataRequestCallback( |
| 173 const std::string& path, | 176 const std::string& path, |
| 174 const content::WebUIDataSource::GotDataCallback& callback) { | 177 const content::WebUIDataSource::GotDataCallback& callback) { |
| 175 if (path != kDataFile) | |
| 176 return false; | |
| 177 | |
| 178 std::set<RenderViewHost*> tab_rvhs; | 178 std::set<RenderViewHost*> tab_rvhs; |
| 179 for (TabContentsIterator it; !it.done(); it.Next()) | 179 for (TabContentsIterator it; !it.done(); it.Next()) |
| 180 tab_rvhs.insert(it->GetRenderViewHost()); | 180 tab_rvhs.insert(it->GetRenderViewHost()); |
| 181 | 181 |
| 182 scoped_ptr<ListValue> rvh_list(new ListValue()); | 182 scoped_ptr<ListValue> rvh_list(new ListValue()); |
| 183 | 183 |
| 184 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | 184 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 185 !it.IsAtEnd(); it.Advance()) { | 185 !it.IsAtEnd(); it.Advance()) { |
| 186 RenderProcessHost* render_process_host = it.GetCurrentValue(); | 186 RenderProcessHost* render_process_host = it.GetCurrentValue(); |
| 187 DCHECK(render_process_host); | 187 DCHECK(render_process_host); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 206 } | 206 } |
| 207 } | 207 } |
| 208 | 208 |
| 209 BrowserThread::PostTask( | 209 BrowserThread::PostTask( |
| 210 BrowserThread::IO, | 210 BrowserThread::IO, |
| 211 FROM_HERE, | 211 FROM_HERE, |
| 212 base::Bind(&SendDescriptors, base::Owned(rvh_list.release()), callback)); | 212 base::Bind(&SendDescriptors, base::Owned(rvh_list.release()), callback)); |
| 213 return true; | 213 return true; |
| 214 } | 214 } |
| 215 | 215 |
| 216 content::WebUIDataSource* CreateInspectUIHTMLSource() { | |
| 217 content::WebUIDataSource* source = | |
| 218 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); | |
| 219 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); | |
| 220 source->AddResourcePath("inspect.js", IDR_INSPECT_JS); | |
| 221 source->SetDefaultResource(IDR_INSPECT_HTML); | |
| 222 source->SetRequestFilter(base::Bind(&HandleRequestCallback)); | |
| 223 return source; | |
| 224 } | |
| 225 | |
| 226 class InspectMessageHandler : public WebUIMessageHandler { | 216 class InspectMessageHandler : public WebUIMessageHandler { |
| 227 public: | 217 public: |
| 228 InspectMessageHandler() {} | 218 InspectMessageHandler() {} |
| 229 virtual ~InspectMessageHandler() {} | 219 virtual ~InspectMessageHandler() {} |
| 230 | 220 |
| 231 private: | 221 private: |
| 232 // WebUIMessageHandler implementation. | 222 // WebUIMessageHandler implementation. |
| 233 virtual void RegisterMessages() OVERRIDE; | 223 virtual void RegisterMessages() OVERRIDE; |
| 234 | 224 |
| 235 // Callback for "openDevTools" message. | 225 // Callback for "openDevTools" message. |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 | 341 |
| 352 void UnregisterObserver() { | 342 void UnregisterObserver() { |
| 353 WorkerService::GetInstance()->RemoveObserver(this); | 343 WorkerService::GetInstance()->RemoveObserver(this); |
| 354 } | 344 } |
| 355 | 345 |
| 356 InspectUI* discovery_ui_; | 346 InspectUI* discovery_ui_; |
| 357 }; | 347 }; |
| 358 | 348 |
| 359 InspectUI::InspectUI(content::WebUI* web_ui) | 349 InspectUI::InspectUI(content::WebUI* web_ui) |
| 360 : WebUIController(web_ui), | 350 : WebUIController(web_ui), |
| 361 observer_(new WorkerCreationDestructionListener(this)) { | 351 observer_(new WorkerCreationDestructionListener(this)), |
| 352 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 362 web_ui->AddMessageHandler(new InspectMessageHandler()); | 353 web_ui->AddMessageHandler(new InspectMessageHandler()); |
| 363 | 354 |
| 364 Profile* profile = Profile::FromWebUI(web_ui); | 355 Profile* profile = Profile::FromWebUI(web_ui); |
| 356 adb_bridge_ = DevToolsAdbBridge::Start(); |
| 365 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); | 357 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); |
| 366 | 358 |
| 367 registrar_.Add(this, | 359 registrar_.Add(this, |
| 368 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, | 360 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, |
| 369 content::NotificationService::AllSources()); | 361 content::NotificationService::AllSources()); |
| 370 registrar_.Add(this, | 362 registrar_.Add(this, |
| 371 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 363 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 372 content::NotificationService::AllSources()); | 364 content::NotificationService::AllSources()); |
| 373 registrar_.Add(this, | 365 registrar_.Add(this, |
| 374 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 366 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 375 content::NotificationService::AllSources()); | 367 content::NotificationService::AllSources()); |
| 376 } | 368 } |
| 377 | 369 |
| 378 InspectUI::~InspectUI() { | 370 InspectUI::~InspectUI() { |
| 379 StopListeningNotifications(); | 371 StopListeningNotifications(); |
| 380 } | 372 } |
| 381 | 373 |
| 382 void InspectUI::RefreshUI() { | 374 void InspectUI::RefreshUI() { |
| 383 web_ui()->CallJavascriptFunction("populateLists"); | 375 web_ui()->CallJavascriptFunction("populateLists"); |
| 384 } | 376 } |
| 385 | 377 |
| 378 // static |
| 379 bool InspectUI::WeakHandleRequestCallback( |
| 380 const base::WeakPtr<InspectUI>& inspect_ui, |
| 381 const std::string& path, |
| 382 const content::WebUIDataSource::GotDataCallback& callback) { |
| 383 if (!inspect_ui.get()) |
| 384 return false; |
| 385 return inspect_ui->HandleRequestCallback(path, callback); |
| 386 } |
| 387 |
| 386 void InspectUI::Observe(int type, | 388 void InspectUI::Observe(int type, |
| 387 const content::NotificationSource& source, | 389 const content::NotificationSource& source, |
| 388 const content::NotificationDetails& details) { | 390 const content::NotificationDetails& details) { |
| 389 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) | 391 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) |
| 390 RefreshUI(); | 392 RefreshUI(); |
| 391 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) | 393 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) |
| 392 StopListeningNotifications(); | 394 StopListeningNotifications(); |
| 393 } | 395 } |
| 394 | 396 |
| 395 void InspectUI::StopListeningNotifications() | 397 void InspectUI::StopListeningNotifications() |
| 396 { | 398 { |
| 397 if (!observer_) | 399 if (!observer_) |
| 398 return; | 400 return; |
| 401 if (adb_bridge_) { |
| 402 adb_bridge_->Stop(); |
| 403 adb_bridge_ = NULL; |
| 404 } |
| 399 observer_->InspectUIDestroyed(); | 405 observer_->InspectUIDestroyed(); |
| 400 observer_ = NULL; | 406 observer_ = NULL; |
| 401 registrar_.RemoveAll(); | 407 registrar_.RemoveAll(); |
| 402 } | 408 } |
| 409 |
| 410 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() { |
| 411 content::WebUIDataSource* source = |
| 412 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost); |
| 413 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS); |
| 414 source->AddResourcePath("inspect.js", IDR_INSPECT_JS); |
| 415 source->SetDefaultResource(IDR_INSPECT_HTML); |
| 416 source->SetRequestFilter(base::Bind(&InspectUI::WeakHandleRequestCallback, |
| 417 weak_factory_.GetWeakPtr())); |
| 418 return source; |
| 419 } |
| 420 |
| 421 bool InspectUI::HandleRequestCallback( |
| 422 const std::string& path, |
| 423 const content::WebUIDataSource::GotDataCallback& callback) { |
| 424 if (path == kDataFile) |
| 425 return HandleDataRequestCallback(path, callback); |
| 426 if (path.find(kAdbQuery) == 0) |
| 427 return HandleAdbQueryCallback(path, callback); |
| 428 if (path.find(kLocalXhr) == 0) |
| 429 return HandleLocalXhrCallback(path, callback); |
| 430 return false; |
| 431 } |
| 432 |
| 433 bool InspectUI::HandleAdbQueryCallback( |
| 434 const std::string& path, |
| 435 const content::WebUIDataSource::GotDataCallback& callback) { |
| 436 std::string query = path.substr(strlen(kAdbQuery)); |
| 437 adb_bridge_->Query(query, base::Bind(&InspectUI::RespondOnUIThread, |
| 438 weak_factory_.GetWeakPtr(), callback)); |
| 439 return true; |
| 440 } |
| 441 |
| 442 bool InspectUI::HandleLocalXhrCallback( |
| 443 const std::string& path, |
| 444 const content::WebUIDataSource::GotDataCallback& callback) { |
| 445 std::string url = "http://localhost:" + path.substr(strlen(kLocalXhr)); |
| 446 new ProtocolHttpRequest(Profile::FromWebUI(web_ui()), url, |
| 447 base::Bind(&InspectUI::RespondOnUIThread, |
| 448 weak_factory_.GetWeakPtr(), callback)); |
| 449 return true; |
| 450 } |
| 451 |
| 452 void InspectUI::RespondOnUIThread( |
| 453 const content::WebUIDataSource::GotDataCallback& callback, |
| 454 const std::string& error, |
| 455 const std::string& data) { |
| 456 ListValue result; |
| 457 result.AppendString(error); |
| 458 result.AppendString(data); |
| 459 std::string json_string; |
| 460 base::JSONWriter::Write(&result, &json_string); |
| 461 callback.Run(base::RefCountedString::TakeString(&json_string)); |
| 462 } |
| OLD | NEW |