1 // Copyright 2013 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/renderer/stats_collection_controller.h"
8 #include "base/bind_helpers.h"
9 #include "base/json/json_writer.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/statistics_recorder.h"
12 #include "base/strings/string_util.h"
13 #include "content/common/child_process_messages.h"
14 #include "content/renderer/render_view_impl.h"
15 #include "third_party/WebKit/public/web/WebFrame.h"
16 #include "third_party/WebKit/public/web/WebView.h"
18 using webkit_glue::CppArgumentList
;
19 using webkit_glue::CppVariant
;
25 bool CurrentRenderViewImpl(RenderViewImpl
** out
) {
26 blink::WebFrame
* web_frame
= blink::WebFrame::frameForCurrentContext();
30 blink::WebView
* web_view
= web_frame
->view();
34 RenderViewImpl
* render_view_impl
=
35 RenderViewImpl::FromWebView(web_view
);
36 if (!render_view_impl
)
39 *out
= render_view_impl
;
43 // Encodes a WebContentsLoadTime as JSON.
45 // - |load_start_time| - time at which page load started.
46 // - |load_stop_time| - time at which page load stopped.
47 // - |result| - returned JSON.
48 // Example return value:
49 // {'load_start_ms': 1, 'load_duration_ms': 2.5}
50 // either value may be null if a web contents hasn't fully loaded.
51 // load_start_ms is represented as milliseconds since system boot.
52 void ConvertLoadTimeToJSON(
53 const base::Time
& load_start_time
,
54 const base::Time
& load_stop_time
,
55 std::string
*result
) {
56 base::DictionaryValue item
;
58 if (load_start_time
.is_null()) {
59 item
.Set("load_start_ms", base::Value::CreateNullValue());
61 item
.SetDouble("load_start_ms", load_start_time
.ToInternalValue() / 1000);
63 if (load_start_time
.is_null() || load_stop_time
.is_null()) {
64 item
.Set("load_duration_ms", base::Value::CreateNullValue());
66 item
.SetDouble("load_duration_ms",
67 (load_stop_time
- load_start_time
).InMillisecondsF());
69 base::JSONWriter::Write(&item
, result
);
74 StatsCollectionController::StatsCollectionController()
76 BindCallback("getHistogram",
77 base::Bind(&StatsCollectionController::GetHistogram
,
78 base::Unretained(this)));
79 BindCallback("getBrowserHistogram",
80 base::Bind(&StatsCollectionController::GetBrowserHistogram
,
81 base::Unretained(this)));
82 BindCallback("tabLoadTiming",
84 &StatsCollectionController::GetTabLoadTiming
,
85 base::Unretained(this)));
88 void StatsCollectionController::GetHistogram(const CppArgumentList
& args
,
90 if (args
.size() != 1) {
94 base::HistogramBase
* histogram
=
95 base::StatisticsRecorder::FindHistogram(args
[0].ToString());
100 histogram
->WriteJSON(&output
);
105 void StatsCollectionController::GetBrowserHistogram(const CppArgumentList
& args
,
106 CppVariant
* result
) {
107 if (args
.size() != 1) {
118 std::string histogram_json
;
119 sender_
->Send(new ChildProcessHostMsg_GetBrowserHistogram(
120 args
[0].ToString(), &histogram_json
));
121 result
->Set(histogram_json
);
124 void StatsCollectionController::GetTabLoadTiming(
125 const CppArgumentList
& args
,
126 CppVariant
* result
) {
133 RenderViewImpl
*render_view_impl
= NULL
;
134 if (!CurrentRenderViewImpl(&render_view_impl
)) {
140 StatsCollectionObserver
* observer
=
141 render_view_impl
->GetStatsCollectionObserver();
148 std::string tab_timing_json
;
149 ConvertLoadTimeToJSON(
150 observer
->load_start_time(), observer
->load_stop_time(),
152 result
->Set(tab_timing_json
);
155 } // namespace content