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

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

Issue 24995003: DevTools: Extract target discovery and manipulation from DevToolsHttpHandlerImpl (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added Android implementations Created 7 years, 3 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 db30d26a9870887846191667aa244cf07ce2cf90..54d4a1e7e9eb6af26e8e081ee5c09247dfa428f9 100644
--- a/content/browser/devtools/devtools_http_handler_impl.cc
+++ b/content/browser/devtools/devtools_http_handler_impl.cc
@@ -11,12 +11,9 @@
#include "base/compiler_specific.h"
#include "base/file_util.h"
#include "base/json/json_writer.h"
-#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/stl_util.h"
-#include "base/strings/string_number_conversions.h"
-#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
#include "base/values.h"
#include "content/browser/devtools/devtools_browser_target.h"
@@ -32,20 +29,14 @@
#include "content/public/browser/devtools_client_host.h"
#include "content/public/browser/devtools_http_handler_delegate.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_types.h"
-#include "content/public/browser/render_view_host.h"
+#include "content/public/browser/devtools_target_descriptor.h"
#include "content/public/common/content_client.h"
#include "content/public/common/url_constants.h"
#include "grit/devtools_resources_map.h"
-#include "net/base/escape.h"
#include "net/base/io_buffer.h"
#include "net/base/ip_endpoint.h"
#include "net/server/http_server_request_info.h"
#include "net/server/http_server_response_info.h"
-#include "ui/base/layout.h"
#include "url/gurl.h"
#include "webkit/common/user_agent/user_agent.h"
#include "webkit/common/user_agent/user_agent_util.h"
@@ -71,24 +62,6 @@ const char kTargetFaviconUrlField[] = "faviconUrl";
const char kTargetWebSocketDebuggerUrlField[] = "webSocketDebuggerUrl";
const char kTargetDevtoolsFrontendUrlField[] = "devtoolsFrontendUrl";
-const char kTargetTypePage[] = "page";
-const char kTargetTypeOther[] = "other";
-
-class DevToolsDefaultBindingHandler
- : public DevToolsHttpHandler::DevToolsAgentHostBinding {
- public:
- DevToolsDefaultBindingHandler() {
- }
-
- virtual std::string GetIdentifier(DevToolsAgentHost* agent_host) OVERRIDE {
- return agent_host->GetId();
- }
-
- virtual DevToolsAgentHost* ForIdentifier(const std::string& id) OVERRIDE {
- return DevToolsAgentHost::GetForId(id).get();
- }
-};
-
// An internal implementation of DevToolsClientHost that delegates
// messages sent for DevToolsClient to a DebuggerShell instance.
class DevToolsClientHostImpl : public DevToolsClientHost {
@@ -149,20 +122,6 @@ 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
@@ -243,14 +202,6 @@ void DevToolsHttpHandlerImpl::Stop() {
base::Bind(&DevToolsHttpHandlerImpl::ResetHandlerThreadAndRelease, this));
}
-void DevToolsHttpHandlerImpl::SetDevToolsAgentHostBinding(
- DevToolsAgentHostBinding* binding) {
- if (binding)
- binding_ = binding;
- else
- binding_ = default_binding_.get();
-}
-
GURL DevToolsHttpHandlerImpl::GetFrontendURL(DevToolsAgentHost* agent_host) {
net::IPEndPoint ip_address;
if (server_->GetLocalAddress(&ip_address))
@@ -260,7 +211,7 @@ GURL DevToolsHttpHandlerImpl::GetFrontendURL(DevToolsAgentHost* agent_host) {
overridden_frontend_url_);
}
std::string host = ip_address.ToString();
- std::string id = binding_->GetIdentifier(agent_host);
+ std::string id = agent_host->GetId();
return GURL(std::string("http://") +
ip_address.ToString() +
GetFrontendURLInternal(id, host));
@@ -306,20 +257,13 @@ void DevToolsHttpHandlerImpl::OnHttpRequest(
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,
- page_url));
+ target_id));
return;
}
@@ -509,40 +453,16 @@ void DevToolsHttpHandlerImpl::OnJsonRequestUI(
}
if (command == "list") {
- 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* target_list = new base::ListValue();
std::string host = info.headers["host"];
- for (PageList::iterator i = page_list.begin(); i != page_list.end(); ++i)
- target_list->Append(SerializePageInfo(i->first, host));
-
- AddRef(); // Balanced in SendTargetList.
- BrowserThread::PostTaskAndReply(
- BrowserThread::IO,
- FROM_HERE,
- base::Bind(&DevToolsHttpHandlerImpl::CollectWorkerInfo,
- base::Unretained(this),
- target_list,
- host),
+ delegate_->RequestTargets(
base::Bind(&DevToolsHttpHandlerImpl::SendTargetList,
- base::Unretained(this),
- connection_id,
- target_list));
+ this, connection_id, host));
return;
}
if (command == "new") {
- RenderViewHost* rvh = delegate_->CreateNewTarget();
- if (!rvh) {
+ Target* target = delegate_->CreateNewTarget();
+ if (!target) {
SendJson(connection_id,
net::HTTP_INTERNAL_SERVER_ERROR,
NULL,
@@ -550,34 +470,33 @@ void DevToolsHttpHandlerImpl::OnJsonRequestUI(
return;
}
std::string host = info.headers["host"];
- scoped_ptr<base::DictionaryValue> dictionary(SerializePageInfo(rvh, host));
+ scoped_ptr<base::DictionaryValue> dictionary(Serialize(*target, host));
SendJson(connection_id, net::HTTP_OK, dictionary.get(), std::string());
return;
}
- if (command == "activate" || command == "close") {
- DevToolsAgentHost* agent_host = binding_->ForIdentifier(target_id);
- RenderViewHost* rvh = agent_host ? agent_host->GetRenderViewHost() : NULL;
- if (!rvh) {
+ if (command == "activate") {
+ if (delegate_->ActivateTarget(target_id))
+ SendJson(connection_id, net::HTTP_OK, NULL, "Target activated");
+ else
SendJson(connection_id,
net::HTTP_NOT_FOUND,
NULL,
"No such target id: " + target_id);
- return;
- }
-
- if (command == "activate") {
- rvh->GetDelegate()->Activate();
- SendJson(connection_id, net::HTTP_OK, NULL, "Target activated");
- return;
- }
+ return;
+ }
- if (command == "close") {
- rvh->ClosePage();
+ if (command == "close") {
+ if (delegate_->CloseTarget(target_id))
SendJson(connection_id, net::HTTP_OK, NULL, "Target is closing");
- return;
- }
+ else
+ SendJson(connection_id,
+ net::HTTP_NOT_FOUND,
+ NULL,
+ "No such target id: " + target_id);
+ return;
}
+
SendJson(connection_id,
net::HTTP_NOT_FOUND,
NULL,
@@ -585,26 +504,20 @@ void DevToolsHttpHandlerImpl::OnJsonRequestUI(
return;
}
-void DevToolsHttpHandlerImpl::CollectWorkerInfo(base::ListValue* target_list,
- std::string host) {
-
- std::vector<WorkerService::WorkerInfo> worker_info =
- WorkerService::GetInstance()->GetWorkers();
-
- for (size_t i = 0; i < worker_info.size(); ++i)
- target_list->Append(SerializeWorkerInfo(worker_info[i], host));
-}
-
void DevToolsHttpHandlerImpl::SendTargetList(int connection_id,
- base::ListValue* target_list) {
- SendJson(connection_id, net::HTTP_OK, target_list, std::string());
- delete target_list;
- Release(); // Balanced OnJsonRequestUI.
+ const std::string& host,
+ const TargetList& targets) {
+ base::ListValue list_value;
+ for (TargetList::const_iterator it = targets.begin();
+ it != targets.end(); ++it)
+ list_value.Append(Serialize(*(*it), host));
+ STLDeleteContainerPointers(targets.begin(), targets.end());
+ SendJson(connection_id, net::HTTP_OK, &list_value, std::string());
}
void DevToolsHttpHandlerImpl::OnThumbnailRequestUI(
- int connection_id, const GURL& page_url) {
- std::string data = delegate_->GetPageThumbnailData(page_url);
+ int connection_id, const std::string& target_id) {
+ std::string data = delegate_->GetPageThumbnailData(target_id);
if (!data.empty())
Send200(connection_id, data, "image/png");
else
@@ -629,7 +542,7 @@ void DevToolsHttpHandlerImpl::OnWebSocketRequestUI(
}
std::string page_id = request.path.substr(strlen(kPageUrlPrefix));
- DevToolsAgentHost* agent = binding_->ForIdentifier(page_id);
+ scoped_refptr<DevToolsAgentHost> agent = delegate_->GetAgentHost(page_id);
if (!agent) {
Send500(connection_id, "No such target id: " + page_id);
return;
@@ -685,9 +598,6 @@ DevToolsHttpHandlerImpl::DevToolsHttpHandlerImpl(
if (overridden_frontend_url_.empty())
overridden_frontend_url_ = "/devtools/devtools.html";
- default_binding_.reset(new DevToolsDefaultBindingHandler);
- binding_ = default_binding_.get();
-
// Balanced in ResetHandlerThreadAndRelease().
AddRef();
}
@@ -786,84 +696,31 @@ void DevToolsHttpHandlerImpl::AcceptWebSocket(
connection_id, request));
}
-base::DictionaryValue* DevToolsHttpHandlerImpl::SerializePageInfo(
- RenderViewHost* rvh,
- const std::string& host) {
- base::DictionaryValue* dictionary = new base::DictionaryValue;
-
- scoped_refptr<DevToolsAgentHost> agent(
- DevToolsAgentHost::GetOrCreateFor(rvh));
-
- std::string id = binding_->GetIdentifier(agent.get());
- dictionary->SetString(kTargetIdField, id);
-
- switch (delegate_->GetTargetType(rvh)) {
- case DevToolsHttpHandlerDelegate::kTargetTypeTab:
- dictionary->SetString(kTargetTypeField, kTargetTypePage);
- break;
- default:
- dictionary->SetString(kTargetTypeField, kTargetTypeOther);
- }
-
- WebContents* web_contents = rvh->GetDelegate()->GetAsWebContents();
- if (web_contents) {
- dictionary->SetString(kTargetTitleField, UTF16ToUTF8(
- net::EscapeForHTML(web_contents->GetTitle())));
- dictionary->SetString(kTargetUrlField, web_contents->GetURL().spec());
- dictionary->SetString(kTargetThumbnailUrlField,
- std::string(kThumbUrlPrefix) + id);
-
- NavigationController& controller = web_contents->GetController();
- NavigationEntry* entry = controller.GetActiveEntry();
- if (entry != NULL && entry->GetURL().is_valid()) {
- dictionary->SetString(kTargetFaviconUrlField,
- entry->GetFavicon().url.spec());
- }
- }
- dictionary->SetString(kTargetDescriptionField,
- delegate_->GetViewDescription(rvh));
-
- if (!agent->IsAttached())
- SerializeDebuggerURLs(dictionary, id, host);
- return dictionary;
-}
-
-base::DictionaryValue* DevToolsHttpHandlerImpl::SerializeWorkerInfo(
- const WorkerService::WorkerInfo& worker,
+base::DictionaryValue* DevToolsHttpHandlerImpl::Serialize(
+ const DevToolsHttpHandlerDelegate::Target& target,
const std::string& host) {
- base::DictionaryValue* dictionary = new base::DictionaryValue;
-
- scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetForWorker(
- worker.process_id, worker.route_id));
+ base::DictionaryValue* dict = target.Serialize();
- std::string id = binding_->GetIdentifier(agent.get());
-
- dictionary->SetString(kTargetIdField, id);
- dictionary->SetString(kTargetTypeField, kTargetTypeOther);
- dictionary->SetString(kTargetTitleField,
- UTF16ToUTF8(net::EscapeForHTML(worker.name)));
- dictionary->SetString(kTargetUrlField, worker.url.spec());
- dictionary->SetString(kTargetDescriptionField,
- base::StringPrintf("Worker pid:%d", base::GetProcId(worker.handle)));
+ const std::string id = target.GetId();
+ if (delegate_->SupportsPageThumbnails()) {
+ dict->SetString(kTargetThumbnailUrlField,
+ std::string(kThumbUrlPrefix) + id);
+ }
- if (!agent->IsAttached())
- SerializeDebuggerURLs(dictionary, id, host);
- return dictionary;
-}
+ scoped_refptr<DevToolsAgentHost> agent = delegate_->GetAgentHost(id);
+ if (!agent || !agent->IsAttached()) {
+ dict->SetString(kTargetWebSocketDebuggerUrlField,
+ base::StringPrintf("ws://%s%s%s",
+ host.c_str(),
+ kPageUrlPrefix,
+ id.c_str()));
+ std::string devtools_frontend_url = GetFrontendURLInternal(
+ id.c_str(),
+ host);
+ dict->SetString(kTargetDevtoolsFrontendUrlField, devtools_frontend_url);
+ }
-void DevToolsHttpHandlerImpl::SerializeDebuggerURLs(
- base::DictionaryValue* dictionary,
- const std::string& id,
- const std::string& host) {
- dictionary->SetString(kTargetWebSocketDebuggerUrlField,
- base::StringPrintf("ws://%s%s%s",
- host.c_str(),
- kPageUrlPrefix,
- id.c_str()));
- std::string devtools_frontend_url = GetFrontendURLInternal(
- id.c_str(),
- host);
- dictionary->SetString(kTargetDevtoolsFrontendUrlField, devtools_frontend_url);
+ return dict;
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698