Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2626)

Unified Diff: content/renderer/memory_benchmarking_extension.cc

Issue 145033004: Convert MemoryBenchmarkingExtension to gin::Wrappable (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/renderer/memory_benchmarking_extension.h ('k') | content/renderer/render_thread_impl.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/renderer/memory_benchmarking_extension.cc
diff --git a/content/renderer/memory_benchmarking_extension.cc b/content/renderer/memory_benchmarking_extension.cc
index 50d5dcc436e107195562a792f9341be54e85b55a..b8dd4b479e1575ad3518648a7a508bec17a122fb 100644
--- a/content/renderer/memory_benchmarking_extension.cc
+++ b/content/renderer/memory_benchmarking_extension.cc
@@ -4,84 +4,85 @@
#include "content/renderer/memory_benchmarking_extension.h"
-#include "base/strings/string_util.h"
#include "content/common/memory_benchmark_messages.h"
#include "content/renderer/render_thread_impl.h"
+#include "gin/arguments.h"
+#include "gin/handle.h"
+#include "gin/object_template_builder.h"
+#include "third_party/WebKit/public/web/WebFrame.h"
+#include "third_party/WebKit/public/web/WebKit.h"
#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
-
#include "third_party/tcmalloc/chromium/src/gperftools/heap-profiler.h"
+#endif
-namespace {
-
-const char kMemoryBenchmarkingExtensionName[] = "v8/MemoryBenchmarking";
+namespace content {
+gin::WrapperInfo MemoryBenchmarkingExtension::kWrapperInfo = {
+ gin::kEmbedderNativeGin};
+
+// static
+void MemoryBenchmarkingExtension::Install(blink::WebFrame* frame) {
+ v8::Isolate* isolate = blink::mainThreadIsolate();
+ v8::HandleScope handle_scope(isolate);
+ v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
+ if (context.IsEmpty())
+ return;
+
+ v8::Context::Scope context_scope(context);
+ gin::Handle<MemoryBenchmarkingExtension> controller =
+ gin::CreateHandle(isolate, new MemoryBenchmarkingExtension());
+ v8::Handle<v8::Object> global = context->Global();
+ v8::Handle<v8::Object> chrome =
+ global->Get(gin::StringToV8(isolate, "chrome"))->ToObject();
+ if (chrome.IsEmpty()) {
+ chrome = v8::Object::New(isolate);
+ global->Set(gin::StringToV8(isolate, "chrome"), chrome);
+ }
+ chrome->Set(gin::StringToV8(isolate, "memoryBenchmarking"),
+ controller.ToV8());
}
-namespace {
+MemoryBenchmarkingExtension::MemoryBenchmarkingExtension() {}
-class MemoryBenchmarkingWrapper : public v8::Extension {
- public:
- MemoryBenchmarkingWrapper() :
- v8::Extension(kMemoryBenchmarkingExtensionName,
- "if (typeof(chrome) == 'undefined') {"
- " chrome = {};"
- "};"
- "if (typeof(chrome.memoryBenchmarking) == 'undefined') {"
- " chrome.memoryBenchmarking = {};"
- "};"
- "chrome.memoryBenchmarking.isHeapProfilerRunning = function() {"
- " native function IsHeapProfilerRunning();"
- " return IsHeapProfilerRunning();"
- "};"
- "chrome.memoryBenchmarking.heapProfilerDump = "
- " function(process_type, reason) {"
- " native function HeapProfilerDump();"
- " HeapProfilerDump(process_type, reason);"
- "};"
- ) {}
+MemoryBenchmarkingExtension::~MemoryBenchmarkingExtension() {}
- virtual v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate(
- v8::Isolate* isolate,
- v8::Handle<v8::String> name) OVERRIDE {
- if (name->Equals(v8::String::NewFromUtf8(isolate, "IsHeapProfilerRunning")))
- return v8::FunctionTemplate::New(isolate, IsHeapProfilerRunning);
- else if (name->Equals(v8::String::NewFromUtf8(isolate, "HeapProfilerDump")))
- return v8::FunctionTemplate::New(isolate, HeapProfilerDump);
+gin::ObjectTemplateBuilder
+MemoryBenchmarkingExtension::GetObjectTemplateBuilder(v8::Isolate* isolate) {
+ return gin::Wrappable<MemoryBenchmarkingExtension>::GetObjectTemplateBuilder(
+ isolate)
+ .SetMethod("isHeapProfilerRunning",
+ &MemoryBenchmarkingExtension::IsHeapProfilerRunning)
+ .SetMethod("heapProfilerDump",
+ &MemoryBenchmarkingExtension::HeapProfilerDump);
+}
- return v8::Handle<v8::FunctionTemplate>();
- }
+bool MemoryBenchmarkingExtension::IsHeapProfilerRunning() {
+#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
+ return ::IsHeapProfilerRunning();
+#else
+ return false;
+#endif
+}
- static void IsHeapProfilerRunning(
- const v8::FunctionCallbackInfo<v8::Value>& args) {
- args.GetReturnValue().Set(::IsHeapProfilerRunning());
- }
+void MemoryBenchmarkingExtension::HeapProfilerDump(gin::Arguments* args) {
+ std::string process_type;
+ std::string reason("benchmarking_extension");
- static void HeapProfilerDump(
- const v8::FunctionCallbackInfo<v8::Value>& args) {
- std::string process_type;
- if (args.Length() && args[0]->IsString())
- process_type = *v8::String::Utf8Value(args[0]);
- std::string reason("benchmarking_extension");
- if (args.Length() > 1 && args[1]->IsString())
- reason = *v8::String::Utf8Value(args[1]);
- if (process_type == "browser") {
- content::RenderThreadImpl::current()->Send(
- new MemoryBenchmarkHostMsg_HeapProfilerDump(reason));
- } else {
- ::HeapProfilerDump(reason.c_str());
- }
+ if (args->PeekNext()->IsString()) {
+ args->GetNext(&process_type);
+ if (args->PeekNext()->IsString())
+ args->GetNext(&reason);
}
-};
-
-} // namespace
-
-namespace content {
-v8::Extension* MemoryBenchmarkingExtension::Get() {
- return new MemoryBenchmarkingWrapper();
+#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
+ if (process_type == "browser") {
+ content::RenderThreadImpl::current()->Send(
+ new MemoryBenchmarkHostMsg_HeapProfilerDump(reason));
+ } else {
+ ::HeapProfilerDump(reason.c_str());
+ }
+#endif
}
} // namespace content
-
-#endif // defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
« no previous file with comments | « content/renderer/memory_benchmarking_extension.h ('k') | content/renderer/render_thread_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698