| OLD | NEW |
| (Empty) |
| 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 "content/renderer/memory_benchmarking_extension.h" | |
| 6 | |
| 7 #include "build/build_config.h" | |
| 8 #include "content/common/memory_benchmark_messages.h" | |
| 9 #include "content/public/renderer/chrome_object_extensions_utils.h" | |
| 10 #include "content/renderer/render_thread_impl.h" | |
| 11 #include "gin/arguments.h" | |
| 12 #include "gin/handle.h" | |
| 13 #include "gin/object_template_builder.h" | |
| 14 #include "third_party/WebKit/public/web/WebFrame.h" | |
| 15 #include "third_party/WebKit/public/web/WebKit.h" | |
| 16 | |
| 17 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) | |
| 18 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace content { | |
| 22 | |
| 23 gin::WrapperInfo MemoryBenchmarkingExtension::kWrapperInfo = { | |
| 24 gin::kEmbedderNativeGin}; | |
| 25 | |
| 26 // static | |
| 27 void MemoryBenchmarkingExtension::Install(blink::WebFrame* frame) { | |
| 28 v8::Isolate* isolate = blink::mainThreadIsolate(); | |
| 29 v8::HandleScope handle_scope(isolate); | |
| 30 v8::Local<v8::Context> context = frame->mainWorldScriptContext(); | |
| 31 if (context.IsEmpty()) | |
| 32 return; | |
| 33 | |
| 34 v8::Context::Scope context_scope(context); | |
| 35 gin::Handle<MemoryBenchmarkingExtension> controller = | |
| 36 gin::CreateHandle(isolate, new MemoryBenchmarkingExtension()); | |
| 37 if (controller.IsEmpty()) | |
| 38 return; | |
| 39 | |
| 40 v8::Local<v8::Object> chrome = GetOrCreateChromeObject(isolate, | |
| 41 context->Global()); | |
| 42 chrome->Set(gin::StringToV8(isolate, "memoryBenchmarking"), | |
| 43 controller.ToV8()); | |
| 44 } | |
| 45 | |
| 46 MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {} | |
| 47 | |
| 48 MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {} | |
| 49 | |
| 50 gin::ObjectTemplateBuilder | |
| 51 MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate* isolate) { | |
| 52 return gin::Wrappable<MemoryBenchmarkingExtension>::GetObjectTemplateBuilder( | |
| 53 isolate) | |
| 54 .SetMethod("isHeapProfilerRunning", | |
| 55 &MemoryBenchmarkingExtension::IsHeapProfilerRunning) | |
| 56 .SetMethod("heapProfilerDump", | |
| 57 &MemoryBenchmarkingExtension::HeapProfilerDump); | |
| 58 } | |
| 59 | |
| 60 bool MemoryBenchmarkingExtension::IsHeapProfilerRunning() { | |
| 61 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) | |
| 62 return ::IsHeapProfilerRunning(); | |
| 63 #else | |
| 64 return false; | |
| 65 #endif | |
| 66 } | |
| 67 | |
| 68 void MemoryBenchmarkingExtension::HeapProfilerDump(gin::Arguments* args) { | |
| 69 std::string process_type; | |
| 70 std::string reason("benchmarking_extension"); | |
| 71 | |
| 72 if (!args->PeekNext().IsEmpty() && args->PeekNext()->IsString()) { | |
| 73 args->GetNext(&process_type); | |
| 74 if (!args->PeekNext().IsEmpty() && args->PeekNext()->IsString()) | |
| 75 args->GetNext(&reason); | |
| 76 } | |
| 77 | |
| 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)); | |
| 82 } else { | |
| 83 ::HeapProfilerDump(reason.c_str()); | |
| 84 } | |
| 85 #endif | |
| 86 } | |
| 87 | |
| 88 } // namespace content | |
| OLD | NEW |