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"
22 const char kWebContentsObserverSanityCheckerKey
[] =
23 "WebContentsObserverSanityChecker";
28 void WebContentsObserverSanityChecker::Enable(WebContents
* web_contents
) {
29 if (web_contents
->GetUserData(&kWebContentsObserverSanityCheckerKey
))
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
);
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.";
54 static_cast<RenderFrameHostImpl
*>(render_frame_host
)->IsRenderFrameLive())
55 << "RenderFrameCreated called on for a RenderFrameHost that thinks it is "
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
76 CHECK(false) << "RenderFrameDeleted called for routing pair "
77 << Format(render_frame_host
)
78 << " for which RenderFrameCreated was never called";
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
) {
92 CHECK_NE(new_host
, 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
);
101 << "RenderFrameHostChanged called with old host that did not exist:"
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
;
112 << "RenderFrameHostChanged called more than once for routing pair:"
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());
133 ongoing_navigations_
.insert(navigation_handle
);
136 void WebContentsObserverSanityChecker::DidRedirectNavigation(
137 NavigationHandle
* navigation_handle
) {
138 CHECK(NavigationIsOngoing(navigation_handle
));
139 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle
));
141 CHECK(navigation_handle
->GetNetErrorCode() == net::OK
);
142 CHECK(!navigation_handle
->HasCommittedDocument());
143 CHECK(!navigation_handle
->HasCommittedErrorPage());
146 void WebContentsObserverSanityChecker::ReadyToCommitNavigation(
147 NavigationHandle
* navigation_handle
) {
148 CHECK(NavigationIsOngoing(navigation_handle
));
149 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle
));
151 CHECK(!navigation_handle
->HasCommittedDocument());
152 CHECK(!navigation_handle
->HasCommittedErrorPage());
155 void WebContentsObserverSanityChecker::DidCommitNavigation(
156 NavigationHandle
* navigation_handle
) {
157 CHECK(NavigationIsOngoing(navigation_handle
));
158 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle
));
160 CHECK_NE(navigation_handle
->HasCommittedDocument(),
161 navigation_handle
->HasCommittedErrorPage());
162 CHECK_IMPLIES(navigation_handle
->HasCommittedDocument(),
163 navigation_handle
->GetNetErrorCode() == net::OK
);
164 CHECK_IMPLIES(navigation_handle
->HasCommittedErrorPage(),
165 navigation_handle
->GetNetErrorCode() != net::OK
);
167 ongoing_committed_navigations_
.insert(navigation_handle
);
170 void WebContentsObserverSanityChecker::DidFinishNavigation(
171 NavigationHandle
* navigation_handle
) {
172 CHECK(NavigationIsOngoing(navigation_handle
));
174 CHECK_IMPLIES(NavigationIsOngoingAndCommitted(navigation_handle
),
175 navigation_handle
->HasCommittedDocument() !=
176 navigation_handle
->HasCommittedErrorPage());
177 CHECK_IMPLIES(navigation_handle
->HasCommittedDocument(),
178 navigation_handle
->GetNetErrorCode() == net::OK
);
179 CHECK_IMPLIES(navigation_handle
->HasCommittedErrorPage(),
180 navigation_handle
->GetNetErrorCode() != net::OK
);
182 if (NavigationIsOngoingAndCommitted(navigation_handle
))
183 ongoing_committed_navigations_
.erase(navigation_handle
);
185 ongoing_navigations_
.erase(navigation_handle
);
188 void WebContentsObserverSanityChecker::DidStartProvisionalLoadForFrame(
189 RenderFrameHost
* render_frame_host
,
190 const GURL
& validated_url
,
192 bool is_iframe_srcdoc
) {
193 AssertRenderFrameExists(render_frame_host
);
196 void WebContentsObserverSanityChecker::DidCommitProvisionalLoadForFrame(
197 RenderFrameHost
* render_frame_host
,
199 ui::PageTransition transition_type
) {
200 AssertRenderFrameExists(render_frame_host
);
203 void WebContentsObserverSanityChecker::DidFailProvisionalLoad(
204 RenderFrameHost
* render_frame_host
,
205 const GURL
& validated_url
,
207 const base::string16
& error_description
,
208 bool was_ignored_by_handler
) {
209 AssertRenderFrameExists(render_frame_host
);
212 void WebContentsObserverSanityChecker::DidNavigateMainFrame(
213 const LoadCommittedDetails
& details
,
214 const FrameNavigateParams
& params
) {
215 AssertMainFrameExists();
218 void WebContentsObserverSanityChecker::DidNavigateAnyFrame(
219 RenderFrameHost
* render_frame_host
,
220 const LoadCommittedDetails
& details
,
221 const FrameNavigateParams
& params
) {
222 AssertRenderFrameExists(render_frame_host
);
225 void WebContentsObserverSanityChecker::DocumentAvailableInMainFrame() {
226 AssertMainFrameExists();
229 void WebContentsObserverSanityChecker::DocumentOnLoadCompletedInMainFrame() {
230 AssertMainFrameExists();
233 void WebContentsObserverSanityChecker::DocumentLoadedInFrame(
234 RenderFrameHost
* render_frame_host
) {
235 AssertRenderFrameExists(render_frame_host
);
238 void WebContentsObserverSanityChecker::DidFinishLoad(
239 RenderFrameHost
* render_frame_host
,
240 const GURL
& validated_url
) {
241 AssertRenderFrameExists(render_frame_host
);
244 void WebContentsObserverSanityChecker::DidFailLoad(
245 RenderFrameHost
* render_frame_host
,
246 const GURL
& validated_url
,
248 const base::string16
& error_description
,
249 bool was_ignored_by_handler
) {
250 AssertRenderFrameExists(render_frame_host
);
253 void WebContentsObserverSanityChecker::DidGetRedirectForResourceRequest(
254 RenderFrameHost
* render_frame_host
,
255 const ResourceRedirectDetails
& details
) {
256 AssertRenderFrameExists(render_frame_host
);
259 void WebContentsObserverSanityChecker::DidOpenRequestedURL(
260 WebContents
* new_contents
,
261 RenderFrameHost
* source_render_frame_host
,
263 const Referrer
& referrer
,
264 WindowOpenDisposition disposition
,
265 ui::PageTransition transition
) {
266 AssertRenderFrameExists(source_render_frame_host
);
269 bool WebContentsObserverSanityChecker::OnMessageReceived(
270 const IPC::Message
& message
,
271 RenderFrameHost
* render_frame_host
) {
272 // TODO(nasko): FrameHostMsg_RenderProcessGone is delivered to
273 // WebContentsObserver since RenderFrameHost allows the delegate to handle
274 // the message first. This shouldn't happen, but for now handle it here.
275 // https://crbug.com/450799
276 if (message
.type() == FrameHostMsg_RenderProcessGone::ID
)
279 #if !defined(OS_MACOSX)
280 // TODO(avi): Disabled because of http://crbug.com/445054
281 AssertRenderFrameExists(render_frame_host
);
286 void WebContentsObserverSanityChecker::WebContentsDestroyed() {
287 CHECK(!web_contents_destroyed_
);
288 web_contents_destroyed_
= true;
289 CHECK(ongoing_navigations_
.empty());
290 CHECK(ongoing_committed_navigations_
.empty());
293 WebContentsObserverSanityChecker::WebContentsObserverSanityChecker(
294 WebContents
* web_contents
)
295 : WebContentsObserver(web_contents
), web_contents_destroyed_(false) {
296 // Prime the pump with the initial objects.
297 // TODO(nasko): Investigate why this is needed.
298 RenderViewCreated(web_contents
->GetRenderViewHost());
301 WebContentsObserverSanityChecker::~WebContentsObserverSanityChecker() {
302 CHECK(web_contents_destroyed_
);
305 void WebContentsObserverSanityChecker::AssertRenderFrameExists(
306 RenderFrameHost
* render_frame_host
) {
307 CHECK(!web_contents_destroyed_
);
308 std::pair
<int, int> routing_pair
=
309 std::make_pair(render_frame_host
->GetProcess()->GetID(),
310 render_frame_host
->GetRoutingID());
312 bool render_frame_created_happened
= live_routes_
.count(routing_pair
) != 0;
313 bool render_frame_deleted_happened
= deleted_routes_
.count(routing_pair
) != 0;
315 CHECK(render_frame_created_happened
)
316 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
317 << "method, but WebContentsObserver::RenderFrameCreated was never called "
318 << "for that RenderFrameHost: " << Format(render_frame_host
);
319 CHECK(!render_frame_deleted_happened
)
320 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
321 << "method, but WebContentsObserver::RenderFrameDeleted had already been "
322 << "called on that frame:" << Format(render_frame_host
);
325 void WebContentsObserverSanityChecker::AssertMainFrameExists() {
326 AssertRenderFrameExists(web_contents()->GetMainFrame());
329 std::string
WebContentsObserverSanityChecker::Format(
330 RenderFrameHost
* render_frame_host
) {
331 return base::StringPrintf(
332 "(%d, %d -> %s)", render_frame_host
->GetProcess()->GetID(),
333 render_frame_host
->GetRoutingID(),
334 render_frame_host
->GetSiteInstance()->GetSiteURL().spec().c_str());
337 bool WebContentsObserverSanityChecker::NavigationIsOngoing(
338 NavigationHandle
* navigation_handle
) {
339 auto it
= ongoing_navigations_
.find(navigation_handle
);
340 return it
!= ongoing_navigations_
.end();
343 bool WebContentsObserverSanityChecker::NavigationIsOngoingAndCommitted(
344 NavigationHandle
* navigation_handle
) {
345 auto it
= ongoing_committed_navigations_
.find(navigation_handle
);
346 return it
!= ongoing_committed_navigations_
.end();
349 } // namespace content