OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/printing/background_printing_manager.h" | 5 #include "chrome/browser/printing/background_printing_manager.h" |
6 | 6 |
7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/memory/ptr_util.h" |
8 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 11 #include "base/threading/thread_task_runner_handle.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/printing/print_job.h" | 13 #include "chrome/browser/printing/print_job.h" |
13 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 14 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/notification_details.h" | 16 #include "content/public/browser/notification_details.h" |
16 #include "content/public/browser/notification_source.h" | 17 #include "content/public/browser/notification_source.h" |
17 #include "content/public/browser/render_view_host.h" | 18 #include "content/public/browser/render_view_host.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
55 } | 56 } |
56 | 57 |
57 BackgroundPrintingManager::~BackgroundPrintingManager() { | 58 BackgroundPrintingManager::~BackgroundPrintingManager() { |
58 DCHECK(CalledOnValidThread()); | 59 DCHECK(CalledOnValidThread()); |
59 // The might be some WebContentses still in |printing_contents_map_| at this | 60 // The might be some WebContentses still in |printing_contents_map_| at this |
60 // point (e.g. when the last remaining tab closes and there is still a print | 61 // point (e.g. when the last remaining tab closes and there is still a print |
61 // preview WebContents trying to print). In such a case it will fail to print, | 62 // preview WebContents trying to print). In such a case it will fail to print, |
62 // but we should at least clean up the observers. | 63 // but we should at least clean up the observers. |
63 // TODO(thestig): Handle this case better. | 64 // TODO(thestig): Handle this case better. |
64 base::STLDeleteValues(&printing_contents_map_); | |
65 } | 65 } |
66 | 66 |
67 void BackgroundPrintingManager::OwnPrintPreviewDialog( | 67 void BackgroundPrintingManager::OwnPrintPreviewDialog( |
68 WebContents* preview_dialog) { | 68 WebContents* preview_dialog) { |
69 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
70 DCHECK(PrintPreviewDialogController::IsPrintPreviewDialog(preview_dialog)); | 70 DCHECK(PrintPreviewDialogController::IsPrintPreviewDialog(preview_dialog)); |
71 CHECK(!HasPrintPreviewDialog(preview_dialog)); | 71 CHECK(!HasPrintPreviewDialog(preview_dialog)); |
72 | 72 |
73 printing_contents_map_[preview_dialog] = new Observer(this, preview_dialog); | 73 printing_contents_map_[preview_dialog] = |
| 74 base::MakeUnique<Observer>(this, preview_dialog); |
74 | 75 |
75 // Watch for print jobs finishing. Everything else is watched for by the | 76 // Watch for print jobs finishing. Everything else is watched for by the |
76 // Observer. TODO(avi, cait): finish the job of removing this last | 77 // Observer. TODO(avi, cait): finish the job of removing this last |
77 // notification. | 78 // notification. |
78 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 79 registrar_.Add(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
79 content::Source<WebContents>(preview_dialog)); | 80 content::Source<WebContents>(preview_dialog)); |
80 | 81 |
81 // Activate the initiator. | 82 // Activate the initiator. |
82 PrintPreviewDialogController* dialog_controller = | 83 PrintPreviewDialogController* dialog_controller = |
83 PrintPreviewDialogController::GetInstance(); | 84 PrintPreviewDialogController::GetInstance(); |
84 if (!dialog_controller) | 85 if (!dialog_controller) |
85 return; | 86 return; |
86 WebContents* initiator = dialog_controller->GetInitiator(preview_dialog); | 87 WebContents* initiator = dialog_controller->GetInitiator(preview_dialog); |
87 if (!initiator) | 88 if (!initiator) |
88 return; | 89 return; |
89 initiator->GetDelegate()->ActivateContents(initiator); | 90 initiator->GetDelegate()->ActivateContents(initiator); |
90 } | 91 } |
91 | 92 |
92 void BackgroundPrintingManager::Observe( | 93 void BackgroundPrintingManager::Observe( |
93 int type, | 94 int type, |
94 const content::NotificationSource& source, | 95 const content::NotificationSource& source, |
95 const content::NotificationDetails& details) { | 96 const content::NotificationDetails& details) { |
96 DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_RELEASED, type); | 97 DCHECK_EQ(chrome::NOTIFICATION_PRINT_JOB_RELEASED, type); |
97 DeletePreviewContents(content::Source<WebContents>(source).ptr()); | 98 DeletePreviewContents(content::Source<WebContents>(source).ptr()); |
98 } | 99 } |
99 | 100 |
100 void BackgroundPrintingManager::DeletePreviewContents( | 101 void BackgroundPrintingManager::DeletePreviewContents( |
101 WebContents* preview_contents) { | 102 WebContents* preview_contents) { |
102 WebContentsObserverMap::iterator i = | 103 auto i = printing_contents_map_.find(preview_contents); |
103 printing_contents_map_.find(preview_contents); | |
104 if (i == printing_contents_map_.end()) { | 104 if (i == printing_contents_map_.end()) { |
105 // Everyone is racing to be the first to delete the |preview_contents|. If | 105 // Everyone is racing to be the first to delete the |preview_contents|. If |
106 // this case is hit, someone else won the race, so there is no need to | 106 // this case is hit, someone else won the race, so there is no need to |
107 // continue. <http://crbug.com/100806> | 107 // continue. <http://crbug.com/100806> |
108 return; | 108 return; |
109 } | 109 } |
110 | 110 |
111 // Stop all observation ... | 111 // Stop all observation ... |
112 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, | 112 registrar_.Remove(this, chrome::NOTIFICATION_PRINT_JOB_RELEASED, |
113 content::Source<WebContents>(preview_contents)); | 113 content::Source<WebContents>(preview_contents)); |
114 Observer* observer = i->second; | |
115 printing_contents_map_.erase(i); | 114 printing_contents_map_.erase(i); |
116 delete observer; | |
117 | 115 |
118 // ... and mortally wound the contents. (Deletion immediately is not a good | 116 // ... and mortally wound the contents. (Deletion immediately is not a good |
119 // idea in case this was called from RenderViewGone.) | 117 // idea in case this was called from RenderViewGone.) |
120 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, preview_contents); | 118 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, preview_contents); |
121 } | 119 } |
122 | 120 |
123 std::set<content::WebContents*> BackgroundPrintingManager::CurrentContentSet() { | 121 std::set<content::WebContents*> BackgroundPrintingManager::CurrentContentSet() { |
124 std::set<content::WebContents*> result; | 122 std::set<content::WebContents*> result; |
125 for (WebContentsObserverMap::iterator i = printing_contents_map_.begin(); | 123 for (const auto& entry : printing_contents_map_) |
126 i != printing_contents_map_.end(); ++i) { | 124 result.insert(entry.first); |
127 result.insert(i->first); | 125 |
128 } | |
129 return result; | 126 return result; |
130 } | 127 } |
131 | 128 |
132 bool BackgroundPrintingManager::HasPrintPreviewDialog( | 129 bool BackgroundPrintingManager::HasPrintPreviewDialog( |
133 WebContents* preview_dialog) { | 130 WebContents* preview_dialog) { |
134 return base::ContainsKey(printing_contents_map_, preview_dialog); | 131 return base::ContainsKey(printing_contents_map_, preview_dialog); |
135 } | 132 } |
136 | 133 |
137 } // namespace printing | 134 } // namespace printing |
OLD | NEW |