summaryrefslogtreecommitdiffstats
path: root/chrome/browser/printing/background_printing_manager.h
blob: 61fa684e9fcffbe7f3d84c6f827d488af5a0da61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PRINTING_BACKGROUND_PRINTING_MANAGER_H_
#define CHROME_BROWSER_PRINTING_BACKGROUND_PRINTING_MANAGER_H_
#pragma once

#include <map>
#include <set>

#include "base/compiler_specific.h"
#include "base/threading/non_thread_safe.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"

class RenderProcessHost;
class TabContentsWrapper;

namespace printing {

// Manages hidden tabs that prints documents in the background.
// The hidden tabs are no longer part of any Browser / TabStripModel.
// They get deleted when the tab finishes printing.
class BackgroundPrintingManager : public base::NonThreadSafe,
                                  public content::NotificationObserver {
 public:
  typedef std::set<TabContentsWrapper*> TabContentsWrapperSet;

  BackgroundPrintingManager();
  virtual ~BackgroundPrintingManager();

  // Takes ownership of |preview_tab| and deletes it when |preview_tab| finishes
  // printing. This removes the TabContentsWrapper from its TabStrip and
  // hides it from the user.
  void OwnPrintPreviewTab(TabContentsWrapper* preview_tab);

  // Takes ownership of |initiator_tab| and deletes it when its preview tab is
  // destroyed by either being canceled, closed or finishing printing. This
  // removes the TabContentsWrapper from its TabStrip and hides it from the
  // user. Returns true if content has an associated print preview tab,
  // otherwise, returns false and does not take ownership of |initiator_tab|.
  bool OwnInitiatorTab(TabContentsWrapper* initiator_tab);

  // Let others iterate over the list of background printing tabs.
  TabContentsWrapperSet::const_iterator begin();
  TabContentsWrapperSet::const_iterator end();

  // Returns true if |printing_tabs_| contains |preview_tab|.
  bool HasPrintPreviewTab(TabContentsWrapper* preview_tab);

  // content::NotificationObserver overrides:
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

 private:
  typedef std::map<TabContentsWrapper*, TabContentsWrapper*>
      TabContentsWrapperMap;

  // Notifications handlers.
  void OnRendererProcessClosed(RenderProcessHost* rph);
  void OnPrintJobReleased(TabContentsWrapper* preview_tab);
  void OnTabContentsDestroyed(TabContentsWrapper* preview_tab);

  // Removes |tab| from its tab strip.
  void RemoveFromTabStrip(TabContentsWrapper* tab);

  // Add |tab| to the pending deletion set and schedule deletion.
  void DeletePreviewTab(TabContentsWrapper* tab);

  // Check if any of the TabContentsWrappers in |set| share a RenderProcessHost
  // with |tab|, excluding |tab|.
  bool HasSharedRenderProcessHost(const TabContentsWrapperSet& set,
                                  TabContentsWrapper* tab);

  // The set of print preview tabs managed by BackgroundPrintingManager.
  TabContentsWrapperSet printing_tabs_;

  // The set of print preview tabs managed by BackgroundPrintingManager that
  // are pending deletion.
  TabContentsWrapperSet printing_tabs_pending_deletion_;

  // 1:1 mapping between an initiator tab managed by BackgroundPrintingManager
  // and its associated print preview tab. The print preview tab need not be in
  // |printing_tabs_|.
  // Key: print preview tab.
  // Value: initiator tab.
  TabContentsWrapperMap map_;

  content::NotificationRegistrar registrar_;

  DISALLOW_COPY_AND_ASSIGN(BackgroundPrintingManager);
};

}  // namespace printing

#endif  // CHROME_BROWSER_PRINTING_BACKGROUND_PRINTING_MANAGER_H_