Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / devtools / browser_list_tabcontents_provider.cc
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_navigator.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/host_desktop.h"
19 #include "chrome/browser/ui/tabs/tab_strip_model.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/url_constants.h"
23 #include "grit/browser_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 namespace {
33
34 const int kMinTetheringPort = 9333;
35 const int kMaxTetheringPort = 9444;
36
37 base::LazyInstance<bool>::Leaky g_tethering_enabled = LAZY_INSTANCE_INITIALIZER;
38
39 }
40
41 // static
42 void BrowserListTabContentsProvider::EnableTethering() {
43   g_tethering_enabled.Get() = true;
44 }
45
46 BrowserListTabContentsProvider::BrowserListTabContentsProvider(
47     chrome::HostDesktopType host_desktop_type)
48     : host_desktop_type_(host_desktop_type),
49       last_tethering_port_(kMinTetheringPort) {
50   g_tethering_enabled.Get() = false;
51 }
52
53 BrowserListTabContentsProvider::~BrowserListTabContentsProvider() {
54 }
55
56 std::string BrowserListTabContentsProvider::GetDiscoveryPageHTML() {
57   std::set<Profile*> profiles;
58   for (chrome::BrowserIterator it; !it.done(); it.Next())
59     profiles.insert((*it)->profile());
60
61   for (std::set<Profile*>::iterator it = profiles.begin();
62        it != profiles.end(); ++it) {
63     history::TopSites* ts = (*it)->GetTopSites();
64     if (ts) {
65       // TopSites updates itself after a delay. Ask TopSites to update itself
66       // when we're about to show the remote debugging landing page.
67       ts->SyncWithHistory();
68     }
69   }
70   return ResourceBundle::GetSharedInstance().GetRawDataResource(
71       IDR_DEVTOOLS_DISCOVERY_PAGE_HTML).as_string();
72 }
73
74 bool BrowserListTabContentsProvider::BundlesFrontendResources() {
75   return true;
76 }
77
78 base::FilePath BrowserListTabContentsProvider::GetDebugFrontendDir() {
79 #if defined(DEBUG_DEVTOOLS)
80   base::FilePath inspector_dir;
81   PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir);
82   return inspector_dir;
83 #else
84   return base::FilePath();
85 #endif
86 }
87
88 std::string BrowserListTabContentsProvider::GetPageThumbnailData(
89     const GURL& url) {
90   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
91     Profile* profile = (*it)->profile();
92     history::TopSites* top_sites = profile->GetTopSites();
93     if (!top_sites)
94       continue;
95     scoped_refptr<base::RefCountedMemory> data;
96     if (top_sites->GetPageThumbnail(url, false, &data))
97       return std::string(data->front_as<char>(), data->size());
98   }
99
100   return std::string();
101 }
102
103 scoped_ptr<DevToolsTarget>
104 BrowserListTabContentsProvider::CreateNewTarget(const GURL& url) {
105   chrome::NavigateParams params(ProfileManager::GetLastUsedProfile(),
106       url, content::PAGE_TRANSITION_AUTO_TOPLEVEL);
107   params.disposition = NEW_FOREGROUND_TAB;
108   chrome::Navigate(&params);
109   if (!params.target_contents)
110     return scoped_ptr<DevToolsTarget>();
111   return scoped_ptr<DevToolsTarget>(
112       DevToolsTargetImpl::CreateForWebContents(params.target_contents, true));
113 }
114
115 void BrowserListTabContentsProvider::EnumerateTargets(TargetCallback callback) {
116   DevToolsTargetImpl::EnumerateAllTargets(
117       *reinterpret_cast<DevToolsTargetImpl::Callback*>(&callback));
118 }
119
120 scoped_ptr<net::StreamListenSocket>
121 BrowserListTabContentsProvider::CreateSocketForTethering(
122     net::StreamListenSocket::Delegate* delegate,
123     std::string* name) {
124   if (!g_tethering_enabled.Get())
125     return scoped_ptr<net::StreamListenSocket>();
126
127   if (last_tethering_port_ == kMaxTetheringPort)
128     last_tethering_port_ = kMinTetheringPort;
129   int port = ++last_tethering_port_;
130   *name = base::IntToString(port);
131   return net::TCPListenSocket::CreateAndListen("127.0.0.1", port, delegate)
132       .PassAs<net::StreamListenSocket>();
133 }