Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_JSObjectHolder_h
8 : #define mozilla_JSObjectHolder_h
9 :
10 : #include "js/RootingAPI.h"
11 : #include "nsISupportsImpl.h"
12 :
13 : namespace mozilla {
14 :
15 : // This class is useful when something on one thread needs to keep alive
16 : // a JS Object from another thread. If they are both on the same thread, the
17 : // owning class should instead be made a cycle collected SCRIPT_HOLDER class.
18 : // This object should only be AddRefed and Released on the same thread as
19 : // mJSObject.
20 : //
21 : // Note that this keeps alive the JS object until it goes away, so be sure not to
22 : // create cycles that keep alive the holder.
23 : //
24 : // JSObjectHolder is ISupports to make it usable with NS_ReleaseOnMainThread.
25 : class JSObjectHolder final : public nsISupports
26 : {
27 : public:
28 0 : JSObjectHolder(JSContext* aCx, JSObject* aObject) : mJSObject(aCx, aObject) {}
29 :
30 : NS_DECL_ISUPPORTS
31 :
32 0 : JSObject* GetJSObject() { return mJSObject; }
33 :
34 : private:
35 0 : ~JSObjectHolder() {}
36 :
37 : JS::PersistentRooted<JSObject*> mJSObject;
38 : };
39 :
40 : } // namespace mozilla
41 :
42 : #endif // mozilla_JSObjectHolder_h
|