blob: 2a5f128d8dd4866b8f587fac5029d585e8655492 [file] [log] [blame]
[email protected]43535512013-02-20 20:41:411// Copyright (c) 2013 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.
4
5#include "chrome/renderer/net_benchmarking_extension.h"
6
7#include "chrome/common/benchmarking_messages.h"
8#include "content/public/renderer/render_thread.h"
[email protected]2255a9332013-06-17 05:12:319#include "third_party/WebKit/public/web/WebCache.h"
[email protected]43535512013-02-20 20:41:4110#include "v8/include/v8.h"
11
[email protected]a1221aea2013-11-07 01:31:3012using blink::WebCache;
[email protected]43535512013-02-20 20:41:4113
14const char kNetBenchmarkingExtensionName[] = "v8/NetBenchmarking";
15
16namespace extensions_v8 {
17
18class NetBenchmarkingWrapper : public v8::Extension {
19 public:
20 NetBenchmarkingWrapper() :
21 v8::Extension(kNetBenchmarkingExtensionName,
22 "if (typeof(chrome) == 'undefined') {"
23 " chrome = {};"
24 "};"
25 "if (typeof(chrome.benchmarking) == 'undefined') {"
26 " chrome.benchmarking = {};"
27 "};"
28 "chrome.benchmarking.clearCache = function() {"
29 " native function ClearCache();"
30 " ClearCache();"
31 "};"
32 "chrome.benchmarking.clearHostResolverCache = function() {"
33 " native function ClearHostResolverCache();"
34 " ClearHostResolverCache();"
35 "};"
36 "chrome.benchmarking.clearPredictorCache = function() {"
37 " native function ClearPredictorCache();"
38 " ClearPredictorCache();"
39 "};"
40 "chrome.benchmarking.closeConnections = function() {"
41 " native function CloseConnections();"
42 " CloseConnections();"
43 "};"
[email protected]43535512013-02-20 20:41:4144 ) {}
45
[email protected]d48ef282013-11-28 15:42:4546 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate(
47 v8::Isolate* isolate,
[email protected]aa3174b2013-03-15 09:12:4848 v8::Handle<v8::String> name) OVERRIDE {
[email protected]d48ef282013-11-28 15:42:4549 if (name->Equals(v8::String::NewFromUtf8(isolate, "ClearCache"))) {
50 return v8::FunctionTemplate::New(isolate, ClearCache);
51 } else if (name->Equals(v8::String::NewFromUtf8(
52 isolate, "ClearHostResolverCache"))) {
53 return v8::FunctionTemplate::New(isolate, ClearHostResolverCache);
54 } else if (name->Equals(
55 v8::String::NewFromUtf8(isolate, "ClearPredictorCache"))) {
56 return v8::FunctionTemplate::New(isolate, ClearPredictorCache);
[email protected]d48ef282013-11-28 15:42:4557 } else if (name->Equals(
58 v8::String::NewFromUtf8(isolate, "CloseConnections"))) {
59 return v8::FunctionTemplate::New(isolate, CloseConnections);
[email protected]43535512013-02-20 20:41:4160 }
61
62 return v8::Handle<v8::FunctionTemplate>();
63 }
64
[email protected]187a3532013-06-13 20:25:0165 static void ClearCache(const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]43535512013-02-20 20:41:4166 int rv;
67 content::RenderThread::Get()->Send(new ChromeViewHostMsg_ClearCache(&rv));
68 WebCache::clear();
[email protected]43535512013-02-20 20:41:4169 }
70
[email protected]187a3532013-06-13 20:25:0171 static void ClearHostResolverCache(
72 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]43535512013-02-20 20:41:4173 int rv;
74 content::RenderThread::Get()->Send(
75 new ChromeViewHostMsg_ClearHostResolverCache(&rv));
[email protected]43535512013-02-20 20:41:4176 }
77
[email protected]187a3532013-06-13 20:25:0178 static void ClearPredictorCache(
79 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]43535512013-02-20 20:41:4180 int rv;
81 content::RenderThread::Get()->Send(
82 new ChromeViewHostMsg_ClearPredictorCache(&rv));
[email protected]43535512013-02-20 20:41:4183 }
84
[email protected]187a3532013-06-13 20:25:0185 static void CloseConnections(
86 const v8::FunctionCallbackInfo<v8::Value>& args) {
[email protected]43535512013-02-20 20:41:4187 content::RenderThread::Get()->Send(
88 new ChromeViewHostMsg_CloseCurrentConnections());
[email protected]43535512013-02-20 20:41:4189 }
[email protected]43535512013-02-20 20:41:4190};
91
92v8::Extension* NetBenchmarkingExtension::Get() {
93 return new NetBenchmarkingWrapper();
94}
95
96} // namespace extensions_v8