[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 1 | // 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] | 2255a933 | 2013-06-17 05:12:31 | [diff] [blame] | 9 | #include "third_party/WebKit/public/web/WebCache.h" |
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 10 | #include "v8/include/v8.h" |
11 | |||||
[email protected] | a1221aea | 2013-11-07 01:31:30 | [diff] [blame^] | 12 | using blink::WebCache; |
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 13 | |
14 | const char kNetBenchmarkingExtensionName[] = "v8/NetBenchmarking"; | ||||
15 | |||||
16 | namespace extensions_v8 { | ||||
17 | |||||
18 | class 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 | "};" | ||||
44 | "chrome.benchmarking.enableSpdy = function(name) {" | ||||
45 | " native function EnableSpdy();" | ||||
46 | " EnableSpdy(name);" | ||||
47 | "};" | ||||
48 | ) {} | ||||
49 | |||||
50 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( | ||||
[email protected] | aa3174b | 2013-03-15 09:12:48 | [diff] [blame] | 51 | v8::Handle<v8::String> name) OVERRIDE { |
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 52 | if (name->Equals(v8::String::New("ClearCache"))) { |
53 | return v8::FunctionTemplate::New(ClearCache); | ||||
54 | } else if (name->Equals(v8::String::New("ClearHostResolverCache"))) { | ||||
55 | return v8::FunctionTemplate::New(ClearHostResolverCache); | ||||
56 | } else if (name->Equals(v8::String::New("ClearPredictorCache"))) { | ||||
57 | return v8::FunctionTemplate::New(ClearPredictorCache); | ||||
58 | } else if (name->Equals(v8::String::New("EnableSpdy"))) { | ||||
59 | return v8::FunctionTemplate::New(EnableSpdy); | ||||
60 | } else if (name->Equals(v8::String::New("CloseConnections"))) { | ||||
61 | return v8::FunctionTemplate::New(CloseConnections); | ||||
62 | } | ||||
63 | |||||
64 | return v8::Handle<v8::FunctionTemplate>(); | ||||
65 | } | ||||
66 | |||||
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 67 | static void ClearCache(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 68 | int rv; |
69 | content::RenderThread::Get()->Send(new ChromeViewHostMsg_ClearCache(&rv)); | ||||
70 | WebCache::clear(); | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 71 | } |
72 | |||||
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 73 | static void ClearHostResolverCache( |
74 | const v8::FunctionCallbackInfo<v8::Value>& args) { | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 75 | int rv; |
76 | content::RenderThread::Get()->Send( | ||||
77 | new ChromeViewHostMsg_ClearHostResolverCache(&rv)); | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 78 | } |
79 | |||||
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 80 | static void ClearPredictorCache( |
81 | const v8::FunctionCallbackInfo<v8::Value>& args) { | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 82 | int rv; |
83 | content::RenderThread::Get()->Send( | ||||
84 | new ChromeViewHostMsg_ClearPredictorCache(&rv)); | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 85 | } |
86 | |||||
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 87 | static void CloseConnections( |
88 | const v8::FunctionCallbackInfo<v8::Value>& args) { | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 89 | content::RenderThread::Get()->Send( |
90 | new ChromeViewHostMsg_CloseCurrentConnections()); | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 91 | } |
92 | |||||
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 93 | static void EnableSpdy(const v8::FunctionCallbackInfo<v8::Value>& args) { |
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 94 | if (!args.Length() || !args[0]->IsBoolean()) |
[email protected] | 187a353 | 2013-06-13 20:25:01 | [diff] [blame] | 95 | return; |
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 96 | |
97 | content::RenderThread::Get()->Send(new ChromeViewHostMsg_EnableSpdy( | ||||
98 | args[0]->BooleanValue())); | ||||
[email protected] | 4353551 | 2013-02-20 20:41:41 | [diff] [blame] | 99 | } |
100 | }; | ||||
101 | |||||
102 | v8::Extension* NetBenchmarkingExtension::Get() { | ||||
103 | return new NetBenchmarkingWrapper(); | ||||
104 | } | ||||
105 | |||||
106 | } // namespace extensions_v8 |