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

Unified Diff: content/browser/devtools/devtools_http_handler_impl.cc

Issue 12319114: Extract debugger target enumeration into a separate class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@debugger
Patch Set: Rebase 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: content/browser/devtools/devtools_http_handler_impl.cc
diff --git a/content/browser/devtools/devtools_http_handler_impl.cc b/content/browser/devtools/devtools_http_handler_impl.cc
index 461c51f4bc6f186b47784044810f387d620fdb62..3106df08e35022dd3d7fe72fcec38c6615e08097 100644
--- a/content/browser/devtools/devtools_http_handler_impl.cc
+++ b/content/browser/devtools/devtools_http_handler_impl.cc
@@ -15,14 +15,11 @@
#include "base/logging.h"
#include "base/message_loop_proxy.h"
#include "base/string_number_conversions.h"
-#include "base/stringprintf.h"
#include "base/threading/thread.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
-#include "content/browser/devtools/devtools_agent_host_impl.h"
#include "content/browser/devtools/devtools_browser_target.h"
#include "content/browser/devtools/devtools_tracing_handler.h"
-#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/devtools_messages.h"
#include "content/public/browser/browser_thread.h"
@@ -34,9 +31,7 @@
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
-#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
-#include "content/public/browser/render_widget_host.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "googleurl/src/gurl.h"
@@ -59,6 +54,8 @@ namespace {
static const char* kDevToolsHandlerThreadName = "Chrome_DevToolsHandlerThread";
+static const char* kThumbUrlPrefix = "/thumb/";
+
class DevToolsDefaultBindingHandler
: public DevToolsHttpHandler::DevToolsAgentHostBinding {
public:
@@ -77,9 +74,7 @@ class DevToolsDefaultBindingHandler
virtual std::string GetIdentifier(DevToolsAgentHost* agent_host) OVERRIDE {
GarbageCollect();
- DevToolsAgentHostImpl* agent_host_impl =
- static_cast<DevToolsAgentHostImpl*>(agent_host);
- std::string id = base::StringPrintf("%d", agent_host_impl->id());
+ std::string id = agent_host->GetId();
agents_map_[id] = agent_host;
return id;
}
@@ -157,6 +152,20 @@ class DevToolsClientHostImpl : public DevToolsClientHost {
std::string detach_reason_;
};
+static base::TimeTicks GetLastSelectedTime(RenderViewHost* rvh) {
+ WebContents* web_contents = rvh->GetDelegate()->GetAsWebContents();
+ if (!web_contents)
+ return base::TimeTicks();
+
+ return web_contents->GetLastSelectedTime();
+}
+
+typedef std::pair<RenderViewHost*, base::TimeTicks> PageInfo;
+
+static bool TimeComparator(const PageInfo& info1, const PageInfo& info2) {
+ return info1.second > info2.second;
+}
+
} // namespace
// static
@@ -308,15 +317,23 @@ void DevToolsHttpHandlerImpl::OnHttpRequest(
return;
}
- if (info.path.find("/thumb/") == 0) {
+ if (info.path.find(kThumbUrlPrefix) == 0) {
// Thumbnail request.
+ const std::string target_id = info.path.substr(strlen(kThumbUrlPrefix));
+ DevToolsAgentHost* agent_host = binding_->ForIdentifier(target_id);
+ GURL page_url;
+ if (agent_host) {
+ RenderViewHost* rvh = agent_host->GetRenderViewHost();
+ if (rvh)
+ page_url = rvh->GetDelegate()->GetURL();
+ }
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(&DevToolsHttpHandlerImpl::OnThumbnailRequestUI,
this,
connection_id,
- info));
+ page_url));
return;
}
@@ -395,60 +412,6 @@ void DevToolsHttpHandlerImpl::OnClose(int connection_id) {
connection_id));
}
-struct DevToolsHttpHandlerImpl::PageInfo {
- PageInfo()
- : attached(false) {
- }
-
- std::string id;
- std::string url;
- std::string type;
- bool attached;
- std::string title;
- std::string thumbnail_url;
- std::string favicon_url;
- std::string description;
- base::TimeTicks last_selected_time;
-};
-
-// static
-bool DevToolsHttpHandlerImpl::SortPageListByTime(const PageInfo& info1,
- const PageInfo& info2) {
- return info1.last_selected_time > info2.last_selected_time;
-}
-
-DevToolsHttpHandlerImpl::PageList DevToolsHttpHandlerImpl::GeneratePageList() {
- PageList page_list;
- for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator());
- !it.IsAtEnd(); it.Advance()) {
- RenderProcessHost* render_process_host = it.GetCurrentValue();
- DCHECK(render_process_host);
-
- // Ignore processes that don't have a connection, such as crashed contents.
- if (!render_process_host->HasConnection())
- continue;
-
- RenderProcessHost::RenderWidgetHostsIterator rwit(
- render_process_host->GetRenderWidgetHostsIterator());
- for (; !rwit.IsAtEnd(); rwit.Advance()) {
- const RenderWidgetHost* widget = rwit.GetCurrentValue();
- DCHECK(widget);
- if (!widget || !widget->IsRenderView())
- continue;
-
- RenderViewHost* host =
- RenderViewHost::From(const_cast<RenderWidgetHost*>(widget));
- // Don't report swapped out views.
- if (static_cast<RenderViewHostImpl*>(host)->is_swapped_out())
- continue;
-
- page_list.push_back(CreatePageInfo(host, delegate_->GetTargetType(host)));
- }
- }
- std::sort(page_list.begin(), page_list.end(), SortPageListByTime);
- return page_list;
-}
-
std::string DevToolsHttpHandlerImpl::GetFrontendURLInternal(
const std::string rvh_id,
const std::string& host) {
@@ -532,11 +495,21 @@ void DevToolsHttpHandlerImpl::OnJsonRequestUI(
}
if (command == "list") {
- PageList page_list = GeneratePageList();
+ typedef std::vector<PageInfo> PageList;
+ PageList page_list;
+
+ std::vector<RenderViewHost*> rvh_list =
+ DevToolsAgentHost::GetValidRenderViewHosts();
+ for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin();
+ it != rvh_list.end(); ++it)
+ page_list.push_back(PageInfo(*it, GetLastSelectedTime(*it)));
+
+ std::sort(page_list.begin(), page_list.end(), TimeComparator);
+
base::ListValue json_pages_list;
std::string host = info.headers["Host"];
for (PageList::iterator i = page_list.begin(); i != page_list.end(); ++i)
- json_pages_list.Append(SerializePageInfo(*i, host));
+ json_pages_list.Append(SerializePageInfo(i->first, host));
SendJson(connection_id, net::HTTP_OK, &json_pages_list, "", jsonp);
return;
}
@@ -551,11 +524,8 @@ void DevToolsHttpHandlerImpl::OnJsonRequestUI(
jsonp);
return;
}
- PageInfo page_info =
- CreatePageInfo(rvh, DevToolsHttpHandlerDelegate::kTargetTypeTab);
std::string host = info.headers["Host"];
- scoped_ptr<base::DictionaryValue> dictionary(
- SerializePageInfo(page_info, host));
+ scoped_ptr<base::DictionaryValue> dictionary(SerializePageInfo(rvh, host));
SendJson(connection_id, net::HTTP_OK, dictionary.get(), "", jsonp);
return;
}
@@ -593,17 +563,8 @@ void DevToolsHttpHandlerImpl::OnJsonRequestUI(
}
void DevToolsHttpHandlerImpl::OnThumbnailRequestUI(
- int connection_id,
- const net::HttpServerRequestInfo& info) {
- std::string prefix = "/thumb/";
- size_t pos = info.path.find(prefix);
- if (pos != 0) {
- Send404(connection_id);
- return;
- }
-
- std::string page_url = info.path.substr(prefix.length());
- std::string data = delegate_->GetPageThumbnailData(GURL(page_url));
+ int connection_id, const GURL& page_url) {
+ std::string data = delegate_->GetPageThumbnailData(page_url);
if (!data.empty())
Send200(connection_id, data, "image/png");
else
@@ -843,66 +804,49 @@ void DevToolsHttpHandlerImpl::AcceptWebSocket(
connection_id, request));
}
-DevToolsHttpHandlerImpl::PageInfo
-DevToolsHttpHandlerImpl::CreatePageInfo(RenderViewHost* rvh,
- DevToolsHttpHandlerDelegate::TargetType type) {
- RenderViewHostDelegate* host_delegate = rvh->GetDelegate();
+base::DictionaryValue* DevToolsHttpHandlerImpl::SerializePageInfo(
+ RenderViewHost* rvh,
+ const std::string& host) {
+ base::DictionaryValue* dictionary = new base::DictionaryValue;
+
scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetFor(rvh));
- DevToolsClientHost* client_host = DevToolsManager::GetInstance()->
- GetDevToolsClientHostFor(agent);
- PageInfo page_info;
- page_info.id = binding_->GetIdentifier(agent);
- page_info.attached = client_host != NULL;
- page_info.url = host_delegate->GetURL().spec();
-
- switch (type) {
+
+ std::string id = binding_->GetIdentifier(agent);
+ dictionary->SetString("id", id);
+
+ switch (delegate_->GetTargetType(rvh)) {
case DevToolsHttpHandlerDelegate::kTargetTypeTab:
- page_info.type = "page";
+ dictionary->SetString("type", "page");
break;
default:
- page_info.type = "other";
+ dictionary->SetString("type", "other");
}
- WebContents* web_contents = host_delegate->GetAsWebContents();
+ WebContents* web_contents = rvh->GetDelegate()->GetAsWebContents();
if (web_contents) {
- page_info.title = UTF16ToUTF8(
- net::EscapeForHTML(web_contents->GetTitle()));
- page_info.last_selected_time = web_contents->GetLastSelectedTime();
+ dictionary->SetString("title", UTF16ToUTF8(
+ net::EscapeForHTML(web_contents->GetTitle())));
+ dictionary->SetString("url", web_contents->GetURL().spec());
+ dictionary->SetString("thumbnailUrl", std::string(kThumbUrlPrefix) + id);
NavigationController& controller = web_contents->GetController();
NavigationEntry* entry = controller.GetActiveEntry();
if (entry != NULL && entry->GetURL().is_valid()) {
- page_info.thumbnail_url = "/thumb/" + entry->GetURL().spec();
- page_info.favicon_url = entry->GetFavicon().url.spec();
+ dictionary->SetString("faviconUrl", entry->GetFavicon().url.spec());
}
}
- page_info.description = delegate_->GetViewDescription(rvh);
-
- return page_info;
-}
-
-base::DictionaryValue* DevToolsHttpHandlerImpl::SerializePageInfo(
- const PageInfo& page_info,
- const std::string& host) {
- base::DictionaryValue* dictionary = new base::DictionaryValue;
- dictionary->SetString("title", page_info.title);
- dictionary->SetString("url", page_info.url);
- dictionary->SetString("type", page_info.type);
- dictionary->SetString("id", page_info.id);
- dictionary->SetString("thumbnailUrl", page_info.thumbnail_url);
- dictionary->SetString("faviconUrl", page_info.favicon_url);
- if (!page_info.attached) {
+ if (!DevToolsManager::GetInstance()->GetDevToolsClientHostFor(agent)) {
dictionary->SetString("webSocketDebuggerUrl",
base::StringPrintf("ws://%s/devtools/page/%s",
host.c_str(),
- page_info.id.c_str()));
+ id.c_str()));
std::string devtools_frontend_url = GetFrontendURLInternal(
- page_info.id.c_str(),
+ id.c_str(),
host);
dictionary->SetString("devtoolsFrontendUrl", devtools_frontend_url);
}
- dictionary->SetString("description", page_info.description);
+ dictionary->SetString("description", delegate_->GetViewDescription(rvh));
return dictionary;
}
« no previous file with comments | « content/browser/devtools/devtools_http_handler_impl.h ('k') | content/browser/devtools/render_view_devtools_agent_host.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698