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.
5 #include "chrome/browser/devtools/browser_list_tabcontents_provider.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "chrome/browser/history/top_sites.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/ui/browser.h"
12 #include "chrome/browser/ui/browser_iterator.h"
13 #include "chrome/browser/ui/host_desktop.h"
14 #include "chrome/common/chrome_paths.h"
15 #include "content/public/common/url_constants.h"
16 #include "grit/browser_resources.h"
17 #include "net/socket/tcp_listen_socket.h"
18 #include "net/url_request/url_request_context_getter.h"
19 #include "ui/base/resource/resource_bundle.h"
23 const int kMinTetheringPort = 9333;
24 const int kMaxTetheringPort = 9444;
26 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER;
31 void BrowserListTabContentsProvider::EnableTethering() {
32 g_tethering_enabled.Get() = true;
35 BrowserListTabContentsProvider::BrowserListTabContentsProvider(
36 chrome::HostDesktopType host_desktop_type)
37 : host_desktop_type_(host_desktop_type),
38 last_tethering_port_(kMinTetheringPort) {
39 g_tethering_enabled.Get() = false;
42 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() {
45 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() {
46 std::set<Profile*> profiles;
47 for (chrome::BrowserIterator it; !it.done(); it.Next())
48 profiles.insert((*it)->profile());
50 for (std::set<Profile*>::iterator it = profiles.begin();
51 it != profiles.end(); ++it) {
52 history::TopSites* ts = (*it)->GetTopSites();
54 // TopSites updates itself after a delay. Ask TopSites to update itself
55 // when we're about to show the remote debugging landing page.
56 ts->SyncWithHistory();
59 return ResourceBundle::GetSharedInstance().GetRawDataResource(
60 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string();
63 bool BrowserListTabContentsProvider::BundlesFrontendResources() {
67 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() {
68 #if defined(DEBUG_DEVTOOLS)
69 base::FilePath inspector_dir;
70 PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir);
73 return base::FilePath();
77 scoped_ptr<net::StreamListenSocket>
78 BrowserListTabContentsProvider::CreateSocketForTethering(
79 net::StreamListenSocket::Delegate* delegate,
81 if (!g_tethering_enabled.Get())
82 return scoped_ptr<net::StreamListenSocket>();
84 if (last_tethering_port_ == kMaxTetheringPort)
85 last_tethering_port_ = kMinTetheringPort;
86 int port = ++last_tethering_port_;
87 *name = base::IntToString(port);
88 return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate)
89 .PassAs<net::StreamListenSocket>();