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/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"
27 using content::DevToolsTarget
;
28 using content::RenderViewHost
;
29 using content::WebContents
;
33 const int kMinTetheringPort
= 9333;
34 const int kMaxTetheringPort
= 9444;
36 base::LazyInstance
<bool>::Leaky g_tethering_enabled
= LAZY_INSTANCE_INITIALIZER
;
41 void BrowserListTabContentsProvider::EnableTethering() {
42 g_tethering_enabled
.Get() = true;
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;
52 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() {
55 std::string
BrowserListTabContentsProvider::GetDiscoveryPageHTML() {
56 std::set
<Profile
*> profiles
;
57 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
58 profiles
.insert((*it
)->profile());
60 for (std::set
<Profile
*>::iterator it
= profiles
.begin();
61 it
!= profiles
.end(); ++it
) {
62 history::TopSites
* ts
= (*it
)->GetTopSites();
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();
69 return ResourceBundle::GetSharedInstance().GetRawDataResource(
70 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML
).as_string();
73 bool BrowserListTabContentsProvider::BundlesFrontendResources() {
77 base::FilePath
BrowserListTabContentsProvider::GetDebugFrontendDir() {
78 #if defined(DEBUG_DEVTOOLS)
79 base::FilePath inspector_dir
;
80 PathService::Get(chrome::DIR_INSPECTOR
, &inspector_dir
);
83 return base::FilePath();
87 std::string
BrowserListTabContentsProvider::GetPageThumbnailData(
89 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
90 Profile
* profile
= (*it
)->profile();
91 history::TopSites
* top_sites
= profile
->GetTopSites();
94 scoped_refptr
<base::RefCountedMemory
> data
;
95 if (top_sites
->GetPageThumbnail(url
, false, &data
))
96 return std::string(data
->front_as
<char>(), data
->size());
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(),
110 if (browser_list
->empty())
111 return scoped_ptr
<DevToolsTarget
>();
113 browser_list
->get(0)->tab_strip_model()->GetActiveWebContents();
114 web_contents
->GetController().LoadURL(url
,
115 content::Referrer(), content::PAGE_TRANSITION_TYPED
, std::string());
117 web_contents
= chrome::AddSelectedTabWithURL(
118 browser_list
->get(0),
120 content::PAGE_TRANSITION_LINK
);
122 content::RenderViewHost
* rvh
= web_contents
->GetRenderViewHost();
124 return scoped_ptr
<DevToolsTarget
>();
125 return scoped_ptr
<DevToolsTarget
>(
126 DevToolsTargetImpl::CreateForRenderViewHost(rvh
, true));
129 void BrowserListTabContentsProvider::EnumerateTargets(TargetCallback callback
) {
130 DevToolsTargetImpl::EnumerateAllTargets(
131 *reinterpret_cast<DevToolsTargetImpl::Callback
*>(&callback
));
134 scoped_ptr
<net::StreamListenSocket
>
135 BrowserListTabContentsProvider::CreateSocketForTethering(
136 net::StreamListenSocket::Delegate
* delegate
,
138 if (!g_tethering_enabled
.Get())
139 return scoped_ptr
<net::StreamListenSocket
>();
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
>();