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"
7 #include "base/json/json_writer.h"
8 #include "base/metrics/histogram.h"
9 #include "base/metrics/statistics_recorder.h"
10 #include "base/strings/string_util.h"
11 #include "content/common/child_process_messages.h"
12 #include "content/renderer/render_view_impl.h"
13 #include "gin/handle.h"
14 #include "gin/object_template_builder.h"
15 #include "third_party/WebKit/public/web/WebKit.h"
16 #include "third_party/WebKit/public/web/WebLocalFrame.h"
17 #include "third_party/WebKit/public/web/WebView.h"
23 bool CurrentRenderViewImpl(RenderViewImpl
** out
) {
24 blink::WebLocalFrame
* web_frame
=
25 blink::WebLocalFrame::frameForCurrentContext();
29 blink::WebView
* web_view
= web_frame
->view();
33 RenderViewImpl
* render_view_impl
=
34 RenderViewImpl::FromWebView(web_view
);
35 if (!render_view_impl
)
38 *out
= render_view_impl
;
42 // Encodes a WebContentsLoadTime as JSON.
44 // - |load_start_time| - time at which page load started.
45 // - |load_stop_time| - time at which page load stopped.
46 // - |result| - returned JSON.
47 // Example return value:
48 // {'load_start_ms': 1, 'load_duration_ms': 2.5}
49 // either value may be null if a web contents hasn't fully loaded.
50 // load_start_ms is represented as milliseconds since the unix epoch.
51 void ConvertLoadTimeToJSON(
52 const base::Time
& load_start_time
,
53 const base::Time
& load_stop_time
,
54 std::string
*result
) {
55 base::DictionaryValue item
;
57 if (load_start_time
.is_null()) {
58 item
.Set("load_start_ms", base::Value::CreateNullValue());
60 item
.SetDouble("load_start_ms", (load_start_time
- base::Time::UnixEpoch())
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
);
75 gin::WrapperInfo
StatsCollectionController::kWrapperInfo
= {
76 gin::kEmbedderNativeGin
80 void StatsCollectionController::Install(blink::WebFrame
* frame
) {
81 v8::Isolate
* isolate
= blink::mainThreadIsolate();
82 v8::HandleScope
handle_scope(isolate
);
83 v8::Handle
<v8::Context
> context
= frame
->mainWorldScriptContext();
84 if (context
.IsEmpty())
87 v8::Context::Scope
context_scope(context
);
89 gin::Handle
<StatsCollectionController
> controller
=
90 gin::CreateHandle(isolate
, new StatsCollectionController());
91 if (controller
.IsEmpty())
93 v8::Handle
<v8::Object
> global
= context
->Global();
94 global
->Set(gin::StringToV8(isolate
, "statsCollectionController"),
98 StatsCollectionController::StatsCollectionController() {}
100 StatsCollectionController::~StatsCollectionController() {}
102 gin::ObjectTemplateBuilder
StatsCollectionController::GetObjectTemplateBuilder(
103 v8::Isolate
* isolate
) {
104 return gin::Wrappable
<StatsCollectionController
>::GetObjectTemplateBuilder(
106 .SetMethod("getHistogram", &StatsCollectionController::GetHistogram
)
107 .SetMethod("getBrowserHistogram",
108 &StatsCollectionController::GetBrowserHistogram
)
109 .SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming
);
112 std::string
StatsCollectionController::GetHistogram(
113 const std::string
& histogram_name
) {
114 base::HistogramBase
* histogram
=
115 base::StatisticsRecorder::FindHistogram(histogram_name
);
120 histogram
->WriteJSON(&output
);
125 std::string
StatsCollectionController::GetBrowserHistogram(
126 const std::string
& histogram_name
) {
127 RenderViewImpl
*render_view_impl
= NULL
;
128 if (!CurrentRenderViewImpl(&render_view_impl
)) {
130 return std::string();
133 std::string histogram_json
;
134 render_view_impl
->Send(new ChildProcessHostMsg_GetBrowserHistogram(
135 histogram_name
, &histogram_json
));
136 return histogram_json
;
139 std::string
StatsCollectionController::GetTabLoadTiming() {
140 RenderViewImpl
*render_view_impl
= NULL
;
141 if (!CurrentRenderViewImpl(&render_view_impl
)) {
143 return std::string();
146 StatsCollectionObserver
* observer
=
147 render_view_impl
->GetStatsCollectionObserver();
150 return std::string();
153 std::string tab_timing_json
;
154 ConvertLoadTimeToJSON(
155 observer
->load_start_time(), observer
->load_stop_time(),
157 return tab_timing_json
;
160 } // namespace content