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::DidCommitNavigation(
147 NavigationHandle
* navigation_handle
) {
148 CHECK(NavigationIsOngoing(navigation_handle
));
149 CHECK(!NavigationIsOngoingAndCommitted(navigation_handle
));
151 CHECK_NE(navigation_handle
->HasCommittedDocument(),
152 navigation_handle
->HasCommittedErrorPage());
153 CHECK_IMPLIES(navigation_handle
->HasCommittedDocument(),
154 navigation_handle
->GetNetErrorCode() == net::OK
);
155 CHECK_IMPLIES(navigation_handle
->HasCommittedErrorPage(),
156 navigation_handle
->GetNetErrorCode() != net::OK
);
158 ongoing_committed_navigations_
.insert(navigation_handle
);
161 void WebContentsObserverSanityChecker::DidFinishNavigation(
162 NavigationHandle
* navigation_handle
) {
163 CHECK(NavigationIsOngoing(navigation_handle
));
165 CHECK_IMPLIES(NavigationIsOngoingAndCommitted(navigation_handle
),
166 navigation_handle
->HasCommittedDocument() !=
167 navigation_handle
->HasCommittedErrorPage());
168 CHECK_IMPLIES(navigation_handle
->HasCommittedDocument(),
169 navigation_handle
->GetNetErrorCode() == net::OK
);
170 CHECK_IMPLIES(navigation_handle
->HasCommittedErrorPage(),
171 navigation_handle
->GetNetErrorCode() != net::OK
);
173 if (NavigationIsOngoingAndCommitted(navigation_handle
))
174 ongoing_committed_navigations_
.erase(navigation_handle
);
176 ongoing_navigations_
.erase(navigation_handle
);
179 void WebContentsObserverSanityChecker::DidStartProvisionalLoadForFrame(
180 RenderFrameHost
* render_frame_host
,
181 const GURL
& validated_url
,
183 bool is_iframe_srcdoc
) {
184 AssertRenderFrameExists(render_frame_host
);
187 void WebContentsObserverSanityChecker::DidCommitProvisionalLoadForFrame(
188 RenderFrameHost
* render_frame_host
,
190 ui::PageTransition transition_type
) {
191 AssertRenderFrameExists(render_frame_host
);
194 void WebContentsObserverSanityChecker::DidFailProvisionalLoad(
195 RenderFrameHost
* render_frame_host
,
196 const GURL
& validated_url
,
198 const base::string16
& error_description
,
199 bool was_ignored_by_handler
) {
200 AssertRenderFrameExists(render_frame_host
);
203 void WebContentsObserverSanityChecker::DidNavigateMainFrame(
204 const LoadCommittedDetails
& details
,
205 const FrameNavigateParams
& params
) {
206 AssertMainFrameExists();
209 void WebContentsObserverSanityChecker::DidNavigateAnyFrame(
210 RenderFrameHost
* render_frame_host
,
211 const LoadCommittedDetails
& details
,
212 const FrameNavigateParams
& params
) {
213 AssertRenderFrameExists(render_frame_host
);
216 void WebContentsObserverSanityChecker::DocumentAvailableInMainFrame() {
217 AssertMainFrameExists();
220 void WebContentsObserverSanityChecker::DocumentOnLoadCompletedInMainFrame() {
221 AssertMainFrameExists();
224 void WebContentsObserverSanityChecker::DocumentLoadedInFrame(
225 RenderFrameHost
* render_frame_host
) {
226 AssertRenderFrameExists(render_frame_host
);
229 void WebContentsObserverSanityChecker::DidFinishLoad(
230 RenderFrameHost
* render_frame_host
,
231 const GURL
& validated_url
) {
232 AssertRenderFrameExists(render_frame_host
);
235 void WebContentsObserverSanityChecker::DidFailLoad(
236 RenderFrameHost
* render_frame_host
,
237 const GURL
& validated_url
,
239 const base::string16
& error_description
,
240 bool was_ignored_by_handler
) {
241 AssertRenderFrameExists(render_frame_host
);
244 void WebContentsObserverSanityChecker::DidGetRedirectForResourceRequest(
245 RenderFrameHost
* render_frame_host
,
246 const ResourceRedirectDetails
& details
) {
247 AssertRenderFrameExists(render_frame_host
);
250 void WebContentsObserverSanityChecker::DidOpenRequestedURL(
251 WebContents
* new_contents
,
252 RenderFrameHost
* source_render_frame_host
,
254 const Referrer
& referrer
,
255 WindowOpenDisposition disposition
,
256 ui::PageTransition transition
) {
257 AssertRenderFrameExists(source_render_frame_host
);
260 bool WebContentsObserverSanityChecker::OnMessageReceived(
261 const IPC::Message
& message
,
262 RenderFrameHost
* render_frame_host
) {
263 // TODO(nasko): FrameHostMsg_RenderProcessGone is delivered to
264 // WebContentsObserver since RenderFrameHost allows the delegate to handle
265 // the message first. This shouldn't happen, but for now handle it here.
266 // https://crbug.com/450799
267 if (message
.type() == FrameHostMsg_RenderProcessGone::ID
)
270 #if !defined(OS_MACOSX)
271 // TODO(avi): Disabled because of http://crbug.com/445054
272 AssertRenderFrameExists(render_frame_host
);
277 void WebContentsObserverSanityChecker::WebContentsDestroyed() {
278 CHECK(!web_contents_destroyed_
);
279 web_contents_destroyed_
= true;
280 CHECK(ongoing_navigations_
.empty());
281 CHECK(ongoing_committed_navigations_
.empty());
284 WebContentsObserverSanityChecker::WebContentsObserverSanityChecker(
285 WebContents
* web_contents
)
286 : WebContentsObserver(web_contents
), web_contents_destroyed_(false) {
287 // Prime the pump with the initial objects.
288 // TODO(nasko): Investigate why this is needed.
289 RenderViewCreated(web_contents
->GetRenderViewHost());
292 WebContentsObserverSanityChecker::~WebContentsObserverSanityChecker() {
293 CHECK(web_contents_destroyed_
);
296 void WebContentsObserverSanityChecker::AssertRenderFrameExists(
297 RenderFrameHost
* render_frame_host
) {
298 CHECK(!web_contents_destroyed_
);
299 std::pair
<int, int> routing_pair
=
300 std::make_pair(render_frame_host
->GetProcess()->GetID(),
301 render_frame_host
->GetRoutingID());
303 bool render_frame_created_happened
= live_routes_
.count(routing_pair
) != 0;
304 bool render_frame_deleted_happened
= deleted_routes_
.count(routing_pair
) != 0;
306 CHECK(render_frame_created_happened
)
307 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
308 << "method, but WebContentsObserver::RenderFrameCreated was never called "
309 << "for that RenderFrameHost: " << Format(render_frame_host
);
310 CHECK(!render_frame_deleted_happened
)
311 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
312 << "method, but WebContentsObserver::RenderFrameDeleted had already been "
313 << "called on that frame:" << Format(render_frame_host
);
316 void WebContentsObserverSanityChecker::AssertMainFrameExists() {
317 AssertRenderFrameExists(web_contents()->GetMainFrame());
320 std::string
WebContentsObserverSanityChecker::Format(
321 RenderFrameHost
* render_frame_host
) {
322 return base::StringPrintf(
323 "(%d, %d -> %s)", render_frame_host
->GetProcess()->GetID(),
324 render_frame_host
->GetRoutingID(),
325 render_frame_host
->GetSiteInstance()->GetSiteURL().spec().c_str());
328 bool WebContentsObserverSanityChecker::NavigationIsOngoing(
329 NavigationHandle
* navigation_handle
) {
330 auto it
= ongoing_navigations_
.find(navigation_handle
);
331 return it
!= ongoing_navigations_
.end();
334 bool WebContentsObserverSanityChecker::NavigationIsOngoingAndCommitted(
335 NavigationHandle
* navigation_handle
) {
336 auto it
= ongoing_committed_navigations_
.find(navigation_handle
);
337 return it
!= ongoing_committed_navigations_
.end();
340 } // namespace content