| 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/navigation_handle_impl.h" | 5 #include "content/browser/frame_host/navigation_handle_impl.h" |
| 6 | 6 |
| 7 #include "content/browser/frame_host/navigator_delegate.h" | 7 #include "content/browser/frame_host/navigator_delegate.h" |
| 8 #include "content/public/browser/content_browser_client.h" |
| 9 #include "content/public/common/content_client.h" |
| 8 #include "net/url_request/redirect_info.h" | 10 #include "net/url_request/redirect_info.h" |
| 9 | 11 |
| 10 namespace content { | 12 namespace content { |
| 11 | 13 |
| 12 // static | |
| 13 scoped_ptr<NavigationHandleImpl> NavigationHandleImpl::Create( | |
| 14 const GURL& url, | |
| 15 const bool is_main_frame, | |
| 16 NavigatorDelegate* delegate) { | |
| 17 return scoped_ptr<NavigationHandleImpl>( | |
| 18 new NavigationHandleImpl(url, is_main_frame, delegate)); | |
| 19 } | |
| 20 | |
| 21 NavigationHandleImpl::NavigationHandleImpl(const GURL& url, | 14 NavigationHandleImpl::NavigationHandleImpl(const GURL& url, |
| 22 const bool is_main_frame, | 15 const bool is_main_frame, |
| 23 NavigatorDelegate* delegate) | 16 NavigatorDelegate* delegate) |
| 24 : url_(url), | 17 : url_(url), |
| 18 is_main_frame_(is_main_frame), |
| 19 is_post_(false), |
| 20 has_user_gesture_(false), |
| 21 transition_(ui::PAGE_TRANSITION_LINK), |
| 22 is_external_protocol_(false), |
| 25 net_error_code_(net::OK), | 23 net_error_code_(net::OK), |
| 26 state_(DID_START), | 24 state_(DID_START), |
| 27 is_main_frame_(is_main_frame), | |
| 28 is_transferring_(false), | 25 is_transferring_(false), |
| 29 delegate_(delegate) { | 26 delegate_(delegate) { |
| 30 delegate_->DidStartNavigation(this); | 27 delegate_->DidStartNavigation(this); |
| 31 } | 28 } |
| 32 | 29 |
| 33 NavigationHandleImpl::~NavigationHandleImpl() { | 30 NavigationHandleImpl::~NavigationHandleImpl() { |
| 34 delegate_->DidFinishNavigation(this); | 31 delegate_->DidFinishNavigation(this); |
| 35 } | 32 } |
| 36 | 33 |
| 37 const GURL& NavigationHandleImpl::GetURL() const { | 34 const GURL& NavigationHandleImpl::GetURL() { |
| 38 return url_; | 35 return url_; |
| 39 } | 36 } |
| 40 | 37 |
| 41 net::Error NavigationHandleImpl::GetNetErrorCode() const { | 38 bool NavigationHandleImpl::IsInMainFrame() { |
| 39 return is_main_frame_; |
| 40 } |
| 41 |
| 42 WebContents* NavigationHandleImpl::GetWebContents() { |
| 43 return web_contents_; |
| 44 } |
| 45 |
| 46 bool NavigationHandleImpl::IsPost() { |
| 47 CHECK_NE(DID_START, state_) |
| 48 << "This accessor should not be called before the request is started."; |
| 49 return is_post_; |
| 50 } |
| 51 |
| 52 const Referrer& NavigationHandleImpl::GetReferrer() { |
| 53 CHECK_NE(DID_START, state_) |
| 54 << "This accessor should not be called before the request is started."; |
| 55 return sanitized_referrer_; |
| 56 } |
| 57 |
| 58 bool NavigationHandleImpl::HasUserGesture() { |
| 59 CHECK_NE(DID_START, state_) |
| 60 << "This accessor should not be called before the request is started."; |
| 61 return has_user_gesture_; |
| 62 } |
| 63 |
| 64 ui::PageTransition NavigationHandleImpl::GetPageTransition() { |
| 65 CHECK_NE(DID_START, state_) |
| 66 << "This accessor should not be called before the request is started."; |
| 67 return transition_; |
| 68 } |
| 69 |
| 70 bool NavigationHandleImpl::IsExternalProtocol() { |
| 71 CHECK_NE(DID_START, state_) |
| 72 << "This accessor should not be called before the request is started."; |
| 73 return is_external_protocol_; |
| 74 } |
| 75 |
| 76 net::Error NavigationHandleImpl::GetNetErrorCode() { |
| 42 return net_error_code_; | 77 return net_error_code_; |
| 43 } | 78 } |
| 44 | 79 |
| 45 bool NavigationHandleImpl::IsInMainFrame() const { | 80 bool NavigationHandleImpl::HasCommittedDocument() { |
| 46 return is_main_frame_; | |
| 47 } | |
| 48 | |
| 49 bool NavigationHandleImpl::HasCommittedDocument() const { | |
| 50 return state_ == DID_COMMIT; | 81 return state_ == DID_COMMIT; |
| 51 } | 82 } |
| 52 | 83 |
| 53 bool NavigationHandleImpl::HasCommittedErrorPage() const { | 84 bool NavigationHandleImpl::HasCommittedErrorPage() { |
| 54 return state_ == DID_COMMIT_ERROR_PAGE; | 85 return state_ == DID_COMMIT_ERROR_PAGE; |
| 55 } | 86 } |
| 56 | 87 |
| 88 void NavigationHandleImpl::RegisterThrottleForTesting( |
| 89 scoped_ptr<NavigationThrottle> navigation_throttle) { |
| 90 throttles_.push_back(navigation_throttle.Pass()); |
| 91 } |
| 92 |
| 93 NavigationThrottle::ThrottleCheckResult NavigationHandleImpl::WillStartRequest( |
| 94 bool is_post, |
| 95 const Referrer& sanitized_referrer, |
| 96 bool has_user_gesture, |
| 97 ui::PageTransition transition, |
| 98 bool is_external_protocol) { |
| 99 // Update the navigation parameters. |
| 100 is_post_ = is_post; |
| 101 sanitized_referrer_ = sanitized_referrer; |
| 102 has_user_gesture_ = has_user_gesture; |
| 103 transition_ = transition; |
| 104 is_external_protocol_ = is_external_protocol; |
| 105 |
| 106 state_ = WILL_SEND_REQUEST; |
| 107 |
| 108 // Register the navigation throttles. The ScopedVector returned by |
| 109 // GetNavigationThrottles is not assigned to throttles_ directly because it |
| 110 // would overwrite any throttle previously added with |
| 111 // RegisterThrottleForTesting. |
| 112 ScopedVector<NavigationThrottle> throttles_to_register = |
| 113 GetContentClient()->browser()->CreateThrottlesForNavigation(this); |
| 114 if (throttles_to_register.size() > 0) { |
| 115 throttles_.insert(throttles_.end(), throttles_to_register.begin(), |
| 116 throttles_to_register.end()); |
| 117 throttles_to_register.weak_clear(); |
| 118 } |
| 119 |
| 120 // Notify each throttle of the request. |
| 121 for (NavigationThrottle* throttle : throttles_) { |
| 122 NavigationThrottle::ThrottleCheckResult result = |
| 123 throttle->WillStartRequest(); |
| 124 if (result == NavigationThrottle::CANCEL_AND_IGNORE) |
| 125 return NavigationThrottle::CANCEL_AND_IGNORE; |
| 126 } |
| 127 return NavigationThrottle::PROCEED; |
| 128 } |
| 129 |
| 130 NavigationThrottle::ThrottleCheckResult |
| 131 NavigationHandleImpl::WillRedirectRequest(const GURL& new_url, |
| 132 bool new_method_is_post, |
| 133 const GURL& new_referrer_url, |
| 134 bool new_is_external_protocol) { |
| 135 // Update the navigation parameters. |
| 136 url_ = new_url; |
| 137 is_post_ = new_method_is_post; |
| 138 sanitized_referrer_.url = new_referrer_url; |
| 139 sanitized_referrer_ = Referrer::SanitizeForRequest(url_, sanitized_referrer_); |
| 140 is_external_protocol_ = new_is_external_protocol; |
| 141 |
| 142 // Have each throttle be notified of the request. |
| 143 for (NavigationThrottle* throttle : throttles_) { |
| 144 NavigationThrottle::ThrottleCheckResult result = |
| 145 throttle->WillRedirectRequest(); |
| 146 if (result == NavigationThrottle::CANCEL_AND_IGNORE) |
| 147 return NavigationThrottle::CANCEL_AND_IGNORE; |
| 148 } |
| 149 return NavigationThrottle::PROCEED; |
| 150 } |
| 151 |
| 57 void NavigationHandleImpl::DidRedirectNavigation(const GURL& new_url) { | 152 void NavigationHandleImpl::DidRedirectNavigation(const GURL& new_url) { |
| 58 url_ = new_url; | 153 url_ = new_url; |
| 59 delegate_->DidRedirectNavigation(this); | 154 delegate_->DidRedirectNavigation(this); |
| 60 } | 155 } |
| 61 | 156 |
| 62 void NavigationHandleImpl::DidCommitNavigation() { | 157 void NavigationHandleImpl::DidCommitNavigation() { |
| 63 state_ = net_error_code_ == net::OK ? DID_COMMIT : DID_COMMIT_ERROR_PAGE; | 158 state_ = net_error_code_ == net::OK ? DID_COMMIT : DID_COMMIT_ERROR_PAGE; |
| 64 delegate_->DidCommitNavigation(this); | 159 delegate_->DidCommitNavigation(this); |
| 65 } | 160 } |
| 66 | 161 |
| 67 } // namespace content | 162 } // namespace content |
| OLD | NEW |