| 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/extensions/extension_host.h" | 9 #include "chrome/browser/extensions/extension_host.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/browser/extensions/extension_system.h" | 11 #include "chrome/browser/extensions/extension_system.h" |
| 12 #include "chrome/browser/history/top_sites.h" | 12 #include "chrome/browser/history/top_sites.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
| 16 #include "chrome/browser/ui/browser_iterator.h" | 16 #include "chrome/browser/ui/browser_iterator.h" |
| 17 #include "chrome/browser/ui/browser_list.h" | 17 #include "chrome/browser/ui/browser_list.h" |
| 18 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
| 19 #include "chrome/browser/ui/host_desktop.h" | 19 #include "chrome/browser/ui/host_desktop.h" |
| 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/common/chrome_paths.h" | 22 #include "chrome/common/chrome_paths.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/devtools_agent_host.h" |
| 25 #include "content/public/browser/devtools_target_descriptor.h" |
| 26 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
| 28 #include "content/public/browser/worker_service.h" |
| 24 #include "content/public/common/url_constants.h" | 29 #include "content/public/common/url_constants.h" |
| 25 #include "grit/devtools_discovery_page_resources.h" | 30 #include "grit/devtools_discovery_page_resources.h" |
| 26 #include "net/socket/tcp_listen_socket.h" | 31 #include "net/socket/tcp_listen_socket.h" |
| 27 #include "net/url_request/url_request_context_getter.h" | 32 #include "net/url_request/url_request_context_getter.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
| 29 | 34 |
| 30 using content::DevToolsHttpHandlerDelegate; | 35 using content::DevToolsHttpHandlerDelegate; |
| 36 using content::DevToolsAgentHost; |
| 31 using content::RenderViewHost; | 37 using content::RenderViewHost; |
| 38 using content::WebContents; |
| 39 |
| 40 namespace { |
| 41 |
| 42 const char kTargetTypeOther[] = "other"; |
| 43 |
| 44 std::string GetDescription(WebContents* web_contents) { |
| 45 Profile* profile = |
| 46 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 47 if (!profile) |
| 48 return std::string(); |
| 49 |
| 50 extensions::ExtensionHost* extension_host = |
| 51 extensions::ExtensionSystem::Get(profile)->process_manager()-> |
| 52 GetBackgroundHostForExtension(web_contents->GetURL().host()); |
| 53 |
| 54 if (!extension_host || extension_host->host_contents() != web_contents) |
| 55 return std::string(); |
| 56 |
| 57 return extension_host->extension()->name(); |
| 58 } |
| 59 |
| 60 RenderViewHost* GetRenderViewHost(const std::string& id) { |
| 61 DevToolsAgentHost* agent_host = DevToolsAgentHost::GetForId(id); |
| 62 return agent_host ? agent_host->GetRenderViewHost() : NULL; |
| 63 } |
| 64 |
| 65 WebContents* GetWebContents(const std::string& id) { |
| 66 RenderViewHost* rvh = GetRenderViewHost(id); |
| 67 return rvh ? WebContents::FromRenderViewHost(rvh) : NULL; |
| 68 } |
| 69 |
| 70 } // namespace |
| 32 | 71 |
| 33 BrowserListTabContentsProvider::BrowserListTabContentsProvider( | 72 BrowserListTabContentsProvider::BrowserListTabContentsProvider( |
| 34 chrome::HostDesktopType host_desktop_type) | 73 chrome::HostDesktopType host_desktop_type) |
| 35 : host_desktop_type_(host_desktop_type) { | 74 : host_desktop_type_(host_desktop_type) { |
| 36 } | 75 } |
| 37 | 76 |
| 38 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() { | 77 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() { |
| 39 } | 78 } |
| 40 | 79 |
| 41 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { | 80 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 63 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() { | 102 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() { |
| 64 #if defined(DEBUG_DEVTOOLS) | 103 #if defined(DEBUG_DEVTOOLS) |
| 65 base::FilePath inspector_dir; | 104 base::FilePath inspector_dir; |
| 66 PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir); | 105 PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir); |
| 67 return inspector_dir; | 106 return inspector_dir; |
| 68 #else | 107 #else |
| 69 return base::FilePath(); | 108 return base::FilePath(); |
| 70 #endif | 109 #endif |
| 71 } | 110 } |
| 72 | 111 |
| 112 bool BrowserListTabContentsProvider::SupportsPageThumbnails() { |
| 113 return true; |
| 114 } |
| 115 |
| 73 std::string BrowserListTabContentsProvider::GetPageThumbnailData( | 116 std::string BrowserListTabContentsProvider::GetPageThumbnailData( |
| 74 const GURL& url) { | 117 const std::string& id) { |
| 118 WebContents* web_contents = GetWebContents(id); |
| 119 if (!web_contents) |
| 120 return std::string(); |
| 121 |
| 122 GURL url = web_contents->GetURL(); |
| 75 for (chrome::BrowserIterator it; !it.done(); it.Next()) { | 123 for (chrome::BrowserIterator it; !it.done(); it.Next()) { |
| 76 Profile* profile = (*it)->profile(); | 124 Profile* profile = (*it)->profile(); |
| 77 history::TopSites* top_sites = profile->GetTopSites(); | 125 history::TopSites* top_sites = profile->GetTopSites(); |
| 78 if (!top_sites) | 126 if (!top_sites) |
| 79 continue; | 127 continue; |
| 80 scoped_refptr<base::RefCountedMemory> data; | 128 scoped_refptr<base::RefCountedMemory> data; |
| 81 if (top_sites->GetPageThumbnail(url, false, &data)) | 129 if (top_sites->GetPageThumbnail(url, false, &data)) |
| 82 return std::string( | 130 return std::string( |
| 83 reinterpret_cast<const char*>(data->front()), data->size()); | 131 reinterpret_cast<const char*>(data->front()), data->size()); |
| 84 } | 132 } |
| 85 | 133 |
| 86 return std::string(); | 134 return std::string(); |
| 87 } | 135 } |
| 88 | 136 |
| 89 RenderViewHost* BrowserListTabContentsProvider::CreateNewTarget() { | 137 DevToolsHttpHandlerDelegate::Target* |
| 138 BrowserListTabContentsProvider::CreateNewTarget() { |
| 139 WebContents* web_contents; |
| 90 const BrowserList* browser_list = | 140 const BrowserList* browser_list = |
| 91 BrowserList::GetInstance(host_desktop_type_); | 141 BrowserList::GetInstance(host_desktop_type_); |
| 92 | |
| 93 if (browser_list->empty()) { | 142 if (browser_list->empty()) { |
| 94 chrome::NewEmptyWindow(ProfileManager::GetLastUsedProfile(), | 143 chrome::NewEmptyWindow(ProfileManager::GetLastUsedProfile(), |
| 95 host_desktop_type_); | 144 host_desktop_type_); |
| 96 return browser_list->empty() ? NULL : | 145 if (browser_list->empty()) |
| 97 browser_list->get(0)->tab_strip_model()->GetActiveWebContents()-> | 146 return NULL; |
| 98 GetRenderViewHost(); | 147 web_contents = |
| 148 browser_list->get(0)->tab_strip_model()->GetActiveWebContents(); |
| 149 } else { |
| 150 web_contents = chrome::AddSelectedTabWithURL( |
| 151 browser_list->get(0), |
| 152 GURL(content::kAboutBlankURL), |
| 153 content::PAGE_TRANSITION_LINK); |
| 99 } | 154 } |
| 100 | 155 return Target::FromWebContents(web_contents); |
| 101 content::WebContents* web_contents = chrome::AddSelectedTabWithURL( | |
| 102 browser_list->get(0), | |
| 103 GURL(content::kAboutBlankURL), | |
| 104 content::PAGE_TRANSITION_LINK); | |
| 105 return web_contents->GetRenderViewHost(); | |
| 106 } | 156 } |
| 107 | 157 |
| 108 content::DevToolsHttpHandlerDelegate::TargetType | 158 bool BrowserListTabContentsProvider::ActivateTarget(const std::string& id) { |
| 109 BrowserListTabContentsProvider::GetTargetType(content::RenderViewHost* rvh) { | 159 WebContents* web_contents = GetWebContents(id); |
| 110 for (TabContentsIterator it; !it.done(); it.Next()) | 160 if (!web_contents) |
| 111 if (rvh == it->GetRenderViewHost()) | 161 return false; |
| 112 return kTargetTypeTab; | 162 web_contents->GetDelegate()->ActivateContents(web_contents); |
| 113 | 163 return true; |
| 114 return kTargetTypeOther; | |
| 115 } | 164 } |
| 116 | 165 |
| 117 std::string BrowserListTabContentsProvider::GetViewDescription( | 166 bool BrowserListTabContentsProvider::CloseTarget(const std::string& id) { |
| 118 content::RenderViewHost* rvh) { | 167 RenderViewHost* rvh = GetRenderViewHost(id); |
| 119 content::WebContents* web_contents = | 168 if (!rvh) |
| 120 content::WebContents::FromRenderViewHost(rvh); | 169 return false; |
| 121 if (!web_contents) | 170 rvh->ClosePage(); |
| 122 return std::string(); | 171 return true; |
| 172 } |
| 123 | 173 |
| 124 Profile* profile = | 174 scoped_refptr<DevToolsAgentHost> BrowserListTabContentsProvider::GetAgentHost( |
| 125 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 175 const std::string& id) { |
| 126 if (!profile) | 176 return DevToolsAgentHost::GetForId(id); |
| 127 return std::string(); | 177 } |
| 128 | 178 |
| 129 extensions::ExtensionHost* extension_host = | 179 void BrowserListTabContentsProvider::RequestTargets(TargetCallback callback) { |
| 130 extensions::ExtensionSystem::Get(profile)->process_manager()-> | 180 content::BrowserThread::PostTaskAndReplyWithResult( |
| 131 GetBackgroundHostForExtension(web_contents->GetURL().host()); | 181 content::BrowserThread::IO, |
| 132 | 182 FROM_HERE, |
| 133 if (!extension_host || extension_host->host_contents() != web_contents) | 183 base::Bind(&BrowserListTabContentsProvider::CollectWorkerTargets, this), |
| 134 return std::string(); | 184 base::Bind(&BrowserListTabContentsProvider::RespondWithTargetList, |
| 135 | 185 this, |
| 136 return extension_host->extension()->name(); | 186 callback)); |
| 137 } | 187 } |
| 138 | 188 |
| 139 #if defined(DEBUG_DEVTOOLS) | 189 #if defined(DEBUG_DEVTOOLS) |
| 140 static int g_last_tethering_port_ = 9333; | 190 static int g_last_tethering_port_ = 9333; |
| 141 | 191 |
| 142 scoped_ptr<net::StreamListenSocket> | 192 scoped_ptr<net::StreamListenSocket> |
| 143 BrowserListTabContentsProvider::CreateSocketForTethering( | 193 BrowserListTabContentsProvider::CreateSocketForTethering( |
| 144 net::StreamListenSocket::Delegate* delegate, | 194 net::StreamListenSocket::Delegate* delegate, |
| 145 std::string* name) { | 195 std::string* name) { |
| 146 if (g_last_tethering_port_ == 9444) | 196 if (g_last_tethering_port_ == 9444) |
| 147 g_last_tethering_port_ = 9333; | 197 g_last_tethering_port_ = 9333; |
| 148 int port = ++g_last_tethering_port_; | 198 int port = ++g_last_tethering_port_; |
| 149 *name = base::IntToString(port); | 199 *name = base::IntToString(port); |
| 150 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate) | 200 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate) |
| 151 .PassAs<net::StreamListenSocket>(); | 201 .PassAs<net::StreamListenSocket>(); |
| 152 } | 202 } |
| 153 #else | 203 #else |
| 154 scoped_ptr<net::StreamListenSocket> | 204 scoped_ptr<net::StreamListenSocket> |
| 155 BrowserListTabContentsProvider::CreateSocketForTethering( | 205 BrowserListTabContentsProvider::CreateSocketForTethering( |
| 156 net::StreamListenSocket::Delegate* delegate, | 206 net::StreamListenSocket::Delegate* delegate, |
| 157 std::string* name) { | 207 std::string* name) { |
| 158 return scoped_ptr<net::StreamListenSocket>(); | 208 return scoped_ptr<net::StreamListenSocket>(); |
| 159 } | 209 } |
| 160 #endif // defined(DEBUG_DEVTOOLS) | 210 #endif // defined(DEBUG_DEVTOOLS) |
| 211 |
| 212 DevToolsHttpHandlerDelegate::TargetList |
| 213 BrowserListTabContentsProvider::CollectWorkerTargets() { |
| 214 TargetList targets; |
| 215 std::vector<content::WorkerService::WorkerInfo> worker_info = |
| 216 content::WorkerService::GetInstance()->GetWorkers(); |
| 217 for (size_t i = 0; i < worker_info.size(); ++i) |
| 218 targets.push_back(Target::FromWorkerInfo(worker_info[i])); |
| 219 return targets; |
| 220 } |
| 221 |
| 222 void BrowserListTabContentsProvider::RespondWithTargetList( |
| 223 TargetCallback callback, const TargetList& worker_targets) { |
| 224 std::set<RenderViewHost*> tab_rvhs; |
| 225 for (TabContentsIterator it; !it.done(); it.Next()) |
| 226 tab_rvhs.insert(it->GetRenderViewHost()); |
| 227 |
| 228 TargetList targets; |
| 229 |
| 230 std::vector<RenderViewHost*> rvh_list = |
| 231 content::DevToolsAgentHost::GetValidRenderViewHosts(); |
| 232 for (std::vector<RenderViewHost*>::iterator it = rvh_list.begin(); |
| 233 it != rvh_list.end(); ++it) { |
| 234 bool is_tab = tab_rvhs.find(*it) != tab_rvhs.end(); |
| 235 WebContents* web_contents = WebContents::FromRenderViewHost(*it); |
| 236 if (!web_contents) |
| 237 continue; |
| 238 Target* target = Target::FromWebContents(web_contents); |
| 239 if (!is_tab) { |
| 240 target->OverrideType(kTargetTypeOther); |
| 241 target->OverrideDescription(GetDescription(web_contents)); |
| 242 } |
| 243 targets.push_back(target); |
| 244 } |
| 245 |
| 246 targets.insert(targets.end(), worker_targets.begin(), worker_targets.end()); |
| 247 |
| 248 callback.Run(targets); |
| 249 } |
| OLD | NEW |