- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / memory_benchmarking_extension.cc
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 "base/strings/string_util.h"
8 #include "content/common/memory_benchmark_messages.h"
9 #include "content/renderer/render_thread_impl.h"
10
11 #if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
12
13 #include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
14
15 namespace {
16
17 const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking";
18
19 }
20
21 namespace {
22
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         ) {}
43
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);
50
51     return v8::Handle<v8::FunctionTemplate>();
52   }
53
54   static void IsHeapProfilerRunning(
55       const v8::FunctionCallbackInfo<v8::Value>& args) {
56     args.GetReturnValue().Set(::IsHeapProfilerRunning());
57   }
58
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());
72     }
73   }
74 };
75
76 }  // namespace
77
78 namespace content {
79
80 v8::Extension* MemoryBenchmarkingExtension::Get() {
81   return new MemoryBenchmarkingWrapper();
82 }
83
84 }  // namespace content
85
86 #endif  // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))