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" | |
10 #include "net/url_request/redirect_info.h" | 8 #include "net/url_request/redirect_info.h" |
11 | 9 |
12 namespace content { | 10 namespace content { |
13 | 11 |
14 // static | 12 // static |
15 scoped_ptr<NavigationHandleImpl> NavigationHandleImpl::Create( | 13 scoped_ptr<NavigationHandleImpl> NavigationHandleImpl::Create( |
16 const GURL& url, | 14 const GURL& url, |
17 bool is_main_frame, | 15 const bool is_main_frame, |
18 NavigatorDelegate* delegate) { | 16 NavigatorDelegate* delegate) { |
19 return scoped_ptr<NavigationHandleImpl>( | 17 return scoped_ptr<NavigationHandleImpl>( |
20 new NavigationHandleImpl(url, is_main_frame, delegate)); | 18 new NavigationHandleImpl(url, is_main_frame, delegate)); |
21 } | 19 } |
22 | 20 |
23 NavigationHandleImpl::NavigationHandleImpl(const GURL& url, | 21 NavigationHandleImpl::NavigationHandleImpl(const GURL& url, |
24 const bool is_main_frame, | 22 const bool is_main_frame, |
25 NavigatorDelegate* delegate) | 23 NavigatorDelegate* delegate) |
26 : url_(url), | 24 : url_(url), |
| 25 net_error_code_(net::OK), |
| 26 state_(DID_START), |
27 is_main_frame_(is_main_frame), | 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), | |
32 net_error_code_(net::OK), | |
33 is_same_page_(false), | 28 is_same_page_(false), |
34 state_(INITIAL), | |
35 is_transferring_(false), | 29 is_transferring_(false), |
36 delegate_(delegate) { | 30 delegate_(delegate) { |
37 delegate_->DidStartNavigation(this); | 31 delegate_->DidStartNavigation(this); |
38 } | 32 } |
39 | 33 |
40 NavigationHandleImpl::~NavigationHandleImpl() { | 34 NavigationHandleImpl::~NavigationHandleImpl() { |
41 delegate_->DidFinishNavigation(this); | 35 delegate_->DidFinishNavigation(this); |
42 } | 36 } |
43 | 37 |
44 const GURL& NavigationHandleImpl::GetURL() { | 38 const GURL& NavigationHandleImpl::GetURL() const { |
45 return url_; | 39 return url_; |
46 } | 40 } |
47 | 41 |
48 bool NavigationHandleImpl::IsInMainFrame() { | 42 net::Error NavigationHandleImpl::GetNetErrorCode() const { |
| 43 return net_error_code_; |
| 44 } |
| 45 |
| 46 bool NavigationHandleImpl::IsInMainFrame() const { |
49 return is_main_frame_; | 47 return is_main_frame_; |
50 } | 48 } |
51 | 49 |
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() { | |
83 return net_error_code_; | |
84 } | |
85 | |
86 bool NavigationHandleImpl::IsSamePage() { | 50 bool NavigationHandleImpl::IsSamePage() { |
87 DCHECK(state_ == DID_COMMIT || state_ == DID_COMMIT_ERROR_PAGE) | 51 DCHECK(state_ == DID_COMMIT || state_ == DID_COMMIT_ERROR_PAGE) |
88 << "This accessor should not be called before the navigation has " | 52 << "This accessor should not be called before the navigation has " |
89 "committed."; | 53 "committed."; |
90 return is_same_page_; | 54 return is_same_page_; |
91 } | 55 } |
92 | 56 |
93 bool NavigationHandleImpl::HasCommittedDocument() { | 57 bool NavigationHandleImpl::HasCommittedDocument() const { |
94 return state_ == DID_COMMIT; | 58 return state_ == DID_COMMIT; |
95 } | 59 } |
96 | 60 |
97 bool NavigationHandleImpl::HasCommittedErrorPage() { | 61 bool NavigationHandleImpl::HasCommittedErrorPage() const { |
98 return state_ == DID_COMMIT_ERROR_PAGE; | 62 return state_ == DID_COMMIT_ERROR_PAGE; |
99 } | 63 } |
100 | 64 |
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 | |
186 void NavigationHandleImpl::DidRedirectNavigation(const GURL& new_url) { | 65 void NavigationHandleImpl::DidRedirectNavigation(const GURL& new_url) { |
187 url_ = new_url; | 66 url_ = new_url; |
188 delegate_->DidRedirectNavigation(this); | 67 delegate_->DidRedirectNavigation(this); |
189 } | 68 } |
190 | 69 |
191 void NavigationHandleImpl::DidCommitNavigation(bool same_page) { | 70 void NavigationHandleImpl::DidCommitNavigation(bool same_page) { |
192 is_same_page_ = same_page; | 71 is_same_page_ = same_page; |
193 state_ = net_error_code_ == net::OK ? DID_COMMIT : DID_COMMIT_ERROR_PAGE; | 72 state_ = net_error_code_ == net::OK ? DID_COMMIT : DID_COMMIT_ERROR_PAGE; |
194 delegate_->DidCommitNavigation(this); | 73 delegate_->DidCommitNavigation(this); |
195 } | 74 } |
196 | 75 |
197 } // namespace content | 76 } // namespace content |
OLD | NEW |