Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/storage/mozStorageAsyncStatementJSHelper.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2
 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
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
#include "nsIXPConnect.h"
8
#include "mozStorageAsyncStatement.h"
9
#include "mozStorageService.h"
10
11
#include "nsMemory.h"
12
#include "nsString.h"
13
#include "nsServiceManagerUtils.h"
14
15
#include "mozStorageAsyncStatementJSHelper.h"
16
17
#include "mozStorageAsyncStatementParams.h"
18
19
#include "jsapi.h"
20
21
#include "xpc_make_class.h"
22
23
namespace mozilla {
24
namespace storage {
25
26
////////////////////////////////////////////////////////////////////////////////
27
//// AsyncStatementJSHelper
28
29
nsresult
30
AsyncStatementJSHelper::getParams(AsyncStatement *aStatement,
31
                                  JSContext *aCtx,
32
                                  JSObject *aScopeObj,
33
                                  JS::Value *_params)
34
0
{
35
0
  MOZ_ASSERT(NS_IsMainThread());
36
0
37
#ifdef DEBUG
38
  int32_t state;
39
  (void)aStatement->GetState(&state);
40
  NS_ASSERTION(state == mozIStorageAsyncStatement::MOZ_STORAGE_STATEMENT_READY,
41
               "Invalid state to get the params object - all calls will fail!");
42
#endif
43
44
0
  JS::RootedObject scope(aCtx, aScopeObj);
45
0
46
0
  if (!aStatement->mStatementParamsHolder) {
47
0
    dom::GlobalObject global(aCtx, scope);
48
0
    if (global.Failed()) {
49
0
      return NS_ERROR_UNEXPECTED;
50
0
    }
51
0
52
0
    nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(global.GetAsSupports());
53
0
54
0
    RefPtr<AsyncStatementParams> params(new AsyncStatementParams(window, aStatement));
55
0
    NS_ENSURE_TRUE(params, NS_ERROR_OUT_OF_MEMORY);
56
0
57
0
    RefPtr<AsyncStatementParamsHolder> paramsHolder = new AsyncStatementParamsHolder(params);
58
0
    NS_ENSURE_TRUE(paramsHolder, NS_ERROR_OUT_OF_MEMORY);
59
0
60
0
    aStatement->mStatementParamsHolder =
61
0
      new nsMainThreadPtrHolder<AsyncStatementParamsHolder>(
62
0
        "Statement::mStatementParamsHolder", paramsHolder);
63
0
  }
64
0
65
0
  RefPtr<AsyncStatementParams> params(aStatement->mStatementParamsHolder->Get());
66
0
  JSObject* obj = params->WrapObject(aCtx, nullptr);
67
0
  if (!obj) {
68
0
    return NS_ERROR_UNEXPECTED;
69
0
  }
70
0
71
0
  _params->setObject(*obj);
72
0
  return NS_OK;
73
0
}
74
75
0
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::AddRef() { return 2; }
76
0
NS_IMETHODIMP_(MozExternalRefCountType) AsyncStatementJSHelper::Release() { return 1; }
77
0
NS_INTERFACE_MAP_BEGIN(AsyncStatementJSHelper)
78
0
  NS_INTERFACE_MAP_ENTRY(nsIXPCScriptable)
79
0
  NS_INTERFACE_MAP_ENTRY(nsISupports)
80
0
NS_INTERFACE_MAP_END
81
82
////////////////////////////////////////////////////////////////////////////////
83
//// nsIXPCScriptable
84
85
#define XPC_MAP_CLASSNAME         AsyncStatementJSHelper
86
0
#define XPC_MAP_QUOTED_CLASSNAME "AsyncStatementJSHelper"
87
0
#define XPC_MAP_FLAGS (XPC_SCRIPTABLE_WANT_RESOLVE | \
88
0
                       XPC_SCRIPTABLE_ALLOW_PROP_MODS_DURING_RESOLVE)
89
#include "xpc_map_end.h"
90
91
NS_IMETHODIMP
92
AsyncStatementJSHelper::Resolve(nsIXPConnectWrappedNative *aWrapper,
93
                                JSContext *aCtx,
94
                                JSObject *aScopeObj,
95
                                jsid aId,
96
                                bool *resolvedp,
97
                                bool *_retval)
98
0
{
99
0
  if (!JSID_IS_STRING(aId))
100
0
    return NS_OK;
101
0
102
0
  // Cast to async via mozI* since direct from nsISupports is ambiguous.
103
0
  JS::RootedObject scope(aCtx, aScopeObj);
104
0
  JS::RootedId id(aCtx, aId);
105
0
  mozIStorageAsyncStatement *iAsyncStmt =
106
0
    static_cast<mozIStorageAsyncStatement *>(aWrapper->Native());
107
0
  AsyncStatement *stmt = static_cast<AsyncStatement *>(iAsyncStmt);
108
0
109
#ifdef DEBUG
110
  {
111
    nsISupports *supp = aWrapper->Native();
112
    nsCOMPtr<mozIStorageAsyncStatement> isStatement(do_QueryInterface(supp));
113
    NS_ASSERTION(isStatement, "How is this not an async statement?!");
114
  }
115
#endif
116
117
0
  if (::JS_FlatStringEqualsAscii(JSID_TO_FLAT_STRING(id), "params")) {
118
0
    JS::RootedValue val(aCtx);
119
0
    nsresult rv = getParams(stmt, aCtx, scope, val.address());
120
0
    NS_ENSURE_SUCCESS(rv, rv);
121
0
    *_retval = ::JS_DefinePropertyById(aCtx, scope, id, val, JSPROP_RESOLVING);
122
0
    *resolvedp = true;
123
0
    return NS_OK;
124
0
  }
125
0
126
0
  return NS_OK;
127
0
}
128
129
////////////////////////////////////////////////////////////////////////////////
130
//// AsyncStatementParamsHolder
131
132
NS_IMPL_ISUPPORTS0(AsyncStatementParamsHolder);
133
134
AsyncStatementParamsHolder::~AsyncStatementParamsHolder()
135
0
{
136
0
  MOZ_ASSERT(NS_IsMainThread());
137
0
  // We are considered dead at this point, so any wrappers for row or params
138
0
  // need to lose their reference to the statement.
139
0
  mParams->mStatement = nullptr;
140
0
}
141
142
} // namespace storage
143
} // namespace mozilla