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

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: Same with Fetch for fetching discovery pages. 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"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 using content::DevToolsManager; 51 using content::DevToolsManager;
52 using content::RenderProcessHost; 52 using content::RenderProcessHost;
53 using content::RenderViewHost; 53 using content::RenderViewHost;
54 using content::RenderViewHostDelegate; 54 using content::RenderViewHostDelegate;
55 using content::RenderWidgetHost; 55 using content::RenderWidgetHost;
56 using content::WebContents; 56 using content::WebContents;
57 using content::WebUIMessageHandler; 57 using content::WebUIMessageHandler;
58 using content::WorkerService; 58 using content::WorkerService;
59 using content::WorkerServiceObserver; 59 using content::WorkerServiceObserver;
60 60
61 namespace {
62
61 static const char kDataFile[] = "targets-data.json"; 63 static const char kDataFile[] = "targets-data.json";
64 static const char kAdbQuery[] = "adb-query/";
65 static const char kLocalXhr[] = "local-xhr/";
62 66
63 static const char kExtensionTargetType[] = "extension"; 67 static const char kExtensionTargetType[] = "extension";
64 static const char kPageTargetType[] = "page"; 68 static const char kPageTargetType[] = "page";
65 static const char kWorkerTargetType[] = "worker"; 69 static const char kWorkerTargetType[] = "worker";
66 70
67 static const char kInspectCommand[] = "inspect"; 71 static const char kInspectCommand[] = "inspect";
68 static const char kTerminateCommand[] = "terminate"; 72 static const char kTerminateCommand[] = "terminate";
69 73
70 static const char kTargetTypeField[] = "type"; 74 static const char kTargetTypeField[] = "type";
71 static const char kAttachedField[] = "attached"; 75 static const char kAttachedField[] = "attached";
72 static const char kProcessIdField[] = "processId"; 76 static const char kProcessIdField[] = "processId";
73 static const char kRouteIdField[] = "routeId"; 77 static const char kRouteIdField[] = "routeId";
74 static const char kUrlField[] = "url"; 78 static const char kUrlField[] = "url";
75 static const char kNameField[] = "name"; 79 static const char kNameField[] = "name";
76 static const char kFaviconUrlField[] = "favicon_url"; 80 static const char kFaviconUrlField[] = "favicon_url";
77 static const char kPidField[] = "pid"; 81 static const char kPidField[] = "pid";
78 82
79 namespace {
80
81 DictionaryValue* BuildTargetDescriptor( 83 DictionaryValue* BuildTargetDescriptor(
82 const std::string& target_type, 84 const std::string& target_type,
83 bool attached, 85 bool attached,
84 const GURL& url, 86 const GURL& url,
85 const std::string& name, 87 const std::string& name,
86 const GURL& favicon_url, 88 const GURL& favicon_url,
87 int process_id, 89 int process_id,
88 int route_id, 90 int route_id,
89 base::ProcessHandle handle = base::kNullProcessHandle) { 91 base::ProcessHandle handle = base::kNullProcessHandle) {
90 DictionaryValue* target_data = new DictionaryValue(); 92 DictionaryValue* target_data = new DictionaryValue();
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 worker_info[i].route_id, 164 worker_info[i].route_id,
163 worker_info[i].handle)); 165 worker_info[i].handle));
164 } 166 }
165 167
166 std::string json_string; 168 std::string json_string;
167 base::JSONWriter::Write(rvh_list, &json_string); 169 base::JSONWriter::Write(rvh_list, &json_string);
168 170
169 callback.Run(base::RefCountedString::TakeString(&json_string)); 171 callback.Run(base::RefCountedString::TakeString(&json_string));
170 } 172 }
171 173
172 bool HandleRequestCallback( 174 bool HandleDataRequestCallback(
173 const std::string& path, 175 const std::string& path,
174 const content::WebUIDataSource::GotDataCallback& callback) { 176 const content::WebUIDataSource::GotDataCallback& callback) {
175 if (path != kDataFile)
176 return false;
177
178 std::set<RenderViewHost*> tab_rvhs; 177 std::set<RenderViewHost*> tab_rvhs;
179 for (TabContentsIterator it; !it.done(); it.Next()) 178 for (TabContentsIterator it; !it.done(); it.Next())
180 tab_rvhs.insert(it->GetRenderViewHost()); 179 tab_rvhs.insert(it->GetRenderViewHost());
181 180
182 scoped_ptr<ListValue> rvh_list(new ListValue()); 181 scoped_ptr<ListValue> rvh_list(new ListValue());
183 182
184 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); 183 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
185 !it.IsAtEnd(); it.Advance()) { 184 !it.IsAtEnd(); it.Advance()) {
186 RenderProcessHost* render_process_host = it.GetCurrentValue(); 185 RenderProcessHost* render_process_host = it.GetCurrentValue();
187 DCHECK(render_process_host); 186 DCHECK(render_process_host);
(...skipping 18 matching lines...) Expand all
206 } 205 }
207 } 206 }
208 207
209 BrowserThread::PostTask( 208 BrowserThread::PostTask(
210 BrowserThread::IO, 209 BrowserThread::IO,
211 FROM_HERE, 210 FROM_HERE,
212 base::Bind(&SendDescriptors, base::Owned(rvh_list.release()), callback)); 211 base::Bind(&SendDescriptors, base::Owned(rvh_list.release()), callback));
213 return true; 212 return true;
214 } 213 }
215 214
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 { 215 class InspectMessageHandler : public WebUIMessageHandler {
227 public: 216 public:
228 InspectMessageHandler() {} 217 InspectMessageHandler() {}
229 virtual ~InspectMessageHandler() {} 218 virtual ~InspectMessageHandler() {}
230 219
231 private: 220 private:
232 // WebUIMessageHandler implementation. 221 // WebUIMessageHandler implementation.
233 virtual void RegisterMessages() OVERRIDE; 222 virtual void RegisterMessages() OVERRIDE;
234 223
235 // Callback for "openDevTools" message. 224 // Callback for "openDevTools" message.
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
355 344
356 InspectUI* discovery_ui_; 345 InspectUI* discovery_ui_;
357 }; 346 };
358 347
359 InspectUI::InspectUI(content::WebUI* web_ui) 348 InspectUI::InspectUI(content::WebUI* web_ui)
360 : WebUIController(web_ui), 349 : WebUIController(web_ui),
361 observer_(new WorkerCreationDestructionListener(this)) { 350 observer_(new WorkerCreationDestructionListener(this)) {
362 web_ui->AddMessageHandler(new InspectMessageHandler()); 351 web_ui->AddMessageHandler(new InspectMessageHandler());
363 352
364 Profile* profile = Profile::FromWebUI(web_ui); 353 Profile* profile = Profile::FromWebUI(web_ui);
354 adb_bridge_ = DevToolsAdbBridge::Start(profile);
365 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource()); 355 content::WebUIDataSource::Add(profile, CreateInspectUIHTMLSource());
366 356
367 registrar_.Add(this, 357 registrar_.Add(this,
368 content::NOTIFICATION_WEB_CONTENTS_CONNECTED, 358 content::NOTIFICATION_WEB_CONTENTS_CONNECTED,
369 content::NotificationService::AllSources()); 359 content::NotificationService::AllSources());
370 registrar_.Add(this, 360 registrar_.Add(this,
371 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, 361 content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED,
372 content::NotificationService::AllSources()); 362 content::NotificationService::AllSources());
373 registrar_.Add(this, 363 registrar_.Add(this,
374 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, 364 content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
(...skipping 12 matching lines...) Expand all
387 const content::NotificationSource& source, 377 const content::NotificationSource& source,
388 const content::NotificationDetails& details) { 378 const content::NotificationDetails& details) {
389 if (source != content::Source<WebContents>(web_ui()->GetWebContents())) 379 if (source != content::Source<WebContents>(web_ui()->GetWebContents()))
390 RefreshUI(); 380 RefreshUI();
391 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) 381 else if (type == content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED)
392 StopListeningNotifications(); 382 StopListeningNotifications();
393 } 383 }
394 384
395 void InspectUI::StopListeningNotifications() 385 void InspectUI::StopListeningNotifications()
396 { 386 {
387 adb_bridge_->Stop();
yurys 2013/03/11 07:21:21 Should it be destroyed like observer_ ?
388
397 if (!observer_) 389 if (!observer_)
398 return; 390 return;
399 observer_->InspectUIDestroyed(); 391 observer_->InspectUIDestroyed();
400 observer_ = NULL; 392 observer_ = NULL;
401 registrar_.RemoveAll(); 393 registrar_.RemoveAll();
402 } 394 }
395
396 content::WebUIDataSource* InspectUI::CreateInspectUIHTMLSource() {
397 content::WebUIDataSource* source =
398 content::WebUIDataSource::Create(chrome::kChromeUIInspectHost);
399 source->AddResourcePath("inspect.css", IDR_INSPECT_CSS);
400 source->AddResourcePath("inspect.js", IDR_INSPECT_JS);
401 source->SetDefaultResource(IDR_INSPECT_HTML);
402 source->SetRequestFilter(base::Bind(&InspectUI::HandleRequestCallback,
403 base::Unretained(this)));
404 return source;
405 }
406
407 bool InspectUI::HandleRequestCallback(
408 const std::string& path,
409 const content::WebUIDataSource::GotDataCallback& callback) {
410 if (path == kDataFile)
411 return HandleDataRequestCallback(path, callback);
412 if (path.find(kAdbQuery) == 0)
413 return HandleAdbQueryCallback(path, callback);
414 if (path.find(kLocalXhr) == 0)
415 return HandleLocalXhrCallback(path, callback);
416 return false;
417 }
418
419 bool InspectUI::HandleAdbQueryCallback(
420 const std::string& path,
421 const content::WebUIDataSource::GotDataCallback& callback) {
422 std::string query = path.substr(strlen(kAdbQuery));
423 adb_bridge_->Query(query, base::Bind(&InspectUI::OnAdbResponse,
424 base::Unretained(this), callback));
425 return true;
426 }
427
428 bool InspectUI::HandleLocalXhrCallback(
429 const std::string& path,
430 const content::WebUIDataSource::GotDataCallback& callback) {
431 std::string url = "http://localhost:" + path.substr(strlen(kLocalXhr));
432 adb_bridge_->Fetch(url, base::Bind(&InspectUI::OnAdbResponse,
433 base::Unretained(this), callback));
434 return true;
435 }
436
437 void InspectUI::OnAdbResponse(
438 const content::WebUIDataSource::GotDataCallback& callback,
439 const std::string& error,
440 const std::string& data) {
441 ListValue result;
442 result.AppendString(error);
443 result.AppendString(data);
444 std::string json_string;
445 base::JSONWriter::Write(&result, &json_string);
446 callback.Run(base::RefCountedString::TakeString(&json_string));
447 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698