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