Reland of ll WebRTC 9687:9699, Libjingle 9690:9699 (patchset #1 id:1 of https://coder...
[chromium-blink-merge.git] / content / renderer / memory_benchmarking_extension.cc
blob8ed0185b58f07cd62d23dd2e556c6d3e814fce3c
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"
18 #endif
20 namespace content {
22 gin::WrapperInfo MemoryBenchmarkingExtension::kWrapperInfo = {
23 gin::kEmbedderNativeGin};
25 // static
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())
31 return;
33 v8::Context::Scope context_scope(context);
34 gin::Handle<MemoryBenchmarkingExtension> controller =
35 gin::CreateHandle(isolate, new MemoryBenchmarkingExtension());
36 if (controller.IsEmpty())
37 return;
39 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate,
40 context->Global());
41 chrome->Set(gin::StringToV8(isolate, "memoryBenchmarking"),
42 controller.ToV8());
45 MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {}
47 MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {}
49 gin::ObjectTemplateBuilder
50 MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate* isolate) {
51 return gin::Wrappable<MemoryBenchmarkingExtension>::GetObjectTemplateBuilder(
52 isolate)
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();
62 #else
63 return false;
64 #endif
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));
81 } else {
82 ::HeapProfilerDump(reason.c_str());
84 #endif
87 } // namespace content