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 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
|
// Copyright 2012 The Chromium Authors
// 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 "base/time/time.h"
#include "extensions/renderer/v8_helpers.h"
#include "net/http/http_connection_info.h"
#include "third_party/blink/public/platform/web_url_response.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/public/web/web_navigation_type.h"
#include "third_party/blink/public/web/web_performance_metrics_for_reporting.h"
#include "v8/include/v8-extension.h"
#include "v8/include/v8-isolate.h"
#include "v8/include/v8-object.h"
#include "v8/include/v8-primitive.h"
#include "v8/include/v8-template.h"
using blink::WebDocumentLoader;
using blink::WebLocalFrame;
using blink::WebNavigationType;
// 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, chrome.loadTimes()
// 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
//
// Note that chrome.loadTimes() is deprecated in favor of performance.timing.
// Many of the timings reported via chrome.loadTimes() match timings available
// in performance.timing. Timing data will be removed from chrome.loadTimes()
// in a future release. No new timings or other information should be exposed
// via these APIs.
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->StringEquals(
v8::String::NewFromUtf8(isolate, "GetLoadTimes",
v8::NewStringType::kInternalized)
.ToLocalChecked())) {
return v8::FunctionTemplate::New(isolate, GetLoadTimes);
} else if (name->StringEquals(
v8::String::NewFromUtf8(isolate, "GetCSI",
v8::NewStringType::kInternalized)
.ToLocalChecked())) {
return v8::FunctionTemplate::New(isolate, GetCSI);
}
return v8::Local<v8::FunctionTemplate>();
}
static constexpr std::string_view GetNavigationType(
WebNavigationType nav_type) {
switch (nav_type) {
case blink::kWebNavigationTypeLinkClicked:
return "LinkClicked";
case blink::kWebNavigationTypeFormSubmitted:
return "FormSubmitted";
case blink::kWebNavigationTypeBackForward:
case blink::kWebNavigationTypeRestore:
return "BackForward";
case blink::kWebNavigationTypeReload:
return "Reload";
case blink::kWebNavigationTypeFormResubmittedBackForward:
case blink::kWebNavigationTypeFormResubmittedReload:
return "Resubmitted";
case blink::kWebNavigationTypeOther:
return "Other";
}
return "";
}
static int GetCSITransitionType(WebNavigationType nav_type) {
switch (nav_type) {
case blink::kWebNavigationTypeLinkClicked:
case blink::kWebNavigationTypeFormSubmitted:
case blink::kWebNavigationTypeFormResubmittedBackForward:
case blink::kWebNavigationTypeFormResubmittedReload:
return kTransitionLink;
case blink::kWebNavigationTypeBackForward:
case blink::kWebNavigationTypeRestore:
return kTransitionForwardBack;
case blink::kWebNavigationTypeReload:
return kTransitionReload;
case blink::kWebNavigationTypeOther:
return kTransitionOther;
}
return kTransitionOther;
}
static void EmptySetter(v8::Local<v8::Name> name,
v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info) {
// Empty setter is required to keep the native data property in "accessor"
// state even in case the value is updated by user code.
}
static void LoadtimesGetter(
v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value>& info) {
if (WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext()) {
frame->UsageCountChromeLoadTimes(blink::WebString::FromUTF8(
*v8::String::Utf8Value(info.GetIsolate(), name)));
}
info.GetReturnValue().Set(info.Data());
}
static void GetLoadTimes(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().SetNull();
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
if (!frame) {
return;
}
WebDocumentLoader* document_loader = frame->GetDocumentLoader();
if (!document_loader) {
return;
}
const blink::WebURLResponse& response = document_loader->GetWebResponse();
blink::WebPerformanceMetricsForReporting web_performance =
frame->PerformanceMetricsForReporting();
// Though request time now tends to be used to describe the time that the
// request for the main resource was issued, when chrome.loadTimes() was
// added, it was used to describe 'The time the request to load the page was
// received', which is the time now known as navigation start. For backward
// compatibility, we continue to provide request_time, setting its value to
// navigation start.
double request_time = web_performance.NavigationStart();
// Developers often use start_load_time as the time the navigation was
// started, so we return navigationStart for this value as well. See
// https://gist.github.com/search?utf8=%E2%9C%93&q=startLoadTime.
// Note that, historically, start_load_time reported the time that a
// provisional load was first processed in the render process. For
// browser-initiated navigations, this is some time after navigation start,
// which means that developers who used this value as a way to track the
// start of a navigation were misusing this timestamp and getting the wrong
// value - they should be using navigationStart instead. Provisional loads
// will not be processed by the render process for browser-initiated
// navigations, so reporting the time a provisional load was processed in
// the render process will no longer make sense. Thus, we now report the
// time for navigationStart, which is a value more consistent with what
// developers currently use start_load_time for.
double start_load_time = web_performance.NavigationStart();
// TODO(bmcquade): Remove this. 'commit' time is a concept internal to
// chrome that shouldn't be exposed to the web platform.
double commit_load_time = web_performance.ResponseStart();
double finish_document_load_time =
web_performance.DomContentLoadedEventEnd();
double finish_load_time = web_performance.LoadEventEnd();
double first_paint_time = web_performance.FirstPaint();
// TODO(bmcquade): remove this. It's misleading to track the first paint
// after the load event, since many pages perform their meaningful paints
// long before the load event fires. We report a time of zero for the
// time being.
double first_paint_after_load_time = 0.0;
std::string_view navigation_type =
GetNavigationType(document_loader->GetNavigationType());
bool was_fetched_via_spdy = response.WasFetchedViaSPDY();
bool was_alpn_negotiated = response.WasAlpnNegotiated();
std::string alpn_negotiated_protocol =
response.AlpnNegotiatedProtocol().Utf8();
bool was_alternate_protocol_available =
response.WasAlternateProtocolAvailable();
std::string_view connection_info =
net::HttpConnectionInfoToString(response.ConnectionInfo());
// Important: |frame| and |document_loader| 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);
// Helper lambdas for creating v8::Number, v8::Boolean and v8::String.
auto v8_num = [=](double value) { return v8::Number::New(isolate, value); };
auto v8_bool = [=](bool value) { return v8::Boolean::New(isolate, value); };
auto v8_str = [=](std::string_view str) {
return v8::String::NewFromUtf8(isolate, str.data(),
v8::NewStringType::kNormal, str.length())
.ToLocalChecked();
};
// Defines a property on |load_times| object with given name and value,
// returns true on success, false otherwise.
auto define_prop = [=](std::string_view name, v8::Local<v8::Value> value) {
v8::Local<v8::String> name_str =
v8::String::NewFromUtf8(isolate, name.data(),
v8::NewStringType::kInternalized,
name.length())
.ToLocalChecked();
return load_times
->SetNativeDataProperty(ctx, name_str, LoadtimesGetter, EmptySetter,
value)
.FromMaybe(false);
};
if (!define_prop("requestTime", v8_num(request_time))) {
return;
}
if (!define_prop("startLoadTime", v8_num(start_load_time))) {
return;
}
if (!define_prop("commitLoadTime", v8_num(commit_load_time))) {
return;
}
if (!define_prop("finishDocumentLoadTime",
v8_num(finish_document_load_time))) {
return;
}
if (!define_prop("finishLoadTime", v8_num(finish_load_time))) {
return;
}
if (!define_prop("firstPaintTime", v8_num(first_paint_time))) {
return;
}
if (!define_prop("firstPaintAfterLoadTime",
v8_num(first_paint_after_load_time))) {
return;
}
if (!define_prop("navigationType", v8_str(navigation_type))) {
return;
}
if (!define_prop("wasFetchedViaSpdy", v8_bool(was_fetched_via_spdy))) {
return;
}
if (!define_prop("wasNpnNegotiated", v8_bool(was_alpn_negotiated))) {
return;
}
if (!define_prop("npnNegotiatedProtocol",
v8_str(alpn_negotiated_protocol))) {
return;
}
if (!define_prop("wasAlternateProtocolAvailable",
v8_bool(was_alternate_protocol_available))) {
return;
}
if (!define_prop("connectionInfo", v8_str(connection_info))) {
return;
}
args.GetReturnValue().Set(load_times);
}
static void CSIGetter(v8::Local<v8::Name> name,
const v8::PropertyCallbackInfo<v8::Value>& info) {
if (WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext()) {
frame->UsageCountChromeCSI(blink::WebString::FromUTF8(
*v8::String::Utf8Value(info.GetIsolate(), name)));
}
info.GetReturnValue().Set(info.Data());
}
static void GetCSI(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().SetNull();
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
if (!frame) {
return;
}
WebDocumentLoader* document_loader = frame->GetDocumentLoader();
if (!document_loader) {
return;
}
blink::WebPerformanceMetricsForReporting web_performance =
frame->PerformanceMetricsForReporting();
base::Time now = base::Time::Now();
base::Time start = base::Time::FromSecondsSinceUnixEpoch(
web_performance.NavigationStart());
base::Time dom_content_loaded_end = base::Time::FromSecondsSinceUnixEpoch(
web_performance.DomContentLoadedEventEnd());
base::TimeDelta page = now - start;
int navigation_type =
GetCSITransitionType(document_loader->GetNavigationType());
// Important: |frame| and |document_loader| 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);
// Helper lambda for creating v8::Number.
auto v8_num = [=](double value) { return v8::Number::New(isolate, value); };
// Defines a property on |csi| object with given name and value,
// returns true on success, false otherwise.
auto define_prop = [=](std::string_view name, v8::Local<v8::Value> value) {
v8::Local<v8::String> name_str =
v8::String::NewFromUtf8(isolate, name.data(),
v8::NewStringType::kInternalized,
name.length())
.ToLocalChecked();
return csi
->SetNativeDataProperty(ctx, name_str, CSIGetter, EmptySetter, value)
.FromMaybe(false);
};
if (!define_prop("startE", v8_num(start.InMillisecondsSinceUnixEpoch()))) {
return;
}
// NOTE: historically, the CSI onload field has reported the time the
// document finishes parsing, which is DOMContentLoaded. Thus, we continue
// to report that here, despite the fact that the field is named onloadT.
if (!define_prop(
"onloadT",
v8_num(dom_content_loaded_end.InMillisecondsSinceUnixEpoch()))) {
return;
}
if (!define_prop("pageT", v8_num(page.InMillisecondsF()))) {
return;
}
if (!define_prop("tran", v8_num(navigation_type))) {
return;
}
args.GetReturnValue().Set(csi);
}
};
std::unique_ptr<v8::Extension> LoadTimesExtension::Get() {
return std::make_unique<LoadTimesExtensionWrapper>();
}
} // namespace extensions_v8
|