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/chrome_object_extensions_utils.h"
9 #include "content/renderer/render_thread_impl.h"
10 #include "gin/arguments.h"
11 #include "gin/handle.h"
12 #include "gin/object_template_builder.h"
13 #include "third_party/WebKit/public/web/WebFrame.h"
14 #include "third_party/WebKit/public/web/WebKit.h"
16 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
17 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
22 gin::WrapperInfo
MemoryBenchmarkingExtension::kWrapperInfo
= {
23 gin::kEmbedderNativeGin
};
26 void MemoryBenchmarkingExtension::Install(blink::WebFrame
* frame
) {
27 v8::Isolate
* isolate
= blink::mainThreadIsolate();
28 v8::HandleScope
handle_scope(isolate
);
29 v8::Local
<v8::Context
> context
= frame
->mainWorldScriptContext();
30 if (context
.IsEmpty())
33 v8::Context::Scope
context_scope(context
);
34 gin::Handle
<MemoryBenchmarkingExtension
> controller
=
35 gin::CreateHandle(isolate
, new MemoryBenchmarkingExtension());
36 if (controller
.IsEmpty())
39 v8::Local
<v8::Object
> chrome
= GetOrCreateChromeObject(isolate
,
41 chrome
->Set(gin::StringToV8(isolate
, "memoryBenchmarking"),
45 MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {}
47 MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {}
49 gin::ObjectTemplateBuilder
50 MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate
* isolate
) {
51 return gin::Wrappable
<MemoryBenchmarkingExtension
>::GetObjectTemplateBuilder(
53 .SetMethod("isHeapProfilerRunning",
54 &MemoryBenchmarkingExtension::IsHeapProfilerRunning
)
55 .SetMethod("heapProfilerDump",
56 &MemoryBenchmarkingExtension::HeapProfilerDump
);
59 bool MemoryBenchmarkingExtension::IsHeapProfilerRunning() {
60 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
61 return ::IsHeapProfilerRunning();
67 void MemoryBenchmarkingExtension::HeapProfilerDump(gin::Arguments
* args
) {
68 std::string process_type
;
69 std::string
reason("benchmarking_extension");
71 if (!args
->PeekNext().IsEmpty() && args
->PeekNext()->IsString()) {
72 args
->GetNext(&process_type
);
73 if (!args
->PeekNext().IsEmpty() && args
->PeekNext()->IsString())
74 args
->GetNext(&reason
);
77 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
78 if (process_type
== "browser") {
79 content::RenderThreadImpl::current()->Send(
80 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason
));
82 ::HeapProfilerDump(reason
.c_str());
87 } // namespace content