Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: content/public/test/browser_test_utils.cc

Issue 2132603002: [page_load_metrics] Add a NavigationThrottle for richer abort metrics (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Attach the throttle first so it gets all notifications before any DEFERs Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "content/public/test/browser_test_utils.h" 5 #include "content/public/test/browser_test_utils.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <tuple> 8 #include <tuple>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 25 matching lines...) Expand all
36 #include "content/browser/renderer_host/render_widget_host_impl.h" 36 #include "content/browser/renderer_host/render_widget_host_impl.h"
37 #include "content/browser/web_contents/web_contents_impl.h" 37 #include "content/browser/web_contents/web_contents_impl.h"
38 #include "content/browser/web_contents/web_contents_view.h" 38 #include "content/browser/web_contents/web_contents_view.h"
39 #include "content/common/input/synthetic_web_input_event_builders.h" 39 #include "content/common/input/synthetic_web_input_event_builders.h"
40 #include "content/common/input_messages.h" 40 #include "content/common/input_messages.h"
41 #include "content/common/view_messages.h" 41 #include "content/common/view_messages.h"
42 #include "content/public/browser/browser_context.h" 42 #include "content/public/browser/browser_context.h"
43 #include "content/public/browser/browser_plugin_guest_manager.h" 43 #include "content/public/browser/browser_plugin_guest_manager.h"
44 #include "content/public/browser/histogram_fetcher.h" 44 #include "content/public/browser/histogram_fetcher.h"
45 #include "content/public/browser/navigation_entry.h" 45 #include "content/public/browser/navigation_entry.h"
46 #include "content/public/browser/navigation_handle.h"
47 #include "content/public/browser/navigation_throttle.h"
46 #include "content/public/browser/notification_service.h" 48 #include "content/public/browser/notification_service.h"
47 #include "content/public/browser/notification_types.h" 49 #include "content/public/browser/notification_types.h"
48 #include "content/public/browser/render_frame_host.h" 50 #include "content/public/browser/render_frame_host.h"
49 #include "content/public/browser/render_process_host.h" 51 #include "content/public/browser/render_process_host.h"
50 #include "content/public/browser/render_view_host.h" 52 #include "content/public/browser/render_view_host.h"
51 #include "content/public/browser/storage_partition.h" 53 #include "content/public/browser/storage_partition.h"
52 #include "content/public/browser/web_contents.h" 54 #include "content/public/browser/web_contents.h"
53 #include "content/public/test/test_navigation_observer.h" 55 #include "content/public/test/test_navigation_observer.h"
54 #include "content/public/test/test_utils.h" 56 #include "content/public/test/test_utils.h"
55 #include "content/test/accessibility_browser_test_utils.h" 57 #include "content/test/accessibility_browser_test_utils.h"
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 GURL redirect_target(redirect_server.Resolve(path)); 341 GURL redirect_target(redirect_server.Resolve(path));
340 DCHECK(redirect_target.is_valid()); 342 DCHECK(redirect_target.is_valid());
341 343
342 std::unique_ptr<net::test_server::BasicHttpResponse> http_response( 344 std::unique_ptr<net::test_server::BasicHttpResponse> http_response(
343 new net::test_server::BasicHttpResponse); 345 new net::test_server::BasicHttpResponse);
344 http_response->set_code(http_status_code); 346 http_response->set_code(http_status_code);
345 http_response->AddCustomHeader("Location", redirect_target.spec()); 347 http_response->AddCustomHeader("Location", redirect_target.spec());
346 return std::move(http_response); 348 return std::move(http_response);
347 } 349 }
348 350
351 // Helper class used by the TestNavigationManager to pause navigations.
352 // Note: the throttle should be added to the *end* of the list of throttles,
353 // so all NavigationThrottles that should be attached observe the
354 // WillStartRequest callback. This throttle is added, and will re-add itself
355 // to the end of the list on WillStartRequest.
356 class TestNavigationManagerThrottle : public NavigationThrottle {
357 public:
358 TestNavigationManagerThrottle(NavigationHandle* handle,
359 base::Closure on_will_start_request_closure,
360 bool attached_in_did_start)
361 : NavigationThrottle(handle),
362 on_will_start_request_closure_(on_will_start_request_closure),
363 attached_in_did_start_(attached_in_did_start) {}
364 ~TestNavigationManagerThrottle() override {}
365
366 private:
367 // NavigationThrottle implementation.
368 NavigationThrottle::ThrottleCheckResult WillStartRequest() override {
clamy 2016/07/26 17:09:22 I'm not terribly fond of this. Attaching a throttl
Charlie Harrison 2016/07/26 19:56:06 Hm okay. I've modified NavigationHandleImpl::Regis
369 if (attached_in_did_start_) {
370 navigation_handle()->RegisterThrottleForTesting(
371 base::WrapUnique(new TestNavigationManagerThrottle(
372 navigation_handle(), on_will_start_request_closure_, false)));
373 return NavigationThrottle::PROCEED;
374 } else {
375 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
376 on_will_start_request_closure_);
377 return NavigationThrottle::DEFER;
378 }
379 }
380
381 base::Closure on_will_start_request_closure_;
382 bool attached_in_did_start_;
383 };
384
349 } // namespace 385 } // namespace
350 386
351 bool NavigateIframeToURL(WebContents* web_contents, 387 bool NavigateIframeToURL(WebContents* web_contents,
352 std::string iframe_id, 388 std::string iframe_id,
353 const GURL& url) { 389 const GURL& url) {
354 std::string script = base::StringPrintf( 390 std::string script = base::StringPrintf(
355 "setTimeout(\"" 391 "setTimeout(\""
356 "var iframes = document.getElementById('%s');iframes.src='%s';" 392 "var iframes = document.getElementById('%s');iframes.src='%s';"
357 "\",0)", 393 "\",0)",
358 iframe_id.c_str(), url.spec().c_str()); 394 iframe_id.c_str(), url.spec().c_str());
(...skipping 1065 matching lines...) Expand 10 before | Expand all | Expand 10 after
1424 uint32_t InputMsgWatcher::WaitForAck() { 1460 uint32_t InputMsgWatcher::WaitForAck() {
1425 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1461 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1426 if (ack_result_ != INPUT_EVENT_ACK_STATE_UNKNOWN) 1462 if (ack_result_ != INPUT_EVENT_ACK_STATE_UNKNOWN)
1427 return ack_result_; 1463 return ack_result_;
1428 base::RunLoop run_loop; 1464 base::RunLoop run_loop;
1429 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure()); 1465 base::AutoReset<base::Closure> reset_quit(&quit_, run_loop.QuitClosure());
1430 run_loop.Run(); 1466 run_loop.Run();
1431 return ack_result_; 1467 return ack_result_;
1432 } 1468 }
1433 1469
1470 TestNavigationManager::TestNavigationManager(int filtering_frame_tree_node_id,
1471 WebContents* web_contents,
1472 const GURL& url)
1473 : WebContentsObserver(web_contents),
1474 filtering_frame_tree_node_id_(filtering_frame_tree_node_id),
1475 url_(url),
1476 navigation_paused_(false),
1477 handle_(nullptr),
1478 handled_navigation_(false),
1479 weak_factory_(this) {}
1480
1481 TestNavigationManager::TestNavigationManager(WebContents* web_contents,
1482 const GURL& url)
1483 : TestNavigationManager(FrameTreeNode::kFrameTreeNodeInvalidId,
1484 web_contents,
1485 url) {}
1486
1487 TestNavigationManager::~TestNavigationManager() {
1488 ResumeNavigation();
1489 }
1490
1491 void TestNavigationManager::WaitForWillStartRequest() {
1492 DCHECK(!did_finish_loop_runner_);
1493 if (!handle_ && handled_navigation_)
1494 return;
1495 if (navigation_paused_)
1496 return;
1497 will_start_loop_runner_ = new MessageLoopRunner();
1498 will_start_loop_runner_->Run();
1499 will_start_loop_runner_ = nullptr;
1500 }
1501
1502 void TestNavigationManager::WaitForNavigationFinished() {
1503 DCHECK(!will_start_loop_runner_);
1504 if (!handle_ && handled_navigation_)
1505 return;
1506 // Ensure the navigation is resumed if the manager paused it previously.
1507 if (navigation_paused_)
1508 ResumeNavigation();
1509 did_finish_loop_runner_ = new MessageLoopRunner();
1510 did_finish_loop_runner_->Run();
1511 did_finish_loop_runner_ = nullptr;
1512 }
1513
1514 void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) {
1515 if (handle_ || handle->GetURL() != url_)
1516 return;
1517 if (handled_navigation_)
1518 return;
1519
1520 if (filtering_frame_tree_node_id_ != FrameTreeNode::kFrameTreeNodeInvalidId &&
1521 handle->GetFrameTreeNodeId() != filtering_frame_tree_node_id_) {
1522 return;
1523 }
1524
1525 handle_ = handle;
1526 std::unique_ptr<NavigationThrottle> throttle(
1527 new TestNavigationManagerThrottle(
1528 handle_, base::Bind(&TestNavigationManager::OnWillStartRequest,
1529 weak_factory_.GetWeakPtr()), true));
1530 handle_->RegisterThrottleForTesting(std::move(throttle));
1531 }
1532
1533 void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) {
1534 if (handle != handle_)
1535 return;
1536 handle_ = nullptr;
1537 handled_navigation_ = true;
1538 navigation_paused_ = false;
1539 if (did_finish_loop_runner_)
clamy 2016/07/26 17:09:22 We may want to also quit the will_start_loop_runne
Charlie Harrison 2016/07/26 19:56:07 Done.
1540 did_finish_loop_runner_->Quit();
1541 }
1542
1543 void TestNavigationManager::OnWillStartRequest() {
1544 navigation_paused_ = true;
1545 if (will_start_loop_runner_)
1546 will_start_loop_runner_->Quit();
1547
1548 // If waiting for the navigation to finish, resume the navigation.
1549 if (did_finish_loop_runner_)
1550 ResumeNavigation();
1551 }
1552
1553 void TestNavigationManager::ResumeNavigation() {
1554 if (!navigation_paused_ || !handle_)
1555 return;
1556 navigation_paused_ = false;
1557 handle_->Resume();
1558 }
1559
1434 } // namespace content 1560 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698