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