Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / shell / renderer / layout_test / gc_controller.cc
blob9741e2b3a2fac60367b94488579b2a6aa850c44a
1 // Copyright 2014 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/shell/renderer/layout_test/gc_controller.h"
7 #include "gin/arguments.h"
8 #include "gin/handle.h"
9 #include "gin/object_template_builder.h"
10 #include "third_party/WebKit/public/web/WebFrame.h"
11 #include "third_party/WebKit/public/web/WebKit.h"
12 #include "v8/include/v8.h"
14 namespace content {
16 gin::WrapperInfo GCController::kWrapperInfo = {gin::kEmbedderNativeGin};
18 // static
19 void GCController::Install(blink::WebFrame* frame) {
20 v8::Isolate* isolate = blink::mainThreadIsolate();
21 v8::HandleScope handle_scope(isolate);
22 v8::Local<v8::Context> context = frame->mainWorldScriptContext();
23 if (context.IsEmpty())
24 return;
26 v8::Context::Scope context_scope(context);
28 gin::Handle<GCController> controller =
29 gin::CreateHandle(isolate, new GCController());
30 if (controller.IsEmpty())
31 return;
32 v8::Local<v8::Object> global = context->Global();
33 global->Set(gin::StringToV8(isolate, "GCController"), controller.ToV8());
36 GCController::GCController() {}
38 GCController::~GCController() {}
40 gin::ObjectTemplateBuilder GCController::GetObjectTemplateBuilder(
41 v8::Isolate* isolate) {
42 return gin::Wrappable<GCController>::GetObjectTemplateBuilder(isolate)
43 .SetMethod("collect", &GCController::Collect)
44 .SetMethod("collectAll", &GCController::CollectAll)
45 .SetMethod("minorCollect", &GCController::MinorCollect);
48 void GCController::Collect(const gin::Arguments& args) {
49 args.isolate()->RequestGarbageCollectionForTesting(
50 v8::Isolate::kFullGarbageCollection);
53 void GCController::CollectAll(const gin::Arguments& args) {
54 // In order to collect a DOM wrapper, two GC cycles are needed.
55 // In the first GC cycle, a weak callback of the DOM wrapper is called back
56 // and the weak callback disposes a persistent handle to the DOM wrapper.
57 // In the second GC cycle, the DOM wrapper is reclaimed.
58 // Given that two GC cycles are needed to collect one DOM wrapper,
59 // more than two GC cycles are needed to collect all DOM wrappers
60 // that are chained. Seven GC cycles look enough in most tests.
61 for (int i = 0; i < 7; i++) {
62 args.isolate()->RequestGarbageCollectionForTesting(
63 v8::Isolate::kFullGarbageCollection);
67 void GCController::MinorCollect(const gin::Arguments& args) {
68 args.isolate()->RequestGarbageCollectionForTesting(
69 v8::Isolate::kMinorGarbageCollection);
72 } // namespace content