summaryrefslogtreecommitdiffstats
path: root/chrome/browser/printing/background_printing_manager.cc
blob: dbe5264157f51242c77fbfcfdd65155e8a46c7ed (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
99
100
101
102
103
104
105
106
107
108
// 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.

#include "chrome/browser/printing/background_printing_manager.h"

#include "chrome/browser/printing/print_job.h"
#include "chrome/browser/printing/print_preview_tab_controller.h"
#include "chrome/browser/sessions/restore_tab_helper.h"
#include "chrome/browser/tabs/tab_strip_model.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"

namespace printing {

BackgroundPrintingManager::BackgroundPrintingManager() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}

BackgroundPrintingManager::~BackgroundPrintingManager() {
  DCHECK(CalledOnValidThread());
  // The might be some TabContentsWrappers still in |printing_contents_| at
  // this point. E.g. when the last remaining tab is a print preview tab and
  // tries to print. In which case it will fail to print.
  // TODO(thestig) handle this case better.
}

void BackgroundPrintingManager::OwnTabContents(TabContentsWrapper* contents) {
  DCHECK(CalledOnValidThread());
  DCHECK(printing::PrintPreviewTabController::IsPrintPreviewTab(
      contents->tab_contents()));
  CHECK(printing_contents_.find(contents) == printing_contents_.end());

  printing_contents_.insert(contents);

  registrar_.Add(this, NotificationType::PRINT_JOB_RELEASED,
                 Source<TabContentsWrapper>(contents));
  registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED,
                 Source<TabContents>(contents->tab_contents()));

  // Detach |contents| from its tab strip.
  Browser* browser = BrowserList::FindBrowserWithID(
      contents->restore_tab_helper()->window_id().id());
  DCHECK(browser);

  TabStripModel* tabstrip = browser->tabstrip_model();
  tabstrip->DetachTabContentsAt(tabstrip->GetIndexOfTabContents(contents));

  // Activate the initiator tab.
  printing::PrintPreviewTabController* tab_controller =
      printing::PrintPreviewTabController::GetInstance();
  if (!tab_controller)
    return;
  TabContents* initiator_tab = tab_controller->GetInitiatorTab(
      contents->tab_contents());
  if (!initiator_tab)
    return;
  initiator_tab->Activate();
}

void BackgroundPrintingManager::Observe(NotificationType type,
                                        const NotificationSource& source,
                                        const NotificationDetails& details) {
  switch (type.value) {
    case NotificationType::PRINT_JOB_RELEASED: {
      // This might be happening in the middle of a RenderViewGone() loop.
      // Deleting |contents| later so the RenderViewGone() loop can finish.
      MessageLoop::current()->DeleteSoon(
          FROM_HERE,
          Source<TabContentsWrapper>(source).ptr());
      break;
    }
    case NotificationType::TAB_CONTENTS_DESTROYED: {
      TabContentsWrapper* tab =
          TabContentsWrapper::GetCurrentWrapperForContents(
              Source<TabContents>(source).ptr());
      registrar_.Remove(this, NotificationType::PRINT_JOB_RELEASED,
                        Source<TabContentsWrapper>(tab));
      registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED,
                        Source<TabContents>(tab->tab_contents()));
      printing_contents_.erase(tab);
      break;
    }
    default: {
      NOTREACHED();
      break;
    }
  }
}

std::set<TabContentsWrapper*>::const_iterator
    BackgroundPrintingManager::begin() {
  return printing_contents_.begin();
}

std::set<TabContentsWrapper*>::const_iterator
    BackgroundPrintingManager::end() {
  return printing_contents_.end();
}

bool BackgroundPrintingManager::HasTabContents(TabContentsWrapper* entry) {
  return printing_contents_.find(entry) != printing_contents_.end();
}

}  // namespace printing