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