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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/devtools/browser_list_tabcontents_provider.h" 6 7 #include "base/path_service.h" 8 #include "base/strings/string_number_conversions.h" 9 #include "chrome/browser/extensions/extension_host.h" 10 #include "chrome/browser/extensions/extension_service.h" 11 #include "chrome/browser/extensions/extension_system.h" 12 #include "chrome/browser/history/top_sites.h" 13 #include "chrome/browser/profiles/profile_manager.h" 14 #include "chrome/browser/ui/browser.h" 15 #include "chrome/browser/ui/browser_commands.h" 16 #include "chrome/browser/ui/browser_iterator.h" 17 #include "chrome/browser/ui/browser_list.h" 18 #include "chrome/browser/ui/browser_tabstrip.h" 19 #include "chrome/browser/ui/host_desktop.h" 20 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" 22 #include "chrome/common/chrome_paths.h" 23 #include "content/public/browser/web_contents.h" 24 #include "content/public/common/url_constants.h" 25 #include "grit/devtools_discovery_page_resources.h" 26 #include "net/socket/tcp_listen_socket.h" 27 #include "net/url_request/url_request_context_getter.h" 28 #include "ui/base/resource/resource_bundle.h" 29 30 using content::DevToolsHttpHandlerDelegate; 31 using content::RenderViewHost; 32 33 BrowserListTabContentsProvider::BrowserListTabContentsProvider( 34 chrome::HostDesktopType host_desktop_type) 35 : host_desktop_type_(host_desktop_type) { 36 } 37 38 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() { 39 } 40 41 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { 42 std::set<Profile*> profiles; 43 for (chrome::BrowserIterator it; !it.done(); it.Next()) 44 profiles.insert((*it)->profile()); 45 46 for (std::set<Profile*>::iterator it = profiles.begin(); 47 it != profiles.end(); ++it) { 48 history::TopSites* ts = (*it)->GetTopSites(); 49 if (ts) { 50 // TopSites updates itself after a delay. Ask TopSites to update itself 51 // when we're about to show the remote debugging landing page. 52 ts->SyncWithHistory(); 53 } 54 } 55 return ResourceBundle::GetSharedInstance().GetRawDataResource( 56 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string(); 57 } 58 59 bool BrowserListTabContentsProvider::BundlesFrontendResources() { 60 return true; 61 } 62 63 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() { 64 #if defined(DEBUG_DEVTOOLS) 65 base::FilePath inspector_dir; 66 PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir); 67 return inspector_dir; 68 #else 69 return base::FilePath(); 70 #endif 71 } 72 73 std::string BrowserListTabContentsProvider::GetPageThumbnailData( 74 const GURL& url) { 75 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 76 Profile* profile = (*it)->profile(); 77 history::TopSites* top_sites = profile->GetTopSites(); 78 if (!top_sites) 79 continue; 80 scoped_refptr<base::RefCountedMemory> data; 81 if (top_sites->GetPageThumbnail(url, &data)) 82 return std::string( 83 reinterpret_cast<const char*>(data->front()), data->size()); 84 } 85 86 return std::string(); 87 } 88 89 RenderViewHost* BrowserListTabContentsProvider::CreateNewTarget() { 90 const BrowserList* browser_list = 91 BrowserList::GetInstance(host_desktop_type_); 92 93 if (browser_list->empty()) { 94 chrome::NewEmptyWindow(ProfileManager::GetLastUsedProfile(), 95 host_desktop_type_); 96 return browser_list->empty() ? NULL : 97 browser_list->get(0)->tab_strip_model()->GetActiveWebContents()-> 98 GetRenderViewHost(); 99 } 100 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 } 107 108 content::DevToolsHttpHandlerDelegate::TargetType 109 BrowserListTabContentsProvider::GetTargetType(content::RenderViewHost* rvh) { 110 for (TabContentsIterator it; !it.done(); it.Next()) 111 if (rvh == it->GetRenderViewHost()) 112 return kTargetTypeTab; 113 114 return kTargetTypeOther; 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 } 138 139 #if defined(DEBUG_DEVTOOLS) 140 static int g_last_tethering_port_ = 9333; 141 142 scoped_refptr<net::StreamListenSocket> 143 BrowserListTabContentsProvider::CreateSocketForTethering( 144 net::StreamListenSocket::Delegate* delegate, 145 std::string* name) { 146 if (g_last_tethering_port_ == 9444) 147 g_last_tethering_port_ = 9333; 148 int port = ++g_last_tethering_port_; 149 *name = base::IntToString(port); 150 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate); 151 } 152 #else 153 scoped_refptr<net::StreamListenSocket> 154 BrowserListTabContentsProvider::CreateSocketForTethering( 155 net::StreamListenSocket::Delegate* delegate, 156 std::string* name) { 157 return NULL; 158 } 159 #endif // defined(DEBUG_DEVTOOLS) 160