Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / extensions / renderer / gc_callback.cc
blob9f4a1d55f7bdf3071dced2228ddd2d86dc8f9f30
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"
7 #include "base/bind.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)
17 : context_(context),
18 object_(context->isolate(), object),
19 callback_(context->isolate(), callback),
20 fallback_(fallback),
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() {}
29 // static
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_));
47 delete this;
50 void GCCallback::OnContextInvalidated() {
51 fallback_.Run();
52 delete this;
55 } // namespace extensions