1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/stats_collection_controller.h"
#include "base/json/json_writer.h"
#include "base/metrics/histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/strings/string_util.h"
#include "content/common/child_process_messages.h"
#include "content/renderer/render_view_impl.h"
#include "gin/handle.h"
#include "gin/object_template_builder.h"
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebView.h"
namespace content {
namespace {
bool CurrentRenderViewImpl(RenderViewImpl** out) {
blink::WebFrame* web_frame = blink::WebFrame::frameForCurrentContext();
if (!web_frame)
return false;
blink::WebView* web_view = web_frame->view();
if (!web_view)
return false;
RenderViewImpl* render_view_impl =
RenderViewImpl::FromWebView(web_view);
if (!render_view_impl)
return false;
*out = render_view_impl;
return true;
}
// Encodes a WebContentsLoadTime as JSON.
// Input:
// - |load_start_time| - time at which page load started.
// - |load_stop_time| - time at which page load stopped.
// - |result| - returned JSON.
// Example return value:
// {'load_start_ms': 1, 'load_duration_ms': 2.5}
// either value may be null if a web contents hasn't fully loaded.
// load_start_ms is represented as milliseconds since system boot.
void ConvertLoadTimeToJSON(
const base::Time& load_start_time,
const base::Time& load_stop_time,
std::string *result) {
base::DictionaryValue item;
if (load_start_time.is_null()) {
item.Set("load_start_ms", base::Value::CreateNullValue());
} else {
item.SetDouble("load_start_ms", load_start_time.ToInternalValue() / 1000);
}
if (load_start_time.is_null() || load_stop_time.is_null()) {
item.Set("load_duration_ms", base::Value::CreateNullValue());
} else {
item.SetDouble("load_duration_ms",
(load_stop_time - load_start_time).InMillisecondsF());
}
base::JSONWriter::Write(&item, result);
}
} // namespace
// static
gin::WrapperInfo StatsCollectionController::kWrapperInfo = {
gin::kEmbedderNativeGin
};
// static
void StatsCollectionController::Install(blink::WebFrame* frame) {
v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
if (context.IsEmpty())
return;
v8::Context::Scope context_scope(context);
gin::Handle<StatsCollectionController> controller =
gin::CreateHandle(isolate, new StatsCollectionController());
v8::Handle<v8::Object> global = context->Global();
global->Set(gin::StringToV8(isolate, "statsCollectionController"),
controller.ToV8());
}
StatsCollectionController::StatsCollectionController() {}
StatsCollectionController::~StatsCollectionController() {}
gin::ObjectTemplateBuilder StatsCollectionController::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin::Wrappable<StatsCollectionController>::GetObjectTemplateBuilder(
isolate)
.SetMethod("getHistogram", &StatsCollectionController::GetHistogram)
.SetMethod("getBrowserHistogram",
&StatsCollectionController::GetBrowserHistogram)
.SetMethod("tabLoadTiming", &StatsCollectionController::GetTabLoadTiming);
}
std::string StatsCollectionController::GetHistogram(
const std::string& histogram_name) {
base::HistogramBase* histogram =
base::StatisticsRecorder::FindHistogram(histogram_name);
std::string output;
if (!histogram) {
output = "{}";
} else {
histogram->WriteJSON(&output);
}
return output;
}
std::string StatsCollectionController::GetBrowserHistogram(
const std::string& histogram_name) {
RenderViewImpl *render_view_impl = NULL;
if (!CurrentRenderViewImpl(&render_view_impl)) {
NOTREACHED();
return std::string();
}
std::string histogram_json;
render_view_impl->Send(new ChildProcessHostMsg_GetBrowserHistogram(
histogram_name, &histogram_json));
return histogram_json;
}
std::string StatsCollectionController::GetTabLoadTiming() {
RenderViewImpl *render_view_impl = NULL;
if (!CurrentRenderViewImpl(&render_view_impl)) {
NOTREACHED();
return std::string();
}
StatsCollectionObserver* observer =
render_view_impl->GetStatsCollectionObserver();
if (!observer) {
NOTREACHED();
return std::string();
}
std::string tab_timing_json;
ConvertLoadTimeToJSON(
observer->load_start_time(), observer->load_stop_time(),
&tab_timing_json);
return tab_timing_json;
}
} // namespace content
|