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/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"
28 using content::DevToolsTarget
;
29 using content::RenderViewHost
;
30 using content::WebContents
;
32 BrowserListTabContentsProvider::BrowserListTabContentsProvider(
33 chrome::HostDesktopType host_desktop_type
)
34 : host_desktop_type_(host_desktop_type
) {
37 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() {
40 std::string
BrowserListTabContentsProvider::GetDiscoveryPageHTML() {
41 std::set
<Profile
*> profiles
;
42 for (chrome::BrowserIterator it
; !it
.done(); it
.Next())
43 profiles
.insert((*it
)->profile());
45 for (std::set
<Profile
*>::iterator it
= profiles
.begin();
46 it
!= profiles
.end(); ++it
) {
47 history::TopSites
* ts
= (*it
)->GetTopSites();
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();
54 return ResourceBundle::GetSharedInstance().GetRawDataResource(
55 IDR_DEVTOOLS_DISCOVERY_PAGE_HTML
).as_string();
58 bool BrowserListTabContentsProvider::BundlesFrontendResources() {
62 base::FilePath
BrowserListTabContentsProvider::GetDebugFrontendDir() {
63 #if defined(DEBUG_DEVTOOLS)
64 base::FilePath inspector_dir
;
65 PathService::Get(chrome::DIR_INSPECTOR
, &inspector_dir
);
68 return base::FilePath();
72 std::string
BrowserListTabContentsProvider::GetPageThumbnailData(
74 for (chrome::BrowserIterator it
; !it
.done(); it
.Next()) {
75 Profile
* profile
= (*it
)->profile();
76 history::TopSites
* top_sites
= profile
->GetTopSites();
79 scoped_refptr
<base::RefCountedMemory
> data
;
80 if (top_sites
->GetPageThumbnail(url
, false, &data
))
82 reinterpret_cast<const char*>(data
->front()), data
->size());
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(),
96 if (browser_list
->empty())
97 return scoped_ptr
<DevToolsTarget
>();
99 browser_list
->get(0)->tab_strip_model()->GetActiveWebContents();
100 web_contents
->GetController().LoadURL(url
,
101 content::Referrer(), content::PAGE_TRANSITION_TYPED
, std::string());
103 web_contents
= chrome::AddSelectedTabWithURL(
104 browser_list
->get(0),
106 content::PAGE_TRANSITION_LINK
);
108 content::RenderViewHost
* rvh
= web_contents
->GetRenderViewHost();
110 return scoped_ptr
<DevToolsTarget
>();
111 return scoped_ptr
<DevToolsTarget
>(
112 DevToolsTargetImpl::CreateForRenderViewHost(rvh
, true));
115 void BrowserListTabContentsProvider::EnumerateTargets(TargetCallback callback
) {
116 DevToolsTargetImpl::EnumerateAllTargets(
117 *reinterpret_cast<DevToolsTargetImpl::Callback
*>(&callback
));
120 #if defined(DEBUG_DEVTOOLS)
121 static int g_last_tethering_port_
= 9333;
123 scoped_ptr
<net::StreamListenSocket
>
124 BrowserListTabContentsProvider::CreateSocketForTethering(
125 net::StreamListenSocket::Delegate
* delegate
,
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
>();
135 scoped_ptr
<net::StreamListenSocket
>
136 BrowserListTabContentsProvider::CreateSocketForTethering(
137 net::StreamListenSocket::Delegate
* delegate
,
139 return scoped_ptr
<net::StreamListenSocket
>();
141 #endif // defined(DEBUG_DEVTOOLS)