blob: 7a5bc17660dedc7c3d4c366221ce253291f41cba [file] [log] [blame]
kalman32d7af22015-07-24 05:10:591// 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.
4
5#ifndef EXTENSIONS_RENDERER_GC_CALLBACK_H_
6#define EXTENSIONS_RENDERER_GC_CALLBACK_H_
7
kalman32d7af22015-07-24 05:10:598#include "base/callback.h"
avi2d124c02015-12-23 06:36:429#include "base/macros.h"
kalman32d7af22015-07-24 05:10:5910#include "base/memory/weak_ptr.h"
11#include "v8/include/v8.h"
12
13namespace extensions {
14
15class ScriptContext;
16
17// Runs |callback| when v8 garbage collects |object|, or |fallback| if
18// |context| is invalidated first. Exactly one of |callback| or |fallback| will
19// be called, after which it deletes itself.
Devlin Cronin75bd36022017-11-22 19:03:0720// This object manages its own lifetime.
21// TODO(devlin): Cleanup. "callback" and "fallback" are odd names here, and
22// we should use OnceCallbacks.
kalman32d7af22015-07-24 05:10:5923class GCCallback {
24 public:
25 GCCallback(ScriptContext* context,
26 const v8::Local<v8::Object>& object,
27 const v8::Local<v8::Function>& callback,
28 const base::Closure& fallback);
Devlin Cronin75bd36022017-11-22 19:03:0729 GCCallback(ScriptContext* context,
30 const v8::Local<v8::Object>& object,
31 const base::Closure& callback,
32 const base::Closure& fallback);
kalman32d7af22015-07-24 05:10:5933
34 private:
Devlin Cronin75bd36022017-11-22 19:03:0735 GCCallback(ScriptContext* context,
36 const v8::Local<v8::Object>& object,
37 const v8::Local<v8::Function> v8_callback,
38 const base::Closure& closure_callback,
39 const base::Closure& fallback);
40 ~GCCallback();
41
kalman32d7af22015-07-24 05:10:5942 static void OnObjectGC(const v8::WeakCallbackInfo<GCCallback>& data);
43 void RunCallback();
44 void OnContextInvalidated();
45
46 // The context which owns |object_|.
47 ScriptContext* context_;
48
49 // The object this GCCallback is bound to.
50 v8::Global<v8::Object> object_;
51
Devlin Cronin75bd36022017-11-22 19:03:0752 // The function to run when |object_| is garbage collected. Can be either a
53 // JS or native function (only one will be set).
54 v8::Global<v8::Function> v8_callback_;
55 base::Closure closure_callback_;
kalman32d7af22015-07-24 05:10:5956
57 // The function to run if |context_| is invalidated before we have a chance
58 // to execute |callback_|.
59 base::Closure fallback_;
60
Jeremy Roman9fc2de62019-07-12 14:15:0361 base::WeakPtrFactory<GCCallback> weak_ptr_factory_{this};
kalman32d7af22015-07-24 05:10:5962
63 DISALLOW_COPY_AND_ASSIGN(GCCallback);
64};
65
66} // namespace extensions
67
68#endif // EXTENSIONS_RENDERER_GC_CALLBACK_H_