Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / shell / renderer / leak_detector.cc
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.
4
5 #include "content/shell/renderer/leak_detector.h"
6
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"
12
13 using blink::WebLeakDetector;
14
15 namespace content {
16
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.
19 //
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;
26
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;
32 }
33
34 LeakDetector::~LeakDetector() {
35 }
36
37 void LeakDetector::TryLeakDetection(blink::WebLocalFrame* frame) {
38   web_leak_detector_->collectGarbageAndGetDOMCounts(frame);
39 }
40
41 void LeakDetector::onLeakDetectionComplete(
42     const WebLeakDetectorClient::Result& result) {
43   LeakDetectionResult report;
44   report.leaked =
45       (previous_result_.numberOfLiveDocuments < result.numberOfLiveDocuments ||
46        previous_result_.numberOfLiveNodes < result.numberOfLiveNodes);
47
48   if (report.leaked) {
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);
54
55     list = new base::ListValue();
56     list->AppendInteger(previous_result_.numberOfLiveNodes);
57     list->AppendInteger(result.numberOfLiveNodes);
58     detail.Set("numberOfLiveNodes", list);
59
60     std::string detail_str;
61     base::JSONWriter::Write(&detail, &detail_str);
62     report.detail = detail_str;
63   }
64
65   previous_result_ = result;
66   test_runner_->ReportLeakDetectionResult(report);
67 }
68
69 }  // namespace content