| 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 <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 void set_quit_closure(const base::Closure& quit_closure) { | 51 void set_quit_closure(const base::Closure& quit_closure) { |
| 52 quit_closure_ = quit_closure; | 52 quit_closure_ = quit_closure; |
| 53 } | 53 } |
| 54 | 54 |
| 55 private: | 55 private: |
| 56 // content::WebContentsObserver implementation. | 56 // content::WebContentsObserver implementation. |
| 57 bool OnMessageReceived(const IPC::Message& message) override { | 57 bool OnMessageReceived(const IPC::Message& message) override { |
| 58 IPC_BEGIN_MESSAGE_MAP(RequestPrintPreviewObserver, message) | 58 IPC_BEGIN_MESSAGE_MAP(RequestPrintPreviewObserver, message) |
| 59 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, | 59 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, |
| 60 OnRequestPrintPreview) | 60 OnRequestPrintPreview) |
| 61 IPC_MESSAGE_HANDLER(PrintHostMsg_SetupScriptedPrintPreview, |
| 62 OnSetupScriptedPrintPreview) |
| 61 IPC_MESSAGE_UNHANDLED(break) | 63 IPC_MESSAGE_UNHANDLED(break) |
| 62 IPC_END_MESSAGE_MAP() | 64 IPC_END_MESSAGE_MAP() |
| 63 return false; // Report not handled so the real handler receives it. | 65 return false; // Report not handled so the real handler receives it. |
| 64 } | 66 } |
| 65 | 67 |
| 66 void OnRequestPrintPreview( | 68 void OnRequestPrintPreview( |
| 67 const PrintHostMsg_RequestPrintPreview_Params& /* params */) { | 69 const PrintHostMsg_RequestPrintPreview_Params& /* params */) { |
| 68 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | 70 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); |
| 69 } | 71 } |
| 70 | 72 |
| 73 void OnSetupScriptedPrintPreview() { |
| 74 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); |
| 75 } |
| 76 |
| 71 base::Closure quit_closure_; | 77 base::Closure quit_closure_; |
| 72 | 78 |
| 73 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver); | 79 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver); |
| 74 }; | 80 }; |
| 75 | 81 |
| 76 class PrintPreviewDialogClonedObserver : public WebContentsObserver { | 82 class PrintPreviewDialogClonedObserver : public WebContentsObserver { |
| 77 public: | 83 public: |
| 78 explicit PrintPreviewDialogClonedObserver(WebContents* dialog) | 84 explicit PrintPreviewDialogClonedObserver(WebContents* dialog) |
| 79 : WebContentsObserver(dialog) { | 85 : WebContentsObserver(dialog) { |
| 80 } | 86 } |
| 81 ~PrintPreviewDialogClonedObserver() override {} | 87 ~PrintPreviewDialogClonedObserver() override {} |
| 82 | 88 |
| 83 RequestPrintPreviewObserver* request_preview_dialog_observer() { | 89 RequestPrintPreviewObserver* request_preview_dialog_observer() { |
| 84 return request_preview_dialog_observer_.get(); | 90 return request_preview_dialog_observer_.get(); |
| 85 } | 91 } |
| 86 | 92 |
| 87 private: | 93 private: |
| 88 // content::WebContentsObserver implementation. | 94 // content::WebContentsObserver implementation. |
| 89 void DidCloneToNewWebContents(WebContents* old_web_contents, | 95 void DidCloneToNewWebContents(WebContents* old_web_contents, |
| 90 WebContents* new_web_contents) override { | 96 WebContents* new_web_contents) override { |
| 91 request_preview_dialog_observer_.reset( | 97 request_preview_dialog_observer_.reset( |
| 92 new RequestPrintPreviewObserver(new_web_contents)); | 98 new RequestPrintPreviewObserver(new_web_contents)); |
| 93 } | 99 } |
| 94 | 100 |
| 95 std::unique_ptr<RequestPrintPreviewObserver> request_preview_dialog_observer_; | 101 std::unique_ptr<RequestPrintPreviewObserver> request_preview_dialog_observer_; |
| 96 | 102 |
| 97 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver); | 103 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver); |
| 98 }; | 104 }; |
| 99 | 105 |
| 106 class PrintPreviewDialogStartedObserver : public WebContentsObserver { |
| 107 public: |
| 108 explicit PrintPreviewDialogStartedObserver(WebContents* dialog) |
| 109 : WebContentsObserver(dialog), waiting_(false), dialog_started_(false) {} |
| 110 ~PrintPreviewDialogStartedObserver() override {} |
| 111 |
| 112 bool dialog_started() const { return dialog_started_; } |
| 113 |
| 114 void WaitForDialogStarted() { |
| 115 waiting_ = true; |
| 116 run_loop_.Run(); |
| 117 } |
| 118 |
| 119 private: |
| 120 // content::WebContentsObserver implementation. |
| 121 void RenderViewReady() override { |
| 122 dialog_started_ = true; |
| 123 if (waiting_) |
| 124 run_loop_.Quit(); |
| 125 } |
| 126 |
| 127 void WebContentsDestroyed() override { |
| 128 if (waiting_) |
| 129 run_loop_.Quit(); |
| 130 } |
| 131 |
| 132 bool waiting_; |
| 133 bool dialog_started_; |
| 134 base::RunLoop run_loop_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogStartedObserver); |
| 137 }; |
| 138 |
| 100 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { | 139 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { |
| 101 public: | 140 public: |
| 102 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) | 141 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) |
| 103 : WebContentsObserver(dialog), | 142 : WebContentsObserver(dialog), |
| 104 dialog_destroyed_(false) { | 143 waiting_(false), |
| 105 } | 144 dialog_destroyed_(false) {} |
| 106 ~PrintPreviewDialogDestroyedObserver() override {} | 145 ~PrintPreviewDialogDestroyedObserver() override {} |
| 107 | 146 |
| 108 bool dialog_destroyed() const { return dialog_destroyed_; } | 147 bool dialog_destroyed() const { return dialog_destroyed_; } |
| 109 | 148 |
| 149 void WaitForDialogDestroyed() { |
| 150 waiting_ = true; |
| 151 run_loop_.Run(); |
| 152 } |
| 153 |
| 110 private: | 154 private: |
| 111 // content::WebContentsObserver implementation. | 155 // content::WebContentsObserver implementation. |
| 112 void WebContentsDestroyed() override { dialog_destroyed_ = true; } | 156 void WebContentsDestroyed() override { |
| 157 dialog_destroyed_ = true; |
| 158 if (waiting_) |
| 159 run_loop_.Quit(); |
| 160 } |
| 113 | 161 |
| 162 bool waiting_; |
| 114 bool dialog_destroyed_; | 163 bool dialog_destroyed_; |
| 164 base::RunLoop run_loop_; |
| 115 | 165 |
| 116 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); | 166 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
| 117 }; | 167 }; |
| 118 | 168 |
| 119 void PluginsLoadedCallback( | 169 void PluginsLoadedCallback( |
| 120 const base::Closure& quit_closure, | 170 const base::Closure& quit_closure, |
| 121 const std::vector<content::WebPluginInfo>& /* info */) { | 171 const std::vector<content::WebPluginInfo>& /* info */) { |
| 122 quit_closure.Run(); | 172 quit_closure.Run(); |
| 123 } | 173 } |
| 124 | 174 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 return initiator_; | 211 return initiator_; |
| 162 } | 212 } |
| 163 | 213 |
| 164 void PrintPreview() { | 214 void PrintPreview() { |
| 165 base::RunLoop run_loop; | 215 base::RunLoop run_loop; |
| 166 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); | 216 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); |
| 167 chrome::Print(browser()); | 217 chrome::Print(browser()); |
| 168 run_loop.Run(); | 218 run_loop.Run(); |
| 169 } | 219 } |
| 170 | 220 |
| 221 void PrintPreviewViaScript(base::StringPiece script) { |
| 222 base::RunLoop run_loop; |
| 223 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); |
| 224 initiator()->GetMainFrame()->ExecuteJavaScriptForTests( |
| 225 base::ASCIIToUTF16(script)); |
| 226 run_loop.Run(); |
| 227 } |
| 228 |
| 171 WebContents* GetPrintPreviewDialog() { | 229 WebContents* GetPrintPreviewDialog() { |
| 172 printing::PrintPreviewDialogController* dialog_controller = | 230 printing::PrintPreviewDialogController* dialog_controller = |
| 173 printing::PrintPreviewDialogController::GetInstance(); | 231 printing::PrintPreviewDialogController::GetInstance(); |
| 174 return dialog_controller->GetPrintPreviewForContents(initiator_); | 232 return dialog_controller->GetPrintPreviewForContents(initiator_); |
| 175 } | 233 } |
| 176 | 234 |
| 177 private: | 235 private: |
| 178 void SetUpOnMainThread() override { | 236 void SetUpOnMainThread() override { |
| 179 WebContents* first_tab = | 237 WebContents* first_tab = |
| 180 browser()->tab_strip_model()->GetActiveWebContents(); | 238 browser()->tab_strip_model()->GetActiveWebContents(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 // Try printing again. | 291 // Try printing again. |
| 234 PrintPreview(); | 292 PrintPreview(); |
| 235 | 293 |
| 236 // Get the print preview dialog for the initiator tab. | 294 // Get the print preview dialog for the initiator tab. |
| 237 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | 295 WebContents* new_preview_dialog = GetPrintPreviewDialog(); |
| 238 | 296 |
| 239 // Check a new preview dialog got created. | 297 // Check a new preview dialog got created. |
| 240 EXPECT_TRUE(new_preview_dialog); | 298 EXPECT_TRUE(new_preview_dialog); |
| 241 } | 299 } |
| 242 | 300 |
| 301 // Ensure that back/forward navigations with ScopedPageLoadDeferrers (e.g., |
| 302 // during printing) end up committing to the correct NavigationEntry. |
| 303 // See https://crbug.com/626838. |
| 304 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
| 305 HistoryNavigateDuringPrint) { |
| 306 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents(); |
| 307 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,page1")); |
| 308 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,page2")); |
| 309 EXPECT_EQ(2, tab->GetController().GetEntryCount()); |
| 310 EXPECT_EQ(1, tab->GetController().GetLastCommittedEntryIndex()); |
| 311 |
| 312 // Go back via script, but before that gets a chance to commit, show a print |
| 313 // dialog with a nested message loop and ScopedPageLoadDeferrer. |
| 314 PrintPreviewViaScript("history.back(); print();"); |
| 315 |
| 316 // Get the preview dialog for the initiator tab. |
| 317 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 318 |
| 319 // Check a new print preview dialog got created. |
| 320 ASSERT_TRUE(preview_dialog); |
| 321 ASSERT_NE(initiator(), preview_dialog); |
| 322 |
| 323 bool started = preview_dialog->GetRenderProcessHost()->IsReady(); |
| 324 if (!started) { |
| 325 PrintPreviewDialogStartedObserver started_observer(preview_dialog); |
| 326 started_observer.WaitForDialogStarted(); |
| 327 started = started_observer.dialog_started(); |
| 328 } |
| 329 |
| 330 if (started) { |
| 331 // Dismiss the dialog. |
| 332 PrintPreviewDialogDestroyedObserver destroyed_observer(preview_dialog); |
| 333 ASSERT_TRUE(preview_dialog->GetRenderProcessHost()->Shutdown(0, true)); |
| 334 if (!destroyed_observer.dialog_destroyed()) { |
| 335 destroyed_observer.WaitForDialogDestroyed(); |
| 336 ASSERT_TRUE(destroyed_observer.dialog_destroyed()); |
| 337 } |
| 338 } |
| 339 ASSERT_FALSE(GetPrintPreviewDialog()); |
| 340 |
| 341 // The back navigation should put us back on the first entry. |
| 342 content::WaitForLoadStop(tab); |
| 343 EXPECT_EQ(2, tab->GetController().GetEntryCount()); |
| 344 EXPECT_EQ(0, tab->GetController().GetLastCommittedEntryIndex()); |
| 345 } |
| 346 |
| 243 // Test to verify that after reloading the initiator, it creates a new print | 347 // Test to verify that after reloading the initiator, it creates a new print |
| 244 // preview dialog. | 348 // preview dialog. |
| 245 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | 349 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
| 246 ReloadInitiatorTab) { | 350 ReloadInitiatorTab) { |
| 247 // Print for the first time. | 351 // Print for the first time. |
| 248 PrintPreview(); | 352 PrintPreview(); |
| 249 | 353 |
| 250 WebContents* preview_dialog = GetPrintPreviewDialog(); | 354 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 251 | 355 |
| 252 // Check a new print preview dialog got created. | 356 // Check a new print preview dialog got created. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 content::BrowserAccessibilityState::GetInstance()->EnableAccessibility(); | 491 content::BrowserAccessibilityState::GetInstance()->EnableAccessibility(); |
| 388 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,HelloWorld")); | 492 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,HelloWorld")); |
| 389 PrintPreview(); | 493 PrintPreview(); |
| 390 WebContents* preview_dialog = GetPrintPreviewDialog(); | 494 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 391 WaitForAccessibilityTreeToContainNodeWithName(preview_dialog, "HelloWorld"); | 495 WaitForAccessibilityTreeToContainNodeWithName(preview_dialog, "HelloWorld"); |
| 392 } | 496 } |
| 393 | 497 |
| 394 } // namespace | 498 } // namespace |
| 395 | 499 |
| 396 #endif // defined(ENABLE_TASK_MANAGER) | 500 #endif // defined(ENABLE_TASK_MANAGER) |
| OLD | NEW |