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