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