Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #include "content/browser/devtools/devtools_agent_host_impl.h" | 5 #include "content/browser/devtools/devtools_agent_host_impl.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/singleton.h" | |
| 9 #include "base/stringprintf.h" | |
| 8 #include "content/common/devtools_messages.h" | 10 #include "content/common/devtools_messages.h" |
| 11 #include "content/public/browser/render_process_host.h" | |
| 12 #include "content/public/browser/render_view_host.h" | |
| 13 #include "content/public/browser/render_widget_host.h" | |
| 9 | 14 |
| 10 namespace content { | 15 namespace content { |
| 11 | 16 |
| 12 namespace { | 17 namespace { |
| 13 static int g_next_agent_host_id = 0; | 18 static int g_next_agent_host_id = 0; |
| 14 } // namespace | 19 } // namespace |
| 15 | 20 |
| 16 DevToolsAgentHostImpl::DevToolsAgentHostImpl() | 21 DevToolsAgentHostImpl::DevToolsAgentHostImpl() |
| 17 : close_listener_(NULL), | 22 : close_listener_(NULL), |
| 18 id_(++g_next_agent_host_id) { | 23 id_(base::StringPrintf("%d", ++g_next_agent_host_id)) { |
| 19 } | 24 } |
| 20 | 25 |
| 21 void DevToolsAgentHostImpl::Attach() { | 26 void DevToolsAgentHostImpl::Attach() { |
| 22 SendMessageToAgent(new DevToolsAgentMsg_Attach(MSG_ROUTING_NONE)); | 27 SendMessageToAgent(new DevToolsAgentMsg_Attach(MSG_ROUTING_NONE)); |
| 23 NotifyClientAttaching(); | 28 NotifyClientAttaching(); |
| 24 } | 29 } |
| 25 | 30 |
| 26 void DevToolsAgentHostImpl::Reattach(const std::string& saved_agent_state) { | 31 void DevToolsAgentHostImpl::Reattach(const std::string& saved_agent_state) { |
| 27 SendMessageToAgent(new DevToolsAgentMsg_Reattach( | 32 SendMessageToAgent(new DevToolsAgentMsg_Reattach( |
| 28 MSG_ROUTING_NONE, | 33 MSG_ROUTING_NONE, |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 59 } | 64 } |
| 60 | 65 |
| 61 void DevToolsAgentHostImpl::NotifyCloseListener() { | 66 void DevToolsAgentHostImpl::NotifyCloseListener() { |
| 62 if (close_listener_) { | 67 if (close_listener_) { |
| 63 scoped_refptr<DevToolsAgentHostImpl> protect(this); | 68 scoped_refptr<DevToolsAgentHostImpl> protect(this); |
| 64 close_listener_->AgentHostClosing(this); | 69 close_listener_->AgentHostClosing(this); |
| 65 close_listener_ = NULL; | 70 close_listener_ = NULL; |
| 66 } | 71 } |
| 67 } | 72 } |
| 68 | 73 |
| 74 namespace { | |
| 75 class DevToolsTargetList { | |
|
jam
2013/03/06 17:41:52
nit: no tabbing inside a namespace. also usually t
| |
| 76 public: | |
| 77 | |
| 78 static DevToolsTargetList* GetInstance() { | |
| 79 return Singleton<DevToolsTargetList>::get(); | |
| 80 } | |
| 81 | |
| 82 DevToolsAgentHost* GetRetained(const std::string& id) { | |
| 83 GarbageCollect(); | |
| 84 AgentsMap::iterator it = agents_map_.find(id); | |
| 85 if (it != agents_map_.end()) | |
| 86 return it->second; | |
| 87 return NULL; | |
| 88 } | |
| 89 | |
| 90 void RetainFor(std::vector<RenderViewHost*> rvh_list) { | |
| 91 GarbageCollect(); | |
| 92 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); | |
| 93 it != rvh_list.end(); it++) { | |
| 94 scoped_refptr<DevToolsAgentHost> agent(DevToolsAgentHost::GetFor(*it)); | |
| 95 agents_map_[agent->GetId()] = agent; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 private: | |
| 100 typedef std::map<std::string, scoped_refptr<DevToolsAgentHost> > AgentsMap; | |
| 101 | |
| 102 AgentsMap agents_map_; | |
| 103 | |
| 104 void GarbageCollect() { | |
| 105 AgentsMap::iterator it = agents_map_.begin(); | |
| 106 while (it != agents_map_.end()) { | |
| 107 if (!it->second->GetRenderViewHost()) | |
| 108 agents_map_.erase(it++); | |
| 109 else | |
| 110 ++it; | |
| 111 } | |
| 112 } | |
| 113 }; | |
| 114 } // namespace | |
| 115 | |
| 116 //static | |
| 117 std::vector<RenderViewHost*> DevToolsAgentHost::GetValidRenderViewHosts() { | |
| 118 std::vector<RenderViewHost*> result; | |
| 119 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); | |
| 120 !it.IsAtEnd(); it.Advance()) { | |
| 121 RenderProcessHost* render_process_host = it.GetCurrentValue(); | |
| 122 DCHECK(render_process_host); | |
| 123 | |
| 124 // Ignore processes that don't have a connection, such as crashed contents. | |
| 125 if (!render_process_host->HasConnection()) | |
| 126 continue; | |
| 127 | |
| 128 RenderProcessHost::RenderWidgetHostsIterator rwit( | |
| 129 render_process_host->GetRenderWidgetHostsIterator()); | |
| 130 for (; !rwit.IsAtEnd(); rwit.Advance()) { | |
| 131 const RenderWidgetHost* widget = rwit.GetCurrentValue(); | |
| 132 DCHECK(widget); | |
| 133 if (!widget || !widget->IsRenderView()) | |
| 134 continue; | |
| 135 | |
| 136 RenderViewHost* rvh = | |
| 137 RenderViewHost::From(const_cast<RenderWidgetHost*>(widget)); | |
| 138 result.push_back(rvh); | |
| 139 } | |
| 140 } | |
| 141 return result; | |
| 142 } | |
| 143 | |
| 144 //static | |
| 145 void DevToolsAgentHost::RetainFor(std::vector<RenderViewHost*> rvh_list) { | |
| 146 DevToolsTargetList::GetInstance()->RetainFor(rvh_list); | |
| 147 } | |
| 148 | |
| 149 // static | |
| 150 DevToolsAgentHost* DevToolsAgentHost::GetRetained(const std::string& id) { | |
| 151 return DevToolsTargetList::GetInstance()->GetRetained(id); | |
| 152 } | |
| 153 | |
| 69 } // namespace content | 154 } // namespace content |
| OLD | NEW |