Autofill: Port r320637 to iOS.
[chromium-blink-merge.git] / chrome / renderer / benchmarking_extension.cc
blob955cbd52e657813e45d56656a71b9558071ec2c1
1 // Copyright (c) 2011 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 "chrome/renderer/benchmarking_extension.h"
7 #include "base/command_line.h"
8 #include "base/time/time.h"
9 #include "content/public/common/content_switches.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "v8/include/v8.h"
13 const char kBenchmarkingExtensionName[] = "v8/Benchmarking";
15 namespace extensions_v8 {
17 class BenchmarkingWrapper : public v8::Extension {
18 public:
19 BenchmarkingWrapper() :
20 v8::Extension(kBenchmarkingExtensionName,
21 "if (typeof(chrome) == 'undefined') {"
22 " chrome = {};"
23 "};"
24 "if (typeof(chrome.benchmarking) == 'undefined') {"
25 " chrome.benchmarking = {};"
26 "};"
27 "chrome.benchmarking.isSingleProcess = function() {"
28 " native function IsSingleProcess();"
29 " return IsSingleProcess();"
30 "};"
31 "chrome.Interval = function() {"
32 " var start_ = 0;"
33 " var stop_ = 0;"
34 " native function HiResTime();"
35 " this.start = function() {"
36 " stop_ = 0;"
37 " start_ = HiResTime();"
38 " };"
39 " this.stop = function() {"
40 " stop_ = HiResTime();"
41 " if (start_ == 0)"
42 " stop_ = 0;"
43 " };"
44 " this.microseconds = function() {"
45 " var stop = stop_;"
46 " if (stop == 0 && start_ != 0)"
47 " stop = HiResTime();"
48 " return Math.ceil(stop - start_);"
49 " };"
50 "}"
51 ) {}
53 v8::Local<v8::FunctionTemplate> GetNativeFunctionTemplate(
54 v8::Isolate* isolate,
55 v8::Local<v8::String> name) override {
56 if (name->Equals(v8::String::NewFromUtf8(isolate, "IsSingleProcess"))) {
57 return v8::FunctionTemplate::New(isolate, IsSingleProcess);
58 } else if (name->Equals(v8::String::NewFromUtf8(isolate, "HiResTime"))) {
59 return v8::FunctionTemplate::New(isolate, HiResTime);
62 return v8::Local<v8::FunctionTemplate>();
65 static void IsSingleProcess(const v8::FunctionCallbackInfo<v8::Value>& args) {
66 args.GetReturnValue().Set(base::CommandLine::ForCurrentProcess()->HasSwitch(
67 switches::kSingleProcess));
70 static void HiResTime(const v8::FunctionCallbackInfo<v8::Value>& args) {
71 args.GetReturnValue().Set(
72 static_cast<double>(base::TimeTicks::Now().ToInternalValue()));
76 v8::Extension* BenchmarkingExtension::Get() {
77 return new BenchmarkingWrapper();
80 } // namespace extensions_v8