Get foreground tab on Android
[chromium-blink-merge.git] / content / renderer / memory_benchmarking_extension.cc
blobe463fcfff0912c3150e7d4c549c83462c3a1deff
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 "base/strings/string_util.h"
8 #include "content/common/memory_benchmark_messages.h"
9 #include "content/renderer/render_thread_impl.h"
11 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
15 namespace {
17 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking";
21 namespace {
23 class MemoryBenchmarkingWrapper : public v8::Extension {
24 public:
25 MemoryBenchmarkingWrapper() :
26 v8::Extension(kMemoryBenchmarkingExtensionName,
27 "if (typeof(chrome) == 'undefined') {"
28 " chrome = {};"
29 "};"
30 "if (typeof(chrome.memoryBenchmarking) == 'undefined') {"
31 " chrome.memoryBenchmarking = {};"
32 "};"
33 "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {"
34 " native function IsHeapProfilerRunning();"
35 " return IsHeapProfilerRunning();"
36 "};"
37 "chrome.memoryBenchmarking.heapProfilerDump = "
38 " function(process_type, reason) {"
39 " native function HeapProfilerDump();"
40 " HeapProfilerDump(process_type, reason);"
41 "};"
42 ) {}
44 virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
45 v8::Handle<v8::String> name) OVERRIDE {
46 if (name->Equals(v8::String::New("IsHeapProfilerRunning")))
47 return v8::FunctionTemplate::New(IsHeapProfilerRunning);
48 else if (name->Equals(v8::String::New("HeapProfilerDump")))
49 return v8::FunctionTemplate::New(HeapProfilerDump);
51 return v8::Handle<v8::FunctionTemplate>();
54 static void IsHeapProfilerRunning(
55 const v8::FunctionCallbackInfo<v8::Value>& args) {
56 args.GetReturnValue().Set(::IsHeapProfilerRunning());
59 static void HeapProfilerDump(
60 const v8::FunctionCallbackInfo<v8::Value>& args) {
61 std::string process_type;
62 if (args.Length() && args[0]->IsString())
63 process_type = *v8::String::AsciiValue(args[0]);
64 std::string reason("benchmarking_extension");
65 if (args.Length() > 1 && args[1]->IsString())
66 reason = *v8::String::AsciiValue(args[1]);
67 if (process_type == "browser") {
68 content::RenderThreadImpl::current()->Send(
69 new MemoryBenchmarkHostMsg_HeapProfilerDump(reason));
70 } else {
71 ::HeapProfilerDump(reason.c_str());
76 } // namespace
78 namespace content {
80 v8::Extension* MemoryBenchmarkingExtension::Get() {
81 return new MemoryBenchmarkingWrapper();
84 } // namespace content
86 #endif // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))