OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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/print_preview_tab_controller.h" | 5 #include "chrome/browser/printing/print_preview_tab_controller.h" |
6 | 6 |
7 #include "base/shared_memory.h" | |
7 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/tab_contents/tab_contents.h" | 9 #include "chrome/browser/tab_contents/tab_contents.h" |
9 #include "chrome/browser/tabs/tab_strip_model.h" | 10 #include "chrome/browser/tabs/tab_strip_model.h" |
10 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_list.h" | 12 #include "chrome/browser/ui/browser_list.h" |
12 #include "chrome/browser/ui/browser_navigator.h" | 13 #include "chrome/browser/ui/browser_navigator.h" |
13 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
14 #include "chrome/common/notification_details.h" | 15 #include "chrome/common/notification_details.h" |
15 #include "chrome/common/notification_source.h" | 16 #include "chrome/common/notification_source.h" |
16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
17 | 18 |
18 namespace printing { | 19 namespace printing { |
19 | 20 |
20 PrintPreviewTabController::PrintPreviewTabController() | 21 PrintPreviewTabController::PrintPreviewTabController() |
21 : waiting_for_new_preview_page_(false) { | 22 : waiting_for_new_preview_page_(false) { |
22 } | 23 } |
23 | 24 |
24 PrintPreviewTabController::~PrintPreviewTabController() {} | 25 PrintPreviewTabController::~PrintPreviewTabController() { |
26 for (PrintPreviewDataMap::iterator it = preview_data_map_.begin(); | |
27 it != preview_data_map_.end(); | |
28 ++it) { | |
kmadhusu
2011/01/13 18:03:14
nit: This can fit in the previous line.
Lei Zhang
2011/01/13 20:59:18
Done.
| |
29 delete it->second.first; // Deleting SharedMemory; | |
30 } | |
31 } | |
25 | 32 |
26 // static | 33 // static |
27 PrintPreviewTabController* PrintPreviewTabController::GetInstance() { | 34 PrintPreviewTabController* PrintPreviewTabController::GetInstance() { |
28 if (!g_browser_process) | 35 if (!g_browser_process) |
29 return NULL; | 36 return NULL; |
30 return g_browser_process->print_preview_tab_controller(); | 37 return g_browser_process->print_preview_tab_controller(); |
31 } | 38 } |
32 | 39 |
40 // static | |
41 bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) { | |
42 const GURL& url = tab->GetURL(); | |
43 return (url.SchemeIs(chrome::kChromeUIScheme) && | |
44 url.host() == chrome::kChromeUIPrintHost); | |
45 } | |
46 | |
33 TabContents* PrintPreviewTabController::GetOrCreatePreviewTab( | 47 TabContents* PrintPreviewTabController::GetOrCreatePreviewTab( |
34 TabContents* initiator_tab, int browser_window_id ) { | 48 TabContents* initiator_tab, int browser_window_id ) { |
35 DCHECK(initiator_tab); | 49 DCHECK(initiator_tab); |
36 | 50 |
37 // Get the print preview tab for |initiator_tab|. | 51 // Get the print preview tab for |initiator_tab|. |
38 TabContents* preview_tab = GetPrintPreviewForTab(initiator_tab); | 52 TabContents* preview_tab = GetPrintPreviewForTab(initiator_tab); |
39 if (preview_tab) { | 53 if (preview_tab) { |
40 // Show current preview tab. | 54 // Show current preview tab. |
41 preview_tab->Activate(); | 55 preview_tab->Activate(); |
42 return preview_tab; | 56 return preview_tab; |
43 } | 57 } |
44 return CreatePrintPreviewTab(initiator_tab, browser_window_id); | 58 return CreatePrintPreviewTab(initiator_tab, browser_window_id); |
45 } | 59 } |
46 | 60 |
61 bool PrintPreviewTabController::GetPrintPreviewData(TabContents* preview_tab, | |
62 PrintPreviewData* data) { | |
63 PrintPreviewDataMap::iterator it = preview_data_map_.find(preview_tab); | |
64 if (it == preview_data_map_.end()) | |
65 return false; | |
66 *data = it->second; | |
67 return true; | |
68 } | |
69 | |
70 bool PrintPreviewTabController::SetPrintPreviewData( | |
71 TabContents* preview_tab, | |
72 const PrintPreviewData& data) { | |
73 PrintPreviewDataMap::iterator it = preview_data_map_.find(preview_tab); | |
74 if (it == preview_data_map_.end()) | |
75 return false; | |
76 delete it->second.first; // Deleting old SharedMemory. | |
77 it->second = data; | |
78 return true; | |
79 } | |
80 | |
47 void PrintPreviewTabController::Observe(NotificationType type, | 81 void PrintPreviewTabController::Observe(NotificationType type, |
48 const NotificationSource& source, | 82 const NotificationSource& source, |
49 const NotificationDetails& details) { | 83 const NotificationDetails& details) { |
50 TabContents* initiator_tab = NULL; | 84 TabContents* initiator_tab = NULL; |
51 TabContents* preview_tab = NULL; | 85 TabContents* preview_tab = NULL; |
52 TabContents* source_tab = NULL; | 86 TabContents* source_tab = NULL; |
53 NavigationController::LoadCommittedDetails* detail_info = NULL; | 87 NavigationController::LoadCommittedDetails* detail_info = NULL; |
54 | 88 |
55 switch (type.value) { | 89 switch (type.value) { |
56 case NotificationType::TAB_CONTENTS_DESTROYED: { | 90 case NotificationType::TAB_CONTENTS_DESTROYED: { |
(...skipping 11 matching lines...) Expand all Loading... | |
68 default: { | 102 default: { |
69 NOTREACHED(); | 103 NOTREACHED(); |
70 return; | 104 return; |
71 } | 105 } |
72 } | 106 } |
73 | 107 |
74 DCHECK(source_tab); | 108 DCHECK(source_tab); |
75 preview_tab = GetPrintPreviewForTab(source_tab); | 109 preview_tab = GetPrintPreviewForTab(source_tab); |
76 | 110 |
77 // |source_tab| is preview tab. | 111 // |source_tab| is preview tab. |
78 if (preview_tab == source_tab) | 112 if (source_tab == preview_tab) |
79 initiator_tab = GetInitiatorTab(source_tab); | 113 initiator_tab = GetInitiatorTab(source_tab); |
80 else | 114 else |
81 initiator_tab = source_tab; | 115 initiator_tab = source_tab; |
82 | 116 |
83 if (detail_info) { | 117 if (detail_info) { |
84 PageTransition::Type transition_type = | 118 PageTransition::Type transition_type = |
85 detail_info->entry->transition_type(); | 119 detail_info->entry->transition_type(); |
86 NavigationType::Type nav_type = detail_info->type; | 120 NavigationType::Type nav_type = detail_info->type; |
87 | 121 |
88 // Don't update/erase the map entry if the page has not changed. | 122 // Don't update/erase the map entry if the page has not changed. |
(...skipping 13 matching lines...) Expand all Loading... | |
102 | 136 |
103 // User navigated to a preview tab using forward/back button. | 137 // User navigated to a preview tab using forward/back button. |
104 if (IsPrintPreviewTab(source_tab) && | 138 if (IsPrintPreviewTab(source_tab) && |
105 transition_type == PageTransition::FORWARD_BACK && | 139 transition_type == PageTransition::FORWARD_BACK && |
106 nav_type == NavigationType::EXISTING_PAGE) { | 140 nav_type == NavigationType::EXISTING_PAGE) { |
107 return; | 141 return; |
108 } | 142 } |
109 } | 143 } |
110 | 144 |
111 // If |source_tab| is |initiator_tab|, update the map entry. | 145 // If |source_tab| is |initiator_tab|, update the map entry. |
112 if (source_tab == initiator_tab) { | 146 if (source_tab == initiator_tab) |
113 preview_tab_map_[preview_tab] = NULL; | 147 preview_tab_map_[preview_tab] = NULL; |
114 } | |
115 | 148 |
116 // If |source_tab| is |preview_tab|, erase the map entry. | 149 // If |source_tab| is |preview_tab|, erase the map entry. |
117 if (source_tab == preview_tab) { | 150 if (source_tab == preview_tab) { |
118 preview_tab_map_.erase(preview_tab); | 151 preview_tab_map_.erase(preview_tab); |
152 delete preview_data_map_[preview_tab].first; // Deleting old SharedMemory. | |
153 preview_data_map_.erase(preview_tab); | |
119 RemoveObservers(preview_tab); | 154 RemoveObservers(preview_tab); |
120 } | 155 } |
121 | 156 |
122 if (initiator_tab) | 157 if (initiator_tab) |
123 RemoveObservers(initiator_tab); | 158 RemoveObservers(initiator_tab); |
124 } | 159 } |
125 | 160 |
126 // static | |
127 bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) { | |
128 const GURL& url = tab->GetURL(); | |
129 return (url.SchemeIs(chrome::kChromeUIScheme) && | |
130 url.host() == chrome::kChromeUIPrintHost); | |
131 } | |
132 | |
133 TabContents* PrintPreviewTabController::GetInitiatorTab( | 161 TabContents* PrintPreviewTabController::GetInitiatorTab( |
134 TabContents* preview_tab) { | 162 TabContents* preview_tab) { |
135 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); | 163 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); |
136 if (it != preview_tab_map_.end()) | 164 if (it != preview_tab_map_.end()) |
137 return preview_tab_map_[preview_tab]; | 165 return preview_tab_map_[preview_tab]; |
138 return NULL; | 166 return NULL; |
139 } | 167 } |
140 | 168 |
141 TabContents* PrintPreviewTabController::GetPrintPreviewForTab( | 169 TabContents* PrintPreviewTabController::GetPrintPreviewForTab( |
142 TabContents* tab) { | 170 TabContents* tab) { |
(...skipping 17 matching lines...) Expand all Loading... | |
160 PageTransition::LINK); | 188 PageTransition::LINK); |
161 params.disposition = NEW_FOREGROUND_TAB; | 189 params.disposition = NEW_FOREGROUND_TAB; |
162 params.tabstrip_index = current_browser->tabstrip_model()-> | 190 params.tabstrip_index = current_browser->tabstrip_model()-> |
163 GetWrapperIndex(initiator_tab) + 1; | 191 GetWrapperIndex(initiator_tab) + 1; |
164 browser::Navigate(¶ms); | 192 browser::Navigate(¶ms); |
165 TabContentsWrapper* preview_tab = params.target_contents; | 193 TabContentsWrapper* preview_tab = params.target_contents; |
166 preview_tab->tab_contents()->Activate(); | 194 preview_tab->tab_contents()->Activate(); |
167 | 195 |
168 // Add an entry to the map. | 196 // Add an entry to the map. |
169 preview_tab_map_[preview_tab->tab_contents()] = initiator_tab; | 197 preview_tab_map_[preview_tab->tab_contents()] = initiator_tab; |
198 preview_data_map_[preview_tab->tab_contents()] = | |
kmadhusu
2011/01/13 18:03:14
Rather than making calls to the same function cons
Lei Zhang
2011/01/13 20:59:18
Done.
| |
199 std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U); | |
170 waiting_for_new_preview_page_ = true; | 200 waiting_for_new_preview_page_ = true; |
171 | 201 |
172 AddObservers(initiator_tab); | 202 AddObservers(initiator_tab); |
173 AddObservers(preview_tab->tab_contents()); | 203 AddObservers(preview_tab->tab_contents()); |
174 | 204 |
175 return preview_tab->tab_contents(); | 205 return preview_tab->tab_contents(); |
176 } | 206 } |
177 | 207 |
178 void PrintPreviewTabController::AddObservers(TabContents* tab) { | 208 void PrintPreviewTabController::AddObservers(TabContents* tab) { |
179 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, | 209 registrar_.Add(this, NotificationType::TAB_CONTENTS_DESTROYED, |
180 Source<TabContents>(tab)); | 210 Source<TabContents>(tab)); |
181 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 211 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
182 Source<NavigationController>(&tab->controller())); | 212 Source<NavigationController>(&tab->controller())); |
183 } | 213 } |
184 | 214 |
185 void PrintPreviewTabController::RemoveObservers(TabContents* tab) { | 215 void PrintPreviewTabController::RemoveObservers(TabContents* tab) { |
186 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, | 216 registrar_.Remove(this, NotificationType::TAB_CONTENTS_DESTROYED, |
187 Source<TabContents>(tab)); | 217 Source<TabContents>(tab)); |
188 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, | 218 registrar_.Remove(this, NotificationType::NAV_ENTRY_COMMITTED, |
189 Source<NavigationController>(&tab->controller())); | 219 Source<NavigationController>(&tab->controller())); |
190 } | 220 } |
191 | 221 |
192 } // namespace printing | 222 } // namespace printing |
OLD | NEW |