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.
5 #include "content/renderer/memory_benchmarking_extension.h"
7 #include "content/common/memory_benchmark_messages.h"
8 #include "content/renderer/render_thread_impl.h"
9 #include "gin/arguments.h"
10 #include "gin/handle.h"
11 #include "gin/object_template_builder.h"
12 #include "third_party/WebKit/public/web/WebFrame.h"
13 #include "third_party/WebKit/public/web/WebKit.h"
15 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
16 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
21 gin::WrapperInfo
MemoryBenchmarkingExtension::kWrapperInfo
= {
22 gin::kEmbedderNativeGin
};
25 void MemoryBenchmarkingExtension::Install(blink::WebFrame
* frame
) {
26 v8::Isolate
* isolate
= blink::mainThreadIsolate();
27 v8::HandleScope
handle_scope(isolate
);
28 v8::Handle
<v8::Context
> context
= frame
->mainWorldScriptContext();
29 if (context
.IsEmpty())
32 v8::Context::Scope
context_scope(context
);
33 gin::Handle
<MemoryBenchmarkingExtension
> controller
=
34 gin::CreateHandle(isolate
, new MemoryBenchmarkingExtension());
35 v8::Handle
<v8::Object
> global
= context
->Global();
36 v8::Handle
<v8::Object
> chrome
=
37 global
->Get(gin::StringToV8(isolate
, "chrome"))->ToObject();
38 if (chrome
.IsEmpty()) {
39 chrome
= v8::Object::New(isolate
);
40 global
->Set(gin::StringToV8(isolate
, "chrome"), chrome
);
42 chrome
->Set(gin::StringToV8(isolate
, "memoryBenchmarking"),
46 MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {}
48 MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {}
50 gin::ObjectTemplateBuilder
51 MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate
* isolate
) {
52 return gin::Wrappable
<MemoryBenchmarkingExtension
>::GetObjectTemplateBuilder(
54 .SetMethod("isHeapProfilerRunning",
55 &MemoryBenchmarkingExtension::IsHeapProfilerRunning
)
56 .SetMethod("heapProfilerDump",
57 &MemoryBenchmarkingExtension::HeapProfilerDump
);
60 bool MemoryBenchmarkingExtension::IsHeapProfilerRunning() {
61 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
62 return ::IsHeapProfilerRunning();
68 void MemoryBenchmarkingExtension::HeapProfilerDump(gin::Arguments
* args
) {
69 std::string process_type
;
70 std::string
reason("benchmarking_extension");
72 if (args
->PeekNext()->IsString()) {
73 args
->GetNext(&process_type
);
74 if (args
->PeekNext()->IsString())
75 args
->GetNext(&reason
);
78 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
79 if (process_type
== "browser") {
80 content::RenderThreadImpl::current()->Send(
81 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason
));
83 ::HeapProfilerDump(reason
.c_str());
88 } // namespace content