Add a NavigationThrottle to the public content/ interface
[chromium-blink-merge.git] / content / test / web_contents_observer_sanity_checker.cc
blob18d38ae0549037f2b752ad7cd00b9938c8c118f4
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/test/web_contents_observer_sanity_checker.h"
7 #include "base/strings/stringprintf.h"
8 #include "content/browser/frame_host/render_frame_host_impl.h"
9 #include "content/common/frame_messages.h"
10 #include "content/public/browser/navigation_handle.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/site_instance.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "net/base/net_errors.h"
18 namespace content {
20 namespace {
22 const char kWebContentsObserverSanityCheckerKey[] =
23 "WebContentsObserverSanityChecker";
25 } // namespace
27 // static
28 void WebContentsObserverSanityChecker::Enable(WebContents* web_contents) {
29 if (web_contents->GetUserData(&kWebContentsObserverSanityCheckerKey))
30 return;
31 web_contents->SetUserData(&kWebContentsObserverSanityCheckerKey,
32 new WebContentsObserverSanityChecker(web_contents));
35 void WebContentsObserverSanityChecker::RenderFrameCreated(
36 RenderFrameHost* render_frame_host) {
37 CHECK(!web_contents_destroyed_);
38 std::pair<int, int> routing_pair =
39 std::make_pair(render_frame_host->GetProcess()->GetID(),
40 render_frame_host->GetRoutingID());
41 bool frame_exists = !live_routes_.insert(routing_pair).second;
42 deleted_routes_.erase(routing_pair);
44 if (frame_exists) {
45 CHECK(false) << "RenderFrameCreated called more than once for routing pair:"
46 << Format(render_frame_host);
49 CHECK(render_frame_host->GetProcess()->HasConnection())
50 << "RenderFrameCreated was called for a RenderFrameHost whose render "
51 "process is not currently live, so there's no way for the RenderFrame "
52 "to have been created.";
53 CHECK(
54 static_cast<RenderFrameHostImpl*>(render_frame_host)->IsRenderFrameLive())
55 << "RenderFrameCreated called on for a RenderFrameHost that thinks it is "
56 "not alive.";
59 void WebContentsObserverSanityChecker::RenderFrameDeleted(
60 RenderFrameHost* render_frame_host) {
61 CHECK(!web_contents_destroyed_);
62 std::pair<int, int> routing_pair =
63 std::make_pair(render_frame_host->GetProcess()->GetID(),
64 render_frame_host->GetRoutingID());
65 bool was_live = !!live_routes_.erase(routing_pair);
66 bool was_dead_already = !deleted_routes_.insert(routing_pair).second;
68 if (was_dead_already) {
69 CHECK(false) << "RenderFrameDeleted called more than once for routing pair "
70 << Format(render_frame_host);
71 } else if (!was_live) {
72 // TODO(nick): Clients can easily ignore an unrecognized object, but it
73 // would be useful from a finding-bugs perspective if we could enable this
74 // check.
75 #if 0
76 CHECK(false) << "RenderFrameDeleted called for routing pair "
77 << Format(render_frame_host)
78 << " for which RenderFrameCreated was never called";
79 #endif
83 void WebContentsObserverSanityChecker::RenderFrameForInterstitialPageCreated(
84 RenderFrameHost* render_frame_host) {
85 // TODO(nick): Record this.
88 void WebContentsObserverSanityChecker::RenderFrameHostChanged(
89 RenderFrameHost* old_host,
90 RenderFrameHost* new_host) {
91 CHECK(new_host);
92 CHECK_NE(new_host, old_host);
94 if (old_host) {
95 std::pair<int, int> routing_pair =
96 std::make_pair(old_host->GetProcess()->GetID(),
97 old_host->GetRoutingID());
98 bool old_did_exist = !!current_hosts_.erase(routing_pair);
99 if (!old_did_exist) {
100 CHECK(false)
101 << "RenderFrameHostChanged called with old host that did not exist:"
102 << Format(old_host);
106 std::pair<int, int> routing_pair =
107 std::make_pair(new_host->GetProcess()->GetID(),
108 new_host->GetRoutingID());
109 bool host_exists = !current_hosts_.insert(routing_pair).second;
110 if (host_exists) {
111 CHECK(false)
112 << "RenderFrameHostChanged called more than once for routing pair:"
113 << Format(new_host);
117 void WebContentsObserverSanityChecker::FrameDeleted(
118 RenderFrameHost* render_frame_host) {
119 // A frame can be deleted before RenderFrame in the renderer process is
120 // created, so there is not much that can be enforced here.
121 CHECK(!web_contents_destroyed_);
124 void WebContentsObserverSanityChecker::DidStartNavigation(
125 NavigationHandle* navigation_handle) {
126 CHECK(!NavigationIsOngoing(navigation_handle));
127 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle));
129 CHECK(navigation_handle->GetNetErrorCode() == net::OK);
130 CHECK(!navigation_handle->HasCommittedDocument());
131 CHECK(!navigation_handle->HasCommittedErrorPage());
132 CHECK_EQ(navigation_handle->GetWebContents(), web_contents());
134 ongoing_navigations_.insert(navigation_handle);
137 void WebContentsObserverSanityChecker::DidRedirectNavigation(
138 NavigationHandle* navigation_handle) {
139 CHECK(NavigationIsOngoing(navigation_handle));
140 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle));
142 CHECK(navigation_handle->GetNetErrorCode() == net::OK);
143 CHECK(!navigation_handle->HasCommittedDocument());
144 CHECK(!navigation_handle->HasCommittedErrorPage());
145 CHECK_EQ(navigation_handle->GetWebContents(), web_contents());
148 void WebContentsObserverSanityChecker::ReadyToCommitNavigation(
149 NavigationHandle* navigation_handle) {
150 CHECK(NavigationIsOngoing(navigation_handle));
151 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle));
153 CHECK(!navigation_handle->HasCommittedDocument());
154 CHECK(!navigation_handle->HasCommittedErrorPage());
157 void WebContentsObserverSanityChecker::DidCommitNavigation(
158 NavigationHandle* navigation_handle) {
159 CHECK(NavigationIsOngoing(navigation_handle));
160 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle));
162 CHECK_NE(navigation_handle->HasCommittedDocument(),
163 navigation_handle->HasCommittedErrorPage());
164 CHECK_IMPLIES(navigation_handle->HasCommittedDocument(),
165 navigation_handle->GetNetErrorCode() == net::OK);
166 CHECK_IMPLIES(navigation_handle->HasCommittedErrorPage(),
167 navigation_handle->GetNetErrorCode() != net::OK);
168 CHECK_EQ(navigation_handle->GetWebContents(), web_contents());
170 ongoing_committed_navigations_.insert(navigation_handle);
173 void WebContentsObserverSanityChecker::DidFinishNavigation(
174 NavigationHandle* navigation_handle) {
175 CHECK(NavigationIsOngoing(navigation_handle));
177 CHECK_IMPLIES(NavigationIsOngoingAndCommitted(navigation_handle),
178 navigation_handle->HasCommittedDocument() !=
179 navigation_handle->HasCommittedErrorPage());
180 CHECK_IMPLIES(navigation_handle->HasCommittedDocument(),
181 navigation_handle->GetNetErrorCode() == net::OK);
182 CHECK_IMPLIES(navigation_handle->HasCommittedErrorPage(),
183 navigation_handle->GetNetErrorCode() != net::OK);
184 CHECK_EQ(navigation_handle->GetWebContents(), web_contents());
186 if (NavigationIsOngoingAndCommitted(navigation_handle))
187 ongoing_committed_navigations_.erase(navigation_handle);
189 ongoing_navigations_.erase(navigation_handle);
192 void WebContentsObserverSanityChecker::DidStartProvisionalLoadForFrame(
193 RenderFrameHost* render_frame_host,
194 const GURL& validated_url,
195 bool is_error_page,
196 bool is_iframe_srcdoc) {
197 AssertRenderFrameExists(render_frame_host);
200 void WebContentsObserverSanityChecker::DidCommitProvisionalLoadForFrame(
201 RenderFrameHost* render_frame_host,
202 const GURL& url,
203 ui::PageTransition transition_type) {
204 AssertRenderFrameExists(render_frame_host);
207 void WebContentsObserverSanityChecker::DidFailProvisionalLoad(
208 RenderFrameHost* render_frame_host,
209 const GURL& validated_url,
210 int error_code,
211 const base::string16& error_description,
212 bool was_ignored_by_handler) {
213 AssertRenderFrameExists(render_frame_host);
216 void WebContentsObserverSanityChecker::DidNavigateMainFrame(
217 const LoadCommittedDetails& details,
218 const FrameNavigateParams& params) {
219 AssertMainFrameExists();
222 void WebContentsObserverSanityChecker::DidNavigateAnyFrame(
223 RenderFrameHost* render_frame_host,
224 const LoadCommittedDetails& details,
225 const FrameNavigateParams& params) {
226 AssertRenderFrameExists(render_frame_host);
229 void WebContentsObserverSanityChecker::DocumentAvailableInMainFrame() {
230 AssertMainFrameExists();
233 void WebContentsObserverSanityChecker::DocumentOnLoadCompletedInMainFrame() {
234 AssertMainFrameExists();
237 void WebContentsObserverSanityChecker::DocumentLoadedInFrame(
238 RenderFrameHost* render_frame_host) {
239 AssertRenderFrameExists(render_frame_host);
242 void WebContentsObserverSanityChecker::DidFinishLoad(
243 RenderFrameHost* render_frame_host,
244 const GURL& validated_url) {
245 AssertRenderFrameExists(render_frame_host);
248 void WebContentsObserverSanityChecker::DidFailLoad(
249 RenderFrameHost* render_frame_host,
250 const GURL& validated_url,
251 int error_code,
252 const base::string16& error_description,
253 bool was_ignored_by_handler) {
254 AssertRenderFrameExists(render_frame_host);
257 void WebContentsObserverSanityChecker::DidGetRedirectForResourceRequest(
258 RenderFrameHost* render_frame_host,
259 const ResourceRedirectDetails& details) {
260 AssertRenderFrameExists(render_frame_host);
263 void WebContentsObserverSanityChecker::DidOpenRequestedURL(
264 WebContents* new_contents,
265 RenderFrameHost* source_render_frame_host,
266 const GURL& url,
267 const Referrer& referrer,
268 WindowOpenDisposition disposition,
269 ui::PageTransition transition) {
270 AssertRenderFrameExists(source_render_frame_host);
273 bool WebContentsObserverSanityChecker::OnMessageReceived(
274 const IPC::Message& message,
275 RenderFrameHost* render_frame_host) {
276 // TODO(nasko): FrameHostMsg_RenderProcessGone is delivered to
277 // WebContentsObserver since RenderFrameHost allows the delegate to handle
278 // the message first. This shouldn't happen, but for now handle it here.
279 // https://crbug.com/450799
280 if (message.type() == FrameHostMsg_RenderProcessGone::ID)
281 return false;
283 #if !defined(OS_MACOSX)
284 // TODO(avi): Disabled because of http://crbug.com/445054
285 AssertRenderFrameExists(render_frame_host);
286 #endif
287 return false;
290 void WebContentsObserverSanityChecker::WebContentsDestroyed() {
291 CHECK(!web_contents_destroyed_);
292 web_contents_destroyed_ = true;
293 CHECK(ongoing_navigations_.empty());
294 CHECK(ongoing_committed_navigations_.empty());
297 WebContentsObserverSanityChecker::WebContentsObserverSanityChecker(
298 WebContents* web_contents)
299 : WebContentsObserver(web_contents), web_contents_destroyed_(false) {
300 // Prime the pump with the initial objects.
301 // TODO(nasko): Investigate why this is needed.
302 RenderViewCreated(web_contents->GetRenderViewHost());
305 WebContentsObserverSanityChecker::~WebContentsObserverSanityChecker() {
306 CHECK(web_contents_destroyed_);
309 void WebContentsObserverSanityChecker::AssertRenderFrameExists(
310 RenderFrameHost* render_frame_host) {
311 CHECK(!web_contents_destroyed_);
312 std::pair<int, int> routing_pair =
313 std::make_pair(render_frame_host->GetProcess()->GetID(),
314 render_frame_host->GetRoutingID());
316 bool render_frame_created_happened = live_routes_.count(routing_pair) != 0;
317 bool render_frame_deleted_happened = deleted_routes_.count(routing_pair) != 0;
319 CHECK(render_frame_created_happened)
320 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
321 << "method, but WebContentsObserver::RenderFrameCreated was never called "
322 << "for that RenderFrameHost: " << Format(render_frame_host);
323 CHECK(!render_frame_deleted_happened)
324 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
325 << "method, but WebContentsObserver::RenderFrameDeleted had already been "
326 << "called on that frame:" << Format(render_frame_host);
329 void WebContentsObserverSanityChecker::AssertMainFrameExists() {
330 AssertRenderFrameExists(web_contents()->GetMainFrame());
333 std::string WebContentsObserverSanityChecker::Format(
334 RenderFrameHost* render_frame_host) {
335 return base::StringPrintf(
336 "(%d, %d -> %s)", render_frame_host->GetProcess()->GetID(),
337 render_frame_host->GetRoutingID(),
338 render_frame_host->GetSiteInstance()->GetSiteURL().spec().c_str());
341 bool WebContentsObserverSanityChecker::NavigationIsOngoing(
342 NavigationHandle* navigation_handle) {
343 auto it = ongoing_navigations_.find(navigation_handle);
344 return it != ongoing_navigations_.end();
347 bool WebContentsObserverSanityChecker::NavigationIsOngoingAndCommitted(
348 NavigationHandle* navigation_handle) {
349 auto it = ongoing_committed_navigations_.find(navigation_handle);
350 return it != ongoing_committed_navigations_.end();
353 } // namespace content