Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/places/PlaceInfo.cpp
Line
Count
Source (jump to first uncovered line)
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "PlaceInfo.h"
6
#include "VisitInfo.h"
7
#include "nsIURI.h"
8
#include "nsServiceManagerUtils.h"
9
#include "nsIXPConnect.h"
10
#include "mozilla/Services.h"
11
#include "jsapi.h"
12
13
namespace mozilla {
14
namespace places {
15
16
////////////////////////////////////////////////////////////////////////////////
17
//// PlaceInfo
18
19
PlaceInfo::PlaceInfo(int64_t aId,
20
                     const nsCString& aGUID,
21
                     already_AddRefed<nsIURI> aURI,
22
                     const nsString& aTitle,
23
                     int64_t aFrecency)
24
: mId(aId)
25
, mGUID(aGUID)
26
, mURI(aURI)
27
, mTitle(aTitle)
28
, mFrecency(aFrecency)
29
, mVisitsAvailable(false)
30
0
{
31
0
  MOZ_ASSERT(mURI, "Must provide a non-null uri!");
32
0
}
33
34
PlaceInfo::PlaceInfo(int64_t aId,
35
                     const nsCString& aGUID,
36
                     already_AddRefed<nsIURI> aURI,
37
                     const nsString& aTitle,
38
                     int64_t aFrecency,
39
                     const VisitsArray& aVisits)
40
: mId(aId)
41
, mGUID(aGUID)
42
, mURI(aURI)
43
, mTitle(aTitle)
44
, mFrecency(aFrecency)
45
, mVisits(aVisits)
46
, mVisitsAvailable(true)
47
0
{
48
0
  MOZ_ASSERT(mURI, "Must provide a non-null uri!");
49
0
}
50
51
////////////////////////////////////////////////////////////////////////////////
52
//// mozIPlaceInfo
53
54
NS_IMETHODIMP
55
PlaceInfo::GetPlaceId(int64_t* _placeId)
56
0
{
57
0
  *_placeId = mId;
58
0
  return NS_OK;
59
0
}
60
61
NS_IMETHODIMP
62
PlaceInfo::GetGuid(nsACString& _guid)
63
0
{
64
0
  _guid = mGUID;
65
0
  return NS_OK;
66
0
}
67
68
NS_IMETHODIMP
69
PlaceInfo::GetUri(nsIURI** _uri)
70
0
{
71
0
  NS_ADDREF(*_uri = mURI);
72
0
  return NS_OK;
73
0
}
74
75
NS_IMETHODIMP
76
PlaceInfo::GetTitle(nsAString& _title)
77
0
{
78
0
  _title = mTitle;
79
0
  return NS_OK;
80
0
}
81
82
NS_IMETHODIMP
83
PlaceInfo::GetFrecency(int64_t* _frecency)
84
0
{
85
0
  *_frecency = mFrecency;
86
0
  return NS_OK;
87
0
}
88
89
NS_IMETHODIMP
90
PlaceInfo::GetVisits(JSContext* aContext,
91
                     JS::MutableHandle<JS::Value> _visits)
92
0
{
93
0
  // If the visits data was not provided, return null rather
94
0
  // than an empty array to distinguish this case from the case
95
0
  // of a place without any visit.
96
0
  if (!mVisitsAvailable) {
97
0
    _visits.setNull();
98
0
    return NS_OK;
99
0
  }
100
0
101
0
  // TODO bug 625913 when we use this in situations that have more than one
102
0
  // visit here, we will likely want to make this cache the value.
103
0
  JS::Rooted<JSObject*> visits(aContext,
104
0
                               JS_NewArrayObject(aContext, 0));
105
0
  NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY);
106
0
107
0
  JS::Rooted<JSObject*> global(aContext, JS::CurrentGlobalOrNull(aContext));
108
0
  NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
109
0
110
0
  nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
111
0
112
0
  for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
113
0
    JS::RootedObject jsobj(aContext);
114
0
    nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx],
115
0
                                  NS_GET_IID(mozIVisitInfo),
116
0
                                  jsobj.address());
117
0
    NS_ENSURE_SUCCESS(rv, rv);
118
0
    NS_ENSURE_STATE(jsobj);
119
0
120
0
    bool rc = JS_DefineElement(aContext, visits, idx, jsobj, JSPROP_ENUMERATE);
121
0
    NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
122
0
  }
123
0
124
0
  _visits.setObject(*visits);
125
0
  return NS_OK;
126
0
}
127
128
////////////////////////////////////////////////////////////////////////////////
129
//// nsISupports
130
131
NS_IMPL_ISUPPORTS(
132
  PlaceInfo
133
, mozIPlaceInfo
134
)
135
136
} // namespace places
137
} // namespace mozilla