OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/test/web_contents_observer_sanity_checker.h" | 5 #include "content/test/web_contents_observer_sanity_checker.h" |
6 | 6 |
7 #include "base/debug/stack_trace.h" | |
7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
8 #include "content/common/frame_messages.h" | 9 #include "content/common/frame_messages.h" |
9 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
10 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
11 #include "content/public/browser/site_instance.h" | 12 #include "content/public/browser/site_instance.h" |
12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
13 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
14 | 15 |
15 namespace content { | 16 namespace content { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char kWebContentsObserverSanityCheckerKey[] = | 20 const char kWebContentsObserverSanityCheckerKey[] = |
20 "WebContentsObserverSanityChecker"; | 21 "WebContentsObserverSanityChecker"; |
21 | 22 |
23 // Set this variable to true to collect stack traces on each RenderFrameCreated | |
24 // and RenderFrameDeleted call, so duplicate calls can be easily debugged. | |
25 static bool g_collect_stack_traces = true; | |
ncarter (slow)
2015/05/26 21:44:14
If you've got to edit the code to turn this off, i
nasko
2015/05/26 22:25:49
As we chatted, I'll land this in a separate CL and
| |
26 | |
22 } // namespace | 27 } // namespace |
23 | 28 |
24 // static | 29 // static |
25 void WebContentsObserverSanityChecker::Enable(WebContents* web_contents) { | 30 void WebContentsObserverSanityChecker::Enable(WebContents* web_contents) { |
26 if (web_contents->GetUserData(&kWebContentsObserverSanityCheckerKey)) | 31 if (web_contents->GetUserData(&kWebContentsObserverSanityCheckerKey)) |
27 return; | 32 return; |
28 web_contents->SetUserData(&kWebContentsObserverSanityCheckerKey, | 33 web_contents->SetUserData(&kWebContentsObserverSanityCheckerKey, |
29 new WebContentsObserverSanityChecker(web_contents)); | 34 new WebContentsObserverSanityChecker(web_contents)); |
30 } | 35 } |
31 | 36 |
32 void WebContentsObserverSanityChecker::RenderFrameCreated( | 37 void WebContentsObserverSanityChecker::RenderFrameCreated( |
33 RenderFrameHost* render_frame_host) { | 38 RenderFrameHost* render_frame_host) { |
34 CHECK(!web_contents_destroyed_); | 39 CHECK(!web_contents_destroyed_); |
35 std::pair<int, int> routing_pair = | 40 std::pair<int, int> routing_pair = |
36 std::make_pair(render_frame_host->GetProcess()->GetID(), | 41 std::make_pair(render_frame_host->GetProcess()->GetID(), |
37 render_frame_host->GetRoutingID()); | 42 render_frame_host->GetRoutingID()); |
38 bool frame_exists = !live_routes_.insert(routing_pair).second; | 43 bool frame_exists = !live_routes_.insert(routing_pair).second; |
39 deleted_routes_.erase(routing_pair); | 44 deleted_routes_.erase(routing_pair); |
40 | 45 |
41 if (frame_exists) { | 46 if (frame_exists) { |
47 std::string trace; | |
48 if (g_collect_stack_traces) { | |
49 trace = "\nPrevious creation stack trace:\n" + | |
50 render_frame_created_stacks_[routing_pair]; | |
51 } | |
42 CHECK(false) << "RenderFrameCreated called more than once for routing pair:" | 52 CHECK(false) << "RenderFrameCreated called more than once for routing pair:" |
43 << Format(render_frame_host); | 53 << Format(render_frame_host) << trace; |
ncarter (slow)
2015/05/26 21:44:14
This is pretty sweet!
nasko
2015/05/26 22:25:49
It indeed is very helpful in tracking down issues.
| |
54 } | |
55 | |
56 if (g_collect_stack_traces) { | |
57 base::debug::StackTrace stack; | |
58 render_frame_created_stacks_.insert( | |
59 std::make_pair(routing_pair, stack.ToString())); | |
60 render_frame_deleted_stacks_.erase(routing_pair); | |
44 } | 61 } |
45 } | 62 } |
46 | 63 |
47 void WebContentsObserverSanityChecker::RenderFrameDeleted( | 64 void WebContentsObserverSanityChecker::RenderFrameDeleted( |
48 RenderFrameHost* render_frame_host) { | 65 RenderFrameHost* render_frame_host) { |
49 CHECK(!web_contents_destroyed_); | 66 CHECK(!web_contents_destroyed_); |
50 std::pair<int, int> routing_pair = | 67 std::pair<int, int> routing_pair = |
51 std::make_pair(render_frame_host->GetProcess()->GetID(), | 68 std::make_pair(render_frame_host->GetProcess()->GetID(), |
52 render_frame_host->GetRoutingID()); | 69 render_frame_host->GetRoutingID()); |
53 bool was_live = !!live_routes_.erase(routing_pair); | 70 bool was_live = !!live_routes_.erase(routing_pair); |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 | 265 |
249 std::string WebContentsObserverSanityChecker::Format( | 266 std::string WebContentsObserverSanityChecker::Format( |
250 RenderFrameHost* render_frame_host) { | 267 RenderFrameHost* render_frame_host) { |
251 return base::StringPrintf( | 268 return base::StringPrintf( |
252 "(%d, %d -> %s)", render_frame_host->GetProcess()->GetID(), | 269 "(%d, %d -> %s)", render_frame_host->GetProcess()->GetID(), |
253 render_frame_host->GetRoutingID(), | 270 render_frame_host->GetRoutingID(), |
254 render_frame_host->GetSiteInstance()->GetSiteURL().spec().c_str()); | 271 render_frame_host->GetSiteInstance()->GetSiteURL().spec().c_str()); |
255 } | 272 } |
256 | 273 |
257 } // namespace content | 274 } // namespace content |
OLD | NEW |