OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/browser/frame_host/interstitial_page_impl.h" | 5 #include "content/browser/frame_host/interstitial_page_impl.h" |
6 | 6 |
7 #include <tuple> | 7 #include <tuple> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 " document.addEventListener('selectionchange'," | 57 " document.addEventListener('selectionchange'," |
58 " function() { document.title='SELECTION_CHANGED'; })" | 58 " function() { document.title='SELECTION_CHANGED'; })" |
59 "}" | 59 "}" |
60 "</script>" | 60 "</script>" |
61 "</head>" | 61 "</head>" |
62 "<body>original body text</body>" | 62 "<body>original body text</body>" |
63 "</html>"; | 63 "</html>"; |
64 } | 64 } |
65 }; | 65 }; |
66 | 66 |
67 // A title watcher for interstitial pages. The existing TitleWatcher does not | |
68 // work for interstitial pages. Note that this title watcher waits for the | |
69 // title update IPC message not the actual title update. So, the new title is | |
70 // probably not propagated completely, yet. | |
71 class InterstitialTitleUpdateWatcher : public BrowserMessageFilter { | |
72 public: | |
73 explicit InterstitialTitleUpdateWatcher(InterstitialPage* interstitial) | |
74 : BrowserMessageFilter(FrameMsgStart) { | |
75 interstitial->GetMainFrame()->GetProcess()->AddFilter(this); | |
76 } | |
77 | |
78 void InitWait(const std::string& expected_title) { | |
79 DCHECK(!run_loop_); | |
80 expected_title_ = base::UTF8ToUTF16(expected_title); | |
81 run_loop_.reset(new base::RunLoop()); | |
82 } | |
83 | |
84 void Wait() { | |
85 DCHECK(run_loop_); | |
86 run_loop_->Run(); | |
87 run_loop_.reset(); | |
88 } | |
89 | |
90 private: | |
91 ~InterstitialTitleUpdateWatcher() override {} | |
92 | |
93 void OnTitleUpdateReceived(const base::string16& title) { | |
94 DCHECK(run_loop_); | |
95 if (title == expected_title_) | |
96 run_loop_->Quit(); | |
97 } | |
98 | |
99 // BrowserMessageFilter: | |
100 bool OnMessageReceived(const IPC::Message& message) override { | |
101 if (!run_loop_) | |
102 return false; | |
103 | |
104 if (message.type() == FrameHostMsg_UpdateTitle::ID) { | |
105 FrameHostMsg_UpdateTitle::Param params; | |
106 if (FrameHostMsg_UpdateTitle::Read(&message, ¶ms)) { | |
107 BrowserThread::PostTask( | |
108 BrowserThread::UI, FROM_HERE, | |
109 base::Bind(&InterstitialTitleUpdateWatcher::OnTitleUpdateReceived, | |
110 this, std::get<0>(params))); | |
111 } | |
112 } | |
113 return false; | |
114 } | |
115 | |
116 base::string16 expected_title_; | |
117 std::unique_ptr<base::RunLoop> run_loop_; | |
118 | |
119 DISALLOW_COPY_AND_ASSIGN(InterstitialTitleUpdateWatcher); | |
120 }; | |
121 | |
122 // A message filter that watches for WriteText and CommitWrite clipboard IPC | 67 // A message filter that watches for WriteText and CommitWrite clipboard IPC |
123 // messages to make sure cut/copy is working properly. It will mark these events | 68 // messages to make sure cut/copy is working properly. It will mark these events |
124 // as handled to prevent modification of the actual clipboard. | 69 // as handled to prevent modification of the actual clipboard. |
125 class ClipboardMessageWatcher : public IPC::MessageFilter { | 70 class ClipboardMessageWatcher : public IPC::MessageFilter { |
126 public: | 71 public: |
127 explicit ClipboardMessageWatcher(InterstitialPage* interstitial) { | 72 explicit ClipboardMessageWatcher(InterstitialPage* interstitial) { |
128 interstitial->GetMainFrame()->GetProcess()->GetChannel()->AddFilter(this); | 73 interstitial->GetMainFrame()->GetProcess()->GetChannel()->AddFilter(this); |
129 } | 74 } |
130 | 75 |
131 void InitWait() { | 76 void InitWait() { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 WaitForInterstitialAttach(web_contents); | 190 WaitForInterstitialAttach(web_contents); |
246 | 191 |
247 // Focus the interstitial frame | 192 // Focus the interstitial frame |
248 FrameTree* frame_tree = static_cast<RenderViewHostDelegate*>( | 193 FrameTree* frame_tree = static_cast<RenderViewHostDelegate*>( |
249 interstitial_.get())->GetFrameTree(); | 194 interstitial_.get())->GetFrameTree(); |
250 frame_tree->SetFocusedFrame(frame_tree->root(), | 195 frame_tree->SetFocusedFrame(frame_tree->root(), |
251 frame_tree->GetMainFrame()->GetSiteInstance()); | 196 frame_tree->GetMainFrame()->GetSiteInstance()); |
252 | 197 |
253 clipboard_message_watcher_ = | 198 clipboard_message_watcher_ = |
254 new ClipboardMessageWatcher(interstitial_.get()); | 199 new ClipboardMessageWatcher(interstitial_.get()); |
255 title_update_watcher_ = | |
256 new InterstitialTitleUpdateWatcher(interstitial_.get()); | |
257 | 200 |
258 // Wait until page loads completely. | 201 // Wait until page loads completely. |
259 ASSERT_TRUE(WaitForRenderFrameReady(interstitial_->GetMainFrame())); | 202 ASSERT_TRUE(WaitForRenderFrameReady(interstitial_->GetMainFrame())); |
260 } | 203 } |
261 | 204 |
262 void TearDownInterstitialPage() { | 205 void TearDownInterstitialPage() { |
263 // Close the interstitial. | 206 // Close the interstitial. |
264 interstitial_->DontProceed(); | 207 interstitial_->DontProceed(); |
265 WaitForInterstitialDetach(shell()->web_contents()); | 208 WaitForInterstitialDetach(shell()->web_contents()); |
266 interstitial_.reset(); | 209 interstitial_.reset(); |
(...skipping 18 matching lines...) Expand all Loading... |
285 "create_input_and_set_text('" + text + "')"); | 228 "create_input_and_set_text('" + text + "')"); |
286 } | 229 } |
287 | 230 |
288 bool SetSelectionChangeListener() { | 231 bool SetSelectionChangeListener() { |
289 return ExecuteScript(interstitial_->GetMainFrame(), | 232 return ExecuteScript(interstitial_->GetMainFrame(), |
290 "set_selection_change_listener()"); | 233 "set_selection_change_listener()"); |
291 } | 234 } |
292 | 235 |
293 std::string PerformCut() { | 236 std::string PerformCut() { |
294 clipboard_message_watcher_->InitWait(); | 237 clipboard_message_watcher_->InitWait(); |
295 title_update_watcher_->InitWait("TEXT_CHANGED"); | 238 const base::string16 expected_title = base::UTF8ToUTF16("TEXT_CHANGED"); |
| 239 content::TitleWatcher title_watcher(shell()->web_contents(), |
| 240 expected_title); |
296 RenderFrameHostImpl* rfh = | 241 RenderFrameHostImpl* rfh = |
297 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 242 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
298 rfh->GetRenderWidgetHost()->delegate()->Cut(); | 243 rfh->GetRenderWidgetHost()->delegate()->Cut(); |
299 clipboard_message_watcher_->WaitForWriteCommit(); | 244 clipboard_message_watcher_->WaitForWriteCommit(); |
300 title_update_watcher_->Wait(); | 245 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
301 return clipboard_message_watcher_->last_text(); | 246 return clipboard_message_watcher_->last_text(); |
302 } | 247 } |
303 | 248 |
304 std::string PerformCopy() { | 249 std::string PerformCopy() { |
305 clipboard_message_watcher_->InitWait(); | 250 clipboard_message_watcher_->InitWait(); |
306 RenderFrameHostImpl* rfh = | 251 RenderFrameHostImpl* rfh = |
307 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 252 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
308 rfh->GetRenderWidgetHost()->delegate()->Copy(); | 253 rfh->GetRenderWidgetHost()->delegate()->Copy(); |
309 clipboard_message_watcher_->WaitForWriteCommit(); | 254 clipboard_message_watcher_->WaitForWriteCommit(); |
310 return clipboard_message_watcher_->last_text(); | 255 return clipboard_message_watcher_->last_text(); |
311 } | 256 } |
312 | 257 |
313 void PerformPaste() { | 258 void PerformPaste() { |
314 title_update_watcher_->InitWait("TEXT_CHANGED"); | 259 const base::string16 expected_title = base::UTF8ToUTF16("TEXT_CHANGED"); |
| 260 content::TitleWatcher title_watcher(shell()->web_contents(), |
| 261 expected_title); |
315 RenderFrameHostImpl* rfh = | 262 RenderFrameHostImpl* rfh = |
316 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 263 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
317 rfh->GetRenderWidgetHost()->delegate()->Paste(); | 264 rfh->GetRenderWidgetHost()->delegate()->Paste(); |
318 title_update_watcher_->Wait(); | 265 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
319 } | 266 } |
320 | 267 |
321 void PerformSelectAll() { | 268 void PerformSelectAll() { |
322 title_update_watcher_->InitWait("SELECTION_CHANGED"); | 269 const base::string16 expected_title = |
| 270 base::UTF8ToUTF16("SELECTION_CHANGED"); |
| 271 content::TitleWatcher title_watcher(shell()->web_contents(), |
| 272 expected_title); |
323 RenderFrameHostImpl* rfh = | 273 RenderFrameHostImpl* rfh = |
324 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); | 274 static_cast<RenderFrameHostImpl*>(interstitial_->GetMainFrame()); |
325 rfh->GetRenderWidgetHost()->delegate()->SelectAll(); | 275 rfh->GetRenderWidgetHost()->delegate()->SelectAll(); |
326 title_update_watcher_->Wait(); | 276 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle()); |
327 } | 277 } |
328 | 278 |
329 private: | 279 private: |
330 void RunTaskOnIOThreadAndWait(const base::Closure& task) { | 280 void RunTaskOnIOThreadAndWait(const base::Closure& task) { |
331 base::WaitableEvent completion( | 281 base::WaitableEvent completion( |
332 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 282 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
333 base::WaitableEvent::InitialState::NOT_SIGNALED); | 283 base::WaitableEvent::InitialState::NOT_SIGNALED); |
334 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 284 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
335 base::Bind(&InterstitialPageImplTest::RunTask, this, | 285 base::Bind(&InterstitialPageImplTest::RunTask, this, |
336 task, &completion)); | 286 task, &completion)); |
337 completion.Wait(); | 287 completion.Wait(); |
338 } | 288 } |
339 | 289 |
340 void RunTask(const base::Closure& task, base::WaitableEvent* completion) { | 290 void RunTask(const base::Closure& task, base::WaitableEvent* completion) { |
341 task.Run(); | 291 task.Run(); |
342 completion->Signal(); | 292 completion->Signal(); |
343 } | 293 } |
344 | 294 |
345 std::unique_ptr<InterstitialPageImpl> interstitial_; | 295 std::unique_ptr<InterstitialPageImpl> interstitial_; |
346 scoped_refptr<ClipboardMessageWatcher> clipboard_message_watcher_; | 296 scoped_refptr<ClipboardMessageWatcher> clipboard_message_watcher_; |
347 scoped_refptr<InterstitialTitleUpdateWatcher> title_update_watcher_; | |
348 | 297 |
349 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); | 298 DISALLOW_COPY_AND_ASSIGN(InterstitialPageImplTest); |
350 }; | 299 }; |
351 | 300 |
352 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { | 301 IN_PROC_BROWSER_TEST_F(InterstitialPageImplTest, Cut) { |
353 SetUpInterstitialPage(); | 302 SetUpInterstitialPage(); |
354 | 303 |
355 ASSERT_TRUE(CreateInputAndSetText("text-to-cut")); | 304 ASSERT_TRUE(CreateInputAndSetText("text-to-cut")); |
356 ASSERT_TRUE(FocusInputAndSelectText()); | 305 ASSERT_TRUE(FocusInputAndSelectText()); |
357 | 306 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
410 | 359 |
411 PerformSelectAll(); | 360 PerformSelectAll(); |
412 | 361 |
413 ASSERT_TRUE(GetSelection(&input_text)); | 362 ASSERT_TRUE(GetSelection(&input_text)); |
414 EXPECT_EQ("original body text", input_text); | 363 EXPECT_EQ("original body text", input_text); |
415 | 364 |
416 TearDownInterstitialPage(); | 365 TearDownInterstitialPage(); |
417 } | 366 } |
418 | 367 |
419 } // namespace content | 368 } // namespace content |
OLD | NEW |