Upstream version 5.34.104.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 "third_party/WebKit/public/web/WebLeakDetector.h"
11
12 using blink::WebLeakDetector;
13
14 namespace content {
15
16 // The initial states of the DOM objects at about:blank. The four nodes are a
17 // Document, a HTML, a HEAD and a BODY.
18 //
19 // TODO(hajimehoshi): Now these are hard-corded. If we add target to count like
20 // RefCoutned objects whose initial state is diffcult to estimate, we stop using
21 // hard-coded values. Instead, we need to load about:blank ahead of the layout
22 // tests actually and initialize LeakDetector by the got values.
23 const int kInitialNumberOfLiveDocuments = 1;
24 const int kInitialNumberOfLiveNodes = 4;
25
26 LeakDetector::LeakDetector()
27     : previous_number_of_live_documents_(kInitialNumberOfLiveDocuments),
28       previous_number_of_live_nodes_(kInitialNumberOfLiveNodes) {
29 }
30
31 LeakDetectionResult LeakDetector::TryLeakDetection(blink::WebFrame* frame) {
32   LeakDetectionResult result;
33   unsigned number_of_live_documents = 0;
34   unsigned number_of_live_nodes = 0;
35
36   WebLeakDetector::collectGarbargeAndGetDOMCounts(
37       frame, &number_of_live_documents, &number_of_live_nodes);
38
39   result.leaked =
40       (previous_number_of_live_documents_ < number_of_live_documents ||
41        previous_number_of_live_nodes_ < number_of_live_nodes);
42
43   if (result.leaked) {
44     base::DictionaryValue detail;
45     base::ListValue* list = new base::ListValue();
46     list->AppendInteger(previous_number_of_live_documents_);
47     list->AppendInteger(number_of_live_documents);
48     detail.Set("numberOfLiveDocuments", list);
49
50     list = new base::ListValue();
51     list->AppendInteger(previous_number_of_live_nodes_);
52     list->AppendInteger(number_of_live_nodes);
53     detail.Set("numberOfLiveNodes", list);
54
55     std::string detail_str;
56     base::JSONWriter::Write(&detail, &detail_str);
57     result.detail = detail_str;
58   }
59
60   previous_number_of_live_documents_ = number_of_live_documents;
61   previous_number_of_live_nodes_ = number_of_live_nodes;
62
63   return result;
64 }
65
66 }  // namespace content