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/printing_information.h"
6e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
7e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/strings/string16.h"
8e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "base/strings/utf_string_conversions.h"
9e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/browser_process.h"
10e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/favicon/favicon_tab_helper.h"
11e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/printing/background_printing_manager.h"
12e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/printing/print_preview_dialog_controller.h"
13e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/profiles/profile.h"
14e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/profiles/profile_manager.h"
15e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/task_manager/renderer_resource.h"
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/task_manager/task_manager.h"
17e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "chrome/browser/task_manager/task_manager_util.h"
1803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/grit/generated_resources.h"
19e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/public/browser/render_frame_host.h"
20e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/public/browser/render_process_host.h"
21e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/public/browser/render_view_host.h"
22e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "content/public/browser/web_contents.h"
23e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/base/l10n/l10n_util.h"
24e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "ui/gfx/image/image_skia.h"
25e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
26e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochusing content::WebContents;
27e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
28e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochnamespace task_manager {
29e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
30e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochclass PrintingResource : public RendererResource {
31e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch public:
32e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  explicit PrintingResource(content::WebContents* web_contents);
33e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual ~PrintingResource();
34e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
35e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Resource methods:
36e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual Type GetType() const OVERRIDE;
37e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual base::string16 GetTitle() const OVERRIDE;
38e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual gfx::ImageSkia GetIcon() const OVERRIDE;
39e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual content::WebContents* GetWebContents() const OVERRIDE;
40e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
41e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch private:
42e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  content::WebContents* web_contents_;
43e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
44e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  DISALLOW_COPY_AND_ASSIGN(PrintingResource);
45e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch};
46e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
47e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochPrintingResource::PrintingResource(WebContents* web_contents)
48e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    : RendererResource(web_contents->GetRenderProcessHost()->GetHandle(),
49e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch                       web_contents->GetRenderViewHost()),
50e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      web_contents_(web_contents) {}
51e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
52e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochPrintingResource::~PrintingResource() {}
53e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
54e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochResource::Type PrintingResource::GetType() const { return RENDERER; }
55e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
56e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbase::string16 PrintingResource::GetTitle() const {
57e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return l10n_util::GetStringFUTF16(
58e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      IDS_TASK_MANAGER_PRINT_PREFIX,
59e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      util::GetTitleFromWebContents(web_contents_));
60e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
61e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
62e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochgfx::ImageSkia PrintingResource::GetIcon() const {
63e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  FaviconTabHelper::CreateForWebContents(web_contents_);
64e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return FaviconTabHelper::FromWebContents(web_contents_)->
65e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      GetFavicon().AsImageSkia();
66e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
67e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochWebContents* PrintingResource::GetWebContents() const { return web_contents_; }
69e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
70e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch////////////////////////////////////////////////////////////////////////////////
71e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch// PrintingInformation class
72e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch////////////////////////////////////////////////////////////////////////////////
73e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
74e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochPrintingInformation::PrintingInformation() {}
75e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
76e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen MurdochPrintingInformation::~PrintingInformation() {}
77e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
78e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool PrintingInformation::CheckOwnership(WebContents* web_contents) {
79e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  printing::BackgroundPrintingManager* backgrounded_print_jobs =
80e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      g_browser_process->background_printing_manager();
81e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Is it a background print job?
82e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (backgrounded_print_jobs->HasPrintPreviewDialog(web_contents)) {
83e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return true;
84e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
85e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Is it a foreground print preview window?
86e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  if (printing::PrintPreviewDialogController::IsPrintPreviewDialog(
87e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch          web_contents)) {
88e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    return true;
89e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
90e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return false;
91e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
92e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
93e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochvoid PrintingInformation::GetAll(const NewWebContentsCallback& callback) {
94e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Add all the pages being background printed.
95e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  printing::BackgroundPrintingManager* printing_manager =
96e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      g_browser_process->background_printing_manager();
97e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  std::set<content::WebContents*> printing_contents =
98e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      printing_manager->CurrentContentSet();
99e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  for (std::set<content::WebContents*>::iterator i = printing_contents.begin();
100e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch       i != printing_contents.end();
101e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch       ++i) {
102e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    callback.Run(*i);
103e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  }
104e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  // Add all the foreground print preview dialogs.
105e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  printing::PrintPreviewDialogController::GetInstance()->ForEachPreviewDialog(
106e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch      callback);
107e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
108e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
109e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochscoped_ptr<RendererResource> PrintingInformation::MakeResource(
110e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch    WebContents* web_contents) {
111e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return scoped_ptr<RendererResource>(new PrintingResource(web_contents));
112e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}
113e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch
114e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch}  // namespace task_manager
115