Home | History | Annotate | Download | only in devtools
      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/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"
     20 
     21 namespace {
     22 
     23 const int kMinTetheringPort = 9333;
     24 const int kMaxTetheringPort = 9444;
     25 
     26 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER;
     27 
     28 }
     29 
     30 // static
     31 void BrowserListTabContentsProvider::EnableTethering() {
     32   g_tethering_enabled.Get() = true;
     33 }
     34 
     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;
     40 }
     41 
     42 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() {
     43 }
     44 
     45 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() {
     46   std::set<Profile*> profiles;
     47   for (chrome::BrowserIterator it; !it.done(); it.Next())
     48     profiles.insert((*it)->profile());
     49 
     50   for (std::set<Profile*>::iterator it = profiles.begin();
     51        it != profiles.end(); ++it) {
     52     history::TopSites* ts = (*it)->GetTopSites();
     53     if (ts) {
     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();
     57     }
     58   }
     59   return ResourceBundle::GetSharedInstance().GetRawDataResource(
     60       IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string();
     61 }
     62 
     63 bool BrowserListTabContentsProvider::BundlesFrontendResources() {
     64   return true;
     65 }
     66 
     67 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() {
     68 #if defined(DEBUG_DEVTOOLS)
     69   base::FilePath inspector_dir;
     70   PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir);
     71   return inspector_dir;
     72 #else
     73   return base::FilePath();
     74 #endif
     75 }
     76 
     77 scoped_ptr<net::StreamListenSocket>
     78 BrowserListTabContentsProvider::CreateSocketForTethering(
     79     net::StreamListenSocket::Delegate* delegate,
     80     std::string* name) {
     81   if (!g_tethering_enabled.Get())
     82     return scoped_ptr<net::StreamListenSocket>();
     83 
     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>();
     90 }
     91