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

Side by Side Diff: chrome/browser/extensions/api/debugger/debugger_api.cc

Issue 12319114: Extract debugger target enumeration into a separate class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@debugger
Patch Set: Cleaner version ready for review 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
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 // Implements the Chrome Extensions Debugger API. 5 // Implements the Chrome Extensions Debugger API.
6 6
7 #include "chrome/browser/extensions/api/debugger/debugger_api.h" 7 #include "chrome/browser/extensions/api/debugger/debugger_api.h"
8 8
9 #include <map> 9 #include <map>
10 #include <set> 10 #include <set>
(...skipping 16 matching lines...) Expand all
27 #include "chrome/browser/extensions/extension_tab_util.h" 27 #include "chrome/browser/extensions/extension_tab_util.h"
28 #include "chrome/browser/infobars/infobar.h" 28 #include "chrome/browser/infobars/infobar.h"
29 #include "chrome/browser/profiles/profile.h" 29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" 30 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
31 #include "chrome/common/chrome_notification_types.h" 31 #include "chrome/common/chrome_notification_types.h"
32 #include "chrome/common/chrome_switches.h" 32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/extensions/extension.h" 33 #include "chrome/common/extensions/extension.h"
34 #include "content/public/browser/devtools_agent_host.h" 34 #include "content/public/browser/devtools_agent_host.h"
35 #include "content/public/browser/devtools_client_host.h" 35 #include "content/public/browser/devtools_client_host.h"
36 #include "content/public/browser/devtools_manager.h" 36 #include "content/public/browser/devtools_manager.h"
37 #include "content/public/browser/devtools_target_list.h"
37 #include "content/public/browser/notification_service.h" 38 #include "content/public/browser/notification_service.h"
38 #include "content/public/browser/notification_source.h" 39 #include "content/public/browser/notification_source.h"
39 #include "content/public/browser/render_process_host.h" 40 #include "content/public/browser/render_process_host.h"
40 #include "content/public/browser/render_view_host.h" 41 #include "content/public/browser/render_view_host.h"
41 #include "content/public/browser/render_widget_host.h" 42 #include "content/public/browser/render_widget_host.h"
42 #include "content/public/browser/web_contents.h" 43 #include "content/public/browser/web_contents.h"
43 #include "content/public/common/content_client.h" 44 #include "content/public/common/content_client.h"
44 #include "content/public/common/url_constants.h" 45 #include "content/public/common/url_constants.h"
45 #include "extensions/common/error_utils.h" 46 #include "extensions/common/error_utils.h"
46 #include "grit/generated_resources.h" 47 #include "grit/generated_resources.h"
47 #include "ui/base/l10n/l10n_util.h" 48 #include "ui/base/l10n/l10n_util.h"
48 #include "webkit/glue/webkit_glue.h" 49 #include "webkit/glue/webkit_glue.h"
49 50
50 using content::DevToolsAgentHost; 51 using content::DevToolsAgentHost;
51 using content::DevToolsClientHost; 52 using content::DevToolsClientHost;
52 using content::DevToolsManager; 53 using content::DevToolsManager;
54 using content::DevToolsTargetList;
53 using content::RenderProcessHost; 55 using content::RenderProcessHost;
54 using content::RenderViewHost; 56 using content::RenderViewHost;
55 using content::RenderWidgetHost; 57 using content::RenderWidgetHost;
56 using content::WebContents; 58 using content::WebContents;
57 using extensions::ErrorUtils; 59 using extensions::ErrorUtils;
58 60
59 namespace keys = debugger_api_constants; 61 namespace keys = debugger_api_constants;
60 namespace Attach = extensions::api::debugger::Attach; 62 namespace Attach = extensions::api::debugger::Attach;
61 namespace Detach = extensions::api::debugger::Detach; 63 namespace Detach = extensions::api::debugger::Detach;
62 namespace OnDetach = extensions::api::debugger::OnDetach; 64 namespace OnDetach = extensions::api::debugger::OnDetach;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 if (rvh && WebContents::FromRenderViewHost(rvh) == contents) 178 if (rvh && WebContents::FromRenderViewHost(rvh) == contents)
177 return static_cast<ExtensionDevToolsClientHost*>(*it); 179 return static_cast<ExtensionDevToolsClientHost*>(*it);
178 } 180 }
179 return NULL; 181 return NULL;
180 } 182 }
181 183
182 private: 184 private:
183 std::set<DevToolsClientHost*> client_hosts_; 185 std::set<DevToolsClientHost*> client_hosts_;
184 }; 186 };
185 187
188 static extensions::ExtensionHost* GetExtensionBackgroundHost(
189 WebContents* web_contents) {
190 Profile* profile =
191 Profile::FromBrowserContext(web_contents->GetBrowserContext());
192 if (!profile)
193 return NULL;
194
195 extensions::ExtensionHost* extension_host =
196 extensions::ExtensionSystem::Get(profile)->process_manager()->
197 GetBackgroundHostForExtension(web_contents->GetURL().host());
198
199 if (extension_host && extension_host->host_contents() == web_contents)
200 return extension_host;
201
202 return NULL;
203 }
204
205 static base::DictionaryValue* SerializeTargetInfo(DevToolsAgentHost* agent) {
206 base::DictionaryValue* dictionary = new base::DictionaryValue;
207
208 dictionary->SetString("id",
209 DevToolsTargetList::GetInstance()->GetIdentifier(agent));
210
211 WebContents* web_contents =
212 WebContents::FromRenderViewHost(agent->GetRenderViewHost());
213 extensions::ExtensionHost* extension_host =
214 GetExtensionBackgroundHost(web_contents);
215 if (extension_host) {
216 dictionary->SetString("type", "extension");
pfeldman 2013/03/01 08:49:30 So remote debugging won't know that it is extensio
Vladislav Kaznacheev 2013/03/01 12:58:35 When I was adding 'type' to the remote debugging p
217 dictionary->SetString("title", extension_host->extension()->name());
218 } else {
219 dictionary->SetString("type", "page");
220 dictionary->SetString("title", agent->title());
221 }
222
223 dictionary->SetBoolean("attached", agent->attached());
224 dictionary->SetString("url", agent->url().spec());
225 dictionary->SetString("thumbnailUrl", agent->thumbnail_url().spec());
226 dictionary->SetString("faviconUrl", agent->favicon_url().spec());
227
228 return dictionary;
229 }
230
186 } // namespace 231 } // namespace
187 232
188 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) { 233 static void CopyDebuggee(Debuggee & dst, const Debuggee& src) {
189 if (src.tab_id) 234 if (src.tab_id)
190 dst.tab_id.reset(new int(*src.tab_id)); 235 dst.tab_id.reset(new int(*src.tab_id));
191 if (src.extension_id) 236 if (src.extension_id)
192 dst.extension_id.reset(new std::string(*src.extension_id)); 237 dst.extension_id.reset(new std::string(*src.extension_id));
238 if (src.target_id)
239 dst.target_id.reset(new std::string(*src.target_id));
193 } 240 }
194 241
195 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost( 242 ExtensionDevToolsClientHost::ExtensionDevToolsClientHost(
196 WebContents* web_contents, 243 WebContents* web_contents,
197 const std::string& extension_id, 244 const std::string& extension_id,
198 const std::string& extension_name, 245 const std::string& extension_name,
199 const Debuggee& debuggee) 246 const Debuggee& debuggee)
200 : web_contents_(web_contents), 247 : web_contents_(web_contents),
201 extension_id_(extension_id), 248 extension_id_(extension_id),
202 last_request_id_(0), 249 last_request_id_(0),
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 DebuggerFunction::DebuggerFunction() 471 DebuggerFunction::DebuggerFunction()
425 : contents_(0), 472 : contents_(0),
426 client_host_(0) { 473 client_host_(0) {
427 } 474 }
428 475
429 void DebuggerFunction::FormatErrorMessage(const std::string& format) { 476 void DebuggerFunction::FormatErrorMessage(const std::string& format) {
430 error_ = ErrorUtils::FormatErrorMessage( 477 error_ = ErrorUtils::FormatErrorMessage(
431 format, 478 format,
432 debuggee_.tab_id ? 479 debuggee_.tab_id ?
433 keys::kTabTargetType : 480 keys::kTabTargetType :
434 keys::kExtensionTargetType, 481 debuggee_.extension_id ?
482 keys::kExtensionTargetType :
483 keys::kOpaqueTargetType,
435 debuggee_.tab_id ? 484 debuggee_.tab_id ?
436 base::IntToString(*debuggee_.tab_id) : 485 base::IntToString(*debuggee_.tab_id) :
437 *debuggee_.extension_id); 486 debuggee_.extension_id ?
487 *debuggee_.extension_id :
488 *debuggee_.target_id);
438 } 489 }
439 490
440 bool DebuggerFunction::InitWebContents() { 491 bool DebuggerFunction::InitWebContents() {
441 // Find the WebContents that contains this tab id. 492 // Find the WebContents that contains this tab id.
442 contents_ = NULL; 493 contents_ = NULL;
443 if (debuggee_.tab_id) { 494 if (debuggee_.tab_id) {
444 WebContents* web_contents = NULL; 495 WebContents* web_contents = NULL;
445 bool result = ExtensionTabUtil::GetTabById( 496 bool result = ExtensionTabUtil::GetTabById(
446 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL, 497 *debuggee_.tab_id, profile(), include_incognito(), NULL, NULL,
447 &web_contents, NULL); 498 &web_contents, NULL);
(...skipping 28 matching lines...) Expand all
476 if (host) { 527 if (host) {
477 contents_ = WebContents::FromRenderViewHost(host->render_view_host()); 528 contents_ = WebContents::FromRenderViewHost(host->render_view_host());
478 if (contents_) 529 if (contents_)
479 return true; 530 return true;
480 } 531 }
481 532
482 FormatErrorMessage(keys::kNoTargetError); 533 FormatErrorMessage(keys::kNoTargetError);
483 return false; 534 return false;
484 } 535 }
485 536
537 if (debuggee_.target_id) {
538 DevToolsAgentHost* agent_host =
539 DevToolsTargetList::GetInstance()->ForIdentifier(*debuggee_.target_id);
540 if (agent_host) {
541 contents_ = WebContents::FromRenderViewHost(
542 agent_host->GetRenderViewHost());
543
544 if (GetExtensionBackgroundHost(contents_) &&
545 !CommandLine::ForCurrentProcess()->
546 HasSwitch(switches::kSilentDebuggerExtensionAPI)) {
547 error_ = ErrorUtils::FormatErrorMessage(
548 keys::kSilentDebuggingRequired,
549 switches::kSilentDebuggerExtensionAPI);
550 return false;
551 }
552 return true;
553 }
554 FormatErrorMessage(keys::kNoTargetError);
555 return false;
556 }
557
486 error_ = keys::kInvalidTargetError; 558 error_ = keys::kInvalidTargetError;
487 return false; 559 return false;
488 } 560 }
489 561
490 bool DebuggerFunction::InitClientHost() { 562 bool DebuggerFunction::InitClientHost() {
491 if (!InitWebContents()) 563 if (!InitWebContents())
492 return false; 564 return false;
493 565
494 // Don't fetch rvh from the contents since it'll be wrong upon navigation. 566 // Don't fetch rvh from the contents since it'll be wrong upon navigation.
495 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_); 567 client_host_ = AttachedClientHosts::GetInstance()->Lookup(contents_);
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 } 657 }
586 658
587 DictionaryValue* result_body; 659 DictionaryValue* result_body;
588 SendCommand::Results::Result result; 660 SendCommand::Results::Result result;
589 if (response->GetDictionary("result", &result_body)) 661 if (response->GetDictionary("result", &result_body))
590 result.additional_properties.Swap(result_body); 662 result.additional_properties.Swap(result_body);
591 663
592 results_ = SendCommand::Results::Create(result); 664 results_ = SendCommand::Results::Create(result);
593 SendResponse(true); 665 SendResponse(true);
594 } 666 }
667
668 DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() {}
669
670 DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() {}
671
672 bool DebuggerGetTargetsFunction::RunImpl() {
673 base::ListValue* results_list = new ListValue();
674
675 DevToolsTargetList::AgentsMap& agents_map =
676 DevToolsTargetList::GetInstance()->GetAgentsMap();
677
678 for (DevToolsTargetList::iterator i = agents_map.begin();
679 i != agents_map.end(); ++i)
680 results_list->Append(SerializeTargetInfo(i->second));
681
682 SetResult(results_list);
683 SendResponse(true);
684 return true;
685 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698