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 "chrome/browser/devtools/browser_list_tabcontents_provider.h" | 5 #include "chrome/browser/devtools/browser_list_tabcontents_provider.h" |
6 | 6 |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "chrome/browser/devtools/devtools_target_impl.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/extensions/extension_host.h" |
| 12 #include "chrome/browser/extensions/extension_service.h" |
| 13 #include "chrome/browser/extensions/extension_system.h" |
10 #include "chrome/browser/history/top_sites.h" | 14 #include "chrome/browser/history/top_sites.h" |
11 #include "chrome/browser/profiles/profile_manager.h" | 15 #include "chrome/browser/profiles/profile_manager.h" |
12 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
13 #include "chrome/browser/ui/browser_commands.h" | 17 #include "chrome/browser/ui/browser_commands.h" |
14 #include "chrome/browser/ui/browser_iterator.h" | 18 #include "chrome/browser/ui/browser_iterator.h" |
15 #include "chrome/browser/ui/browser_list.h" | 19 #include "chrome/browser/ui/browser_list.h" |
16 #include "chrome/browser/ui/browser_tabstrip.h" | 20 #include "chrome/browser/ui/browser_tabstrip.h" |
17 #include "chrome/browser/ui/host_desktop.h" | 21 #include "chrome/browser/ui/host_desktop.h" |
| 22 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
18 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
19 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
20 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
| 26 #include "content/public/browser/devtools_target.h" |
| 27 #include "content/public/browser/favicon_status.h" |
| 28 #include "content/public/browser/navigation_entry.h" |
| 29 #include "content/public/browser/render_view_host.h" |
21 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
22 #include "content/public/common/url_constants.h" | 31 #include "content/public/common/url_constants.h" |
23 #include "grit/devtools_discovery_page_resources.h" | 32 #include "grit/devtools_discovery_page_resources.h" |
24 #include "net/socket/tcp_listen_socket.h" | 33 #include "net/socket/tcp_listen_socket.h" |
25 #include "net/url_request/url_request_context_getter.h" | 34 #include "net/url_request/url_request_context_getter.h" |
26 #include "ui/base/resource/resource_bundle.h" | 35 #include "ui/base/resource/resource_bundle.h" |
27 | 36 |
| 37 using content::DevToolsAgentHost; |
| 38 using content::DevToolsHttpHandlerDelegate; |
28 using content::DevToolsTarget; | 39 using content::DevToolsTarget; |
| 40 using content::RenderViewHost; |
29 using content::WebContents; | 41 using content::WebContents; |
30 | 42 |
| 43 namespace { |
| 44 |
| 45 const char kTargetTypePage[] = "page"; |
| 46 const char kTargetTypeOther[] = "other"; |
| 47 |
| 48 std::string GetExtensionName(WebContents* web_contents) { |
| 49 Profile* profile = |
| 50 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 51 if (!profile) |
| 52 return std::string(); |
| 53 |
| 54 extensions::ExtensionHost* extension_host = |
| 55 extensions::ExtensionSystem::Get(profile)->process_manager()-> |
| 56 GetBackgroundHostForExtension(web_contents->GetURL().host()); |
| 57 |
| 58 if (!extension_host || extension_host->host_contents() != web_contents) |
| 59 return std::string(); |
| 60 |
| 61 return extension_host->extension()->name(); |
| 62 } |
| 63 |
| 64 class Target : public content::DevToolsTarget { |
| 65 public: |
| 66 Target(WebContents* web_contents, bool is_tab); |
| 67 |
| 68 virtual std::string GetId() const OVERRIDE { return id_; } |
| 69 virtual std::string GetType() const OVERRIDE { return type_; } |
| 70 virtual std::string GetTitle() const OVERRIDE { return title_; } |
| 71 virtual std::string GetDescription() const OVERRIDE { return description_; } |
| 72 virtual GURL GetUrl() const OVERRIDE { return url_; } |
| 73 virtual GURL GetFaviconUrl() const OVERRIDE { return favicon_url_; } |
| 74 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { |
| 75 return last_activity_time_; |
| 76 } |
| 77 virtual bool IsAttached() const OVERRIDE { |
| 78 return agent_host_->IsAttached(); |
| 79 } |
| 80 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { |
| 81 return agent_host_; |
| 82 } |
| 83 virtual bool Activate() const OVERRIDE; |
| 84 virtual bool Close() const OVERRIDE; |
| 85 |
| 86 private: |
| 87 scoped_refptr<DevToolsAgentHost> agent_host_; |
| 88 std::string id_; |
| 89 std::string type_; |
| 90 std::string title_; |
| 91 std::string description_; |
| 92 GURL url_; |
| 93 GURL favicon_url_; |
| 94 base::TimeTicks last_activity_time_; |
| 95 }; |
| 96 |
| 97 Target::Target(WebContents* web_contents, bool is_tab) { |
| 98 agent_host_ = |
| 99 DevToolsAgentHost::GetOrCreateFor(web_contents->GetRenderViewHost()); |
| 100 id_ = agent_host_->GetId(); |
| 101 type_ = is_tab ? kTargetTypePage : kTargetTypeOther; |
| 102 description_ = GetExtensionName(web_contents); |
| 103 title_ = UTF16ToUTF8(web_contents->GetTitle()); |
| 104 url_ = web_contents->GetURL(); |
| 105 content::NavigationController& controller = web_contents->GetController(); |
| 106 content::NavigationEntry* entry = controller.GetActiveEntry(); |
| 107 if (entry != NULL && entry->GetURL().is_valid()) |
| 108 favicon_url_ = entry->GetFavicon().url; |
| 109 last_activity_time_ = web_contents->GetLastSelectedTime(); |
| 110 } |
| 111 |
| 112 bool Target::Activate() const { |
| 113 RenderViewHost* rvh = agent_host_->GetRenderViewHost(); |
| 114 if (!rvh) |
| 115 return false; |
| 116 WebContents* web_contents = WebContents::FromRenderViewHost(rvh); |
| 117 if (!web_contents) |
| 118 return false; |
| 119 web_contents->GetDelegate()->ActivateContents(web_contents); |
| 120 return true; |
| 121 } |
| 122 |
| 123 bool Target::Close() const { |
| 124 RenderViewHost* rvh = agent_host_->GetRenderViewHost(); |
| 125 if (!rvh) |
| 126 return false; |
| 127 rvh->ClosePage(); |
| 128 return true; |
| 129 } |
| 130 |
| 131 class WorkerTarget : public content::DevToolsTarget { |
| 132 public: |
| 133 explicit WorkerTarget(const content::WorkerService::WorkerInfo& worker_info); |
| 134 |
| 135 virtual std::string GetId() const OVERRIDE { return id_; } |
| 136 virtual std::string GetType() const OVERRIDE { return "other"; } |
| 137 virtual std::string GetTitle() const OVERRIDE { return title_; } |
| 138 virtual std::string GetDescription() const OVERRIDE { return description_; } |
| 139 virtual GURL GetUrl() const OVERRIDE { return url_; } |
| 140 virtual GURL GetFaviconUrl() const OVERRIDE { return GURL(); } |
| 141 virtual base::TimeTicks GetLastActivityTime() const OVERRIDE { |
| 142 return base::TimeTicks(); |
| 143 } |
| 144 virtual bool IsAttached() const OVERRIDE { |
| 145 return agent_host_->IsAttached(); |
| 146 } |
| 147 virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const OVERRIDE { |
| 148 return agent_host_; |
| 149 } |
| 150 virtual bool Activate() const OVERRIDE { return false; } |
| 151 virtual bool Close() const OVERRIDE { return false; } |
| 152 |
| 153 private: |
| 154 scoped_refptr<DevToolsAgentHost> agent_host_; |
| 155 std::string id_; |
| 156 std::string title_; |
| 157 std::string description_; |
| 158 GURL url_; |
| 159 }; |
| 160 |
| 161 WorkerTarget::WorkerTarget(const content::WorkerService::WorkerInfo& worker) { |
| 162 agent_host_ = |
| 163 DevToolsAgentHost::GetForWorker(worker.process_id, worker.route_id); |
| 164 id_ = agent_host_->GetId(); |
| 165 title_ = UTF16ToUTF8(worker.name); |
| 166 description_ = |
| 167 base::StringPrintf("Worker pid:%d", base::GetProcId(worker.handle)); |
| 168 url_ = worker.url; |
| 169 } |
| 170 |
| 171 } // namespace |
| 172 |
31 BrowserListTabContentsProvider::BrowserListTabContentsProvider( | 173 BrowserListTabContentsProvider::BrowserListTabContentsProvider( |
32 chrome::HostDesktopType host_desktop_type) | 174 chrome::HostDesktopType host_desktop_type) |
33 : host_desktop_type_(host_desktop_type) { | 175 : host_desktop_type_(host_desktop_type) { |
34 } | 176 } |
35 | 177 |
36 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() { | 178 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() { |
37 } | 179 } |
38 | 180 |
39 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { | 181 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { |
40 std::set<Profile*> profiles; | 182 std::set<Profile*> profiles; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 scoped_refptr<base::RefCountedMemory> data; | 220 scoped_refptr<base::RefCountedMemory> data; |
79 if (top_sites->GetPageThumbnail(url, false, &data)) | 221 if (top_sites->GetPageThumbnail(url, false, &data)) |
80 return std::string( | 222 return std::string( |
81 reinterpret_cast<const char*>(data->front()), data->size()); | 223 reinterpret_cast<const char*>(data->front()), data->size()); |
82 } | 224 } |
83 | 225 |
84 return std::string(); | 226 return std::string(); |
85 } | 227 } |
86 | 228 |
87 scoped_ptr<DevToolsTarget> | 229 scoped_ptr<DevToolsTarget> |
88 BrowserListTabContentsProvider::CreateNewTarget(const GURL& url) { | 230 BrowserListTabContentsProvider::CreateNewTarget() { |
89 const BrowserList* browser_list = | 231 const BrowserList* browser_list = |
90 BrowserList::GetInstance(host_desktop_type_); | 232 BrowserList::GetInstance(host_desktop_type_); |
91 WebContents* web_contents; | 233 WebContents* web_contents; |
92 if (browser_list->empty()) { | 234 if (browser_list->empty()) { |
93 chrome::NewEmptyWindow(ProfileManager::GetLastUsedProfile(), | 235 chrome::NewEmptyWindow(ProfileManager::GetLastUsedProfile(), |
94 host_desktop_type_); | 236 host_desktop_type_); |
95 if (browser_list->empty()) | 237 if (browser_list->empty()) |
96 return scoped_ptr<DevToolsTarget>(); | 238 return scoped_ptr<DevToolsTarget>(); |
97 web_contents = | 239 web_contents = |
98 browser_list->get(0)->tab_strip_model()->GetActiveWebContents(); | 240 browser_list->get(0)->tab_strip_model()->GetActiveWebContents(); |
99 web_contents->GetController().LoadURL(url, | |
100 content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string()); | |
101 } else { | 241 } else { |
102 web_contents = chrome::AddSelectedTabWithURL( | 242 web_contents = chrome::AddSelectedTabWithURL( |
103 browser_list->get(0), | 243 browser_list->get(0), |
104 url, | 244 GURL(content::kAboutBlankURL), |
105 content::PAGE_TRANSITION_LINK); | 245 content::PAGE_TRANSITION_LINK); |
106 } | 246 } |
107 return scoped_ptr<DevToolsTarget>( | 247 return scoped_ptr<DevToolsTarget>(new Target(web_contents, true)); |
108 DevToolsTargetImpl::CreateForWebContents(web_contents)); | |
109 } | 248 } |
110 | |
111 void BrowserListTabContentsProvider::EnumerateTargets(TargetCallback callback) { | 249 void BrowserListTabContentsProvider::EnumerateTargets(TargetCallback callback) { |
112 DevToolsTargetImpl::EnumerateAllTargets( | 250 content::BrowserThread::PostTaskAndReplyWithResult( |
113 *reinterpret_cast<DevToolsTargetImpl::Callback*>(&callback)); | 251 content::BrowserThread::IO, |
| 252 FROM_HERE, |
| 253 base::Bind(&BrowserListTabContentsProvider::GetWorkerInfo, |
| 254 base::Unretained(this)), |
| 255 base::Bind(&BrowserListTabContentsProvider::RespondWithTargetList, |
| 256 base::Unretained(this), |
| 257 callback)); |
114 } | 258 } |
115 | 259 |
116 #if defined(DEBUG_DEVTOOLS) | 260 #if defined(DEBUG_DEVTOOLS) |
117 static int g_last_tethering_port_ = 9333; | 261 static int g_last_tethering_port_ = 9333; |
118 | 262 |
119 scoped_ptr<net::StreamListenSocket> | 263 scoped_ptr<net::StreamListenSocket> |
120 BrowserListTabContentsProvider::CreateSocketForTethering( | 264 BrowserListTabContentsProvider::CreateSocketForTethering( |
121 net::StreamListenSocket::Delegate* delegate, | 265 net::StreamListenSocket::Delegate* delegate, |
122 std::string* name) { | 266 std::string* name) { |
123 if (g_last_tethering_port_ == 9444) | 267 if (g_last_tethering_port_ == 9444) |
124 g_last_tethering_port_ = 9333; | 268 g_last_tethering_port_ = 9333; |
125 int port = ++g_last_tethering_port_; | 269 int port = ++g_last_tethering_port_; |
126 *name = base::IntToString(port); | 270 *name = base::IntToString(port); |
127 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate) | 271 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate) |
128 .PassAs<net::StreamListenSocket>(); | 272 .PassAs<net::StreamListenSocket>(); |
129 } | 273 } |
130 #else | 274 #else |
131 scoped_ptr<net::StreamListenSocket> | 275 scoped_ptr<net::StreamListenSocket> |
132 BrowserListTabContentsProvider::CreateSocketForTethering( | 276 BrowserListTabContentsProvider::CreateSocketForTethering( |
133 net::StreamListenSocket::Delegate* delegate, | 277 net::StreamListenSocket::Delegate* delegate, |
134 std::string* name) { | 278 std::string* name) { |
135 return scoped_ptr<net::StreamListenSocket>(); | 279 return scoped_ptr<net::StreamListenSocket>(); |
136 } | 280 } |
137 #endif // defined(DEBUG_DEVTOOLS) | 281 #endif // defined(DEBUG_DEVTOOLS) |
| 282 |
| 283 BrowserListTabContentsProvider::WorkerInfoList |
| 284 BrowserListTabContentsProvider::GetWorkerInfo() { |
| 285 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 286 return content::WorkerService::GetInstance()->GetWorkers(); |
| 287 } |
| 288 |
| 289 void BrowserListTabContentsProvider::RespondWithTargetList( |
| 290 TargetCallback callback, const WorkerInfoList& worker_info_list) { |
| 291 std::set<RenderViewHost*> tab_rvhs; |
| 292 for (TabContentsIterator it; !it.done(); it.Next()) |
| 293 tab_rvhs.insert(it->GetRenderViewHost()); |
| 294 |
| 295 TargetList targets; |
| 296 |
| 297 std::vector<RenderViewHost*> rvh_list = |
| 298 content::DevToolsAgentHost::GetValidRenderViewHosts(); |
| 299 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); |
| 300 it != rvh_list.end(); ++it) { |
| 301 bool is_tab = tab_rvhs.find(*it) != tab_rvhs.end(); |
| 302 WebContents* web_contents = WebContents::FromRenderViewHost(*it); |
| 303 if (web_contents) |
| 304 targets.push_back(new Target(web_contents, is_tab)); |
| 305 } |
| 306 |
| 307 for (size_t i = 0; i < worker_info_list.size(); ++i) |
| 308 targets.push_back(new WorkerTarget(worker_info_list[i])); |
| 309 |
| 310 callback.Run(targets); |
| 311 } |
OLD | NEW |