blob: af86c9ea0ef1db4adfce35a7bca02ca8251cf899 [file] [log] [blame]
[email protected]27c521a2013-05-29 20:44:321// 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.
4
5#include "content/renderer/stats_collection_controller.h"
6
[email protected]27c521a2013-05-29 20:44:327#include "base/json/json_writer.h"
8#include "base/metrics/histogram.h"
9#include "base/metrics/statistics_recorder.h"
[email protected]21aa99682013-06-11 07:17:0110#include "base/strings/string_util.h"
[email protected]27c521a2013-05-29 20:44:3211#include "content/common/child_process_messages.h"
12#include "content/renderer/render_view_impl.h"
[email protected]a79712f2013-12-13 21:33:2513#include "gin/handle.h"
14#include "gin/object_template_builder.h"
15#include "gin/per_isolate_data.h"
[email protected]2255a9332013-06-17 05:12:3116#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]a79712f2013-12-13 21:33:2517#include "third_party/WebKit/public/web/WebKit.h"
[email protected]2255a9332013-06-17 05:12:3118#include "third_party/WebKit/public/web/WebView.h"
[email protected]27c521a2013-05-29 20:44:3219
[email protected]27c521a2013-05-29 20:44:3220namespace content {
21
22namespace {
23
24bool CurrentRenderViewImpl(RenderViewImpl** out) {
[email protected]180ef242013-11-07 06:50:4625 blink::WebFrame* web_frame = blink::WebFrame::frameForCurrentContext();
[email protected]27c521a2013-05-29 20:44:3226 if (!web_frame)
27 return false;
28
[email protected]180ef242013-11-07 06:50:4629 blink::WebView* web_view = web_frame->view();
[email protected]27c521a2013-05-29 20:44:3230 if (!web_view)
31 return false;
32
33 RenderViewImpl* render_view_impl =
34 RenderViewImpl::FromWebView(web_view);
35 if (!render_view_impl)
36 return false;
37
38 *out = render_view_impl;
39 return true;
40}
41
42// Encodes a WebContentsLoadTime as JSON.
43// Input:
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 system boot.
51void ConvertLoadTimeToJSON(
[email protected]538dde872013-11-19 16:44:1752 const base::Time& load_start_time,
53 const base::Time& load_stop_time,
[email protected]27c521a2013-05-29 20:44:3254 std::string *result) {
[email protected]716775252013-06-14 17:58:0055 base::DictionaryValue item;
[email protected]27c521a2013-05-29 20:44:3256
57 if (load_start_time.is_null()) {
[email protected]538dde872013-11-19 16:44:1758 item.Set("load_start_ms", base::Value::CreateNullValue());
[email protected]27c521a2013-05-29 20:44:3259 } else {
[email protected]27c521a2013-05-29 20:44:3260 item.SetDouble("load_start_ms", load_start_time.ToInternalValue() / 1000);
61 }
[email protected]c81e3fd2013-08-05 16:30:2262 if (load_start_time.is_null() || load_stop_time.is_null()) {
[email protected]716775252013-06-14 17:58:0063 item.Set("load_duration_ms", base::Value::CreateNullValue());
[email protected]27c521a2013-05-29 20:44:3264 } else {
65 item.SetDouble("load_duration_ms",
[email protected]538dde872013-11-19 16:44:1766 (load_stop_time - load_start_time).InMillisecondsF());
[email protected]27c521a2013-05-29 20:44:3267 }
68 base::JSONWriter::Write(&item, result);
69}
70
71} // namespace
72
[email protected]a79712f2013-12-13 21:33:2573// static
74gin::WrapperInfo StatsCollectionController::kWrapperInfo = {
75 gin::kEmbedderNativeGin
76};
77
78// static
79void StatsCollectionController::Install(blink::WebFrame* frame) {
80 v8::Isolate* isolate = blink::mainThreadIsolate();
81 v8::HandleScope handle_scope(isolate);
82 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
83 if (context.IsEmpty())
84 return;
85
86 v8::Context::Scope context_scope(context);
87
88 gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
89 if (data->GetObjectTemplate(&StatsCollectionController::kWrapperInfo)
90 .IsEmpty()) {
91 v8::Handle<v8::ObjectTemplate> templ =
92 gin::ObjectTemplateBuilder(isolate)
93 .SetMethod("getHistogram", &StatsCollectionController::GetHistogram)
94 .SetMethod("getBrowserHistogram",
95 &StatsCollectionController::GetBrowserHistogram)
96 .SetMethod("tabLoadTiming",
97 &StatsCollectionController::GetTabLoadTiming)
98 .Build();
99 templ->SetInternalFieldCount(gin::kNumberOfInternalFields);
100 data->SetObjectTemplate(&StatsCollectionController::kWrapperInfo, templ);
101 }
102
103 gin::Handle<StatsCollectionController> controller =
104 gin::CreateHandle(isolate, new StatsCollectionController());
105 v8::Handle<v8::Object> global = context->Global();
106 global->Set(gin::StringToV8(isolate, "statsCollectionController"),
107 controller.ToV8());
[email protected]27c521a2013-05-29 20:44:32108}
109
[email protected]a79712f2013-12-13 21:33:25110StatsCollectionController::StatsCollectionController() {}
111
112StatsCollectionController::~StatsCollectionController() {}
113
114std::string StatsCollectionController::GetHistogram(
115 const std::string& histogram_name) {
[email protected]27c521a2013-05-29 20:44:32116 base::HistogramBase* histogram =
[email protected]a79712f2013-12-13 21:33:25117 base::StatisticsRecorder::FindHistogram(histogram_name);
[email protected]27c521a2013-05-29 20:44:32118 std::string output;
119 if (!histogram) {
120 output = "{}";
121 } else {
122 histogram->WriteJSON(&output);
123 }
[email protected]a79712f2013-12-13 21:33:25124 return output;
[email protected]27c521a2013-05-29 20:44:32125}
126
[email protected]a79712f2013-12-13 21:33:25127std::string StatsCollectionController::GetBrowserHistogram(
128 const std::string& histogram_name) {
[email protected]27c521a2013-05-29 20:44:32129 RenderViewImpl *render_view_impl = NULL;
130 if (!CurrentRenderViewImpl(&render_view_impl)) {
131 NOTREACHED();
[email protected]a79712f2013-12-13 21:33:25132 return std::string();
133 }
134
135 std::string histogram_json;
136 render_view_impl->Send(new ChildProcessHostMsg_GetBrowserHistogram(
137 histogram_name, &histogram_json));
138 return histogram_json;
139}
140
141std::string StatsCollectionController::GetTabLoadTiming() {
142 RenderViewImpl *render_view_impl = NULL;
143 if (!CurrentRenderViewImpl(&render_view_impl)) {
144 NOTREACHED();
145 return std::string();
[email protected]27c521a2013-05-29 20:44:32146 }
147
148 StatsCollectionObserver* observer =
149 render_view_impl->GetStatsCollectionObserver();
150 if (!observer) {
151 NOTREACHED();
[email protected]a79712f2013-12-13 21:33:25152 return std::string();
[email protected]27c521a2013-05-29 20:44:32153 }
154
155 std::string tab_timing_json;
156 ConvertLoadTimeToJSON(
157 observer->load_start_time(), observer->load_stop_time(),
158 &tab_timing_json);
[email protected]a79712f2013-12-13 21:33:25159 return tab_timing_json;
[email protected]27c521a2013-05-29 20:44:32160}
161
162} // namespace content