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

Unified Diff: chrome/browser/extensions/api/debugger/debugger_api.cc

Issue 12377047: Add chrome.debugger.getTargets method to discover available debug targets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@targets
Patch Set: Rebased 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/debugger/debugger_api.cc
diff --git a/chrome/browser/extensions/api/debugger/debugger_api.cc b/chrome/browser/extensions/api/debugger/debugger_api.cc
index 9d1da2c6de3a6153cb44d712bc9bf94fc3d9b800..3cc8af1243bca81079b9620a3f9641882d428f28 100644
--- a/chrome/browser/extensions/api/debugger/debugger_api.cc
+++ b/chrome/browser/extensions/api/debugger/debugger_api.cc
@@ -34,6 +34,8 @@
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_client_host.h"
#include "content/public/browser/devtools_manager.h"
+#include "content/public/browser/favicon_status.h"
+#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/render_process_host.h"
@@ -183,6 +185,57 @@ class AttachedClientHosts {
std::set<DevToolsClientHost*> client_hosts_;
};
+static extensions::ExtensionHost* GetExtensionBackgroundHost(
+ WebContents* web_contents) {
+ Profile* profile =
+ Profile::FromBrowserContext(web_contents->GetBrowserContext());
+ if (!profile)
+ return NULL;
+
+ extensions::ExtensionHost* extension_host =
+ extensions::ExtensionSystem::Get(profile)->process_manager()->
+ GetBackgroundHostForExtension(web_contents->GetURL().host());
+
+ if (extension_host && extension_host->host_contents() == web_contents)
+ return extension_host;
+
+ return NULL;
+}
+
+static base::DictionaryValue* SerializePageInfo(RenderViewHost* rvh) {
+ DevToolsAgentHost* agent_host = DevToolsAgentHost::GetFor(rvh);
+
+ base::DictionaryValue* dictionary = new base::DictionaryValue;
pfeldman 2013/03/15 11:25:45 new base::DictionaryValue()
Vladislav Kaznacheev 2013/03/18 06:40:36 Done.
+
+ dictionary->SetString("id", agent_host->GetId());
pfeldman 2013/03/15 11:25:45 You should use constants for these.
Vladislav Kaznacheev 2013/03/18 06:40:37 Done.
+
+ WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
+ extensions::ExtensionHost* extension_host =
+ GetExtensionBackgroundHost(web_contents);
+ if (extension_host) {
+ dictionary->SetString("type", "extension");
pfeldman 2013/03/15 11:25:45 I don't think we are ready to finalize this field.
Vladislav Kaznacheev 2013/03/18 06:40:37 Do you mean you prefer this called "other"? Please
+ dictionary->SetString("title", extension_host->extension()->name());
+ } else {
+ dictionary->SetString("type", "page");
+ if (web_contents)
+ dictionary->SetString("title",
+ UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle())));
+ }
+
+ dictionary->SetBoolean("attached",
+ !!DevToolsManager::GetInstance()->GetDevToolsClientHostFor(agent_host));
+ if (web_contents) {
+ dictionary->SetString("url", web_contents->GetURL().spec());
+ content::NavigationController& controller = web_contents->GetController();
+ content::NavigationEntry* entry = controller.GetActiveEntry();
+ if (entry != NULL && entry->GetURL().is_valid()) {
+ dictionary->SetString("faviconUrl", entry->GetFavicon().url.spec());
+ }
+ }
+
+ return dictionary;
+}
+
} // namespace
static void CopyDebuggee(Debuggee & dst, const Debuggee& src) {
@@ -190,6 +243,8 @@ static void CopyDebuggee(Debuggee & dst, const Debuggee& src) {
dst.tab_id.reset(new int(*src.tab_id));
if (src.extension_id)
dst.extension_id.reset(new std::string(*src.extension_id));
+ if (src.target_id)
+ dst.target_id.reset(new std::string(*src.target_id));
}
ExtensionDevToolsClientHost::ExtensionDevToolsClientHost(
@@ -431,10 +486,14 @@ void DebuggerFunction::FormatErrorMessage(const std::string& format) {
format,
debuggee_.tab_id ?
keys::kTabTargetType :
- keys::kExtensionTargetType,
+ debuggee_.extension_id ?
pfeldman 2013/03/15 11:25:45 Consider extracting this.
Vladislav Kaznacheev 2013/03/18 06:40:37 Done.
+ keys::kExtensionTargetType :
+ keys::kOpaqueTargetType,
debuggee_.tab_id ?
base::IntToString(*debuggee_.tab_id) :
- *debuggee_.extension_id);
+ debuggee_.extension_id ?
+ *debuggee_.extension_id :
+ *debuggee_.target_id);
}
bool DebuggerFunction::InitWebContents() {
@@ -483,6 +542,29 @@ bool DebuggerFunction::InitWebContents() {
return false;
}
+ if (debuggee_.target_id) {
+ DevToolsAgentHost* agent_host =
+ DevToolsAgentHost::GetForId(*debuggee_.target_id);
+ if (agent_host) {
+ contents_ = WebContents::FromRenderViewHost(
+ agent_host->GetRenderViewHost());
+
+ if (!CommandLine::ForCurrentProcess()->
+ HasSwitch(switches::kSilentDebuggerExtensionAPI)) {
+ // Allow only tabs, reject background pages.
+ if (GetExtensionBackgroundHost(contents_)) {
+ error_ = ErrorUtils::FormatErrorMessage(
+ keys::kSilentDebuggingRequired,
+ switches::kSilentDebuggerExtensionAPI);
+ return false;
+ }
+ }
+ return true;
+ }
+ FormatErrorMessage(keys::kNoTargetError);
+ return false;
+ }
+
error_ = keys::kInvalidTargetError;
return false;
}
@@ -592,3 +674,21 @@ void DebuggerSendCommandFunction::SendResponseBody(
results_ = SendCommand::Results::Create(result);
SendResponse(true);
}
+
+DebuggerGetTargetsFunction::DebuggerGetTargetsFunction() {}
+
+DebuggerGetTargetsFunction::~DebuggerGetTargetsFunction() {}
+
+bool DebuggerGetTargetsFunction::RunImpl() {
+ base::ListValue* results_list = new ListValue();
+
+ std::vector<RenderViewHost*> rvh_list =
+ DevToolsAgentHost::GetValidRenderViewHosts();
+ for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
+ it != rvh_list.end(); ++it)
+ results_list->Append(SerializePageInfo(*it));
+
+ SetResult(results_list);
+ SendResponse(true);
+ return true;
+}

Powered by Google App Engine
This is Rietveld 408576698