Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / test / web_contents_observer_sanity_checker.cc
blob1e76ca23473e89e8ade73086af84d3f07c548a01
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/render_frame_host.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "content/public/browser/site_instance.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_observer.h"
16 namespace content {
18 namespace {
20 const char kWebContentsObserverSanityCheckerKey[] =
21 "WebContentsObserverSanityChecker";
23 } // namespace
25 // static
26 void WebContentsObserverSanityChecker::Enable(WebContents* web_contents) {
27 if (web_contents->GetUserData(&kWebContentsObserverSanityCheckerKey))
28 return;
29 web_contents->SetUserData(&kWebContentsObserverSanityCheckerKey,
30 new WebContentsObserverSanityChecker(web_contents));
33 void WebContentsObserverSanityChecker::RenderFrameCreated(
34 RenderFrameHost* render_frame_host) {
35 CHECK(!web_contents_destroyed_);
36 std::pair<int, int> routing_pair =
37 std::make_pair(render_frame_host->GetProcess()->GetID(),
38 render_frame_host->GetRoutingID());
39 bool frame_exists = !live_routes_.insert(routing_pair).second;
40 deleted_routes_.erase(routing_pair);
42 if (frame_exists) {
43 CHECK(false) << "RenderFrameCreated called more than once for routing pair:"
44 << Format(render_frame_host);
47 CHECK(render_frame_host->GetProcess()->HasConnection())
48 << "RenderFrameCreated was called for a RenderFrameHost whose render "
49 "process is not currently live, so there's no way for the RenderFrame "
50 "to have been created.";
51 CHECK(
52 static_cast<RenderFrameHostImpl*>(render_frame_host)->IsRenderFrameLive())
53 << "RenderFrameCreated called on for a RenderFrameHost that thinks it is "
54 "not alive.";
57 void WebContentsObserverSanityChecker::RenderFrameDeleted(
58 RenderFrameHost* render_frame_host) {
59 CHECK(!web_contents_destroyed_);
60 std::pair<int, int> routing_pair =
61 std::make_pair(render_frame_host->GetProcess()->GetID(),
62 render_frame_host->GetRoutingID());
63 bool was_live = !!live_routes_.erase(routing_pair);
64 bool was_dead_already = !deleted_routes_.insert(routing_pair).second;
66 if (was_dead_already) {
67 CHECK(false) << "RenderFrameDeleted called more than once for routing pair "
68 << Format(render_frame_host);
69 } else if (!was_live) {
70 // TODO(nick): Clients can easily ignore an unrecognized object, but it
71 // would be useful from a finding-bugs perspective if we could enable this
72 // check.
73 #if 0
74 CHECK(false) << "RenderFrameDeleted called for routing pair "
75 << Format(render_frame_host)
76 << " for which RenderFrameCreated was never called";
77 #endif
81 void WebContentsObserverSanityChecker::RenderFrameForInterstitialPageCreated(
82 RenderFrameHost* render_frame_host) {
83 // TODO(nick): Record this.
86 void WebContentsObserverSanityChecker::RenderFrameHostChanged(
87 RenderFrameHost* old_host,
88 RenderFrameHost* new_host) {
89 CHECK(new_host);
90 CHECK_NE(new_host, old_host);
92 if (old_host) {
93 std::pair<int, int> routing_pair =
94 std::make_pair(old_host->GetProcess()->GetID(),
95 old_host->GetRoutingID());
96 bool old_did_exist = !!current_hosts_.erase(routing_pair);
97 if (!old_did_exist) {
98 CHECK(false)
99 << "RenderFrameHostChanged called with old host that did not exist:"
100 << Format(old_host);
104 std::pair<int, int> routing_pair =
105 std::make_pair(new_host->GetProcess()->GetID(),
106 new_host->GetRoutingID());
107 bool host_exists = !current_hosts_.insert(routing_pair).second;
108 if (host_exists) {
109 CHECK(false)
110 << "RenderFrameHostChanged called more than once for routing pair:"
111 << Format(new_host);
115 void WebContentsObserverSanityChecker::FrameDeleted(
116 RenderFrameHost* render_frame_host) {
117 // A frame can be deleted before RenderFrame in the renderer process is
118 // created, so there is not much that can be enforced here.
119 CHECK(!web_contents_destroyed_);
122 void WebContentsObserverSanityChecker::DidStartProvisionalLoadForFrame(
123 RenderFrameHost* render_frame_host,
124 const GURL& validated_url,
125 bool is_error_page,
126 bool is_iframe_srcdoc) {
127 AssertRenderFrameExists(render_frame_host);
130 void WebContentsObserverSanityChecker::DidCommitProvisionalLoadForFrame(
131 RenderFrameHost* render_frame_host,
132 const GURL& url,
133 ui::PageTransition transition_type) {
134 AssertRenderFrameExists(render_frame_host);
137 void WebContentsObserverSanityChecker::DidFailProvisionalLoad(
138 RenderFrameHost* render_frame_host,
139 const GURL& validated_url,
140 int error_code,
141 const base::string16& error_description,
142 bool was_ignored_by_handler) {
143 AssertRenderFrameExists(render_frame_host);
146 void WebContentsObserverSanityChecker::DidNavigateMainFrame(
147 const LoadCommittedDetails& details,
148 const FrameNavigateParams& params) {
149 AssertMainFrameExists();
152 void WebContentsObserverSanityChecker::DidNavigateAnyFrame(
153 RenderFrameHost* render_frame_host,
154 const LoadCommittedDetails& details,
155 const FrameNavigateParams& params) {
156 AssertRenderFrameExists(render_frame_host);
159 void WebContentsObserverSanityChecker::DocumentAvailableInMainFrame() {
160 AssertMainFrameExists();
163 void WebContentsObserverSanityChecker::DocumentOnLoadCompletedInMainFrame() {
164 AssertMainFrameExists();
167 void WebContentsObserverSanityChecker::DocumentLoadedInFrame(
168 RenderFrameHost* render_frame_host) {
169 AssertRenderFrameExists(render_frame_host);
172 void WebContentsObserverSanityChecker::DidFinishLoad(
173 RenderFrameHost* render_frame_host,
174 const GURL& validated_url) {
175 AssertRenderFrameExists(render_frame_host);
178 void WebContentsObserverSanityChecker::DidFailLoad(
179 RenderFrameHost* render_frame_host,
180 const GURL& validated_url,
181 int error_code,
182 const base::string16& error_description,
183 bool was_ignored_by_handler) {
184 AssertRenderFrameExists(render_frame_host);
187 void WebContentsObserverSanityChecker::DidGetRedirectForResourceRequest(
188 RenderFrameHost* render_frame_host,
189 const ResourceRedirectDetails& details) {
190 AssertRenderFrameExists(render_frame_host);
193 void WebContentsObserverSanityChecker::DidOpenRequestedURL(
194 WebContents* new_contents,
195 RenderFrameHost* source_render_frame_host,
196 const GURL& url,
197 const Referrer& referrer,
198 WindowOpenDisposition disposition,
199 ui::PageTransition transition) {
200 AssertRenderFrameExists(source_render_frame_host);
203 bool WebContentsObserverSanityChecker::OnMessageReceived(
204 const IPC::Message& message,
205 RenderFrameHost* render_frame_host) {
206 // TODO(nasko): FrameHostMsg_RenderProcessGone is delivered to
207 // WebContentsObserver since RenderFrameHost allows the delegate to handle
208 // the message first. This shouldn't happen, but for now handle it here.
209 // https://crbug.com/450799
210 if (message.type() == FrameHostMsg_RenderProcessGone::ID)
211 return false;
213 #if !defined(OS_MACOSX)
214 // TODO(avi): Disabled because of http://crbug.com/445054
215 AssertRenderFrameExists(render_frame_host);
216 #endif
217 return false;
220 void WebContentsObserverSanityChecker::WebContentsDestroyed() {
221 CHECK(!web_contents_destroyed_);
222 web_contents_destroyed_ = true;
225 WebContentsObserverSanityChecker::WebContentsObserverSanityChecker(
226 WebContents* web_contents)
227 : WebContentsObserver(web_contents), web_contents_destroyed_(false) {
228 // Prime the pump with the initial objects.
229 // TODO(nasko): Investigate why this is needed.
230 RenderViewCreated(web_contents->GetRenderViewHost());
233 WebContentsObserverSanityChecker::~WebContentsObserverSanityChecker() {
234 CHECK(web_contents_destroyed_);
237 void WebContentsObserverSanityChecker::AssertRenderFrameExists(
238 RenderFrameHost* render_frame_host) {
239 CHECK(!web_contents_destroyed_);
240 std::pair<int, int> routing_pair =
241 std::make_pair(render_frame_host->GetProcess()->GetID(),
242 render_frame_host->GetRoutingID());
244 bool render_frame_created_happened = live_routes_.count(routing_pair) != 0;
245 bool render_frame_deleted_happened = deleted_routes_.count(routing_pair) != 0;
247 CHECK(render_frame_created_happened)
248 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
249 << "method, but WebContentsObserver::RenderFrameCreated was never called "
250 << "for that RenderFrameHost: " << Format(render_frame_host);
251 CHECK(!render_frame_deleted_happened)
252 << "A RenderFrameHost pointer was passed to a WebContentsObserver "
253 << "method, but WebContentsObserver::RenderFrameDeleted had already been "
254 << "called on that frame:" << Format(render_frame_host);
257 void WebContentsObserverSanityChecker::AssertMainFrameExists() {
258 AssertRenderFrameExists(web_contents()->GetMainFrame());
261 std::string WebContentsObserverSanityChecker::Format(
262 RenderFrameHost* render_frame_host) {
263 return base::StringPrintf(
264 "(%d, %d -> %s)", render_frame_host->GetProcess()->GetID(),
265 render_frame_host->GetRoutingID(),
266 render_frame_host->GetSiteInstance()->GetSiteURL().spec().c_str());
269 } // namespace content