blob: 4ef63bd247b977f478b2824d153416aed042ceb0 [file] [log] [blame]
[email protected]41fba0e2014-01-16 18:19:421// 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.
4
Scott Violetfdda96d2018-07-27 20:17:235#include "content/shell/test_runner/gc_controller.h"
[email protected]41fba0e2014-01-16 18:19:426
Michael Lippautz5b64e892018-09-24 11:10:007#include "content/shell/test_runner/test_interfaces.h"
8#include "content/shell/test_runner/web_test_delegate.h"
[email protected]41fba0e2014-01-16 18:19:429#include "gin/arguments.h"
10#include "gin/handle.h"
11#include "gin/object_template_builder.h"
Blink Reformata30d4232018-04-07 15:31:0612#include "third_party/blink/public/web/blink.h"
13#include "third_party/blink/public/web/web_local_frame.h"
[email protected]41fba0e2014-01-16 18:19:4214#include "v8/include/v8.h"
15
sadrul43405772015-10-15 23:12:3716namespace test_runner {
[email protected]41fba0e2014-01-16 18:19:4217
18gin::WrapperInfo GCController::kWrapperInfo = {gin::kEmbedderNativeGin};
19
20// static
Michael Lippautz5b64e892018-09-24 11:10:0021void GCController::Install(TestInterfaces* interfaces,
22 blink::WebLocalFrame* frame) {
Blink Reformat1c4d759e2017-04-09 16:34:5423 v8::Isolate* isolate = blink::MainThreadIsolate();
[email protected]41fba0e2014-01-16 18:19:4224 v8::HandleScope handle_scope(isolate);
Blink Reformat1c4d759e2017-04-09 16:34:5425 v8::Local<v8::Context> context = frame->MainWorldScriptContext();
[email protected]41fba0e2014-01-16 18:19:4226 if (context.IsEmpty())
27 return;
28
29 v8::Context::Scope context_scope(context);
30
31 gin::Handle<GCController> controller =
Michael Lippautz5b64e892018-09-24 11:10:0032 gin::CreateHandle(isolate, new GCController(interfaces));
[email protected]ad4d2032014-04-28 13:50:5933 if (controller.IsEmpty())
34 return;
deepak.s750d68f2015-04-30 07:32:4135 v8::Local<v8::Object> global = context->Global();
[email protected]41fba0e2014-01-16 18:19:4236 global->Set(gin::StringToV8(isolate, "GCController"), controller.ToV8());
37}
38
Michael Lippautz5b64e892018-09-24 11:10:0039GCController::GCController(TestInterfaces* interfaces)
40 : interfaces_(interfaces) {}
[email protected]41fba0e2014-01-16 18:19:4241
Michael Lippautz5b64e892018-09-24 11:10:0042GCController::~GCController() = default;
[email protected]41fba0e2014-01-16 18:19:4243
44gin::ObjectTemplateBuilder GCController::GetObjectTemplateBuilder(
45 v8::Isolate* isolate) {
46 return gin::Wrappable<GCController>::GetObjectTemplateBuilder(isolate)
47 .SetMethod("collect", &GCController::Collect)
[email protected]c7d90042014-02-05 08:25:1548 .SetMethod("collectAll", &GCController::CollectAll)
Michael Lippautz5b64e892018-09-24 11:10:0049 .SetMethod("minorCollect", &GCController::MinorCollect)
50 .SetMethod("asyncCollectAll", &GCController::AsyncCollectAll);
[email protected]41fba0e2014-01-16 18:19:4251}
52
53void GCController::Collect(const gin::Arguments& args) {
54 args.isolate()->RequestGarbageCollectionForTesting(
55 v8::Isolate::kFullGarbageCollection);
56}
57
[email protected]c7d90042014-02-05 08:25:1558void GCController::CollectAll(const gin::Arguments& args) {
Michael Lippautz5b64e892018-09-24 11:10:0059 for (int i = 0; i < kNumberOfGCsForFullCollection; i++) {
[email protected]c7d90042014-02-05 08:25:1560 args.isolate()->RequestGarbageCollectionForTesting(
61 v8::Isolate::kFullGarbageCollection);
62 }
63}
64
Michael Lippautz5b64e892018-09-24 11:10:0065void GCController::AsyncCollectAll(const gin::Arguments& args) {
66 if (args.PeekNext().IsEmpty()) {
67 NOTREACHED() << "AsyncCollectAll should be called with callback argument.";
68 }
69
70 v8::HandleScope scope(args.isolate());
71 v8::UniquePersistent<v8::Function> func(
72 args.isolate(), v8::Local<v8::Function>::Cast(args.PeekNext()));
73
74 CHECK(interfaces_->GetDelegate());
75 CHECK(!func.IsEmpty());
76 interfaces_->GetDelegate()->PostTask(
77 base::BindOnce(&GCController::AsyncCollectAllWithEmptyStack,
78 base::Unretained(this), std::move(func)));
79}
80
81void GCController::AsyncCollectAllWithEmptyStack(
82 v8::UniquePersistent<v8::Function> callback) {
83 v8::Isolate* const isolate = blink::MainThreadIsolate();
84
85 for (int i = 0; i < kNumberOfGCsForFullCollection; i++) {
86 isolate->GetEmbedderHeapTracer()->GarbageCollectionForTesting(
87 v8::EmbedderHeapTracer::kEmpty);
88 }
89
90 v8::HandleScope scope(isolate);
91 v8::Local<v8::Function> func = callback.Get(isolate);
92 v8::Local<v8::Context> context = func->CreationContext();
93 v8::Context::Scope context_scope(context);
94 func->Call(context, v8::Undefined(isolate), 0, nullptr).ToLocalChecked();
95}
96
[email protected]41fba0e2014-01-16 18:19:4297void GCController::MinorCollect(const gin::Arguments& args) {
98 args.isolate()->RequestGarbageCollectionForTesting(
99 v8::Isolate::kMinorGarbageCollection);
100}
101
sadrul43405772015-10-15 23:12:37102} // namespace test_runner