Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2011 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 // For print preview, a print preview (PP) tab is linked with the initiator tab
      6 // that initiated the printing operation. If the tab initiates a second
      7 // printing operation while the first print preview tab is still open, that PP
      8 // tab is focused/activated. There may be more than one PP tab open. There is a
      9 // 1:1 relationship between PP tabs and initiating tabs. This class manages PP
     10 // tabs and initiator tabs.
     11 #ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_
     12 
     13 #define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_
     14 #pragma once
     15 
     16 #include <map>
     17 
     18 #include "base/memory/ref_counted.h"
     19 #include "chrome/browser/sessions/session_id.h"
     20 #include "content/common/notification_observer.h"
     21 #include "content/common/notification_registrar.h"
     22 
     23 class Browser;
     24 class TabContents;
     25 
     26 namespace printing {
     27 
     28 class PrintPreviewTabController
     29     : public base::RefCounted<PrintPreviewTabController>,
     30       public NotificationObserver {
     31  public:
     32   PrintPreviewTabController();
     33 
     34   virtual ~PrintPreviewTabController();
     35 
     36   static PrintPreviewTabController* GetInstance();
     37 
     38   // Initiate print preview for |initiator_tab|.
     39   // Call this instead of GetOrCreatePreviewTab().
     40   static void PrintPreview(TabContents* initiator_tab);
     41 
     42   // Get/Create the print preview tab for |initiator_tab|.
     43   // Exposed for unit tests.
     44   TabContents* GetOrCreatePreviewTab(TabContents* initiator_tab);
     45 
     46   // Returns preview tab for |tab|.
     47   // Returns |tab| if |tab| is a preview tab.
     48   // Returns NULL if no preview tab exists for |tab|.
     49   TabContents* GetPrintPreviewForTab(TabContents* tab) const;
     50 
     51   // Returns initiator tab for |preview_tab|.
     52   // Returns NULL if no initiator tab exists for |preview_tab|.
     53   TabContents* GetInitiatorTab(TabContents* preview_tab);
     54 
     55   // Notification observer implementation.
     56   virtual void Observe(NotificationType type,
     57                        const NotificationSource& source,
     58                        const NotificationDetails& details);
     59 
     60   // Returns true if |tab| is a print preview tab.
     61   static bool IsPrintPreviewTab(TabContents* tab);
     62 
     63  private:
     64   friend class base::RefCounted<PrintPreviewTabController>;
     65 
     66   // 1:1 relationship between initiator tab and print preview tab.
     67   // Key: Preview tab.
     68   // Value: Initiator tab.
     69   typedef std::map<TabContents*, TabContents*> PrintPreviewTabMap;
     70 
     71   // Creates a new print preview tab.
     72   TabContents* CreatePrintPreviewTab(TabContents* initiator_tab);
     73 
     74   // Adds/Removes observers for notifications from |tab|.
     75   void AddObservers(TabContents* tab);
     76   void RemoveObservers(TabContents* tab);
     77 
     78   PrintPreviewTabMap preview_tab_map_;
     79 
     80   // A registrar for listening notifications.
     81   NotificationRegistrar registrar_;
     82 
     83   // True if the controller is waiting for a new preview tab via
     84   // NavigationType::NEW_PAGE.
     85   bool waiting_for_new_preview_page_;
     86 
     87   DISALLOW_COPY_AND_ASSIGN(PrintPreviewTabController);
     88 };
     89 
     90 }  // namespace printing
     91 
     92 #endif  // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_TAB_CONTROLLER_H_
     93