Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/Promise-inl.h
Line
Count
Source (jump to first uncovered line)
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 file,
5
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_Promise_inl_h
8
#define mozilla_dom_Promise_inl_h
9
10
#include "mozilla/TupleCycleCollection.h"
11
#include "mozilla/TypeTraits.h"
12
#include "mozilla/ResultExtensions.h"
13
#include "mozilla/dom/PromiseNativeHandler.h"
14
15
namespace mozilla {
16
namespace dom {
17
18
class PromiseNativeThenHandlerBase : public PromiseNativeHandler
19
{
20
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
21
  NS_DECL_CYCLE_COLLECTION_CLASS(PromiseNativeThenHandlerBase)
22
23
  PromiseNativeThenHandlerBase(Promise& aPromise)
24
    : mPromise(&aPromise)
25
0
  {}
26
27
  void
28
  ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
29
30
  void
31
  RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
32
33
protected:
34
0
  virtual ~PromiseNativeThenHandlerBase() = default;
35
36
  virtual already_AddRefed<Promise>
37
  CallResolveCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) = 0;
38
39
  virtual void Traverse(nsCycleCollectionTraversalCallback&) = 0;
40
  virtual void Unlink() = 0;
41
42
  RefPtr<Promise> mPromise;
43
};
44
45
namespace {
46
47
template <typename T,
48
          bool = IsRefcounted<typename RemovePointer<T>::Type>::value,
49
          bool = (IsConvertible<T, nsISupports*>::value ||
50
                  IsConvertible<T*, nsISupports*>::value)>
51
struct StorageTypeHelper
52
{
53
  using Type = T;
54
};
55
56
template <typename T>
57
struct StorageTypeHelper<T, true, true>
58
{
59
  using Type = nsCOMPtr<T>;
60
};
61
62
template <typename T>
63
struct StorageTypeHelper<nsCOMPtr<T>, true, true>
64
{
65
  using Type = nsCOMPtr<T>;
66
};
67
68
template <typename T>
69
struct StorageTypeHelper<T*, true, false>
70
{
71
  using Type = RefPtr<T>;
72
};
73
74
template <template <typename> class SmartPtr, typename T>
75
struct StorageTypeHelper<SmartPtr<T>, true, false>
76
  : EnableIf<IsConvertible<SmartPtr<T>, T*>::value,
77
             RefPtr<T>>
78
{
79
};
80
81
template <typename T>
82
using StorageType = typename StorageTypeHelper<typename Decay<T>::Type>::Type;
83
84
// Helpers to choose the correct argument type based on the storage type. Smart
85
// pointers are converted to the corresponding raw pointer type. Everything else
86
// is passed by move reference.
87
//
88
// Note: We can't just use std::forward for this because the input type may be a
89
// raw pointer which does not match the argument type, and while the
90
// spec-compliant behavior there should still give us the expected results, MSVC
91
// considers it an illegal use of std::forward.
92
template <template <typename> class SmartPtr, typename T>
93
decltype(DeclVal<SmartPtr<T>>().get())
94
ArgType(SmartPtr<T>& aVal)
95
0
{
96
0
  return aVal.get();
97
0
}
Unexecuted instantiation: Unified_cpp_extensions0.cpp:decltype ((((DeclVal<RefPtr<mozilla::ExtensionPolicyService> >)()).get)()) mozilla::dom::(anonymous namespace)::ArgType<RefPtr, mozilla::ExtensionPolicyService>(RefPtr<mozilla::ExtensionPolicyService>&)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:decltype ((((DeclVal<nsCOMPtr<nsPIDOMWindowInner> >)()).get)()) mozilla::dom::(anonymous namespace)::ArgType<nsCOMPtr, nsPIDOMWindowInner>(nsCOMPtr<nsPIDOMWindowInner>&)
98
99
template <typename T>
100
T&&
101
ArgType(T& aVal)
102
0
{
103
0
  return std::move(aVal);
104
0
}
105
106
using ::ImplCycleCollectionUnlink;
107
108
template <typename Callback, typename... Args>
109
class NativeThenHandler final : public PromiseNativeThenHandlerBase
110
{
111
public:
112
  NativeThenHandler(Promise& aPromise, Callback&& aOnResolve,
113
                    Args&&... aArgs)
114
    : PromiseNativeThenHandlerBase(aPromise)
115
    , mOnResolve(std::forward<Callback>(aOnResolve))
116
    , mArgs(std::forward<Args>(aArgs)...)
117
0
  {}
Unexecuted instantiation: Unified_cpp_dom_promise0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&>::NativeThenHandler(mozilla::dom::Promise&, std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<mozilla::ExtensionPolicyService::InjectContentScripts(mozilla::extensions::WebExtensionPolicy*)::$_0::operator()(nsIDocShell*) const::{lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}, mozilla::ExtensionPolicyService*, nsCOMPtr<nsPIDOMWindowInner>&, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul> >::NativeThenHandler(mozilla::dom::Promise&, {lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}&&, mozilla::ExtensionPolicyService*&&, nsCOMPtr<nsPIDOMWindowInner>, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom12_GLOBAL__N_117NativeThenHandlerIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrINS0_7PromiseEEEEDaSA_EUlP9JSContextN2JS6HandleINSI_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSM_R8nsCOMPtrISN_ESS_EEC2ERSE_OSU_OSM_SX_ST_
118
119
protected:
120
  void Traverse(nsCycleCollectionTraversalCallback& cb) override
121
0
  {
122
0
    auto* tmp = this;
123
0
    NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mArgs)
124
0
  }
Unexecuted instantiation: Unified_cpp_dom_promise0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&>::Traverse(nsCycleCollectionTraversalCallback&)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<mozilla::ExtensionPolicyService::InjectContentScripts(mozilla::extensions::WebExtensionPolicy*)::$_0::operator()(nsIDocShell*) const::{lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}, mozilla::ExtensionPolicyService*, nsCOMPtr<nsPIDOMWindowInner>&, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul> >::Traverse(nsCycleCollectionTraversalCallback&)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom12_GLOBAL__N_117NativeThenHandlerIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrINS0_7PromiseEEEEDaSA_EUlP9JSContextN2JS6HandleINSI_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSM_R8nsCOMPtrISN_ESS_EE8TraverseER34nsCycleCollectionTraversalCallback
125
126
  void Unlink() override
127
0
  {
128
0
    auto* tmp = this;
129
0
    NS_IMPL_CYCLE_COLLECTION_UNLINK(mArgs)
130
0
  }
Unexecuted instantiation: Unified_cpp_dom_promise0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&>::Unlink()
Unexecuted instantiation: Unified_cpp_extensions0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<mozilla::ExtensionPolicyService::InjectContentScripts(mozilla::extensions::WebExtensionPolicy*)::$_0::operator()(nsIDocShell*) const::{lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}, mozilla::ExtensionPolicyService*, nsCOMPtr<nsPIDOMWindowInner>&, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul> >::Unlink()
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom12_GLOBAL__N_117NativeThenHandlerIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrINS0_7PromiseEEEEDaSA_EUlP9JSContextN2JS6HandleINSI_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSM_R8nsCOMPtrISN_ESS_EE6UnlinkEv
131
132
  already_AddRefed<Promise>
133
  CallResolveCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override
134
0
  {
135
0
    return CallCallback(aCx, mOnResolve, aValue);
136
0
  }
Unexecuted instantiation: Unified_cpp_dom_promise0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&>::CallResolveCallback(JSContext*, JS::Handle<JS::Value>)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<mozilla::ExtensionPolicyService::InjectContentScripts(mozilla::extensions::WebExtensionPolicy*)::$_0::operator()(nsIDocShell*) const::{lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}, mozilla::ExtensionPolicyService*, nsCOMPtr<nsPIDOMWindowInner>&, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul> >::CallResolveCallback(JSContext*, JS::Handle<JS::Value>)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom12_GLOBAL__N_117NativeThenHandlerIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrINS0_7PromiseEEEEDaSA_EUlP9JSContextN2JS6HandleINSI_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSM_R8nsCOMPtrISN_ESS_EE19CallResolveCallbackESH_SL_
137
138
  template <size_t... Indices>
139
  already_AddRefed<Promise>
140
  CallCallback(JSContext* aCx, const Callback& aHandler, JS::Handle<JS::Value> aValue,
141
               std::index_sequence<Indices...>)
142
0
  {
143
0
    return mOnResolve(aCx, aValue, ArgType(Get<Indices>(mArgs))...);
144
0
  }
Unexecuted instantiation: Unified_cpp_dom_promise0.cpp:already_AddRefed<mozilla::dom::Promise> mozilla::dom::(anonymous namespace)::NativeThenHandler<std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&>::CallCallback<>(JSContext*, std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&, JS::Handle<JS::Value>, std::__1::integer_sequence<unsigned long>)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:already_AddRefed<mozilla::dom::Promise> mozilla::dom::(anonymous namespace)::NativeThenHandler<mozilla::ExtensionPolicyService::InjectContentScripts(mozilla::extensions::WebExtensionPolicy*)::$_0::operator()(nsIDocShell*) const::{lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}, mozilla::ExtensionPolicyService*, nsCOMPtr<nsPIDOMWindowInner>&, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul> >::CallCallback<0ul, 1ul, 2ul>(JSContext*, {lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1} const&, JS::Handle<JS::Value>, std::__1::integer_sequence<unsigned long, 0ul, 1ul, 2ul>)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom12_GLOBAL__N_117NativeThenHandlerIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrINS0_7PromiseEEEEDaSA_EUlP9JSContextN2JS6HandleINSI_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSM_R8nsCOMPtrISN_ESS_EE12CallCallbackIJLm0ELm1ELm2EEEE16already_AddRefedISE_ESH_RKSU_SL_NSt3__116integer_sequenceImJXspT_EEEE
145
146
  already_AddRefed<Promise>
147
  CallCallback(JSContext* aCx, const Callback& aHandler, JS::Handle<JS::Value> aValue)
148
0
  {
149
0
    return CallCallback(aCx, aHandler, aValue, std::index_sequence_for<Args...>{});
150
0
  }
Unexecuted instantiation: Unified_cpp_dom_promise0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&>::CallCallback(JSContext*, std::__1::function<already_AddRefed<mozilla::dom::Promise> (JSContext*, JS::Handle<JS::Value>)> const&, JS::Handle<JS::Value>)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:mozilla::dom::(anonymous namespace)::NativeThenHandler<mozilla::ExtensionPolicyService::InjectContentScripts(mozilla::extensions::WebExtensionPolicy*)::$_0::operator()(nsIDocShell*) const::{lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1}, mozilla::ExtensionPolicyService*, nsCOMPtr<nsPIDOMWindowInner>&, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul> >::CallCallback(JSContext*, {lambda(JSContext*, JS::Handle<JS::Value>, mozilla::ExtensionPolicyService*, nsPIDOMWindowInner*, AutoTArray<RefPtr<mozilla::extensions::WebExtensionContentScript>, 8ul>&&)#1} const&, JS::Handle<JS::Value>)
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom12_GLOBAL__N_117NativeThenHandlerIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrINS0_7PromiseEEEEDaSA_EUlP9JSContextN2JS6HandleINSI_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSM_R8nsCOMPtrISN_ESS_EE12CallCallbackESH_RKSU_SL_
151
152
  Callback mOnResolve;
153
154
  Tuple<StorageType<Args>...> mArgs;
155
};
156
157
} // anonymous namespace
158
159
template <typename Callback, typename... Args>
160
Promise::ThenResult<Callback, Args...>
161
Promise::ThenWithCycleCollectedArgs(Callback&& aOnResolve, Args&&... aArgs)
162
0
{
163
0
  using HandlerType = NativeThenHandler<Callback, Args...>;
164
0
165
0
  ErrorResult rv;
166
0
  RefPtr<Promise> promise = Promise::Create(GetParentObject(), rv);
167
0
  if (rv.Failed()) {
168
0
    return Err(rv.StealNSResult());
169
0
  }
170
0
171
0
  auto* handler = new (fallible) HandlerType(
172
0
    *promise, std::forward<Callback>(aOnResolve),
173
0
    std::forward<Args>(aArgs)...);
174
0
175
0
  if (!handler) {
176
0
    return Err(NS_ERROR_OUT_OF_MEMORY);
177
0
  }
178
0
179
0
  AppendNativeHandler(handler);
180
0
  return std::move(promise);
181
0
}
Unexecuted instantiation: _ZN7mozilla3dom7Promise26ThenWithCycleCollectedArgsIRKNSt3__18functionIF16already_AddRefedIS1_EP9JSContextN2JS6HandleINS9_5ValueEEEEEEJEEENS_8EnableIfIXsr17IsHandlerCallbackIT_DpT0_EE5valueENS_6ResultI6RefPtrIS1_E8nsresultEEE4TypeEOSI_DpOSJ_
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom7Promise26ThenWithCycleCollectedArgsIZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellEUlP9JSContextN2JS6HandleINSC_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayI6RefPtrINS4_25WebExtensionContentScriptEELm8EEE_JSG_R8nsCOMPtrISH_ESN_EEENS_8EnableIfIXsr17IsHandlerCallbackIT_DpT0_EE5valueENS_6ResultISK_IS1_E8nsresultEEE4TypeEOSU_DpOSV_
Unexecuted instantiation: Unified_cpp_extensions0.cpp:_ZN7mozilla3dom7Promise26ThenWithCycleCollectedArgsIZZZNS_22ExtensionPolicyService20InjectContentScriptsEPNS_10extensions18WebExtensionPolicyEENK3$_0clEP11nsIDocShellENKUlT_E_clI6RefPtrIS1_EEEDaSA_EUlP9JSContextN2JS6HandleINSH_5ValueEEEPS3_P18nsPIDOMWindowInnerO10AutoTArrayISD_INS4_25WebExtensionContentScriptEELm8EEE_JSL_R8nsCOMPtrISM_ESR_EEENS_8EnableIfIXsr17IsHandlerCallbackISA_DpT0_EE5valueENS_6ResultISE_8nsresultEEE4TypeEOSA_DpOSY_
182
183
} // namespace dom
184
} // namespace mozilla
185
186
#endif // mozilla_dom_Promise_inl_h