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