Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: chrome/renderer/loadtimes_extension_bindings.cc

Issue 2149933003: Pre-work for adding usage counter for chrome.loadtimes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: use ObjectTemplate and SetNativeDataProperty Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/renderer/loadtimes_extension_bindings.h" 5 #include "chrome/renderer/loadtimes_extension_bindings.h"
6 6
7 #include <math.h> 7 #include <math.h>
8 8
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "content/public/renderer/document_state.h" 10 #include "content/public/renderer/document_state.h"
11 #include "extensions/renderer/v8_helpers.h"
11 #include "net/http/http_response_info.h" 12 #include "net/http/http_response_info.h"
12 #include "third_party/WebKit/public/web/WebLocalFrame.h" 13 #include "third_party/WebKit/public/web/WebLocalFrame.h"
13 #include "third_party/WebKit/public/web/WebPerformance.h" 14 #include "third_party/WebKit/public/web/WebPerformance.h"
14 #include "v8/include/v8.h" 15 #include "v8/include/v8.h"
15 16
16 using blink::WebDataSource; 17 using blink::WebDataSource;
17 using blink::WebLocalFrame; 18 using blink::WebLocalFrame;
18 using blink::WebNavigationType; 19 using blink::WebNavigationType;
19 using blink::WebPerformance; 20 using blink::WebPerformance;
20 using content::DocumentState; 21 using content::DocumentState;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
98 case blink::WebNavigationTypeBackForward: 99 case blink::WebNavigationTypeBackForward:
99 return kTransitionForwardBack; 100 return kTransitionForwardBack;
100 case blink::WebNavigationTypeReload: 101 case blink::WebNavigationTypeReload:
101 return kTransitionReload; 102 return kTransitionReload;
102 case blink::WebNavigationTypeOther: 103 case blink::WebNavigationTypeOther:
103 return kTransitionOther; 104 return kTransitionOther;
104 } 105 }
105 return kTransitionOther; 106 return kTransitionOther;
106 } 107 }
107 108
109 static void LoadtimesGetter(
110 v8::Local<v8::String> name,
111 const v8::PropertyCallbackInfo<v8::Value>& info) {
112 // TODO(panicker): Add Usage counter.
113 info.GetReturnValue().Set(info.Data());
114 }
115
108 static void GetLoadTimes(const v8::FunctionCallbackInfo<v8::Value>& args) { 116 static void GetLoadTimes(const v8::FunctionCallbackInfo<v8::Value>& args) {
109 args.GetReturnValue().SetNull(); 117 args.GetReturnValue().SetNull();
110 WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext(); 118 WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext();
111 if (!frame) { 119 if (!frame) {
112 return; 120 return;
113 } 121 }
114 WebDataSource* data_source = frame->dataSource(); 122 WebDataSource* data_source = frame->dataSource();
115 if (!data_source) { 123 if (!data_source) {
116 return; 124 return;
117 } 125 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 std::string npn_negotiated_protocol = 169 std::string npn_negotiated_protocol =
162 document_state->npn_negotiated_protocol(); 170 document_state->npn_negotiated_protocol();
163 bool was_alternate_protocol_available = 171 bool was_alternate_protocol_available =
164 document_state->was_alternate_protocol_available(); 172 document_state->was_alternate_protocol_available();
165 std::string connection_info = net::HttpResponseInfo::ConnectionInfoToString( 173 std::string connection_info = net::HttpResponseInfo::ConnectionInfoToString(
166 document_state->connection_info()); 174 document_state->connection_info());
167 // Important: |frame|, |data_source| and |document_state| should not be 175 // Important: |frame|, |data_source| and |document_state| should not be
168 // referred to below this line, as JS setters below can invalidate these 176 // referred to below this line, as JS setters below can invalidate these
169 // pointers. 177 // pointers.
170 v8::Isolate* isolate = args.GetIsolate(); 178 v8::Isolate* isolate = args.GetIsolate();
171 v8::Local<v8::Context> ctx = isolate->GetCurrentContext();
172 v8::Local<v8::Object> load_times = v8::Object::New(isolate);
173 if (!load_times
174 ->Set(ctx, v8::String::NewFromUtf8(isolate, "requestTime",
175 v8::NewStringType::kNormal)
176 .ToLocalChecked(),
177 v8::Number::New(isolate, request_time))
178 .FromMaybe(false)) {
179 return;
180 }
181 179
182 if (!load_times 180 v8::Local<v8::ObjectTemplate> load_times_tpl = v8::ObjectTemplate::New(
183 ->Set(ctx, v8::String::NewFromUtf8(isolate, "startLoadTime", 181 isolate);
184 v8::NewStringType::kNormal) 182
185 .ToLocalChecked(), 183 load_times_tpl->SetNativeDataProperty(
186 v8::Number::New(isolate, start_load_time)) 184 v8::String::NewFromUtf8(isolate, "requestTime",
187 .FromMaybe(false)) { 185 v8::NewStringType::kNormal).ToLocalChecked(),
188 return; 186 LoadtimesGetter,
189 } 187 nullptr,
190 if (!load_times 188 v8::Number::New(isolate, request_time));
191 ->Set(ctx, v8::String::NewFromUtf8(isolate, "commitLoadTime", 189
192 v8::NewStringType::kNormal) 190 load_times_tpl->SetNativeDataProperty(
193 .ToLocalChecked(), 191 v8::String::NewFromUtf8(isolate, "startLoadTime",
194 v8::Number::New(isolate, commit_load_time)) 192 v8::NewStringType::kNormal).ToLocalChecked(),
195 .FromMaybe(false)) { 193 LoadtimesGetter,
196 return; 194 nullptr,
197 } 195 v8::Number::New(isolate, start_load_time));
198 if (!load_times 196
199 ->Set(ctx, 197 load_times_tpl->SetNativeDataProperty(
200 v8::String::NewFromUtf8(isolate, "finishDocumentLoadTime", 198 v8::String::NewFromUtf8(isolate, "commitLoadTime",
201 v8::NewStringType::kNormal) 199 v8::NewStringType::kNormal).ToLocalChecked(),
202 .ToLocalChecked(), 200 LoadtimesGetter,
203 v8::Number::New(isolate, finish_document_load_time)) 201 nullptr,
204 .FromMaybe(false)) { 202 v8::Number::New(isolate, commit_load_time));
205 return; 203
206 } 204 load_times_tpl->SetNativeDataProperty(
207 if (!load_times 205 v8::String::NewFromUtf8(isolate, "finishDocumentLoadTime",
208 ->Set(ctx, v8::String::NewFromUtf8(isolate, "finishLoadTime", 206 v8::NewStringType::kNormal).ToLocalChecked(),
209 v8::NewStringType::kNormal) 207 LoadtimesGetter,
210 .ToLocalChecked(), 208 nullptr,
211 v8::Number::New(isolate, finish_load_time)) 209 v8::Number::New(isolate, finish_document_load_time));
212 .FromMaybe(false)) { 210
213 return; 211 load_times_tpl->SetNativeDataProperty(
214 } 212 v8::String::NewFromUtf8(isolate, "finishLoadTime",
215 if (!load_times 213 v8::NewStringType::kNormal).ToLocalChecked(),
216 ->Set(ctx, v8::String::NewFromUtf8(isolate, "firstPaintTime", 214 LoadtimesGetter,
217 v8::NewStringType::kNormal) 215 nullptr,
218 .ToLocalChecked(), 216 v8::Number::New(isolate, finish_load_time));
219 v8::Number::New(isolate, first_paint_time)) 217
220 .FromMaybe(false)) { 218 load_times_tpl->SetNativeDataProperty(
221 return; 219 v8::String::NewFromUtf8(isolate, "firstPaintTime",
222 } 220 v8::NewStringType::kNormal).ToLocalChecked(),
223 if (!load_times 221 LoadtimesGetter,
224 ->Set(ctx, 222 nullptr,
225 v8::String::NewFromUtf8(isolate, "firstPaintAfterLoadTime", 223 v8::Number::New(isolate, first_paint_time));
226 v8::NewStringType::kNormal) 224
227 .ToLocalChecked(), 225 load_times_tpl->SetNativeDataProperty(
228 v8::Number::New(isolate, first_paint_after_load_time)) 226 v8::String::NewFromUtf8(isolate, "firstPaintAfterLoadTime",
229 .FromMaybe(false)) { 227 v8::NewStringType::kNormal).ToLocalChecked(),
230 return; 228 LoadtimesGetter,
231 } 229 nullptr,
232 if (!load_times 230 v8::Number::New(isolate, first_paint_after_load_time));
233 ->Set(ctx, v8::String::NewFromUtf8(isolate, "navigationType", 231
234 v8::NewStringType::kNormal) 232 load_times_tpl->SetNativeDataProperty(
235 .ToLocalChecked(), 233 v8::String::NewFromUtf8(isolate, "navigationType",
236 v8::String::NewFromUtf8(isolate, navigation_type.c_str(), 234 v8::NewStringType::kNormal).ToLocalChecked(),
237 v8::NewStringType::kNormal) 235 LoadtimesGetter,
238 .ToLocalChecked()) 236 nullptr,
239 .FromMaybe(false)) { 237 v8::String::NewFromUtf8(isolate, navigation_type.c_str(),
240 return; 238 v8::NewStringType::kNormal).ToLocalChecked());
241 } 239
242 if (!load_times 240 load_times_tpl->SetNativeDataProperty(
243 ->Set(ctx, v8::String::NewFromUtf8(isolate, "wasFetchedViaSpdy", 241 v8::String::NewFromUtf8(isolate, "wasFetchedViaSpdy",
244 v8::NewStringType::kNormal) 242 v8::NewStringType::kNormal).ToLocalChecked(),
245 .ToLocalChecked(), 243 LoadtimesGetter,
246 v8::Boolean::New(isolate, was_fetched_via_spdy)) 244 nullptr,
247 .FromMaybe(false)) { 245 v8::Boolean::New(isolate, was_fetched_via_spdy));
248 return; 246
249 } 247 load_times_tpl->SetNativeDataProperty(
250 if (!load_times 248 v8::String::NewFromUtf8(isolate, "wasNpnNegotiated",
251 ->Set(ctx, v8::String::NewFromUtf8(isolate, "wasNpnNegotiated", 249 v8::NewStringType::kNormal).ToLocalChecked(),
252 v8::NewStringType::kNormal) 250 LoadtimesGetter,
253 .ToLocalChecked(), 251 nullptr,
254 v8::Boolean::New(isolate, was_npn_negotiated)) 252 v8::Boolean::New(isolate, was_npn_negotiated));
255 .FromMaybe(false)) { 253
256 return; 254 load_times_tpl->SetNativeDataProperty(
257 } 255 v8::String::NewFromUtf8(isolate, "npnNegotiatedProtocol",
258 if (!load_times 256 v8::NewStringType::kNormal).ToLocalChecked(),
259 ->Set(ctx, 257 LoadtimesGetter,
260 v8::String::NewFromUtf8(isolate, "npnNegotiatedProtocol", 258 nullptr,
261 v8::NewStringType::kNormal) 259 v8::String::NewFromUtf8(isolate,
262 .ToLocalChecked(), 260 npn_negotiated_protocol.c_str(),
263 v8::String::NewFromUtf8(isolate, 261 v8::NewStringType::kNormal).ToLocalChecked());
264 npn_negotiated_protocol.c_str(), 262
265 v8::NewStringType::kNormal) 263 load_times_tpl->SetNativeDataProperty(
266 .ToLocalChecked()) 264 v8::String::NewFromUtf8(isolate, "wasAlternateProtocolAvailable",
267 .FromMaybe(false)) { 265 v8::NewStringType::kNormal).ToLocalChecked(),
268 return; 266 LoadtimesGetter,
269 } 267 nullptr,
270 if (!load_times 268 v8::Boolean::New(isolate, was_alternate_protocol_available));
271 ->Set(ctx, v8::String::NewFromUtf8(isolate, 269
272 "wasAlternateProtocolAvailable", 270 load_times_tpl->SetNativeDataProperty(
273 v8::NewStringType::kNormal) 271 v8::String::NewFromUtf8(isolate, "connectionInfo",
274 .ToLocalChecked(), 272 v8::NewStringType::kNormal).ToLocalChecked(),
275 v8::Boolean::New(isolate, was_alternate_protocol_available)) 273 LoadtimesGetter,
276 .FromMaybe(false)) { 274 nullptr,
277 return; 275 v8::String::NewFromUtf8(isolate, connection_info.c_str(),
278 } 276 v8::NewStringType::kNormal).ToLocalChecked());
279 if (!load_times 277
280 ->Set(ctx, v8::String::NewFromUtf8(isolate, "connectionInfo", 278 args.GetReturnValue().Set(load_times_tpl->NewInstance());
adamk 2016/07/14 23:48:56 So the only thing really bad about this approach i
panicker 2016/07/15 00:32:25 Since loadtimes is applicable to a specific load,
Bryan McQuade 2016/07/18 18:33:55 Yes, in code I've looked at, the common pattern is
281 v8::NewStringType::kNormal)
282 .ToLocalChecked(),
283 v8::String::NewFromUtf8(isolate, connection_info.c_str(),
284 v8::NewStringType::kNormal)
285 .ToLocalChecked())
286 .FromMaybe(false)) {
287 return;
288 }
289 args.GetReturnValue().Set(load_times);
290 } 279 }
291 280
292 static void GetCSI(const v8::FunctionCallbackInfo<v8::Value>& args) { 281 static void GetCSI(const v8::FunctionCallbackInfo<v8::Value>& args) {
293 args.GetReturnValue().SetNull(); 282 args.GetReturnValue().SetNull();
294 WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext(); 283 WebLocalFrame* frame = WebLocalFrame::frameForCurrentContext();
295 if (!frame) { 284 if (!frame) {
296 return; 285 return;
297 } 286 }
298 WebDataSource* data_source = frame->dataSource(); 287 WebDataSource* data_source = frame->dataSource();
299 if (!data_source) { 288 if (!data_source) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 } 335 }
347 args.GetReturnValue().Set(csi); 336 args.GetReturnValue().Set(csi);
348 } 337 }
349 }; 338 };
350 339
351 v8::Extension* LoadTimesExtension::Get() { 340 v8::Extension* LoadTimesExtension::Get() {
352 return new LoadTimesExtensionWrapper(); 341 return new LoadTimesExtensionWrapper();
353 } 342 }
354 343
355 } // namespace extensions_v8 344 } // namespace extensions_v8
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698