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