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

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, addressed comments 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 37743ceb6f9ef86cc413cbbe54a02aec32881725..80f0fa4792eac097dbe8e6f1bd9c6603e24a9b0f 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,69 @@ 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 const char kTargetIdField[] = "id";
+static const char kTargetTypeField[] = "type";
+static const char kTargetTitleField[] = "title";
+static const char kTargetAttachedField[] = "attached";
+static const char kTargetUrlField[] = "url";
+static const char kTargetFaviconUrlField[] = "favicon_url";
+
+static const char kTargetTypePage[] = "page";
+static const char kTargetTypeExtension[] = "extension";
+
+static base::DictionaryValue* SerializePageInfo(RenderViewHost* rvh) {
+ DevToolsAgentHost* agent_host = DevToolsAgentHost::GetOrCreateFor(rvh);
+
+ base::DictionaryValue* dictionary = new base::DictionaryValue();
+
+ dictionary->SetString(kTargetIdField, agent_host->GetId());
+
+ WebContents* web_contents = WebContents::FromRenderViewHost(rvh);
+ extensions::ExtensionHost* extension_host =
+ GetExtensionBackgroundHost(web_contents);
Matt Perry 2013/03/18 19:53:44 This is a very roundabout way to get the extension
Vladislav Kaznacheev 2013/03/19 05:56:23 It is not only only about the name. I need to know
Matt Perry 2013/03/19 18:02:01 OK, so you really do only want this to apply to ba
Vladislav Kaznacheev 2013/03/20 07:20:49 Done.
+ if (extension_host) {
+ dictionary->SetString(kTargetTypeField, kTargetTypeExtension);
+ dictionary->SetString(kTargetTitleField,
+ extension_host->extension()->name());
+ } else {
+ dictionary->SetString(kTargetTypeField, kTargetTypePage);
+ if (web_contents)
+ dictionary->SetString(kTargetTitleField,
+ UTF16ToUTF8(net::EscapeForHTML(web_contents->GetTitle())));
+ }
+
+ dictionary->SetBoolean(kTargetAttachedField,
+ !!DevToolsManager::GetInstance()->GetDevToolsClientHostFor(agent_host));
+ if (web_contents) {
Matt Perry 2013/03/18 19:53:44 Can web_contents actually be NULL? You use it in G
Vladislav Kaznacheev 2013/03/19 05:56:23 You are right. This came from inspect_ui.cc where
+ dictionary->SetString(kTargetUrlField, web_contents->GetURL().spec());
+ content::NavigationController& controller = web_contents->GetController();
+ content::NavigationEntry* entry = controller.GetActiveEntry();
+ if (entry != NULL && entry->GetURL().is_valid()) {
+ dictionary->SetString(kTargetFaviconUrlField,
+ entry->GetFavicon().url.spec());
+ }
+ }
+
+ return dictionary;
+}
+
} // namespace
static void CopyDebuggee(Debuggee & dst, const Debuggee& src) {
@@ -190,6 +255,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(
@@ -427,14 +494,15 @@ DebuggerFunction::DebuggerFunction()
}
void DebuggerFunction::FormatErrorMessage(const std::string& format) {
- error_ = ErrorUtils::FormatErrorMessage(
- format,
- debuggee_.tab_id ?
- keys::kTabTargetType :
- keys::kExtensionTargetType,
- debuggee_.tab_id ?
- base::IntToString(*debuggee_.tab_id) :
- *debuggee_.extension_id);
+ if (debuggee_.tab_id)
+ error_ = ErrorUtils::FormatErrorMessage(
+ format, keys::kTabTargetType, base::IntToString(*debuggee_.tab_id));
+ else if (debuggee_.extension_id)
+ error_ = ErrorUtils::FormatErrorMessage(
+ format, keys::kExtensionTargetType, *debuggee_.extension_id);
+ else
+ error_ = ErrorUtils::FormatErrorMessage(
+ format, keys::kOpaqueTargetType, *debuggee_.target_id);
}
bool DebuggerFunction::InitWebContents() {
@@ -483,6 +551,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.
Matt Perry 2013/03/18 19:53:44 Why? And what about popups or extension pages with
Vladislav Kaznacheev 2013/03/19 05:56:23 When an extension attaches to the page using chrom
Matt Perry 2013/03/19 18:02:01 Might this apply to extension popups as well? If n
Vladislav Kaznacheev 2013/03/20 07:20:49 Yes, I missed that. I have refactored the infobar
+ 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 +683,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;
+}
« no previous file with comments | « chrome/browser/extensions/api/debugger/debugger_api.h ('k') | chrome/browser/extensions/api/debugger/debugger_api_constants.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698