OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/gc_callback.h" | 5 #include "extensions/renderer/gc_callback.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" |
9 #include "extensions/renderer/script_context.h" | 11 #include "extensions/renderer/script_context.h" |
10 | 12 |
11 namespace extensions { | 13 namespace extensions { |
12 | 14 |
13 GCCallback::GCCallback(ScriptContext* context, | 15 GCCallback::GCCallback(ScriptContext* context, |
14 const v8::Local<v8::Object>& object, | 16 const v8::Local<v8::Object>& object, |
15 const v8::Local<v8::Function>& callback, | 17 const v8::Local<v8::Function>& callback, |
16 const base::Closure& fallback) | 18 const base::Closure& fallback) |
17 : context_(context), | 19 : context_(context), |
18 object_(context->isolate(), object), | 20 object_(context->isolate(), object), |
19 callback_(context->isolate(), callback), | 21 callback_(context->isolate(), callback), |
20 fallback_(fallback), | 22 fallback_(fallback), |
21 weak_ptr_factory_(this) { | 23 weak_ptr_factory_(this) { |
22 object_.SetWeak(this, OnObjectGC, v8::WeakCallbackType::kParameter); | 24 object_.SetWeak(this, OnObjectGC, v8::WeakCallbackType::kParameter); |
23 context->AddInvalidationObserver(base::Bind(&GCCallback::OnContextInvalidated, | 25 context->AddInvalidationObserver(base::Bind(&GCCallback::OnContextInvalidated, |
24 weak_ptr_factory_.GetWeakPtr())); | 26 weak_ptr_factory_.GetWeakPtr())); |
25 } | 27 } |
26 | 28 |
27 GCCallback::~GCCallback() {} | 29 GCCallback::~GCCallback() {} |
28 | 30 |
29 // static | 31 // static |
30 void GCCallback::OnObjectGC(const v8::WeakCallbackInfo<GCCallback>& data) { | 32 void GCCallback::OnObjectGC(const v8::WeakCallbackInfo<GCCallback>& data) { |
31 // Usually FirstWeakCallback should do nothing other than reset |object_| | 33 // Usually FirstWeakCallback should do nothing other than reset |object_| |
32 // and then set a second weak callback to run later. We can sidestep that, | 34 // 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 | 35 // 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 | 36 // DO NOT add any more work to this method. The only acceptable place to add |
35 // code is RunCallback. | 37 // code is RunCallback. |
36 GCCallback* self = data.GetParameter(); | 38 GCCallback* self = data.GetParameter(); |
37 self->object_.Reset(); | 39 self->object_.Reset(); |
38 base::MessageLoop::current()->PostTask( | 40 base::ThreadTaskRunnerHandle::Get()->PostTask( |
39 FROM_HERE, base::Bind(&GCCallback::RunCallback, | 41 FROM_HERE, base::Bind(&GCCallback::RunCallback, |
40 self->weak_ptr_factory_.GetWeakPtr())); | 42 self->weak_ptr_factory_.GetWeakPtr())); |
41 } | 43 } |
42 | 44 |
43 void GCCallback::RunCallback() { | 45 void GCCallback::RunCallback() { |
44 fallback_.Reset(); | 46 fallback_.Reset(); |
45 v8::Isolate* isolate = context_->isolate(); | 47 v8::Isolate* isolate = context_->isolate(); |
46 v8::HandleScope handle_scope(isolate); | 48 v8::HandleScope handle_scope(isolate); |
47 context_->CallFunction(v8::Local<v8::Function>::New(isolate, callback_)); | 49 context_->CallFunction(v8::Local<v8::Function>::New(isolate, callback_)); |
48 delete this; | 50 delete this; |
49 } | 51 } |
50 | 52 |
51 void GCCallback::OnContextInvalidated() { | 53 void GCCallback::OnContextInvalidated() { |
52 if (!fallback_.is_null()) { | 54 if (!fallback_.is_null()) { |
53 fallback_.Run(); | 55 fallback_.Run(); |
54 delete this; | 56 delete this; |
55 } | 57 } |
56 } | 58 } |
57 | 59 |
58 } // namespace extensions | 60 } // namespace extensions |
OLD | NEW |