[email protected] | 6941ac5c | 2013-02-07 03:59:56 | [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 "content/renderer/memory_benchmarking_extension.h" |
| 6 | |
[email protected] | a70ed8b3 | 2013-05-24 15:16:34 | [diff] [blame] | 7 | #include "content/common/memory_benchmark_messages.h" |
| 8 | #include "content/renderer/render_thread_impl.h" |
[email protected] | 3fad220d | 2014-01-23 11:30:06 | [diff] [blame] | 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" |
[email protected] | 6941ac5c | 2013-02-07 03:59:56 | [diff] [blame] | 14 | |
[email protected] | a70ed8b3 | 2013-05-24 15:16:34 | [diff] [blame] | 15 | #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) |
[email protected] | 6941ac5c | 2013-02-07 03:59:56 | [diff] [blame] | 16 | #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h" |
[email protected] | 3fad220d | 2014-01-23 11:30:06 | [diff] [blame] | 17 | #endif |
[email protected] | 6941ac5c | 2013-02-07 03:59:56 | [diff] [blame] | 18 | |
| 19 | namespace content { |
| 20 | |
[email protected] | 3fad220d | 2014-01-23 11:30:06 | [diff] [blame] | 21 | gin::WrapperInfo MemoryBenchmarkingExtension::kWrapperInfo = { |
| 22 | gin::kEmbedderNativeGin}; |
| 23 | |
| 24 | // static |
| 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()) |
| 30 | return; |
| 31 | |
| 32 | v8::Context::Scope context_scope(context); |
| 33 | gin::Handle<MemoryBenchmarkingExtension> controller = |
| 34 | gin::CreateHandle(isolate, new MemoryBenchmarkingExtension()); |
[email protected] | ad4d203 | 2014-04-28 13:50:59 | [diff] [blame] | 35 | if (controller.IsEmpty()) |
| 36 | return; |
| 37 | |
[email protected] | 3fad220d | 2014-01-23 11:30:06 | [diff] [blame] | 38 | v8::Handle<v8::Object> global = context->Global(); |
| 39 | v8::Handle<v8::Object> chrome = |
| 40 | global->Get(gin::StringToV8(isolate, "chrome"))->ToObject(); |
| 41 | if (chrome.IsEmpty()) { |
| 42 | chrome = v8::Object::New(isolate); |
| 43 | global->Set(gin::StringToV8(isolate, "chrome"), chrome); |
| 44 | } |
| 45 | chrome->Set(gin::StringToV8(isolate, "memoryBenchmarking"), |
| 46 | controller.ToV8()); |
| 47 | } |
| 48 | |
| 49 | MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {} |
| 50 | |
| 51 | MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {} |
| 52 | |
| 53 | gin::ObjectTemplateBuilder |
| 54 | MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate* isolate) { |
| 55 | return gin::Wrappable<MemoryBenchmarkingExtension>::GetObjectTemplateBuilder( |
| 56 | isolate) |
| 57 | .SetMethod("isHeapProfilerRunning", |
| 58 | &MemoryBenchmarkingExtension::IsHeapProfilerRunning) |
| 59 | .SetMethod("heapProfilerDump", |
| 60 | &MemoryBenchmarkingExtension::HeapProfilerDump); |
| 61 | } |
| 62 | |
| 63 | bool MemoryBenchmarkingExtension::IsHeapProfilerRunning() { |
| 64 | #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) |
| 65 | return ::IsHeapProfilerRunning(); |
| 66 | #else |
| 67 | return false; |
| 68 | #endif |
| 69 | } |
| 70 | |
| 71 | void MemoryBenchmarkingExtension::HeapProfilerDump(gin::Arguments* args) { |
| 72 | std::string process_type; |
| 73 | std::string reason("benchmarking_extension"); |
| 74 | |
| 75 | if (args->PeekNext()->IsString()) { |
| 76 | args->GetNext(&process_type); |
| 77 | if (args->PeekNext()->IsString()) |
| 78 | args->GetNext(&reason); |
| 79 | } |
| 80 | |
| 81 | #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID)) |
| 82 | if (process_type == "browser") { |
| 83 | content::RenderThreadImpl::current()->Send( |
| 84 | new MemoryBenchmarkHostMsg_HeapProfilerDump(reason)); |
| 85 | } else { |
| 86 | ::HeapProfilerDump(reason.c_str()); |
| 87 | } |
| 88 | #endif |
[email protected] | 6941ac5c | 2013-02-07 03:59:56 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace content |