Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: chrome/browser/printing/print_preview_tab_controller.cc

Issue 10535045: TabContentsWrapper -> TabContents, for printing code. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/print_preview_tab_controller.h" 5 #include "chrome/browser/printing/print_preview_tab_controller.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
11 #include "base/auto_reset.h" 11 #include "base/auto_reset.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/utf_string_conversions.h" 13 #include "base/utf_string_conversions.h"
14 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_plugin_service_filter.h" 15 #include "chrome/browser/chrome_plugin_service_filter.h"
16 #include "chrome/browser/printing/print_view_manager.h" 16 #include "chrome/browser/printing/print_view_manager.h"
17 #include "chrome/browser/profiles/profile.h" 17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/ui/browser.h" 18 #include "chrome/browser/ui/browser.h"
19 #include "chrome/browser/ui/browser_finder.h" 19 #include "chrome/browser/ui/browser_finder.h"
20 #include "chrome/browser/ui/browser_navigator.h" 20 #include "chrome/browser/ui/browser_navigator.h"
21 #include "chrome/browser/ui/browser_window.h" 21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" 22 #include "chrome/browser/ui/tab_contents/tab_contents.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" 24 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
25 #include "chrome/browser/ui/webui/web_dialog_web_contents_delegate.h" 25 #include "chrome/browser/ui/webui/web_dialog_web_contents_delegate.h"
26 #include "chrome/common/chrome_content_client.h" 26 #include "chrome/common/chrome_content_client.h"
27 #include "chrome/common/chrome_notification_types.h" 27 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/chrome_switches.h" 28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
30 #include "content/public/browser/navigation_controller.h" 30 #include "content/public/browser/navigation_controller.h"
31 #include "content/public/browser/navigation_details.h" 31 #include "content/public/browser/navigation_details.h"
32 #include "content/public/browser/navigation_entry.h" 32 #include "content/public/browser/navigation_entry.h"
33 #include "content/public/browser/notification_details.h" 33 #include "content/public/browser/notification_details.h"
34 #include "content/public/browser/notification_source.h" 34 #include "content/public/browser/notification_source.h"
35 #include "content/public/browser/render_process_host.h" 35 #include "content/public/browser/render_process_host.h"
36 #include "content/public/browser/render_view_host.h" 36 #include "content/public/browser/render_view_host.h"
37 #include "content/public/browser/web_contents.h" 37 #include "content/public/browser/web_contents.h"
38 #include "content/public/browser/web_contents_delegate.h" 38 #include "content/public/browser/web_contents_delegate.h"
39 #include "ui/web_dialogs/constrained_web_dialog_ui.h" 39 #include "ui/web_dialogs/constrained_web_dialog_ui.h"
40 #include "ui/web_dialogs/web_dialog_delegate.h" 40 #include "ui/web_dialogs/web_dialog_delegate.h"
41 #include "webkit/plugins/webplugininfo.h" 41 #include "webkit/plugins/webplugininfo.h"
42 42
43 using content::NativeWebKeyboardEvent; 43 using content::NativeWebKeyboardEvent;
44 using content::NavigationController; 44 using content::NavigationController;
45 using content::WebContents; 45 using content::WebContents;
46 using content::WebUIMessageHandler; 46 using content::WebUIMessageHandler;
47 using ui::ConstrainedWebDialogDelegate; 47 using ui::ConstrainedWebDialogDelegate;
48 using ui::WebDialogDelegate; 48 using ui::WebDialogDelegate;
49 49
50 namespace { 50 namespace {
51 51
52 void EnableInternalPDFPluginForTab(TabContentsWrapper* preview_tab) { 52 void EnableInternalPDFPluginForTab(TabContents* preview_tab) {
53 // Always enable the internal PDF plugin for the print preview page. 53 // Always enable the internal PDF plugin for the print preview page.
54 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( 54 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab(
55 preview_tab->web_contents()->GetRenderProcessHost()->GetID(), 55 preview_tab->web_contents()->GetRenderProcessHost()->GetID(),
56 preview_tab->web_contents()->GetRenderViewHost()->GetRoutingID(), 56 preview_tab->web_contents()->GetRenderViewHost()->GetRoutingID(),
57 GURL(), 57 GURL(),
58 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName)); 58 ASCIIToUTF16(chrome::ChromeContentClient::kPDFPluginName));
59 } 59 }
60 60
61 // WebDialogDelegate that specifies what the print preview dialog 61 // WebDialogDelegate that specifies what the print preview dialog
62 // will look like. 62 // will look like.
63 class PrintPreviewTabDelegate : public WebDialogDelegate { 63 class PrintPreviewTabDelegate : public WebDialogDelegate {
64 public: 64 public:
65 explicit PrintPreviewTabDelegate(TabContentsWrapper* initiator_tab); 65 explicit PrintPreviewTabDelegate(TabContents* initiator_tab);
66 virtual ~PrintPreviewTabDelegate(); 66 virtual ~PrintPreviewTabDelegate();
67 67
68 // Overridden from WebDialogDelegate: 68 // Overridden from WebDialogDelegate:
69 virtual ui::ModalType GetDialogModalType() const OVERRIDE; 69 virtual ui::ModalType GetDialogModalType() const OVERRIDE;
70 virtual string16 GetDialogTitle() const OVERRIDE; 70 virtual string16 GetDialogTitle() const OVERRIDE;
71 virtual GURL GetDialogContentURL() const OVERRIDE; 71 virtual GURL GetDialogContentURL() const OVERRIDE;
72 virtual void GetWebUIMessageHandlers( 72 virtual void GetWebUIMessageHandlers(
73 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; 73 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE;
74 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; 74 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE;
75 virtual std::string GetDialogArgs() const OVERRIDE; 75 virtual std::string GetDialogArgs() const OVERRIDE;
76 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; 76 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE;
77 virtual void OnCloseContents(WebContents* source, 77 virtual void OnCloseContents(WebContents* source,
78 bool* out_close_dialog) OVERRIDE; 78 bool* out_close_dialog) OVERRIDE;
79 virtual bool ShouldShowDialogTitle() const OVERRIDE; 79 virtual bool ShouldShowDialogTitle() const OVERRIDE;
80 80
81 private: 81 private:
82 gfx::Size size_; 82 gfx::Size size_;
83 83
84 DISALLOW_COPY_AND_ASSIGN(PrintPreviewTabDelegate); 84 DISALLOW_COPY_AND_ASSIGN(PrintPreviewTabDelegate);
85 }; 85 };
86 86
87 PrintPreviewTabDelegate::PrintPreviewTabDelegate( 87 PrintPreviewTabDelegate::PrintPreviewTabDelegate(TabContents* initiator_tab) {
88 TabContentsWrapper* initiator_tab) {
89 const gfx::Size kMinDialogSize(800, 480); 88 const gfx::Size kMinDialogSize(800, 480);
90 const int kBorder = 50; 89 const int kBorder = 50;
91 gfx::Rect rect; 90 gfx::Rect rect;
92 initiator_tab->web_contents()->GetContainerBounds(&rect); 91 initiator_tab->web_contents()->GetContainerBounds(&rect);
93 size_.set_width(std::max(rect.width(), kMinDialogSize.width()) - kBorder); 92 size_.set_width(std::max(rect.width(), kMinDialogSize.width()) - kBorder);
94 size_.set_height(std::max(rect.height(), kMinDialogSize.height()) - kBorder); 93 size_.set_height(std::max(rect.height(), kMinDialogSize.height()) - kBorder);
95 94
96 #if defined(OS_MACOSX) 95 #if defined(OS_MACOSX)
97 // Limit the maximum size on MacOS X. 96 // Limit the maximum size on MacOS X.
98 // http://crbug.com/105815 97 // http://crbug.com/105815
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 bool PrintPreviewTabDelegate::ShouldShowDialogTitle() const { 146 bool PrintPreviewTabDelegate::ShouldShowDialogTitle() const {
148 // Not used, returning dummy value. 147 // Not used, returning dummy value.
149 NOTREACHED(); 148 NOTREACHED();
150 return false; 149 return false;
151 } 150 }
152 151
153 // WebContentsDelegate that forwards shortcut keys in the print preview 152 // WebContentsDelegate that forwards shortcut keys in the print preview
154 // renderer to the browser. 153 // renderer to the browser.
155 class PrintPreviewWebContentDelegate : public WebDialogWebContentsDelegate { 154 class PrintPreviewWebContentDelegate : public WebDialogWebContentsDelegate {
156 public: 155 public:
157 PrintPreviewWebContentDelegate(Profile* profile, 156 PrintPreviewWebContentDelegate(Profile* profile, TabContents* initiator_tab);
158 TabContentsWrapper* initiator_tab);
159 virtual ~PrintPreviewWebContentDelegate(); 157 virtual ~PrintPreviewWebContentDelegate();
160 158
161 virtual bool CanReloadContents(WebContents* source) const OVERRIDE; 159 virtual bool CanReloadContents(WebContents* source) const OVERRIDE;
162 virtual void HandleKeyboardEvent( 160 virtual void HandleKeyboardEvent(
163 const NativeWebKeyboardEvent& event) OVERRIDE; 161 const NativeWebKeyboardEvent& event) OVERRIDE;
164 162
165 private: 163 private:
166 TabContentsWrapper* tab_; 164 TabContents* tab_;
167 165
168 DISALLOW_COPY_AND_ASSIGN(PrintPreviewWebContentDelegate); 166 DISALLOW_COPY_AND_ASSIGN(PrintPreviewWebContentDelegate);
169 }; 167 };
170 168
171 PrintPreviewWebContentDelegate::PrintPreviewWebContentDelegate( 169 PrintPreviewWebContentDelegate::PrintPreviewWebContentDelegate(
172 Profile* profile, 170 Profile* profile,
173 TabContentsWrapper* initiator_tab) 171 TabContents* initiator_tab)
174 : WebDialogWebContentsDelegate(profile), 172 : WebDialogWebContentsDelegate(profile),
175 tab_(initiator_tab) {} 173 tab_(initiator_tab) {}
176 174
177 PrintPreviewWebContentDelegate::~PrintPreviewWebContentDelegate() {} 175 PrintPreviewWebContentDelegate::~PrintPreviewWebContentDelegate() {}
178 176
179 bool PrintPreviewWebContentDelegate::CanReloadContents( 177 bool PrintPreviewWebContentDelegate::CanReloadContents(
180 WebContents* source) const { 178 WebContents* source) const {
181 return false; 179 return false;
182 } 180 }
183 181
(...skipping 19 matching lines...) Expand all
203 } 201 }
204 202
205 // static 203 // static
206 PrintPreviewTabController* PrintPreviewTabController::GetInstance() { 204 PrintPreviewTabController* PrintPreviewTabController::GetInstance() {
207 if (!g_browser_process) 205 if (!g_browser_process)
208 return NULL; 206 return NULL;
209 return g_browser_process->print_preview_tab_controller(); 207 return g_browser_process->print_preview_tab_controller();
210 } 208 }
211 209
212 // static 210 // static
213 void PrintPreviewTabController::PrintPreview(TabContentsWrapper* tab) { 211 void PrintPreviewTabController::PrintPreview(TabContents* tab) {
214 if (tab->web_contents()->ShowingInterstitialPage()) 212 if (tab->web_contents()->ShowingInterstitialPage())
215 return; 213 return;
216 214
217 PrintPreviewTabController* tab_controller = GetInstance(); 215 PrintPreviewTabController* tab_controller = GetInstance();
218 if (!tab_controller) 216 if (!tab_controller)
219 return; 217 return;
220 if (!tab_controller->GetOrCreatePreviewTab(tab)) 218 if (!tab_controller->GetOrCreatePreviewTab(tab))
221 tab->print_view_manager()->PrintPreviewDone(); 219 tab->print_view_manager()->PrintPreviewDone();
222 } 220 }
223 221
224 TabContentsWrapper* PrintPreviewTabController::GetOrCreatePreviewTab( 222 TabContents* PrintPreviewTabController::GetOrCreatePreviewTab(
225 TabContentsWrapper* initiator_tab) { 223 TabContents* initiator_tab) {
226 DCHECK(initiator_tab); 224 DCHECK(initiator_tab);
227 225
228 // Get the print preview tab for |initiator_tab|. 226 // Get the print preview tab for |initiator_tab|.
229 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); 227 TabContents* preview_tab = GetPrintPreviewForTab(initiator_tab);
230 if (!preview_tab) 228 if (!preview_tab)
231 return CreatePrintPreviewTab(initiator_tab); 229 return CreatePrintPreviewTab(initiator_tab);
232 230
233 // Show the initiator tab holding the existing preview tab. 231 // Show the initiator tab holding the existing preview tab.
234 WebContents* web_contents = initiator_tab->web_contents(); 232 WebContents* web_contents = initiator_tab->web_contents();
235 web_contents->GetDelegate()->ActivateContents(web_contents); 233 web_contents->GetDelegate()->ActivateContents(web_contents);
236 return preview_tab; 234 return preview_tab;
237 } 235 }
238 236
239 TabContentsWrapper* PrintPreviewTabController::GetPrintPreviewForTab( 237 TabContents* PrintPreviewTabController::GetPrintPreviewForTab(
240 TabContentsWrapper* tab) const { 238 TabContents* tab) const {
241 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then 239 // |preview_tab_map_| is keyed by the preview tab, so if find() succeeds, then
242 // |tab| is the preview tab. 240 // |tab| is the preview tab.
243 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab); 241 PrintPreviewTabMap::const_iterator it = preview_tab_map_.find(tab);
244 if (it != preview_tab_map_.end()) 242 if (it != preview_tab_map_.end())
245 return tab; 243 return tab;
246 244
247 for (it = preview_tab_map_.begin(); it != preview_tab_map_.end(); ++it) { 245 for (it = preview_tab_map_.begin(); it != preview_tab_map_.end(); ++it) {
248 // If |tab| is an initiator tab. 246 // If |tab| is an initiator tab.
249 if (tab == it->second) { 247 if (tab == it->second) {
250 // Return the associated preview tab. 248 // Return the associated preview tab.
251 return it->first; 249 return it->first;
252 } 250 }
253 } 251 }
254 return NULL; 252 return NULL;
255 } 253 }
256 254
257 TabContentsWrapper* PrintPreviewTabController::GetInitiatorTab( 255 TabContents* PrintPreviewTabController::GetInitiatorTab(
258 TabContentsWrapper* preview_tab) { 256 TabContents* preview_tab) {
259 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); 257 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab);
260 if (it != preview_tab_map_.end()) 258 if (it != preview_tab_map_.end())
261 return preview_tab_map_[preview_tab]; 259 return preview_tab_map_[preview_tab];
262 return NULL; 260 return NULL;
263 } 261 }
264 262
265 void PrintPreviewTabController::Observe( 263 void PrintPreviewTabController::Observe(
266 int type, 264 int type,
267 const content::NotificationSource& source, 265 const content::NotificationSource& source,
268 const content::NotificationDetails& details) { 266 const content::NotificationDetails& details) {
269 switch (type) { 267 switch (type) {
270 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { 268 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: {
271 OnRendererProcessClosed( 269 OnRendererProcessClosed(
272 content::Source<content::RenderProcessHost>(source).ptr()); 270 content::Source<content::RenderProcessHost>(source).ptr());
273 break; 271 break;
274 } 272 }
275 case chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED: { 273 case chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED: {
276 OnTabContentsDestroyed(content::Source<TabContentsWrapper>(source).ptr()); 274 OnTabContentsDestroyed(content::Source<TabContents>(source).ptr());
277 break; 275 break;
278 } 276 }
279 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { 277 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
280 NavigationController* controller = 278 NavigationController* controller =
281 content::Source<NavigationController>(source).ptr(); 279 content::Source<NavigationController>(source).ptr();
282 TabContentsWrapper* wrapper = 280 TabContents* tab = TabContents::GetCurrentWrapperForContents(
283 TabContentsWrapper::GetCurrentWrapperForContents( 281 controller->GetWebContents());
284 controller->GetWebContents());
285 content::LoadCommittedDetails* load_details = 282 content::LoadCommittedDetails* load_details =
286 content::Details<content::LoadCommittedDetails>(details).ptr(); 283 content::Details<content::LoadCommittedDetails>(details).ptr();
287 OnNavEntryCommitted(wrapper, load_details); 284 OnNavEntryCommitted(tab, load_details);
288 break; 285 break;
289 } 286 }
290 default: { 287 default: {
291 NOTREACHED(); 288 NOTREACHED();
292 break; 289 break;
293 } 290 }
294 } 291 }
295 } 292 }
296 293
297 // static 294 // static
298 bool PrintPreviewTabController::IsPrintPreviewTab(TabContentsWrapper* tab) { 295 bool PrintPreviewTabController::IsPrintPreviewTab(TabContents* tab) {
299 return IsPrintPreviewURL(tab->web_contents()->GetURL()); 296 return IsPrintPreviewURL(tab->web_contents()->GetURL());
300 } 297 }
301 298
302 // static 299 // static
303 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) { 300 bool PrintPreviewTabController::IsPrintPreviewURL(const GURL& url) {
304 return (url.SchemeIs(chrome::kChromeUIScheme) && 301 return (url.SchemeIs(chrome::kChromeUIScheme) &&
305 url.host() == chrome::kChromeUIPrintHost); 302 url.host() == chrome::kChromeUIPrintHost);
306 } 303 }
307 304
308 void PrintPreviewTabController::EraseInitiatorTabInfo( 305 void PrintPreviewTabController::EraseInitiatorTabInfo(
309 TabContentsWrapper* preview_tab) { 306 TabContents* preview_tab) {
310 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab); 307 PrintPreviewTabMap::iterator it = preview_tab_map_.find(preview_tab);
311 if (it == preview_tab_map_.end()) 308 if (it == preview_tab_map_.end())
312 return; 309 return;
313 310
314 RemoveObservers(it->second); 311 RemoveObservers(it->second);
315 preview_tab_map_[preview_tab] = NULL; 312 preview_tab_map_[preview_tab] = NULL;
316 } 313 }
317 314
318 bool PrintPreviewTabController::is_creating_print_preview_tab() const { 315 bool PrintPreviewTabController::is_creating_print_preview_tab() const {
319 return is_creating_print_preview_tab_; 316 return is_creating_print_preview_tab_;
320 } 317 }
321 318
322 PrintPreviewTabController::~PrintPreviewTabController() {} 319 PrintPreviewTabController::~PrintPreviewTabController() {}
323 320
324 void PrintPreviewTabController::OnRendererProcessClosed( 321 void PrintPreviewTabController::OnRendererProcessClosed(
325 content::RenderProcessHost* rph) { 322 content::RenderProcessHost* rph) {
326 // Store tabs in a vector and deal with them after iterating through 323 // Store tabs in a vector and deal with them after iterating through
327 // |preview_tab_map_| because RemoveFooTab() can change |preview_tab_map_|. 324 // |preview_tab_map_| because RemoveFooTab() can change |preview_tab_map_|.
328 std::vector<TabContentsWrapper*> closed_initiator_tabs; 325 std::vector<TabContents*> closed_initiator_tabs;
329 std::vector<TabContentsWrapper*> closed_preview_tabs; 326 std::vector<TabContents*> closed_preview_tabs;
330 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin(); 327 for (PrintPreviewTabMap::iterator iter = preview_tab_map_.begin();
331 iter != preview_tab_map_.end(); ++iter) { 328 iter != preview_tab_map_.end(); ++iter) {
332 TabContentsWrapper* preview_tab = iter->first; 329 TabContents* preview_tab = iter->first;
333 TabContentsWrapper* initiator_tab = iter->second; 330 TabContents* initiator_tab = iter->second;
334 if (preview_tab->web_contents()->GetRenderProcessHost() == rph) { 331 if (preview_tab->web_contents()->GetRenderProcessHost() == rph) {
335 closed_preview_tabs.push_back(preview_tab); 332 closed_preview_tabs.push_back(preview_tab);
336 } else if (initiator_tab && 333 } else if (initiator_tab &&
337 initiator_tab->web_contents()->GetRenderProcessHost() == rph) { 334 initiator_tab->web_contents()->GetRenderProcessHost() == rph) {
338 closed_initiator_tabs.push_back(initiator_tab); 335 closed_initiator_tabs.push_back(initiator_tab);
339 } 336 }
340 } 337 }
341 338
342 for (size_t i = 0; i < closed_preview_tabs.size(); ++i) { 339 for (size_t i = 0; i < closed_preview_tabs.size(); ++i) {
343 RemovePreviewTab(closed_preview_tabs[i]); 340 RemovePreviewTab(closed_preview_tabs[i]);
344 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 341 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
345 closed_preview_tabs[i]->web_contents()->GetWebUI()->GetController()); 342 closed_preview_tabs[i]->web_contents()->GetWebUI()->GetController());
346 if (print_preview_ui) 343 if (print_preview_ui)
347 print_preview_ui->OnPrintPreviewTabClosed(); 344 print_preview_ui->OnPrintPreviewTabClosed();
348 } 345 }
349 346
350 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i) 347 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i)
351 RemoveInitiatorTab(closed_initiator_tabs[i], false); 348 RemoveInitiatorTab(closed_initiator_tabs[i], false);
352 } 349 }
353 350
354 void PrintPreviewTabController::OnTabContentsDestroyed( 351 void PrintPreviewTabController::OnTabContentsDestroyed(TabContents* tab) {
355 TabContentsWrapper* tab) { 352 TabContents* preview_tab = GetPrintPreviewForTab(tab);
356 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab);
357 if (!preview_tab) { 353 if (!preview_tab) {
358 NOTREACHED(); 354 NOTREACHED();
359 return; 355 return;
360 } 356 }
361 357
362 if (tab == preview_tab) 358 if (tab == preview_tab)
363 RemovePreviewTab(tab); 359 RemovePreviewTab(tab);
364 else 360 else
365 RemoveInitiatorTab(tab, false); 361 RemoveInitiatorTab(tab, false);
366 } 362 }
367 363
368 void PrintPreviewTabController::OnNavEntryCommitted( 364 void PrintPreviewTabController::OnNavEntryCommitted(
369 TabContentsWrapper* tab, content::LoadCommittedDetails* details) { 365 TabContents* tab, content::LoadCommittedDetails* details) {
370 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(tab); 366 TabContents* preview_tab = GetPrintPreviewForTab(tab);
371 if (!preview_tab) { 367 if (!preview_tab) {
372 NOTREACHED(); 368 NOTREACHED();
373 return; 369 return;
374 } 370 }
375 bool source_tab_is_preview_tab = (tab == preview_tab); 371 bool source_tab_is_preview_tab = (tab == preview_tab);
376 372
377 if (source_tab_is_preview_tab) { 373 if (source_tab_is_preview_tab) {
378 // Preview tab navigated. 374 // Preview tab navigated.
379 if (details) { 375 if (details) {
380 content::PageTransition transition_type = 376 content::PageTransition transition_type =
(...skipping 18 matching lines...) Expand all
399 } 395 }
400 } 396 }
401 NOTREACHED(); 397 NOTREACHED();
402 return; 398 return;
403 } 399 }
404 400
405 // Initiator tab navigated. 401 // Initiator tab navigated.
406 RemoveInitiatorTab(tab, true); 402 RemoveInitiatorTab(tab, true);
407 } 403 }
408 404
409 TabContentsWrapper* PrintPreviewTabController::CreatePrintPreviewTab( 405 TabContents* PrintPreviewTabController::CreatePrintPreviewTab(
410 TabContentsWrapper* initiator_tab) { 406 TabContents* initiator_tab) {
411 AutoReset<bool> auto_reset(&is_creating_print_preview_tab_, true); 407 AutoReset<bool> auto_reset(&is_creating_print_preview_tab_, true);
412 WebContents* web_contents = initiator_tab->web_contents(); 408 WebContents* web_contents = initiator_tab->web_contents();
413 Browser* current_browser = 409 Browser* current_browser =
414 browser::FindBrowserWithWebContents(web_contents); 410 browser::FindBrowserWithWebContents(web_contents);
415 if (!current_browser) { 411 if (!current_browser) {
416 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { 412 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) {
417 Profile* profile = 413 Profile* profile =
418 Profile::FromBrowserContext(web_contents->GetBrowserContext()); 414 Profile::FromBrowserContext(web_contents->GetBrowserContext());
419 current_browser = Browser::CreateWithParams( 415 current_browser = Browser::CreateWithParams(
420 Browser::CreateParams(Browser::TYPE_POPUP, profile)); 416 Browser::CreateParams(Browser::TYPE_POPUP, profile));
(...skipping 13 matching lines...) Expand all
434 // |web_tab_content_delegate|'s owner is |constrained_web_ui_delegate|. 430 // |web_tab_content_delegate|'s owner is |constrained_web_ui_delegate|.
435 PrintPreviewWebContentDelegate* pp_wcd = 431 PrintPreviewWebContentDelegate* pp_wcd =
436 new PrintPreviewWebContentDelegate(current_browser->profile(), 432 new PrintPreviewWebContentDelegate(current_browser->profile(),
437 initiator_tab); 433 initiator_tab);
438 ConstrainedWebDialogDelegate* constrained_delegate = 434 ConstrainedWebDialogDelegate* constrained_delegate =
439 ui::CreateConstrainedWebDialog( 435 ui::CreateConstrainedWebDialog(
440 current_browser->profile(), 436 current_browser->profile(),
441 web_dialog_delegate, 437 web_dialog_delegate,
442 pp_wcd, 438 pp_wcd,
443 initiator_tab); 439 initiator_tab);
444 TabContentsWrapper* preview_tab = constrained_delegate->tab(); 440 TabContents* preview_tab = constrained_delegate->tab();
445 EnableInternalPDFPluginForTab(preview_tab); 441 EnableInternalPDFPluginForTab(preview_tab);
446 442
447 // Add an entry to the map. 443 // Add an entry to the map.
448 preview_tab_map_[preview_tab] = initiator_tab; 444 preview_tab_map_[preview_tab] = initiator_tab;
449 waiting_for_new_preview_page_ = true; 445 waiting_for_new_preview_page_ = true;
450 446
451 AddObservers(initiator_tab); 447 AddObservers(initiator_tab);
452 AddObservers(preview_tab); 448 AddObservers(preview_tab);
453 449
454 return preview_tab; 450 return preview_tab;
455 } 451 }
456 452
457 void PrintPreviewTabController::SetInitiatorTabURLAndTitle( 453 void PrintPreviewTabController::SetInitiatorTabURLAndTitle(
458 TabContentsWrapper* preview_tab) { 454 TabContents* preview_tab) {
459 TabContentsWrapper* initiator_tab = GetInitiatorTab(preview_tab); 455 TabContents* initiator_tab = GetInitiatorTab(preview_tab);
460 if (initiator_tab && preview_tab->web_contents()->GetWebUI()) { 456 if (initiator_tab && preview_tab->web_contents()->GetWebUI()) {
461 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 457 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
462 preview_tab->web_contents()->GetWebUI()->GetController()); 458 preview_tab->web_contents()->GetWebUI()->GetController());
463 print_preview_ui->SetInitiatorTabURLAndTitle( 459 print_preview_ui->SetInitiatorTabURLAndTitle(
464 initiator_tab->web_contents()->GetURL().spec(), 460 initiator_tab->web_contents()->GetURL().spec(),
465 initiator_tab->print_view_manager()->RenderSourceName()); 461 initiator_tab->print_view_manager()->RenderSourceName());
466 } 462 }
467 } 463 }
468 464
469 void PrintPreviewTabController::AddObservers(TabContentsWrapper* tab) { 465 void PrintPreviewTabController::AddObservers(TabContents* tab) {
470 WebContents* contents = tab->web_contents(); 466 WebContents* contents = tab->web_contents();
471 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, 467 registrar_.Add(this, chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED,
472 content::Source<TabContentsWrapper>(tab)); 468 content::Source<TabContents>(tab));
473 registrar_.Add( 469 registrar_.Add(
474 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 470 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
475 content::Source<NavigationController>(&contents->GetController())); 471 content::Source<NavigationController>(&contents->GetController()));
476 472
477 // Multiple sites may share the same RenderProcessHost, so check if this 473 // Multiple sites may share the same RenderProcessHost, so check if this
478 // notification has already been added. 474 // notification has already been added.
479 content::RenderProcessHost* rph = tab->web_contents()->GetRenderProcessHost(); 475 content::RenderProcessHost* rph = tab->web_contents()->GetRenderProcessHost();
480 if (!registrar_.IsRegistered(this, 476 if (!registrar_.IsRegistered(this,
481 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 477 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
482 content::Source<content::RenderProcessHost>( 478 content::Source<content::RenderProcessHost>(
483 rph))) { 479 rph))) {
484 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 480 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
485 content::Source<content::RenderProcessHost>(rph)); 481 content::Source<content::RenderProcessHost>(rph));
486 } 482 }
487 } 483 }
488 484
489 void PrintPreviewTabController::RemoveObservers(TabContentsWrapper* tab) { 485 void PrintPreviewTabController::RemoveObservers(TabContents* tab) {
490 WebContents* contents = tab->web_contents(); 486 WebContents* contents = tab->web_contents();
491 registrar_.Remove(this, chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED, 487 registrar_.Remove(this, chrome::NOTIFICATION_TAB_CONTENTS_DESTROYED,
492 content::Source<TabContentsWrapper>(tab)); 488 content::Source<TabContents>(tab));
493 registrar_.Remove( 489 registrar_.Remove(
494 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, 490 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
495 content::Source<NavigationController>(&contents->GetController())); 491 content::Source<NavigationController>(&contents->GetController()));
496 492
497 // Multiple sites may share the same RenderProcessHost, so check if this 493 // Multiple sites may share the same RenderProcessHost, so check if this
498 // notification has already been added. 494 // notification has already been added.
499 content::RenderProcessHost* rph = tab->web_contents()->GetRenderProcessHost(); 495 content::RenderProcessHost* rph = tab->web_contents()->GetRenderProcessHost();
500 if (registrar_.IsRegistered(this, 496 if (registrar_.IsRegistered(this,
501 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 497 content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
502 content::Source<content::RenderProcessHost>( 498 content::Source<content::RenderProcessHost>(
503 rph))) { 499 rph))) {
504 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, 500 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED,
505 content::Source<content::RenderProcessHost>(rph)); 501 content::Source<content::RenderProcessHost>(rph));
506 } 502 }
507 } 503 }
508 504
509 void PrintPreviewTabController::RemoveInitiatorTab( 505 void PrintPreviewTabController::RemoveInitiatorTab(TabContents* initiator_tab,
510 TabContentsWrapper* initiator_tab, bool is_navigation) { 506 bool is_navigation) {
511 TabContentsWrapper* preview_tab = GetPrintPreviewForTab(initiator_tab); 507 TabContents* preview_tab = GetPrintPreviewForTab(initiator_tab);
512 DCHECK(preview_tab); 508 DCHECK(preview_tab);
513 // Update the map entry first, so when the print preview tab gets destroyed 509 // Update the map entry first, so when the print preview tab gets destroyed
514 // and reaches RemovePreviewTab(), it does not attempt to also remove the 510 // and reaches RemovePreviewTab(), it does not attempt to also remove the
515 // initiator tab's observers. 511 // initiator tab's observers.
516 preview_tab_map_[preview_tab] = NULL; 512 preview_tab_map_[preview_tab] = NULL;
517 RemoveObservers(initiator_tab); 513 RemoveObservers(initiator_tab);
518 514
519 // For the navigation case, PrintPreviewDone() has already been called in 515 // For the navigation case, PrintPreviewDone() has already been called in
520 // PrintPreviewMessageHandler::NavigateToPendingEntry(). 516 // PrintPreviewMessageHandler::NavigateToPendingEntry().
521 if (!is_navigation) 517 if (!is_navigation)
522 initiator_tab->print_view_manager()->PrintPreviewDone(); 518 initiator_tab->print_view_manager()->PrintPreviewDone();
523 519
524 // Initiator tab is closed. Close the print preview tab too. 520 // Initiator tab is closed. Close the print preview tab too.
525 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 521 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
526 preview_tab->web_contents()->GetWebUI()->GetController()); 522 preview_tab->web_contents()->GetWebUI()->GetController());
527 if (print_preview_ui) 523 if (print_preview_ui)
528 print_preview_ui->OnInitiatorTabClosed(); 524 print_preview_ui->OnInitiatorTabClosed();
529 } 525 }
530 526
531 void PrintPreviewTabController::RemovePreviewTab( 527 void PrintPreviewTabController::RemovePreviewTab(TabContents* preview_tab) {
532 TabContentsWrapper* preview_tab) {
533 // Remove the initiator tab's observers before erasing the mapping. 528 // Remove the initiator tab's observers before erasing the mapping.
534 TabContentsWrapper* initiator_tab = GetInitiatorTab(preview_tab); 529 TabContents* initiator_tab = GetInitiatorTab(preview_tab);
535 if (initiator_tab) { 530 if (initiator_tab) {
536 RemoveObservers(initiator_tab); 531 RemoveObservers(initiator_tab);
537 initiator_tab->print_view_manager()->PrintPreviewDone(); 532 initiator_tab->print_view_manager()->PrintPreviewDone();
538 } 533 }
539 534
540 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort 535 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort
541 // the initiator tab preview request. 536 // the initiator tab preview request.
542 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 537 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
543 preview_tab->web_contents()->GetWebUI()->GetController()); 538 preview_tab->web_contents()->GetWebUI()->GetController());
544 if (print_preview_ui) 539 if (print_preview_ui)
545 print_preview_ui->OnTabDestroyed(); 540 print_preview_ui->OnTabDestroyed();
546 541
547 preview_tab_map_.erase(preview_tab); 542 preview_tab_map_.erase(preview_tab);
548 RemoveObservers(preview_tab); 543 RemoveObservers(preview_tab);
549 } 544 }
550 545
551 } // namespace printing 546 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698