| 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/print_preview_dialog_controller.h" | 5 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( | 67 ChromePluginServiceFilter::GetInstance()->OverridePluginForTab( |
| 68 preview_dialog->GetRenderProcessHost()->GetID(), | 68 preview_dialog->GetRenderProcessHost()->GetID(), |
| 69 preview_dialog->GetRenderViewHost()->GetRoutingID(), | 69 preview_dialog->GetRenderViewHost()->GetRoutingID(), |
| 70 GURL(), pdf_plugin); | 70 GURL(), pdf_plugin); |
| 71 } | 71 } |
| 72 | 72 |
| 73 // WebDialogDelegate that specifies what the print preview dialog | 73 // WebDialogDelegate that specifies what the print preview dialog |
| 74 // will look like. | 74 // will look like. |
| 75 class PrintPreviewDialogDelegate : public WebDialogDelegate { | 75 class PrintPreviewDialogDelegate : public WebDialogDelegate { |
| 76 public: | 76 public: |
| 77 explicit PrintPreviewDialogDelegate(WebContents* initiator_tab); | 77 explicit PrintPreviewDialogDelegate(WebContents* initiator); |
| 78 virtual ~PrintPreviewDialogDelegate(); | 78 virtual ~PrintPreviewDialogDelegate(); |
| 79 | 79 |
| 80 virtual ui::ModalType GetDialogModalType() const OVERRIDE; | 80 virtual ui::ModalType GetDialogModalType() const OVERRIDE; |
| 81 virtual string16 GetDialogTitle() const OVERRIDE; | 81 virtual string16 GetDialogTitle() const OVERRIDE; |
| 82 virtual GURL GetDialogContentURL() const OVERRIDE; | 82 virtual GURL GetDialogContentURL() const OVERRIDE; |
| 83 virtual void GetWebUIMessageHandlers( | 83 virtual void GetWebUIMessageHandlers( |
| 84 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; | 84 std::vector<WebUIMessageHandler*>* handlers) const OVERRIDE; |
| 85 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; | 85 virtual void GetDialogSize(gfx::Size* size) const OVERRIDE; |
| 86 virtual std::string GetDialogArgs() const OVERRIDE; | 86 virtual std::string GetDialogArgs() const OVERRIDE; |
| 87 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; | 87 virtual void OnDialogClosed(const std::string& json_retval) OVERRIDE; |
| 88 virtual void OnCloseContents(WebContents* source, | 88 virtual void OnCloseContents(WebContents* source, |
| 89 bool* out_close_dialog) OVERRIDE; | 89 bool* out_close_dialog) OVERRIDE; |
| 90 virtual bool ShouldShowDialogTitle() const OVERRIDE; | 90 virtual bool ShouldShowDialogTitle() const OVERRIDE; |
| 91 | 91 |
| 92 private: | 92 private: |
| 93 WebContents* initiator_tab_; | 93 WebContents* initiator_; |
| 94 | 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDelegate); | 95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDelegate); |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 PrintPreviewDialogDelegate::PrintPreviewDialogDelegate( | 98 PrintPreviewDialogDelegate::PrintPreviewDialogDelegate(WebContents* initiator) |
| 99 WebContents* initiator_tab) { | 99 : initiator_(initiator) { |
| 100 initiator_tab_ = initiator_tab; | |
| 101 } | 100 } |
| 102 | 101 |
| 103 PrintPreviewDialogDelegate::~PrintPreviewDialogDelegate() { | 102 PrintPreviewDialogDelegate::~PrintPreviewDialogDelegate() { |
| 104 } | 103 } |
| 105 | 104 |
| 106 ui::ModalType PrintPreviewDialogDelegate::GetDialogModalType() const { | 105 ui::ModalType PrintPreviewDialogDelegate::GetDialogModalType() const { |
| 107 // Not used, returning dummy value. | 106 // Not used, returning dummy value. |
| 108 NOTREACHED(); | 107 NOTREACHED(); |
| 109 return ui::MODAL_TYPE_WINDOW; | 108 return ui::MODAL_TYPE_WINDOW; |
| 110 } | 109 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 122 std::vector<WebUIMessageHandler*>* /* handlers */) const { | 121 std::vector<WebUIMessageHandler*>* /* handlers */) const { |
| 123 // PrintPreviewUI adds its own message handlers. | 122 // PrintPreviewUI adds its own message handlers. |
| 124 } | 123 } |
| 125 | 124 |
| 126 void PrintPreviewDialogDelegate::GetDialogSize(gfx::Size* size) const { | 125 void PrintPreviewDialogDelegate::GetDialogSize(gfx::Size* size) const { |
| 127 DCHECK(size); | 126 DCHECK(size); |
| 128 const gfx::Size kMinDialogSize(800, 480); | 127 const gfx::Size kMinDialogSize(800, 480); |
| 129 const int kBorder = 25; | 128 const int kBorder = 25; |
| 130 const int kConstrainedWindowOverlap = 3; | 129 const int kConstrainedWindowOverlap = 3; |
| 131 gfx::Rect rect; | 130 gfx::Rect rect; |
| 132 initiator_tab_->GetView()->GetContainerBounds(&rect); | 131 initiator_->GetView()->GetContainerBounds(&rect); |
| 133 size->set_width(std::max(rect.width(), kMinDialogSize.width()) - 2 * kBorder); | 132 size->set_width(std::max(rect.width(), kMinDialogSize.width()) - 2 * kBorder); |
| 134 size->set_height(std::max(rect.height(), kMinDialogSize.height()) - kBorder + | 133 size->set_height(std::max(rect.height(), kMinDialogSize.height()) - kBorder + |
| 135 kConstrainedWindowOverlap); | 134 kConstrainedWindowOverlap); |
| 136 | 135 |
| 137 #if defined(OS_MACOSX) | 136 #if defined(OS_MACOSX) |
| 138 // Limit the maximum size on MacOS X. | 137 // Limit the maximum size on MacOS X. |
| 139 // http://crbug.com/105815 | 138 // http://crbug.com/105815 |
| 140 const gfx::Size kMaxDialogSize(1000, 660); | 139 const gfx::Size kMaxDialogSize(1000, 660); |
| 141 size->set_width(std::min(size->width(), kMaxDialogSize.width())); | 140 size->set_width(std::min(size->width(), kMaxDialogSize.width())); |
| 142 size->set_height(std::min(size->height(), kMaxDialogSize.height())); | 141 size->set_height(std::min(size->height(), kMaxDialogSize.height())); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 158 } | 157 } |
| 159 | 158 |
| 160 bool PrintPreviewDialogDelegate::ShouldShowDialogTitle() const { | 159 bool PrintPreviewDialogDelegate::ShouldShowDialogTitle() const { |
| 161 return false; | 160 return false; |
| 162 } | 161 } |
| 163 | 162 |
| 164 // WebContentsDelegate that forwards shortcut keys in the print preview | 163 // WebContentsDelegate that forwards shortcut keys in the print preview |
| 165 // renderer to the browser. | 164 // renderer to the browser. |
| 166 class PrintPreviewWebContentDelegate : public WebDialogWebContentsDelegate { | 165 class PrintPreviewWebContentDelegate : public WebDialogWebContentsDelegate { |
| 167 public: | 166 public: |
| 168 PrintPreviewWebContentDelegate(Profile* profile, WebContents* initiator_tab); | 167 PrintPreviewWebContentDelegate(Profile* profile, WebContents* initiator); |
| 169 virtual ~PrintPreviewWebContentDelegate(); | 168 virtual ~PrintPreviewWebContentDelegate(); |
| 170 | 169 |
| 171 // Overridden from WebDialogWebContentsDelegate: | 170 // Overridden from WebDialogWebContentsDelegate: |
| 172 virtual void HandleKeyboardEvent( | 171 virtual void HandleKeyboardEvent( |
| 173 WebContents* source, | 172 WebContents* source, |
| 174 const NativeWebKeyboardEvent& event) OVERRIDE; | 173 const NativeWebKeyboardEvent& event) OVERRIDE; |
| 175 | 174 |
| 176 private: | 175 private: |
| 177 WebContents* tab_; | 176 WebContents* tab_; |
| 178 | 177 |
| 179 DISALLOW_COPY_AND_ASSIGN(PrintPreviewWebContentDelegate); | 178 DISALLOW_COPY_AND_ASSIGN(PrintPreviewWebContentDelegate); |
| 180 }; | 179 }; |
| 181 | 180 |
| 182 PrintPreviewWebContentDelegate::PrintPreviewWebContentDelegate( | 181 PrintPreviewWebContentDelegate::PrintPreviewWebContentDelegate( |
| 183 Profile* profile, | 182 Profile* profile, |
| 184 WebContents* initiator_tab) | 183 WebContents* initiator) |
| 185 : WebDialogWebContentsDelegate(profile, new ChromeWebContentsHandler), | 184 : WebDialogWebContentsDelegate(profile, new ChromeWebContentsHandler), |
| 186 tab_(initiator_tab) {} | 185 tab_(initiator) {} |
| 187 | 186 |
| 188 PrintPreviewWebContentDelegate::~PrintPreviewWebContentDelegate() {} | 187 PrintPreviewWebContentDelegate::~PrintPreviewWebContentDelegate() {} |
| 189 | 188 |
| 190 void PrintPreviewWebContentDelegate::HandleKeyboardEvent( | 189 void PrintPreviewWebContentDelegate::HandleKeyboardEvent( |
| 191 WebContents* source, | 190 WebContents* source, |
| 192 const NativeWebKeyboardEvent& event) { | 191 const NativeWebKeyboardEvent& event) { |
| 193 // Disabled on Mac due to http://crbug.com/112173 | 192 // Disabled on Mac due to http://crbug.com/112173 |
| 194 #if !defined(OS_MACOSX) | 193 #if !defined(OS_MACOSX) |
| 195 Browser* current_browser = chrome::FindBrowserWithWebContents(tab_); | 194 Browser* current_browser = chrome::FindBrowserWithWebContents(tab_); |
| 196 if (!current_browser) | 195 if (!current_browser) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 209 } | 208 } |
| 210 | 209 |
| 211 // static | 210 // static |
| 212 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { | 211 PrintPreviewDialogController* PrintPreviewDialogController::GetInstance() { |
| 213 if (!g_browser_process) | 212 if (!g_browser_process) |
| 214 return NULL; | 213 return NULL; |
| 215 return g_browser_process->print_preview_dialog_controller(); | 214 return g_browser_process->print_preview_dialog_controller(); |
| 216 } | 215 } |
| 217 | 216 |
| 218 // static | 217 // static |
| 219 void PrintPreviewDialogController::PrintPreview(WebContents* initiator_tab) { | 218 void PrintPreviewDialogController::PrintPreview(WebContents* initiator) { |
| 220 if (initiator_tab->ShowingInterstitialPage()) | 219 if (initiator->ShowingInterstitialPage()) |
| 221 return; | 220 return; |
| 222 | 221 |
| 223 PrintPreviewDialogController* dialog_controller = GetInstance(); | 222 PrintPreviewDialogController* dialog_controller = GetInstance(); |
| 224 if (!dialog_controller) | 223 if (!dialog_controller) |
| 225 return; | 224 return; |
| 226 if (!dialog_controller->GetOrCreatePreviewDialog(initiator_tab)) | 225 if (!dialog_controller->GetOrCreatePreviewDialog(initiator)) |
| 227 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); | 226 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); |
| 228 } | 227 } |
| 229 | 228 |
| 230 WebContents* PrintPreviewDialogController::GetOrCreatePreviewDialog( | 229 WebContents* PrintPreviewDialogController::GetOrCreatePreviewDialog( |
| 231 WebContents* initiator_tab) { | 230 WebContents* initiator) { |
| 232 DCHECK(initiator_tab); | 231 DCHECK(initiator); |
| 233 | 232 |
| 234 // Get the print preview dialog for |initiator_tab|. | 233 // Get the print preview dialog for |initiator|. |
| 235 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab); | 234 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); |
| 236 if (!preview_dialog) | 235 if (!preview_dialog) |
| 237 return CreatePrintPreviewDialog(initiator_tab); | 236 return CreatePrintPreviewDialog(initiator); |
| 238 | 237 |
| 239 // Show the initiator tab holding the existing preview dialog. | 238 // Show the initiator holding the existing preview dialog. |
| 240 initiator_tab->GetDelegate()->ActivateContents(initiator_tab); | 239 initiator->GetDelegate()->ActivateContents(initiator); |
| 241 return preview_dialog; | 240 return preview_dialog; |
| 242 } | 241 } |
| 243 | 242 |
| 244 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( | 243 WebContents* PrintPreviewDialogController::GetPrintPreviewForContents( |
| 245 WebContents* contents) const { | 244 WebContents* contents) const { |
| 246 // |preview_dialog_map_| is keyed by the preview dialog, so if find() | 245 // |preview_dialog_map_| is keyed by the preview dialog, so if find() |
| 247 // succeeds, then |contents| is the preview dialog. | 246 // succeeds, then |contents| is the preview dialog. |
| 248 PrintPreviewDialogMap::const_iterator it = preview_dialog_map_.find(contents); | 247 PrintPreviewDialogMap::const_iterator it = preview_dialog_map_.find(contents); |
| 249 if (it != preview_dialog_map_.end()) | 248 if (it != preview_dialog_map_.end()) |
| 250 return contents; | 249 return contents; |
| 251 | 250 |
| 252 for (it = preview_dialog_map_.begin(); | 251 for (it = preview_dialog_map_.begin(); |
| 253 it != preview_dialog_map_.end(); | 252 it != preview_dialog_map_.end(); |
| 254 ++it) { | 253 ++it) { |
| 255 // If |contents| is an initiator tab. | 254 // If |contents| is an initiator. |
| 256 if (contents == it->second) { | 255 if (contents == it->second) { |
| 257 // Return the associated preview dialog. | 256 // Return the associated preview dialog. |
| 258 return it->first; | 257 return it->first; |
| 259 } | 258 } |
| 260 } | 259 } |
| 261 return NULL; | 260 return NULL; |
| 262 } | 261 } |
| 263 | 262 |
| 264 WebContents* PrintPreviewDialogController::GetInitiatorTab( | 263 WebContents* PrintPreviewDialogController::GetInitiator( |
| 265 WebContents* preview_dialog) { | 264 WebContents* preview_dialog) { |
| 266 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); | 265 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); |
| 267 return (it != preview_dialog_map_.end()) ? it->second : NULL; | 266 return (it != preview_dialog_map_.end()) ? it->second : NULL; |
| 268 } | 267 } |
| 269 | 268 |
| 270 void PrintPreviewDialogController::Observe( | 269 void PrintPreviewDialogController::Observe( |
| 271 int type, | 270 int type, |
| 272 const content::NotificationSource& source, | 271 const content::NotificationSource& source, |
| 273 const content::NotificationDetails& details) { | 272 const content::NotificationDetails& details) { |
| 274 if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { | 273 if (type == content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 290 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { | 289 bool PrintPreviewDialogController::IsPrintPreviewDialog(WebContents* contents) { |
| 291 return IsPrintPreviewURL(contents->GetURL()); | 290 return IsPrintPreviewURL(contents->GetURL()); |
| 292 } | 291 } |
| 293 | 292 |
| 294 // static | 293 // static |
| 295 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { | 294 bool PrintPreviewDialogController::IsPrintPreviewURL(const GURL& url) { |
| 296 return (url.SchemeIs(chrome::kChromeUIScheme) && | 295 return (url.SchemeIs(chrome::kChromeUIScheme) && |
| 297 url.host() == chrome::kChromeUIPrintHost); | 296 url.host() == chrome::kChromeUIPrintHost); |
| 298 } | 297 } |
| 299 | 298 |
| 300 void PrintPreviewDialogController::EraseInitiatorTabInfo( | 299 void PrintPreviewDialogController::EraseInitiatorInfo( |
| 301 WebContents* preview_dialog) { | 300 WebContents* preview_dialog) { |
| 302 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); | 301 PrintPreviewDialogMap::iterator it = preview_dialog_map_.find(preview_dialog); |
| 303 if (it == preview_dialog_map_.end()) | 302 if (it == preview_dialog_map_.end()) |
| 304 return; | 303 return; |
| 305 | 304 |
| 306 RemoveObservers(it->second); | 305 RemoveObservers(it->second, INITIATOR); |
| 307 preview_dialog_map_[preview_dialog] = NULL; | 306 preview_dialog_map_[preview_dialog] = NULL; |
| 308 } | 307 } |
| 309 | 308 |
| 310 PrintPreviewDialogController::~PrintPreviewDialogController() {} | 309 PrintPreviewDialogController::~PrintPreviewDialogController() {} |
| 311 | 310 |
| 312 void PrintPreviewDialogController::OnRendererProcessClosed( | 311 void PrintPreviewDialogController::OnRendererProcessClosed( |
| 313 content::RenderProcessHost* rph) { | 312 content::RenderProcessHost* rph) { |
| 314 // Store contents in a vector and deal with them after iterating through | 313 // Store contents in a vector and deal with them after iterating through |
| 315 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|. | 314 // |preview_dialog_map_| because RemoveFoo() can change |preview_dialog_map_|. |
| 316 std::vector<WebContents*> closed_initiator_tabs; | 315 std::vector<WebContents*> closed_initiators; |
| 317 std::vector<WebContents*> closed_preview_dialogs; | 316 std::vector<WebContents*> closed_preview_dialogs; |
| 318 for (PrintPreviewDialogMap::iterator iter = preview_dialog_map_.begin(); | 317 for (PrintPreviewDialogMap::iterator iter = preview_dialog_map_.begin(); |
| 319 iter != preview_dialog_map_.end(); ++iter) { | 318 iter != preview_dialog_map_.end(); ++iter) { |
| 320 WebContents* preview_dialog = iter->first; | 319 WebContents* preview_dialog = iter->first; |
| 321 WebContents* initiator_tab = iter->second; | 320 WebContents* initiator = iter->second; |
| 322 if (preview_dialog->GetRenderProcessHost() == rph) { | 321 if (preview_dialog->GetRenderProcessHost() == rph) { |
| 323 closed_preview_dialogs.push_back(preview_dialog); | 322 closed_preview_dialogs.push_back(preview_dialog); |
| 324 } else if (initiator_tab && | 323 } else if (initiator && |
| 325 initiator_tab->GetRenderProcessHost() == rph) { | 324 initiator->GetRenderProcessHost() == rph) { |
| 326 closed_initiator_tabs.push_back(initiator_tab); | 325 closed_initiators.push_back(initiator); |
| 327 } | 326 } |
| 328 } | 327 } |
| 329 | 328 |
| 330 for (size_t i = 0; i < closed_preview_dialogs.size(); ++i) { | 329 for (size_t i = 0; i < closed_preview_dialogs.size(); ++i) { |
| 331 RemovePreviewDialog(closed_preview_dialogs[i]); | 330 RemovePreviewDialog(closed_preview_dialogs[i]); |
| 332 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 331 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 333 closed_preview_dialogs[i]->GetWebUI()->GetController()); | 332 closed_preview_dialogs[i]->GetWebUI()->GetController()); |
| 334 if (print_preview_ui) | 333 if (print_preview_ui) |
| 335 print_preview_ui->OnPrintPreviewDialogClosed(); | 334 print_preview_ui->OnPrintPreviewDialogClosed(); |
| 336 } | 335 } |
| 337 | 336 |
| 338 for (size_t i = 0; i < closed_initiator_tabs.size(); ++i) | 337 for (size_t i = 0; i < closed_initiators.size(); ++i) |
| 339 RemoveInitiatorTab(closed_initiator_tabs[i]); | 338 RemoveInitiator(closed_initiators[i]); |
| 340 } | 339 } |
| 341 | 340 |
| 342 void PrintPreviewDialogController::OnWebContentsDestroyed( | 341 void PrintPreviewDialogController::OnWebContentsDestroyed( |
| 343 WebContents* contents) { | 342 WebContents* contents) { |
| 344 WebContents* preview_dialog = GetPrintPreviewForContents(contents); | 343 WebContents* preview_dialog = GetPrintPreviewForContents(contents); |
| 345 if (!preview_dialog) { | 344 if (!preview_dialog) { |
| 346 NOTREACHED(); | 345 NOTREACHED(); |
| 347 return; | 346 return; |
| 348 } | 347 } |
| 349 | 348 |
| 350 if (contents == preview_dialog) | 349 if (contents == preview_dialog) |
| 351 RemovePreviewDialog(contents); | 350 RemovePreviewDialog(contents); |
| 352 else | 351 else |
| 353 RemoveInitiatorTab(contents); | 352 RemoveInitiator(contents); |
| 354 } | 353 } |
| 355 | 354 |
| 356 void PrintPreviewDialogController::OnNavEntryCommitted( | 355 void PrintPreviewDialogController::OnNavEntryCommitted( |
| 357 WebContents* contents, content::LoadCommittedDetails* details) { | 356 WebContents* contents, content::LoadCommittedDetails* details) { |
| 358 WebContents* preview_dialog = GetPrintPreviewForContents(contents); | 357 WebContents* preview_dialog = GetPrintPreviewForContents(contents); |
| 359 if (!preview_dialog) { | 358 if (!preview_dialog) { |
| 360 NOTREACHED(); | 359 NOTREACHED(); |
| 361 return; | 360 return; |
| 362 } | 361 } |
| 363 | 362 |
| 364 if (contents == preview_dialog) { | 363 DCHECK_EQ(contents, preview_dialog); |
| 365 // Preview dialog navigated. | |
| 366 if (details) { | |
| 367 content::PageTransition transition_type = | |
| 368 details->entry->GetTransitionType(); | |
| 369 content::NavigationType nav_type = details->type; | |
| 370 | 364 |
| 371 // New |preview_dialog| is created. Don't update/erase map entry. | 365 // Preview dialog navigated. |
| 372 if (waiting_for_new_preview_page_ && | 366 if (details) { |
| 373 transition_type == content::PAGE_TRANSITION_AUTO_TOPLEVEL && | 367 content::PageTransition transition_type = |
| 374 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { | 368 details->entry->GetTransitionType(); |
| 375 waiting_for_new_preview_page_ = false; | 369 content::NavigationType nav_type = details->type; |
| 376 SaveInitiatorTabTitle(preview_dialog); | |
| 377 return; | |
| 378 } | |
| 379 | 370 |
| 380 // Cloud print sign-in causes a reload. | 371 // New |preview_dialog| is created. Don't update/erase map entry. |
| 381 if (!waiting_for_new_preview_page_ && | 372 if (waiting_for_new_preview_page_ && |
| 382 transition_type == content::PAGE_TRANSITION_RELOAD && | 373 transition_type == content::PAGE_TRANSITION_AUTO_TOPLEVEL && |
| 383 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE && | 374 nav_type == content::NAVIGATION_TYPE_NEW_PAGE) { |
| 384 IsPrintPreviewURL(details->previous_url)) { | 375 waiting_for_new_preview_page_ = false; |
| 385 return; | 376 SaveInitiatorTitle(preview_dialog); |
| 386 } | 377 return; |
| 387 } | 378 } |
| 388 NOTREACHED(); | 379 |
| 389 return; | 380 // Cloud print sign-in causes a reload. |
| 381 if (!waiting_for_new_preview_page_ && |
| 382 transition_type == content::PAGE_TRANSITION_RELOAD && |
| 383 nav_type == content::NAVIGATION_TYPE_EXISTING_PAGE && |
| 384 IsPrintPreviewURL(details->previous_url)) { |
| 385 return; |
| 386 } |
| 390 } | 387 } |
| 391 | 388 |
| 392 RemoveInitiatorTab(contents); | 389 NOTREACHED(); |
| 393 } | 390 } |
| 394 | 391 |
| 395 WebContents* PrintPreviewDialogController::CreatePrintPreviewDialog( | 392 WebContents* PrintPreviewDialogController::CreatePrintPreviewDialog( |
| 396 WebContents* initiator_tab) { | 393 WebContents* initiator) { |
| 397 base::AutoReset<bool> auto_reset(&is_creating_print_preview_dialog_, true); | 394 base::AutoReset<bool> auto_reset(&is_creating_print_preview_dialog_, true); |
| 398 Profile* profile = | 395 Profile* profile = |
| 399 Profile::FromBrowserContext(initiator_tab->GetBrowserContext()); | 396 Profile::FromBrowserContext(initiator->GetBrowserContext()); |
| 400 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { | 397 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kChromeFrame)) { |
| 401 // Chrome Frame only ever runs on the native desktop, so it is safe to | 398 // Chrome Frame only ever runs on the native desktop, so it is safe to |
| 402 // create the popup on the native desktop. | 399 // create the popup on the native desktop. |
| 403 Browser* current_browser = new Browser( | 400 Browser* current_browser = new Browser( |
| 404 Browser::CreateParams(Browser::TYPE_POPUP, profile, | 401 Browser::CreateParams(Browser::TYPE_POPUP, profile, |
| 405 chrome::GetActiveDesktop())); | 402 chrome::GetActiveDesktop())); |
| 406 if (!current_browser) { | 403 if (!current_browser) { |
| 407 NOTREACHED() << "Failed to create popup browser window"; | 404 NOTREACHED() << "Failed to create popup browser window"; |
| 408 return NULL; | 405 return NULL; |
| 409 } | 406 } |
| 410 } | 407 } |
| 411 | 408 |
| 412 // |web_dialog_ui_delegate| deletes itself in | 409 // |web_dialog_ui_delegate| deletes itself in |
| 413 // PrintPreviewDialogDelegate::OnDialogClosed(). | 410 // PrintPreviewDialogDelegate::OnDialogClosed(). |
| 414 WebDialogDelegate* web_dialog_delegate = | 411 WebDialogDelegate* web_dialog_delegate = |
| 415 new PrintPreviewDialogDelegate(initiator_tab); | 412 new PrintPreviewDialogDelegate(initiator); |
| 416 // |web_dialog_delegate|'s owner is |constrained_delegate|. | 413 // |web_dialog_delegate|'s owner is |constrained_delegate|. |
| 417 PrintPreviewWebContentDelegate* pp_wcd = | 414 PrintPreviewWebContentDelegate* pp_wcd = |
| 418 new PrintPreviewWebContentDelegate(profile, initiator_tab); | 415 new PrintPreviewWebContentDelegate(profile, initiator); |
| 419 ConstrainedWebDialogDelegate* constrained_delegate = | 416 ConstrainedWebDialogDelegate* constrained_delegate = |
| 420 CreateConstrainedWebDialog(profile, | 417 CreateConstrainedWebDialog(profile, |
| 421 web_dialog_delegate, | 418 web_dialog_delegate, |
| 422 pp_wcd, | 419 pp_wcd, |
| 423 initiator_tab); | 420 initiator); |
| 424 WebContents* preview_dialog = constrained_delegate->GetWebContents(); | 421 WebContents* preview_dialog = constrained_delegate->GetWebContents(); |
| 425 EnableInternalPDFPluginForContents(preview_dialog); | 422 EnableInternalPDFPluginForContents(preview_dialog); |
| 426 PrintViewManager::CreateForWebContents(preview_dialog); | 423 PrintViewManager::CreateForWebContents(preview_dialog); |
| 427 | 424 |
| 428 // Add an entry to the map. | 425 // Add an entry to the map. |
| 429 preview_dialog_map_[preview_dialog] = initiator_tab; | 426 preview_dialog_map_[preview_dialog] = initiator; |
| 430 waiting_for_new_preview_page_ = true; | 427 waiting_for_new_preview_page_ = true; |
| 431 | 428 |
| 432 AddObservers(initiator_tab); | 429 AddObservers(initiator, INITIATOR); |
| 433 AddObservers(preview_dialog); | 430 AddObservers(preview_dialog, PREVIEW_DIALOG); |
| 434 | 431 |
| 435 return preview_dialog; | 432 return preview_dialog; |
| 436 } | 433 } |
| 437 | 434 |
| 438 void PrintPreviewDialogController::SaveInitiatorTabTitle( | 435 void PrintPreviewDialogController::SaveInitiatorTitle( |
| 439 WebContents* preview_dialog) { | 436 WebContents* preview_dialog) { |
| 440 WebContents* initiator_tab = GetInitiatorTab(preview_dialog); | 437 WebContents* initiator = GetInitiator(preview_dialog); |
| 441 if (initiator_tab && preview_dialog->GetWebUI()) { | 438 if (initiator && preview_dialog->GetWebUI()) { |
| 442 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 439 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
| 443 preview_dialog->GetWebUI()->GetController()); | 440 preview_dialog->GetWebUI()->GetController()); |
| 444 print_preview_ui->SetInitiatorTabTitle( | 441 print_preview_ui->SetInitiatorTitle( |
| 445 PrintViewManager::FromWebContents(initiator_tab)->RenderSourceName()); | 442 PrintViewManager::FromWebContents(initiator)->RenderSourceName()); |
| 446 } | 443 } |
| 447 } | 444 } |
| 448 | 445 |
| 449 void PrintPreviewDialogController::AddObservers(WebContents* contents) { | 446 void PrintPreviewDialogController::AddObservers(WebContents* contents, |
| 447 ContentsType contents_type) { |
| 450 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 448 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 451 content::Source<WebContents>(contents)); | 449 content::Source<WebContents>(contents)); |
| 452 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 450 if (contents_type == PREVIEW_DIALOG) { |
| 453 content::Source<NavigationController>(&contents->GetController())); | 451 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 452 content::Source<NavigationController>(&contents->GetController())); |
| 453 } |
| 454 | 454 |
| 455 // Multiple sites may share the same RenderProcessHost, so check if this | 455 // Multiple sites may share the same RenderProcessHost, so check if this |
| 456 // notification has already been added. | 456 // notification has already been added. |
| 457 content::Source<content::RenderProcessHost> rph_source( | 457 content::Source<content::RenderProcessHost> rph_source( |
| 458 contents->GetRenderProcessHost()); | 458 contents->GetRenderProcessHost()); |
| 459 if (!registrar_.IsRegistered(this, | 459 if (!registrar_.IsRegistered(this, |
| 460 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | 460 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { |
| 461 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 461 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 462 rph_source); | 462 rph_source); |
| 463 } | 463 } |
| 464 } | 464 } |
| 465 | 465 |
| 466 void PrintPreviewDialogController::RemoveObservers(WebContents* contents) { | 466 void PrintPreviewDialogController::RemoveObservers(WebContents* contents, |
| 467 ContentsType contents_type) { |
| 467 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | 468 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, |
| 468 content::Source<WebContents>(contents)); | 469 content::Source<WebContents>(contents)); |
| 469 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 470 if (contents_type == PREVIEW_DIALOG) { |
| 470 content::Source<NavigationController>(&contents->GetController())); | 471 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 472 content::Source<NavigationController>(&contents->GetController())); |
| 473 } |
| 471 | 474 |
| 472 // Multiple sites may share the same RenderProcessHost, so check if this | 475 // Multiple sites may share the same RenderProcessHost, so check if this |
| 473 // notification has already been added. | 476 // notification has already been added. |
| 474 content::Source<content::RenderProcessHost> rph_source( | 477 content::Source<content::RenderProcessHost> rph_source( |
| 475 contents->GetRenderProcessHost()); | 478 contents->GetRenderProcessHost()); |
| 476 if (registrar_.IsRegistered(this, | 479 if (registrar_.IsRegistered(this, |
| 477 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { | 480 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, rph_source)) { |
| 478 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 481 registrar_.Remove(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 479 rph_source); | 482 rph_source); |
| 480 } | 483 } |
| 481 } | 484 } |
| 482 | 485 |
| 483 void PrintPreviewDialogController::RemoveInitiatorTab( | 486 void PrintPreviewDialogController::RemoveInitiator( |
| 484 WebContents* initiator_tab) { | 487 WebContents* initiator) { |
| 485 WebContents* preview_dialog = GetPrintPreviewForContents(initiator_tab); | 488 WebContents* preview_dialog = GetPrintPreviewForContents(initiator); |
| 486 DCHECK(preview_dialog); | 489 DCHECK(preview_dialog); |
| 487 // Update the map entry first, so when the print preview dialog gets destroyed | 490 // Update the map entry first, so when the print preview dialog gets destroyed |
| 488 // and reaches RemovePreviewDialog(), it does not attempt to also remove the | 491 // and reaches RemovePreviewDialog(), it does not attempt to also remove the |
| 489 // initiator tab's observers. | 492 // initiator's observers. |
| 490 preview_dialog_map_[preview_dialog] = NULL; | 493 preview_dialog_map_[preview_dialog] = NULL; |
| 491 RemoveObservers(initiator_tab); | 494 RemoveObservers(initiator, INITIATOR); |
| 492 | 495 |
| 493 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); | 496 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); |
| 494 | 497 |
| 495 // Initiator tab is closed. Close the print preview dialog too. | 498 // initiator is closed. Close the print preview dialog too. |
| 496 PrintPreviewUI* print_preview_ui = | 499 PrintPreviewUI* print_preview_ui = |
| 497 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); | 500 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); |
| 498 if (print_preview_ui) | 501 if (print_preview_ui) |
| 499 print_preview_ui->OnInitiatorTabClosed(); | 502 print_preview_ui->OnInitiatorClosed(); |
| 500 } | 503 } |
| 501 | 504 |
| 502 void PrintPreviewDialogController::RemovePreviewDialog( | 505 void PrintPreviewDialogController::RemovePreviewDialog( |
| 503 WebContents* preview_dialog) { | 506 WebContents* preview_dialog) { |
| 504 // Remove the initiator tab's observers before erasing the mapping. | 507 // Remove the initiator's observers before erasing the mapping. |
| 505 WebContents* initiator_tab = GetInitiatorTab(preview_dialog); | 508 WebContents* initiator = GetInitiator(preview_dialog); |
| 506 if (initiator_tab) { | 509 if (initiator) { |
| 507 RemoveObservers(initiator_tab); | 510 RemoveObservers(initiator, INITIATOR); |
| 508 PrintViewManager::FromWebContents(initiator_tab)->PrintPreviewDone(); | 511 PrintViewManager::FromWebContents(initiator)->PrintPreviewDone(); |
| 509 } | 512 } |
| 510 | 513 |
| 511 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort | 514 // Print preview WebContents is destroyed. Notify |PrintPreviewUI| to abort |
| 512 // the initiator tab preview request. | 515 // the initiator preview request. |
| 513 PrintPreviewUI* print_preview_ui = | 516 PrintPreviewUI* print_preview_ui = |
| 514 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); | 517 static_cast<PrintPreviewUI*>(preview_dialog->GetWebUI()->GetController()); |
| 515 if (print_preview_ui) | 518 if (print_preview_ui) |
| 516 print_preview_ui->OnPrintPreviewDialogDestroyed(); | 519 print_preview_ui->OnPrintPreviewDialogDestroyed(); |
| 517 | 520 |
| 518 preview_dialog_map_.erase(preview_dialog); | 521 preview_dialog_map_.erase(preview_dialog); |
| 519 RemoveObservers(preview_dialog); | 522 RemoveObservers(preview_dialog, PREVIEW_DIALOG); |
| 520 } | 523 } |
| 521 | 524 |
| 522 } // namespace printing | 525 } // namespace printing |
| OLD | NEW |