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