Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Side by Side Diff: chrome/browser/ui/webui/inspect_ui.cc

Issue 12559008: DevTools: add backend for discovering connected ADB devices. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Extracted protocol http request helper. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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,
(...skipping 12 matching lines...) Expand all
387 const content::NotificationSource& source, 379 const content::NotificationSource& source,
388 const content::NotificationDetails& details) { 380 const content::NotificationDetails& details) {
389 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) 381 if (source != content::Source<WebContents>(web_ui()->GetWebContents()))
390 RefreshUI(); 382 RefreshUI();
391 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) 383 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED)
392 StopListeningNotifications(); 384 StopListeningNotifications();
393 } 385 }
394 386
395 void InspectUI::StopListeningNotifications() 387 void InspectUI::StopListeningNotifications()
396 { 388 {
389 adb_bridge_->Stop();
390
397 if (!observer_) 391 if (!observer_)
398 return; 392 return;
399 observer_->InspectUIDestroyed(); 393 observer_->InspectUIDestroyed();
400 observer_ = NULL; 394 observer_ = NULL;
401 registrar_.RemoveAll(); 395 registrar_.RemoveAll();
402 } 396 }
397
398 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() {
399 content::WebUIDataSource* source =
400 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost);
401 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS);
402 source->AddResourcePath("inspect.js", IDR_INSPECT_JS);
403 source->SetDefaultResource(IDR_INSPECT_HTML);
404 source->SetRequestFilter(base::Bind(&InspectUI::HandleRequestCallback,
405 base::Unretained(this)));
406 return source;
407 }
408
409 bool InspectUI::HandleRequestCallback(
410 const std::string& path,
411 const content::WebUIDataSource::GotDataCallback& callback) {
412 if (path == kDataFile)
413 return HandleDataRequestCallback(path, callback);
414 if (path.find(kAdbQuery) == 0)
415 return HandleAdbQueryCallback(path, callback);
416 if (path.find(kLocalXhr) == 0)
417 return HandleLocalXhrCallback(path, callback);
418 return false;
419 }
420
421 bool InspectUI::HandleAdbQueryCallback(
422 const std::string& path,
423 const content::WebUIDataSource::GotDataCallback& callback) {
424 std::string query = path.substr(strlen(kAdbQuery));
425 adb_bridge_->Query(query, base::Bind(&InspectUI::RespondOnUIThread,
426 weak_factory_.GetWeakPtr(), callback));
427 return true;
428 }
429
430 bool InspectUI::HandleLocalXhrCallback(
431 const std::string& path,
432 const content::WebUIDataSource::GotDataCallback& callback) {
433 std::string url = "http://localhost:" + path.substr(strlen(kLocalXhr));
434 new ProtocolHttpRequest(Profile::FromWebUI(web_ui()), url,
435 base::Bind(&InspectUI::RespondOnUIThread,
436 weak_factory_.GetWeakPtr(), callback));
437 return true;
438 }
439
440 void InspectUI::RespondOnUIThread(
441 const content::WebUIDataSource::GotDataCallback& callback,
442 const std::string& error,
443 const std::string& data) {
444 ListValue result;
445 result.AppendString(error);
446 result.AppendString(data);
447 std::string json_string;
448 base::JSONWriter::Write(&result, &json_string);
449 callback.Run(base::RefCountedString::TakeString(&json_string));
450 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698