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/shell/renderer/leak_detector.h"
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
9 #include "base/values.h"
10 #include "content/shell/renderer/webkit_test_runner.h"
11 #include "third_party/WebKit/public/web/WebLeakDetector.h"
13 using blink::WebLeakDetector;
17 // The initial states of the DOM objects at about:blank. The four nodes are a
18 // Document, a HTML, a HEAD and a BODY.
20 // TODO(hajimehoshi): Now these are hard-corded. If we add target to count like
21 // RefCoutned objects whose initial state is diffcult to estimate, we stop using
22 // hard-coded values. Instead, we need to load about:blank ahead of the layout
23 // tests actually and initialize LeakDetector by the got values.
24 const int kInitialNumberOfLiveDocuments = 1;
25 const int kInitialNumberOfLiveNodes = 4;
27 LeakDetector::LeakDetector(WebKitTestRunner* test_runner)
28 : test_runner_(test_runner),
29 web_leak_detector_(blink::WebLeakDetector::create(this)) {
30 previous_result_.numberOfLiveDocuments = kInitialNumberOfLiveDocuments;
31 previous_result_.numberOfLiveNodes = kInitialNumberOfLiveNodes;
34 LeakDetector::~LeakDetector() {
37 void LeakDetector::TryLeakDetection(blink::WebLocalFrame* frame) {
38 web_leak_detector_->collectGarbageAndGetDOMCounts(frame);
41 void LeakDetector::onLeakDetectionComplete(
42 const WebLeakDetectorClient::Result& result) {
43 LeakDetectionResult report;
45 (previous_result_.numberOfLiveDocuments < result.numberOfLiveDocuments ||
46 previous_result_.numberOfLiveNodes < result.numberOfLiveNodes);
49 base::DictionaryValue detail;
50 base::ListValue* list = new base::ListValue();
51 list->AppendInteger(previous_result_.numberOfLiveDocuments);
52 list->AppendInteger(result.numberOfLiveDocuments);
53 detail.Set("numberOfLiveDocuments", list);
55 list = new base::ListValue();
56 list->AppendInteger(previous_result_.numberOfLiveNodes);
57 list->AppendInteger(result.numberOfLiveNodes);
58 detail.Set("numberOfLiveNodes", list);
60 std::string detail_str;
61 base::JSONWriter::Write(&detail, &detail_str);
62 report.detail = detail_str;
65 previous_result_ = result;
66 test_runner_->ReportLeakDetectionResult(report);
69 } // namespace content