forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadtimes_extension_bindings.cc
321 lines (305 loc) · 12.7 KB
/
loadtimes_extension_bindings.cc
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
// Copyright (c) 2012 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 "chrome/renderer/loadtimes_extension_bindings.h"
#include <math.h>
#include "base/time/time.h"
#include "content/public/renderer/document_state.h"
#include "net/http/http_response_info.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "v8/include/v8.h"
using blink::WebDataSource;
using blink::WebLocalFrame;
using blink::WebNavigationType;
using content::DocumentState;
// Values for CSI "tran" property
const int kTransitionLink = 0;
const int kTransitionForwardBack = 6;
const int kTransitionOther = 15;
const int kTransitionReload = 16;
namespace extensions_v8 {
static const char* const kLoadTimesExtensionName = "v8/LoadTimes";
class LoadTimesExtensionWrapper : public v8::Extension {
public:
// Creates an extension which adds a new function, chromium.GetLoadTimes()
// This function returns an object containing the following members:
// requestTime: The time the request to load the page was received
// loadTime: The time the renderer started the load process
// finishDocumentLoadTime: The time the document itself was loaded
// (this is before the onload() method is fired)
// finishLoadTime: The time all loading is done, after the onload()
// method and all resources
// navigationType: A string describing what user action initiated the load
LoadTimesExtensionWrapper() :
v8::Extension(kLoadTimesExtensionName,
"var chrome;"
"if (!chrome)"
" chrome = {};"
"chrome.loadTimes = function() {"
" native function GetLoadTimes();"
" return GetLoadTimes();"
"};"
"chrome.csi = function() {"
" native function GetCSI();"
" return GetCSI();"
"}") {}
v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
v8::Isolate* isolate,
v8::Local<v8::String> name) override {
if (name->Equals(v8::String::NewFromUtf8(isolate, "GetLoadTimes"))) {
return v8::FunctionTemplate::New(isolate, GetLoadTimes);
} else if (name->Equals(v8::String::NewFromUtf8(isolate, "GetCSI"))) {
return v8::FunctionTemplate::New(isolate, GetCSI);
}
return v8::Local<v8::FunctionTemplate>();
}
static const char* GetNavigationType(WebNavigationType nav_type) {
switch (nav_type) {
case blink::WebNavigationTypeLinkClicked:
return "LinkClicked";
case blink::WebNavigationTypeFormSubmitted:
return "FormSubmitted";
case blink::WebNavigationTypeBackForward:
return "BackForward";
case blink::WebNavigationTypeReload:
return "Reload";
case blink::WebNavigationTypeFormResubmitted:
return "Resubmitted";
case blink::WebNavigationTypeOther:
return "Other";
}
return "";
}
static int GetCSITransitionType(WebNavigationType nav_type) {
switch (nav_type) {
case blink::WebNavigationTypeLinkClicked:
case blink::WebNavigationTypeFormSubmitted:
case blink::WebNavigationTypeFormResubmitted:
return kTransitionLink;
case blink::WebNavigationTypeBackForward:
return kTransitionForwardBack;
case blink::WebNavigationTypeReload:
return kTransitionReload;
case blink::WebNavigationTypeOther:
return kTransitionOther;
}
return kTransitionOther;
}
static void GetLoadTimes(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().SetNull();
WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext();
if (!frame) {
return;
}
WebDataSource* data_source = frame->dataSource();
if (!data_source) {
return;
}
DocumentState* document_state = DocumentState::FromDataSource(data_source);
if (!document_state) {
return;
}
double request_time = document_state->request_time().ToDoubleT();
double start_load_time = document_state->start_load_time().ToDoubleT();
double commit_load_time = document_state->commit_load_time().ToDoubleT();
double finish_document_load_time =
document_state->finish_document_load_time().ToDoubleT();
double finish_load_time = document_state->finish_load_time().ToDoubleT();
double first_paint_time = document_state->first_paint_time().ToDoubleT();
double first_paint_after_load_time =
document_state->first_paint_after_load_time().ToDoubleT();
std::string navigation_type =
GetNavigationType(data_source->navigationType());
bool was_fetched_via_spdy = document_state->was_fetched_via_spdy();
bool was_npn_negotiated = document_state->was_npn_negotiated();
std::string npn_negotiated_protocol =
document_state->npn_negotiated_protocol();
bool was_alternate_protocol_available =
document_state->was_alternate_protocol_available();
std::string connection_info = net::HttpResponseInfo::ConnectionInfoToString(
document_state->connection_info());
// Important: |frame|, |data_source| and |document_state| should not be
// referred to below this line, as JS setters below can invalidate these
// pointers.
v8::Isolate* isolate = args.GetIsolate();
v8::Local<v8::Context> ctx = isolate->GetCurrentContext();
v8::Local<v8::Object> load_times = v8::Object::New(isolate);
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "requestTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, request_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "startLoadTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, start_load_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "commitLoadTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, commit_load_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx,
v8::String::NewFromUtf8(isolate, "finishDocumentLoadTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, finish_document_load_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "finishLoadTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, finish_load_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "firstPaintTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, first_paint_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx,
v8::String::NewFromUtf8(isolate, "firstPaintAfterLoadTime",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, first_paint_after_load_time))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "navigationType",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::String::NewFromUtf8(isolate, navigation_type.c_str(),
v8::NewStringType::kNormal)
.ToLocalChecked())
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "wasFetchedViaSpdy",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Boolean::New(isolate, was_fetched_via_spdy))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "wasNpnNegotiated",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Boolean::New(isolate, was_npn_negotiated))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx,
v8::String::NewFromUtf8(isolate, "npnNegotiatedProtocol",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::String::NewFromUtf8(isolate,
npn_negotiated_protocol.c_str(),
v8::NewStringType::kNormal)
.ToLocalChecked())
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate,
"wasAlternateProtocolAvailable",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Boolean::New(isolate, was_alternate_protocol_available))
.FromMaybe(false)) {
return;
}
if (!load_times
->Set(ctx, v8::String::NewFromUtf8(isolate, "connectionInfo",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::String::NewFromUtf8(isolate, connection_info.c_str(),
v8::NewStringType::kNormal)
.ToLocalChecked())
.FromMaybe(false)) {
return;
}
args.GetReturnValue().Set(load_times);
}
static void GetCSI(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().SetNull();
WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext();
if (!frame) {
return;
}
WebDataSource* data_source = frame->dataSource();
if (!data_source) {
return;
}
DocumentState* document_state = DocumentState::FromDataSource(data_source);
if (!document_state) {
return;
}
base::Time now = base::Time::Now();
base::Time start = document_state->request_time().is_null()
? document_state->start_load_time()
: document_state->request_time();
base::Time onload = document_state->finish_document_load_time();
base::TimeDelta page = now - start;
int navigation_type = GetCSITransitionType(data_source->navigationType());
// Important: |frame|, |data_source| and |document_state| should not be
// referred to below this line, as JS setters below can invalidate these
// pointers.
v8::Isolate* isolate = args.GetIsolate();
v8::Local<v8::Context> ctx = isolate->GetCurrentContext();
v8::Local<v8::Object> csi = v8::Object::New(isolate);
if (!csi->Set(ctx, v8::String::NewFromUtf8(isolate, "startE",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, floor(start.ToDoubleT() * 1000)))
.FromMaybe(false)) {
return;
}
if (!csi->Set(ctx, v8::String::NewFromUtf8(isolate, "onloadT",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, floor(onload.ToDoubleT() * 1000)))
.FromMaybe(false)) {
return;
}
if (!csi->Set(ctx, v8::String::NewFromUtf8(isolate, "pageT",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, page.InMillisecondsF()))
.FromMaybe(false)) {
return;
}
if (!csi->Set(ctx, v8::String::NewFromUtf8(isolate, "tran",
v8::NewStringType::kNormal)
.ToLocalChecked(),
v8::Number::New(isolate, navigation_type))
.FromMaybe(false)) {
return;
}
args.GetReturnValue().Set(csi);
}
};
v8::Extension* LoadTimesExtension::Get() {
return new LoadTimesExtensionWrapper();
}
} // namespace extensions_v8