[telemetry] Allow users to authenticate to Cloud Storage using prodaccess.
[chromium-blink-merge.git] / content / renderer / stats_collection_controller.cc
blobcbbdc403698fa00551cd1950729636ef52681e81
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/WebFrame.h"
16 #include "third_party/WebKit/public/web/WebKit.h"
17 #include "third_party/WebKit/public/web/WebView.h"
19 namespace content {
21 namespace {
23 bool CurrentRenderViewImpl(RenderViewImpl** out) {
24 blink::WebFrame* web_frame = blink::WebFrame::frameForCurrentContext();
25 if (!web_frame)
26 return false;
28 blink::WebView* web_view = web_frame->view();
29 if (!web_view)
30 return false;
32 RenderViewImpl* render_view_impl =
33 RenderViewImpl::FromWebView(web_view);
34 if (!render_view_impl)
35 return false;
37 *out = render_view_impl;
38 return true;
41 // Encodes a WebContentsLoadTime as JSON.
42 // Input:
43 // - |load_start_time| - time at which page load started.
44 // - |load_stop_time| - time at which page load stopped.
45 // - |result| - returned JSON.
46 // Example return value:
47 // {'load_start_ms': 1, 'load_duration_ms': 2.5}
48 // either value may be null if a web contents hasn't fully loaded.
49 // load_start_ms is represented as milliseconds since system boot.
50 void ConvertLoadTimeToJSON(
51 const base::Time& load_start_time,
52 const base::Time& load_stop_time,
53 std::string *result) {
54 base::DictionaryValue item;
56 if (load_start_time.is_null()) {
57 item.Set("load_start_ms", base::Value::CreateNullValue());
58 } else {
59 item.SetDouble("load_start_ms", load_start_time.ToInternalValue() / 1000);
61 if (load_start_time.is_null() || load_stop_time.is_null()) {
62 item.Set("load_duration_ms", base::Value::CreateNullValue());
63 } else {
64 item.SetDouble("load_duration_ms",
65 (load_stop_time - load_start_time).InMillisecondsF());
67 base::JSONWriter::Write(&item, result);
70 } // namespace
72 // static
73 gin::WrapperInfo StatsCollectionController::kWrapperInfo = {
74 gin::kEmbedderNativeGin
77 // static
78 void StatsCollectionController::Install(blink::WebFrame* frame) {
79 v8::Isolate* isolate = blink::mainThreadIsolate();
80 v8::HandleScope handle_scope(isolate);
81 v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
82 if (context.IsEmpty())
83 return;
85 v8::Context::Scope context_scope(context);
87 gin::Handle<StatsCollectionController> controller =
88 gin::CreateHandle(isolate, new StatsCollectionController());
89 v8::Handle<v8::Object> global = context->Global();
90 global->Set(gin::StringToV8(isolate, "statsCollectionController"),
91 controller.ToV8());
94 StatsCollectionController::StatsCollectionController() {}
96 StatsCollectionController::~StatsCollectionController() {}
98 gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder(
99 v8::Isolate* isolate) {
100 return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder(
101 isolate)
102 .SetMethod("getHistogram", &StatsCollectionController::GetHistogram)
103 .SetMethod("getBrowserHistogram",
104 &StatsCollectionController::GetBrowserHistogram)
105 .SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming);
108 std::string StatsCollectionController::GetHistogram(
109 const std::string& histogram_name) {
110 base::HistogramBase* histogram =
111 base::StatisticsRecorder::FindHistogram(histogram_name);
112 std::string output;
113 if (!histogram) {
114 output = "{}";
115 } else {
116 histogram->WriteJSON(&output);
118 return output;
121 std::string StatsCollectionController::GetBrowserHistogram(
122 const std::string& histogram_name) {
123 RenderViewImpl *render_view_impl = NULL;
124 if (!CurrentRenderViewImpl(&render_view_impl)) {
125 NOTREACHED();
126 return std::string();
129 std::string histogram_json;
130 render_view_impl->Send(new ChildProcessHostMsg_GetBrowserHistogram(
131 histogram_name, &histogram_json));
132 return histogram_json;
135 std::string StatsCollectionController::GetTabLoadTiming() {
136 RenderViewImpl *render_view_impl = NULL;
137 if (!CurrentRenderViewImpl(&render_view_impl)) {
138 NOTREACHED();
139 return std::string();
142 StatsCollectionObserver* observer =
143 render_view_impl->GetStatsCollectionObserver();
144 if (!observer) {
145 NOTREACHED();
146 return std::string();
149 std::string tab_timing_json;
150 ConvertLoadTimeToJSON(
151 observer->load_start_time(), observer->load_stop_time(),
152 &tab_timing_json);
153 return tab_timing_json;
156 } // namespace content