| 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) | |
| 63 IPC_MESSAGE_UNHANDLED(break) | 61 IPC_MESSAGE_UNHANDLED(break) |
| 64 IPC_END_MESSAGE_MAP() | 62 IPC_END_MESSAGE_MAP() |
| 65 return false; // Report not handled so the real handler receives it. | 63 return false; // Report not handled so the real handler receives it. |
| 66 } | 64 } |
| 67 | 65 |
| 68 void OnRequestPrintPreview( | 66 void OnRequestPrintPreview( |
| 69 const PrintHostMsg_RequestPrintPreview_Params& /* params */) { | 67 const PrintHostMsg_RequestPrintPreview_Params& /* params */) { |
| 70 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | 68 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); |
| 71 } | 69 } |
| 72 | 70 |
| 73 void OnSetupScriptedPrintPreview() { | |
| 74 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, quit_closure_); | |
| 75 } | |
| 76 | |
| 77 base::Closure quit_closure_; | 71 base::Closure quit_closure_; |
| 78 | 72 |
| 79 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver); | 73 DISALLOW_COPY_AND_ASSIGN(RequestPrintPreviewObserver); |
| 80 }; | 74 }; |
| 81 | 75 |
| 82 class PrintPreviewDialogClonedObserver : public WebContentsObserver { | 76 class PrintPreviewDialogClonedObserver : public WebContentsObserver { |
| 83 public: | 77 public: |
| 84 explicit PrintPreviewDialogClonedObserver(WebContents* dialog) | 78 explicit PrintPreviewDialogClonedObserver(WebContents* dialog) |
| 85 : WebContentsObserver(dialog) { | 79 : WebContentsObserver(dialog) { |
| 86 } | 80 } |
| 87 ~PrintPreviewDialogClonedObserver() override {} | 81 ~PrintPreviewDialogClonedObserver() override {} |
| 88 | 82 |
| 89 RequestPrintPreviewObserver* request_preview_dialog_observer() { | 83 RequestPrintPreviewObserver* request_preview_dialog_observer() { |
| 90 return request_preview_dialog_observer_.get(); | 84 return request_preview_dialog_observer_.get(); |
| 91 } | 85 } |
| 92 | 86 |
| 93 private: | 87 private: |
| 94 // content::WebContentsObserver implementation. | 88 // content::WebContentsObserver implementation. |
| 95 void DidCloneToNewWebContents(WebContents* old_web_contents, | 89 void DidCloneToNewWebContents(WebContents* old_web_contents, |
| 96 WebContents* new_web_contents) override { | 90 WebContents* new_web_contents) override { |
| 97 request_preview_dialog_observer_.reset( | 91 request_preview_dialog_observer_.reset( |
| 98 new RequestPrintPreviewObserver(new_web_contents)); | 92 new RequestPrintPreviewObserver(new_web_contents)); |
| 99 } | 93 } |
| 100 | 94 |
| 101 std::unique_ptr<RequestPrintPreviewObserver> request_preview_dialog_observer_; | 95 std::unique_ptr<RequestPrintPreviewObserver> request_preview_dialog_observer_; |
| 102 | 96 |
| 103 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver); | 97 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogClonedObserver); |
| 104 }; | 98 }; |
| 105 | 99 |
| 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 | |
| 139 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { | 100 class PrintPreviewDialogDestroyedObserver : public WebContentsObserver { |
| 140 public: | 101 public: |
| 141 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) | 102 explicit PrintPreviewDialogDestroyedObserver(WebContents* dialog) |
| 142 : WebContentsObserver(dialog), | 103 : WebContentsObserver(dialog), |
| 143 waiting_(false), | 104 dialog_destroyed_(false) { |
| 144 dialog_destroyed_(false) {} | 105 } |
| 145 ~PrintPreviewDialogDestroyedObserver() override {} | 106 ~PrintPreviewDialogDestroyedObserver() override {} |
| 146 | 107 |
| 147 bool dialog_destroyed() const { return dialog_destroyed_; } | 108 bool dialog_destroyed() const { return dialog_destroyed_; } |
| 148 | 109 |
| 149 void WaitForDialogDestroyed() { | |
| 150 waiting_ = true; | |
| 151 run_loop_.Run(); | |
| 152 } | |
| 153 | |
| 154 private: | 110 private: |
| 155 // content::WebContentsObserver implementation. | 111 // content::WebContentsObserver implementation. |
| 156 void WebContentsDestroyed() override { | 112 void WebContentsDestroyed() override { dialog_destroyed_ = true; } |
| 157 dialog_destroyed_ = true; | |
| 158 if (waiting_) | |
| 159 run_loop_.Quit(); | |
| 160 } | |
| 161 | 113 |
| 162 bool waiting_; | |
| 163 bool dialog_destroyed_; | 114 bool dialog_destroyed_; |
| 164 base::RunLoop run_loop_; | |
| 165 | 115 |
| 166 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); | 116 DISALLOW_COPY_AND_ASSIGN(PrintPreviewDialogDestroyedObserver); |
| 167 }; | 117 }; |
| 168 | 118 |
| 169 void PluginsLoadedCallback( | 119 void PluginsLoadedCallback( |
| 170 const base::Closure& quit_closure, | 120 const base::Closure& quit_closure, |
| 171 const std::vector<content::WebPluginInfo>& /* info */) { | 121 const std::vector<content::WebPluginInfo>& /* info */) { |
| 172 quit_closure.Run(); | 122 quit_closure.Run(); |
| 173 } | 123 } |
| 174 | 124 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 return initiator_; | 161 return initiator_; |
| 212 } | 162 } |
| 213 | 163 |
| 214 void PrintPreview() { | 164 void PrintPreview() { |
| 215 base::RunLoop run_loop; | 165 base::RunLoop run_loop; |
| 216 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); | 166 request_preview_dialog_observer()->set_quit_closure(run_loop.QuitClosure()); |
| 217 chrome::Print(browser()); | 167 chrome::Print(browser()); |
| 218 run_loop.Run(); | 168 run_loop.Run(); |
| 219 } | 169 } |
| 220 | 170 |
| 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 | |
| 229 WebContents* GetPrintPreviewDialog() { | 171 WebContents* GetPrintPreviewDialog() { |
| 230 printing::PrintPreviewDialogController* dialog_controller = | 172 printing::PrintPreviewDialogController* dialog_controller = |
| 231 printing::PrintPreviewDialogController::GetInstance(); | 173 printing::PrintPreviewDialogController::GetInstance(); |
| 232 return dialog_controller->GetPrintPreviewForContents(initiator_); | 174 return dialog_controller->GetPrintPreviewForContents(initiator_); |
| 233 } | 175 } |
| 234 | 176 |
| 235 private: | 177 private: |
| 236 void SetUpOnMainThread() override { | 178 void SetUpOnMainThread() override { |
| 237 WebContents* first_tab = | 179 WebContents* first_tab = |
| 238 browser()->tab_strip_model()->GetActiveWebContents(); | 180 browser()->tab_strip_model()->GetActiveWebContents(); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // Try printing again. | 233 // Try printing again. |
| 292 PrintPreview(); | 234 PrintPreview(); |
| 293 | 235 |
| 294 // Get the print preview dialog for the initiator tab. | 236 // Get the print preview dialog for the initiator tab. |
| 295 WebContents* new_preview_dialog = GetPrintPreviewDialog(); | 237 WebContents* new_preview_dialog = GetPrintPreviewDialog(); |
| 296 | 238 |
| 297 // Check a new preview dialog got created. | 239 // Check a new preview dialog got created. |
| 298 EXPECT_TRUE(new_preview_dialog); | 240 EXPECT_TRUE(new_preview_dialog); |
| 299 } | 241 } |
| 300 | 242 |
| 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 | |
| 347 // Test to verify that after reloading the initiator, it creates a new print | 243 // Test to verify that after reloading the initiator, it creates a new print |
| 348 // preview dialog. | 244 // preview dialog. |
| 349 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, | 245 IN_PROC_BROWSER_TEST_F(PrintPreviewDialogControllerBrowserTest, |
| 350 ReloadInitiatorTab) { | 246 ReloadInitiatorTab) { |
| 351 // Print for the first time. | 247 // Print for the first time. |
| 352 PrintPreview(); | 248 PrintPreview(); |
| 353 | 249 |
| 354 WebContents* preview_dialog = GetPrintPreviewDialog(); | 250 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 355 | 251 |
| 356 // Check a new print preview dialog got created. | 252 // Check a new print preview dialog got created. |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 content::BrowserAccessibilityState::GetInstance()->EnableAccessibility(); | 387 content::BrowserAccessibilityState::GetInstance()->EnableAccessibility(); |
| 492 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,HelloWorld")); | 388 ui_test_utils::NavigateToURL(browser(), GURL("data:text/html,HelloWorld")); |
| 493 PrintPreview(); | 389 PrintPreview(); |
| 494 WebContents* preview_dialog = GetPrintPreviewDialog(); | 390 WebContents* preview_dialog = GetPrintPreviewDialog(); |
| 495 WaitForAccessibilityTreeToContainNodeWithName(preview_dialog, "HelloWorld"); | 391 WaitForAccessibilityTreeToContainNodeWithName(preview_dialog, "HelloWorld"); |
| 496 } | 392 } |
| 497 | 393 |
| 498 } // namespace | 394 } // namespace |
| 499 | 395 |
| 500 #endif // defined(ENABLE_TASK_MANAGER) | 396 #endif // defined(ENABLE_TASK_MANAGER) |
| OLD | NEW |