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