Source code

Revision control

Other Tools

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 "jsapi.h"
11
12
namespace mozilla {
13
namespace places {
14
15
////////////////////////////////////////////////////////////////////////////////
16
//// PlaceInfo
17
18
PlaceInfo::PlaceInfo(int64_t aId, const nsCString& aGUID,
19
already_AddRefed<nsIURI> aURI, const nsString& aTitle,
20
int64_t aFrecency)
21
: mId(aId),
22
mGUID(aGUID),
23
mURI(aURI),
24
mTitle(aTitle),
25
mFrecency(aFrecency),
26
mVisitsAvailable(false) {
27
MOZ_ASSERT(mURI, "Must provide a non-null uri!");
28
}
29
30
PlaceInfo::PlaceInfo(int64_t aId, const nsCString& aGUID,
31
already_AddRefed<nsIURI> aURI, const nsString& aTitle,
32
int64_t aFrecency, const VisitsArray& aVisits)
33
: mId(aId),
34
mGUID(aGUID),
35
mURI(aURI),
36
mTitle(aTitle),
37
mFrecency(aFrecency),
38
mVisits(aVisits),
39
mVisitsAvailable(true) {
40
MOZ_ASSERT(mURI, "Must provide a non-null uri!");
41
}
42
43
////////////////////////////////////////////////////////////////////////////////
44
//// mozIPlaceInfo
45
46
NS_IMETHODIMP
47
PlaceInfo::GetPlaceId(int64_t* _placeId) {
48
*_placeId = mId;
49
return NS_OK;
50
}
51
52
NS_IMETHODIMP
53
PlaceInfo::GetGuid(nsACString& _guid) {
54
_guid = mGUID;
55
return NS_OK;
56
}
57
58
NS_IMETHODIMP
59
PlaceInfo::GetUri(nsIURI** _uri) {
60
NS_ADDREF(*_uri = mURI);
61
return NS_OK;
62
}
63
64
NS_IMETHODIMP
65
PlaceInfo::GetTitle(nsAString& _title) {
66
_title = mTitle;
67
return NS_OK;
68
}
69
70
NS_IMETHODIMP
71
PlaceInfo::GetFrecency(int64_t* _frecency) {
72
*_frecency = mFrecency;
73
return NS_OK;
74
}
75
76
NS_IMETHODIMP
77
PlaceInfo::GetVisits(JSContext* aContext,
78
JS::MutableHandle<JS::Value> _visits) {
79
// If the visits data was not provided, return null rather
80
// than an empty array to distinguish this case from the case
81
// of a place without any visit.
82
if (!mVisitsAvailable) {
83
_visits.setNull();
84
return NS_OK;
85
}
86
87
// TODO bug 625913 when we use this in situations that have more than one
88
// visit here, we will likely want to make this cache the value.
89
JS::Rooted<JSObject*> visits(aContext, JS_NewArrayObject(aContext, 0));
90
NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY);
91
92
JS::Rooted<JSObject*> global(aContext, JS::CurrentGlobalOrNull(aContext));
93
NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
94
95
nsCOMPtr<nsIXPConnect> xpc = nsIXPConnect::XPConnect();
96
97
for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
98
JS::RootedObject jsobj(aContext);
99
nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx],
100
NS_GET_IID(mozIVisitInfo), jsobj.address());
101
NS_ENSURE_SUCCESS(rv, rv);
102
NS_ENSURE_STATE(jsobj);
103
104
bool rc = JS_DefineElement(aContext, visits, idx, jsobj, JSPROP_ENUMERATE);
105
NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
106
}
107
108
_visits.setObject(*visits);
109
return NS_OK;
110
}
111
112
////////////////////////////////////////////////////////////////////////////////
113
//// nsISupports
114
115
NS_IMPL_ISUPPORTS(PlaceInfo, mozIPlaceInfo)
116
117
} // namespace places
118
} // namespace mozilla