1e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// found in the LICENSE file.
4e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
5e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/task_manager/tab_contents_information.h"
6e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
7e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/callback.h"
8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/strings/utf_string_conversions.h"
9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/browser_process.h"
10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/devtools/devtools_window.h"
11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/favicon/favicon_tab_helper.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/prerender/prerender_manager.h"
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/prerender/prerender_manager_factory.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/profiles/profile.h"
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/profiles/profile_manager.h"
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/task_manager/renderer_resource.h"
17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/task_manager/task_manager_util.h"
18e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/ui/browser.h"
19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/ui/browser_finder.h"
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/ui/browser_iterator.h"
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/ui/tab_contents/tab_contents_iterator.h"
22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/public/browser/render_process_host.h"
23e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/public/browser/web_contents.h"
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "extensions/browser/extension_registry.h"
25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "extensions/browser/process_map.h"
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "extensions/common/constants.h"
27a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "extensions/common/extension_set.h"
28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "grit/theme_resources.h"
29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/base/l10n/l10n_util.h"
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/base/resource/resource_bundle.h"
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/gfx/image/image_skia.h"
32e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochusing content::WebContents;
34e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochnamespace task_manager {
36e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
37e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochnamespace {
38e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
39e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Returns true if the WebContents is currently owned by the prerendering
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// manager.
41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool IsContentsPrerendering(WebContents* web_contents) {
42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  Profile* profile =
43e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      Profile::FromBrowserContext(web_contents->GetBrowserContext());
44e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  prerender::PrerenderManager* prerender_manager =
45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      prerender::PrerenderManagerFactory::GetForProfile(profile);
46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return prerender_manager &&
47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch         prerender_manager->IsWebContentsPrerendering(web_contents, NULL);
48e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
49e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}  // namespace
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// Tracks a single tab contents, prerendered page, or Instant page.
53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass TabContentsResource : public RendererResource {
54e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public:
55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  explicit TabContentsResource(content::WebContents* web_contents);
56e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual ~TabContentsResource();
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Resource methods:
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual Type GetType() const OVERRIDE;
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual base::string16 GetTitle() const OVERRIDE;
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual gfx::ImageSkia GetIcon() const OVERRIDE;
62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual content::WebContents* GetWebContents() const OVERRIDE;
63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch private:
65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Returns true if contains content rendered by an extension.
66e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  bool HostsExtension() const;
67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  static gfx::ImageSkia* prerender_icon_;
69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  content::WebContents* web_contents_;
70e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  Profile* profile_;
71e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
72e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DISALLOW_COPY_AND_ASSIGN(TabContentsResource);
73e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch};
74e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
75e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochgfx::ImageSkia* TabContentsResource::prerender_icon_ = NULL;
76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
77e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochTabContentsResource::TabContentsResource(WebContents* web_contents)
78e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    : RendererResource(web_contents->GetRenderProcessHost()->GetHandle(),
79e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                       web_contents->GetRenderViewHost()),
80e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      web_contents_(web_contents),
81e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (!prerender_icon_) {
83e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    ResourceBundle& rb = ResourceBundle::GetSharedInstance();
84e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    prerender_icon_ = rb.GetImageSkiaNamed(IDR_PRERENDER);
85e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochTabContentsResource::~TabContentsResource() {}
89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool TabContentsResource::HostsExtension() const {
91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return web_contents_->GetURL().SchemeIs(extensions::kExtensionScheme);
92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
94e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochResource::Type TabContentsResource::GetType() const {
95e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // A tab that loads an extension URL is considered to be an extension for
96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // these purposes, although it's tracked as a TabContentsResource.
97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return HostsExtension() ? EXTENSION : RENDERER;
98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
99e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
100e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbase::string16 TabContentsResource::GetTitle() const {
101e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Fall back on the URL if there's no title.
102e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  GURL url = web_contents_->GetURL();
103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  base::string16 tab_title = util::GetTitleFromWebContents(web_contents_);
104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Only classify as an app if the URL is an app and the tab is hosting an
106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // extension process.  (It's possible to be showing the URL from before it
107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // was installed as an app.)
108e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  extensions::ProcessMap* process_map = extensions::ProcessMap::Get(profile_);
109a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  bool is_app =
110a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      extensions::ExtensionRegistry::Get(profile_)
111a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch          ->enabled_extensions().GetAppByURL(url) != NULL &&
112e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      process_map->Contains(web_contents_->GetRenderProcessHost()->GetID());
113e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
114e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  int message_id = util::GetMessagePrefixID(
115e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      is_app,
116e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      HostsExtension(),
117e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      profile_->IsOffTheRecord(),
118e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      IsContentsPrerendering(web_contents_),
119e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      false);  // is_background
120e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return l10n_util::GetStringFUTF16(message_id, tab_title);
121e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
122e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
123e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochgfx::ImageSkia TabContentsResource::GetIcon() const {
124e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (IsContentsPrerendering(web_contents_))
125e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return *prerender_icon_;
126e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  FaviconTabHelper::CreateForWebContents(web_contents_);
127e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return FaviconTabHelper::FromWebContents(web_contents_)->
128e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      GetFavicon().AsImageSkia();
129e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
130e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
131e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochWebContents* TabContentsResource::GetWebContents() const {
132e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return web_contents_;
133e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
134e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
135e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochTabContentsInformation::TabContentsInformation() {}
136e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
137e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochTabContentsInformation::~TabContentsInformation() {}
138e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
139e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool TabContentsInformation::CheckOwnership(
140e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    content::WebContents* web_contents) {
141e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return chrome::FindBrowserWithWebContents(web_contents) ||
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch         DevToolsWindow::IsDevToolsWindow(web_contents) ||
143e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch         IsContentsPrerendering(web_contents);
144e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
145e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
146e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochvoid TabContentsInformation::GetAll(const NewWebContentsCallback& callback) {
147e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  for (TabContentsIterator iterator; !iterator.done(); iterator.Next()) {
148e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    callback.Run(*iterator);
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    WebContents* devtools =
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        DevToolsWindow::GetInTabWebContents(*iterator, NULL);
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (devtools)
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      callback.Run(devtools);
153e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
154e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
155e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Because a WebContents* may start its life as a prerender, and later be
156e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // put into a tab, this class tracks the prerender contents in the same
157e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // way as the tab contents.
158e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  std::vector<Profile*> profiles(
159e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      g_browser_process->profile_manager()->GetLoadedProfiles());
160e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  for (size_t i = 0; i < profiles.size(); ++i) {
161e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    prerender::PrerenderManager* prerender_manager =
162e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch        prerender::PrerenderManagerFactory::GetForProfile(profiles[i]);
163e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    if (prerender_manager) {
164e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      const std::vector<content::WebContents*> contentses =
165e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          prerender_manager->GetAllPrerenderingContents();
166e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      for (size_t j = 0; j < contentses.size(); ++j)
167e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch        callback.Run(contentses[j]);
168e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    }
169e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
170e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
171e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
172e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochscoped_ptr<RendererResource> TabContentsInformation::MakeResource(
173e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    content::WebContents* web_contents) {
174e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return scoped_ptr<RendererResource>(new TabContentsResource(web_contents));
175e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
176e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
177e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}  // namespace task_manager
178