1 // Copyright 2015 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 "extensions/renderer/gc_callback.h"
8 #include "base/message_loop/message_loop.h"
9 #include "extensions/renderer/script_context.h"
11 namespace extensions
{
13 GCCallback::GCCallback(ScriptContext
* context
,
14 const v8::Local
<v8::Object
>& object
,
15 const v8::Local
<v8::Function
>& callback
,
16 const base::Closure
& fallback
)
18 object_(context
->isolate(), object
),
19 callback_(context
->isolate(), callback
),
21 weak_ptr_factory_(this) {
22 object_
.SetWeak(this, OnObjectGC
, v8::WeakCallbackType::kParameter
);
23 context
->AddInvalidationObserver(base::Bind(&GCCallback::OnContextInvalidated
,
24 weak_ptr_factory_
.GetWeakPtr()));
27 GCCallback::~GCCallback() {}
30 void GCCallback::OnObjectGC(const v8::WeakCallbackInfo
<GCCallback
>& data
) {
31 // Usually FirstWeakCallback should do nothing other than reset |object_|
32 // and then set a second weak callback to run later. We can sidestep that,
33 // because posting a task to the current message loop is all but free - but
34 // DO NOT add any more work to this method. The only acceptable place to add
35 // code is RunCallback.
36 GCCallback
* self
= data
.GetParameter();
37 self
->object_
.Reset();
38 base::MessageLoop::current()->PostTask(
39 FROM_HERE
, base::Bind(&GCCallback::RunCallback
,
40 self
->weak_ptr_factory_
.GetWeakPtr()));
43 void GCCallback::RunCallback() {
44 v8::Isolate
* isolate
= context_
->isolate();
45 v8::HandleScope
handle_scope(isolate
);
46 context_
->CallFunction(v8::Local
<v8::Function
>::New(isolate
, callback_
));
50 void GCCallback::OnContextInvalidated() {
55 } // namespace extensions