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/devtools/devtools_target_impl.h" 10 #include "chrome/browser/history/top_sites.h" 11 #include "chrome/browser/profiles/profile_manager.h" 12 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser_commands.h" 14 #include "chrome/browser/ui/browser_iterator.h" 15 #include "chrome/browser/ui/browser_list.h" 16 #include "chrome/browser/ui/browser_tabstrip.h" 17 #include "chrome/browser/ui/host_desktop.h" 18 #include "chrome/browser/ui/tabs/tab_strip_model.h" 19 #include "chrome/common/chrome_paths.h" 20 #include "content/public/browser/web_contents.h" 21 #include "content/public/common/url_constants.h" 22 #include "grit/browser_resources.h" 23 #include "net/socket/tcp_listen_socket.h" 24 #include "net/url_request/url_request_context_getter.h" 25 #include "ui/base/resource/resource_bundle.h" 26 27 using content::DevToolsTarget; 28 using content::RenderViewHost; 29 using content::WebContents; 30 31 namespace { 32 33 const int kMinTetheringPort = 9333; 34 const int kMaxTetheringPort = 9444; 35 36 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER; 37 38 } 39 40 // static 41 void BrowserListTabContentsProvider::EnableTethering() { 42 g_tethering_enabled.Get() = true; 43 } 44 45 BrowserListTabContentsProvider::BrowserListTabContentsProvider( 46 chrome::HostDesktopType host_desktop_type) 47 : host_desktop_type_(host_desktop_type), 48 last_tethering_port_(kMinTetheringPort) { 49 g_tethering_enabled.Get() = false; 50 } 51 52 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() { 53 } 54 55 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() { 56 std::set<Profile*> profiles; 57 for (chrome::BrowserIterator it; !it.done(); it.Next()) 58 profiles.insert((*it)->profile()); 59 60 for (std::set<Profile*>::iterator it = profiles.begin(); 61 it != profiles.end(); ++it) { 62 history::TopSites* ts = (*it)->GetTopSites(); 63 if (ts) { 64 // TopSites updates itself after a delay. Ask TopSites to update itself 65 // when we're about to show the remote debugging landing page. 66 ts->SyncWithHistory(); 67 } 68 } 69 return ResourceBundle::GetSharedInstance().GetRawDataResource( 70 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string(); 71 } 72 73 bool BrowserListTabContentsProvider::BundlesFrontendResources() { 74 return true; 75 } 76 77 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() { 78 #if defined(DEBUG_DEVTOOLS) 79 base::FilePath inspector_dir; 80 PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir); 81 return inspector_dir; 82 #else 83 return base::FilePath(); 84 #endif 85 } 86 87 std::string BrowserListTabContentsProvider::GetPageThumbnailData( 88 const GURL& url) { 89 for (chrome::BrowserIterator it; !it.done(); it.Next()) { 90 Profile* profile = (*it)->profile(); 91 history::TopSites* top_sites = profile->GetTopSites(); 92 if (!top_sites) 93 continue; 94 scoped_refptr<base::RefCountedMemory> data; 95 if (top_sites->GetPageThumbnail(url, false, &data)) 96 return std::string(data->front_as<char>(), data->size()); 97 } 98 99 return std::string(); 100 } 101 102 scoped_ptr<DevToolsTarget> 103 BrowserListTabContentsProvider::CreateNewTarget(const GURL& url) { 104 const BrowserList* browser_list = 105 BrowserList::GetInstance(host_desktop_type_); 106 WebContents* web_contents; 107 if (browser_list->empty()) { 108 chrome::NewEmptyWindow(ProfileManager::GetLastUsedProfile(), 109 host_desktop_type_); 110 if (browser_list->empty()) 111 return scoped_ptr<DevToolsTarget>(); 112 web_contents = 113 browser_list->get(0)->tab_strip_model()->GetActiveWebContents(); 114 web_contents->GetController().LoadURL(url, 115 content::Referrer(), content::PAGE_TRANSITION_TYPED, std::string()); 116 } else { 117 web_contents = chrome::AddSelectedTabWithURL( 118 browser_list->get(0), 119 url, 120 content::PAGE_TRANSITION_LINK); 121 } 122 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); 123 if (!rvh) 124 return scoped_ptr<DevToolsTarget>(); 125 return scoped_ptr<DevToolsTarget>( 126 DevToolsTargetImpl::CreateForRenderViewHost(rvh, true)); 127 } 128 129 void BrowserListTabContentsProvider::EnumerateTargets(TargetCallback callback) { 130 DevToolsTargetImpl::EnumerateAllTargets( 131 *reinterpret_cast<DevToolsTargetImpl::Callback*>(&callback)); 132 } 133 134 scoped_ptr<net::StreamListenSocket> 135 BrowserListTabContentsProvider::CreateSocketForTethering( 136 net::StreamListenSocket::Delegate* delegate, 137 std::string* name) { 138 if (!g_tethering_enabled.Get()) 139 return scoped_ptr<net::StreamListenSocket>(); 140 141 if (last_tethering_port_ == kMaxTetheringPort) 142 last_tethering_port_ = kMinTetheringPort; 143 int port = ++last_tethering_port_; 144 *name = base::IntToString(port); 145 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate) 146 .PassAs<net::StreamListenSocket>(); 147 } 148