toolkit/components/places/PlaceInfo.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:220ec19fcf3b
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 {
31 NS_PRECONDITION(mURI, "Must provide a non-null uri!");
32 }
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 {
48 NS_PRECONDITION(mURI, "Must provide a non-null uri!");
49 }
50
51 ////////////////////////////////////////////////////////////////////////////////
52 //// mozIPlaceInfo
53
54 NS_IMETHODIMP
55 PlaceInfo::GetPlaceId(int64_t* _placeId)
56 {
57 *_placeId = mId;
58 return NS_OK;
59 }
60
61 NS_IMETHODIMP
62 PlaceInfo::GetGuid(nsACString& _guid)
63 {
64 _guid = mGUID;
65 return NS_OK;
66 }
67
68 NS_IMETHODIMP
69 PlaceInfo::GetUri(nsIURI** _uri)
70 {
71 NS_ADDREF(*_uri = mURI);
72 return NS_OK;
73 }
74
75 NS_IMETHODIMP
76 PlaceInfo::GetTitle(nsAString& _title)
77 {
78 _title = mTitle;
79 return NS_OK;
80 }
81
82 NS_IMETHODIMP
83 PlaceInfo::GetFrecency(int64_t* _frecency)
84 {
85 *_frecency = mFrecency;
86 return NS_OK;
87 }
88
89 NS_IMETHODIMP
90 PlaceInfo::GetVisits(JSContext* aContext,
91 JS::MutableHandle<JS::Value> _visits)
92 {
93 // If the visits data was not provided, return null rather
94 // than an empty array to distinguish this case from the case
95 // of a place without any visit.
96 if (!mVisitsAvailable) {
97 _visits.setNull();
98 return NS_OK;
99 }
100
101 // TODO bug 625913 when we use this in situations that have more than one
102 // visit here, we will likely want to make this cache the value.
103 JS::Rooted<JSObject*> visits(aContext,
104 JS_NewArrayObject(aContext, 0));
105 NS_ENSURE_TRUE(visits, NS_ERROR_OUT_OF_MEMORY);
106
107 JS::Rooted<JSObject*> global(aContext, JS::CurrentGlobalOrNull(aContext));
108 NS_ENSURE_TRUE(global, NS_ERROR_UNEXPECTED);
109
110 nsCOMPtr<nsIXPConnect> xpc = mozilla::services::GetXPConnect();
111
112 for (VisitsArray::size_type idx = 0; idx < mVisits.Length(); idx++) {
113 nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
114 nsresult rv = xpc->WrapNative(aContext, global, mVisits[idx],
115 NS_GET_IID(mozIVisitInfo),
116 getter_AddRefs(wrapper));
117 NS_ENSURE_SUCCESS(rv, rv);
118
119 JS::Rooted<JSObject*> jsobj(aContext, wrapper->GetJSObject());
120 NS_ENSURE_STATE(jsobj);
121
122 bool rc = JS_SetElement(aContext, visits, idx, jsobj);
123 NS_ENSURE_TRUE(rc, NS_ERROR_UNEXPECTED);
124 }
125
126 _visits.setObject(*visits);
127 return NS_OK;
128 }
129
130 ////////////////////////////////////////////////////////////////////////////////
131 //// nsISupports
132
133 NS_IMPL_ISUPPORTS(
134 PlaceInfo
135 , mozIPlaceInfo
136 )
137
138 } // namespace places
139 } // namespace mozilla

mercurial