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