OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/browser/loader/cross_site_resource_handler.h" |
| 6 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 7 #include "content/browser/loader/resource_request_info_impl.h" |
| 8 #include "content/browser/transition_request_manager.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 10 #include "content/public/browser/web_contents_observer.h" |
| 11 #include "content/public/test/content_browser_test.h" |
| 12 #include "content/public/test/content_browser_test_utils.h" |
| 13 #include "content/public/test/test_utils.h" |
| 14 #include "content/shell/browser/shell.h" |
| 15 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h" |
| 16 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 17 #include "net/url_request/url_request.h" |
| 18 |
| 19 namespace content { |
| 20 |
| 21 class TransitionBrowserTest : public ContentBrowserTest { |
| 22 public: |
| 23 TransitionBrowserTest() {} |
| 24 |
| 25 private: |
| 26 DISALLOW_COPY_AND_ASSIGN(TransitionBrowserTest); |
| 27 }; |
| 28 |
| 29 class TransitionBrowserTestObserver |
| 30 : public WebContentsObserver, |
| 31 public ShellResourceDispatcherHostDelegate { |
| 32 public: |
| 33 TransitionBrowserTestObserver(Shell* shell) |
| 34 : WebContentsObserver(shell->web_contents()), |
| 35 request_(NULL), |
| 36 shell_(shell), |
| 37 provisional_load_started_(false), |
| 38 did_defer_response_(false), |
| 39 is_transition_request_(false) {} |
| 40 |
| 41 virtual void RequestBeginning( |
| 42 net::URLRequest* request, |
| 43 ResourceContext* resource_context, |
| 44 appcache::AppCacheService* appcache_service, |
| 45 ResourceType::Type resource_type, |
| 46 int child_id, |
| 47 int route_id, |
| 48 ScopedVector<ResourceThrottle>* throttles) OVERRIDE { |
| 49 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 50 ShellResourceDispatcherHostDelegate::RequestBeginning(request, |
| 51 resource_context, |
| 52 appcache_service, |
| 53 resource_type, |
| 54 child_id, |
| 55 route_id, |
| 56 throttles); |
| 57 request_ = request; |
| 58 } |
| 59 |
| 60 virtual void DidStartProvisionalLoadForFrame( |
| 61 int64 frame_id, |
| 62 int64 parent_frame_id, |
| 63 bool is_main_frame, |
| 64 const GURL& validated_url, |
| 65 bool is_error_page, |
| 66 bool is_iframe_srcdoc, |
| 67 RenderViewHost* render_view_host) OVERRIDE { |
| 68 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 69 provisional_load_started_ = true; |
| 70 |
| 71 static_cast<WebContentsImpl*>(shell_->web_contents())-> |
| 72 SetHasPendingTransitionRequest(is_transition_request_); |
| 73 } |
| 74 |
| 75 virtual void DidDeferAfterResponseStarted() OVERRIDE { |
| 76 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 77 ASSERT_TRUE(provisional_load_started_); |
| 78 did_defer_response_ = true; |
| 79 |
| 80 // Check that the request is currently blocked |
| 81 ResourceRequestInfoImpl* info = |
| 82 ResourceRequestInfoImpl::ForRequest(request_); |
| 83 |
| 84 ASSERT_TRUE(info->cross_site_handler()->is_deferred_for_testing()); |
| 85 |
| 86 static_cast<WebContentsImpl*>(shell_->web_contents())-> |
| 87 ResumeResponseDeferredAtStart(); |
| 88 } |
| 89 |
| 90 void set_pending_transition_request(bool is_transition_request) { |
| 91 is_transition_request_ = is_transition_request; |
| 92 } |
| 93 |
| 94 bool did_defer_response() const { return did_defer_response_; } |
| 95 |
| 96 private: |
| 97 net::URLRequest* request_; |
| 98 Shell* shell_; |
| 99 bool provisional_load_started_; |
| 100 bool did_defer_response_; |
| 101 bool is_transition_request_; |
| 102 }; |
| 103 |
| 104 // This tests that DidDeferAfterResponseStarted is not called on normal |
| 105 // navigations. |
| 106 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest, |
| 107 DidDeferAfterResponseStartedNotCalled) { |
| 108 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 109 scoped_ptr<TransitionBrowserTestObserver> observer( |
| 110 new TransitionBrowserTestObserver(shell())); |
| 111 |
| 112 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); |
| 113 |
| 114 ASSERT_FALSE(observer->did_defer_response()); |
| 115 } |
| 116 |
| 117 // This tests that DidDeferAfterResponseStarted is called on a transition |
| 118 // navigation, and that the request is deferred. |
| 119 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest, ShouldDeferAtFirstResponse) { |
| 120 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 121 scoped_ptr<TransitionBrowserTestObserver> observer( |
| 122 new TransitionBrowserTestObserver(shell())); |
| 123 |
| 124 ResourceDispatcherHost::Get()->SetDelegate(observer.get()); |
| 125 observer->set_pending_transition_request(true); |
| 126 |
| 127 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); |
| 128 |
| 129 ASSERT_TRUE(observer->did_defer_response()); |
| 130 } |
| 131 |
| 132 } // namespace content |
OLD | NEW |